<template>
  <div class="delivery-setting">
    <div class="model">
      <div class="title">运力设置<span class="tips">(推荐的运力在聚合发单时会置顶展示，屏蔽的运力在发单时将不进行计价）</span></div>
      <div class="list">
        <div class="item" v-for="(v,i) in deliveryList" :key=i>
          <div class="left">
            <div class="left-top">
              <div class="name">{{v.name}}</div>
            </div>
            <div class="left-bot">{{v.tips}}</div>
          </div>
          <div class="right">
            <div class="con" @click="changeDelivery(v,3)" v-if="billDeliveryIds.includes(String(v.deliveryId))">取消推荐</div>
            <div class="con con1" @click="changeDelivery(v,2)" v-if="billDeliveryIds.includes(String(v.deliveryId)) || !shieldDeliveryIds.includes(String(v.deliveryId))">屏蔽</div>
            <div class="con con1" @click="changeDelivery(v,3)" v-if="shieldDeliveryIds.includes(String(v.deliveryId))">取消屏蔽</div>
            <div class="con" @click="changeDelivery(v,1)" v-if="shieldDeliveryIds.includes(String(v.deliveryId)) || !billDeliveryIds.includes(String(v.deliveryId))">推荐</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getDeliveryFloorList,
  getConfig,
  updateConfig,
} from "../../api/setting.js";
export default {
  name: "deliverySetting",
  data() {
    return {
      billDeliveryIds: [], // 推荐运力
      shieldDeliveryIds: [], // 屏蔽运力
      deliveryList: [],
      centerDialogVisible: false,
      curIndex: 0,
      time: 0,
    };
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getDelivery();
    this.getConfig();
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  // 生命周期 - 创建之前
  beforeCreate() {},
  // 生命周期 - 挂载之前
  beforeMount() {},
  // 生命周期 - 更新之前
  beforeUpdate() {},
  // 生命周期 - 更新之后
  updated() {},
  // 生命周期 - 销毁之前
  beforeDestroy() {},
  // 生命周期 - 销毁完成
  destroyed() {},
  // 如果页面有keep-alive缓存功能，这个函数会触发
  activated() {},
  // 方法集合
  methods: {
    changeDelivery(v, type) {
      let id = v.deliveryId;
      // 推荐该运力
      if (type === 1) {
        this.shieldDeliveryIds = this.shieldDeliveryIds.filter((v) => {
          return v !== String(id);
        });
        this.billDeliveryIds.push(String(id));
        this.billDeliveryIds = [...new Set(this.billDeliveryIds)];
      }
      // 屏蔽该运力
      if (type === 2) {
        this.billDeliveryIds = this.billDeliveryIds.filter((v) => {
          return v !== String(id);
        });
        this.shieldDeliveryIds.push(String(id));
        this.shieldDeliveryIds = [...new Set(this.shieldDeliveryIds)];
      }
      // 恢复为普通运力
      if (type === 3) {
        this.billDeliveryIds = this.billDeliveryIds.filter((v) => {
          return v !== String(id);
        });
        this.shieldDeliveryIds = this.shieldDeliveryIds.filter((v) => {
          return v !== String(id);
        });
      }
      let data = {
        billDeliveryIds: String(this.billDeliveryIds),
        shieldDeliveryIds: String(this.shieldDeliveryIds),
      };
      this.updateConfig(data);
    },
    change(e) {
      let data = { [e.field]: e.value };
      this.updateConfig(data);
    },
    getDelivery() {
      getDeliveryFloorList().then((res) => {
        if (res.code === 200) {
          // 电脑端暂时不支持货拉拉
          this.deliveryList = res.data.filter((v) => {
            return v.type !== 12;
          });
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    getConfig() {
      getConfig().then((res) => {
        if (res.code === 200) {
          this.billDeliveryIds = res.data.billDeliveryIds
            ? res.data.billDeliveryIds.split(",")
            : [];
          this.shieldDeliveryIds = res.data.shieldDeliveryIds
            ? res.data.shieldDeliveryIds.split(",")
            : [];
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
        this.centerDialogVisible = false;
      });
    },
    updateConfig(data) {
      updateConfig(data).then((res) => {
        if (res.code !== 200) {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
        this.getConfig();
      });
    },
  },
};
</script>

<style lang="scss" scoped  type="text/css">
/deep/ .el-button--default:hover {
  background-color: rgba(252, 114, 0, 0.2);
  border-color: rgba(252, 114, 0, 0.2);
  color: #fc7200;
}
/deep/ .el-button--primary {
  background-color: #fc7200;
  border-color: #fc7200;
}
.model {
  .title {
    position: relative;
    font-size: 16px;
    font-weight: bold;
    line-height: 22px;
    color: #333333;
    padding-left: 10px;
    margin-top: 24px;
    .tips {
      font-size: 14px;
      font-weight: 400;
      line-height: 22px;
      color: #999999;
    }
  }
  .title::before {
    width: 3px;
    height: 16px;
    background: #fc7200;
    border-radius: 2px;
    content: "";
    position: absolute;
    top: 2px;
    left: 0;
  }
  .list {
    .item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background: #ffffff;
      margin-top: 10px;
      padding: 16px 20px;
      .left {
        display: flex;
        flex-direction: column;
        font-size: 14px;
        font-weight: bold;
        line-height: 20px;
        color: #000000;
        .left-top {
          display: flex;
          align-items: center;
          .set-tips {
            font-weight: 400;
            color: #fc7200;
            margin-left: 10px;
            cursor: pointer;
          }
        }
        .left-bot {
          margin-top: 2px;
          font-size: 12px;
          font-weight: 400;
          line-height: 17px;
          color: #999999;
        }
      }
      .right {
        display: flex;
        align-items: center;
        .time {
          font-size: 12px;
          font-family: PingFang SC;
          font-weight: 400;
          line-height: 17px;
          color: #333333;
          margin-right: 20px;
        }
        .time1 {
          color: #999999;
        }
        .con {
          font-size: 14px;
          font-weight: 400;
          line-height: 20px;
          color: #fc7200;
          padding-left: 20px;
          cursor: pointer;
        }
        .con1 {
          color: #999999;
        }
        .img {
          width: 20px;
          height: 20px;
          cursor: pointer;
        }
      }
    }
  }
}
</style>