<template>
  <div id="card-2">
    <el-row>
      <!-- 自营店铺订单详情--物流信息 -->
      <el-col :span="24" class="main">
        <el-col :span="9" class="one">
          <!-- 快递列表 -->
          <el-col :span="24" v-if="form.transport_type == '0'">
            <el-col v-for="(item, index) in transport" :key="index" class="transport">
              <el-col v-for="(good, index) in item.goods" :key="index">
                <el-col :span="24" class="bode">
                  <el-col :span="6">发货商品{{ index + 1 }}</el-col>
                  <el-col :span="18" class="other">{{ good.goods_name }}</el-col>
                </el-col>
                <el-col :span="24" class="bode">
                  <el-col :span="6">发货数量</el-col>
                  <el-col :span="18" class="other">{{ good.num }}</el-col>
                </el-col>
              </el-col>
              <el-col :span="24" class="bode">
                <el-col :span="6">寄出运单号</el-col>
                <el-col :span="18" class="other">{{ item.shop_transport_no || '暂无单号' }}</el-col>
              </el-col>
              <el-col :span="24" class="bode">
                <el-col :span="6">寄出快递公司</el-col>
                <el-col :span="18" class="other">{{ item.shop_transport_name || '暂无快递信息' }}</el-col>
              </el-col>
              <el-col :span="24" style="text-align: center">
                <el-button type="danger" @click="toDel(item)" v-if="form.status == '1' || form.status == '2' || form.status == '2-' || form.status == '3'">
                  删除
                </el-button>
                <!-- <el-button type="success" @click="toExpre(item)"> 修改快递信息 </el-button> -->
                <el-button type="primary" @click="toLog(item)"> 查看物流 </el-button>
              </el-col>
            </el-col>
          </el-col>
          <!-- 添加修改快递信息 -->
          <el-col v-if="(form.status == '1' || form.status == '2-' || form.status == '2') && form.transport_type == '0'">
            <el-radio-group v-model="status" @change="onstatus">
              <el-radio :label="0" border>全部发货</el-radio>
              <el-radio :label="1" border>部分发货</el-radio>
            </el-radio-group>
          </el-col>
          <el-col :span="24" v-if="status == '1' && form.transport_type == '0'">
            <el-col>发货商品</el-col>
            <el-col :span="24" style="margin: 0 0 10px 0">
              <el-button type="primary" size="mini" @click="toAdd()">添加</el-button>
            </el-col>
            <data-table :usePage="false" :fields="fieldsList" :opera="opera" :data="goodsList" @edit="toEdit" @del="toDelete"> </data-table>
          </el-col>
          <el-col :span="24" class="form" v-if="form.status == '1' || form.status == '2' || form.status == '2-'">
            <el-form :model="form" ref="form" :rules="rules" label-width="100px" class="demo-ruleForm">
              <el-form-item label="快递类型" prop="transport_type">
                <el-select v-model="form.transport_type" clearable filterable placeholder="请选择快递类型" size="small" style="width: 100%">
                  <el-option v-for="i in transport_typeList" :key="i.label" :label="i.label" :value="i.value"> </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="寄出运单号" prop="shop_transport_no" v-if="form.transport_type == '0'">
                <el-input v-model="form.shop_transport_no" placeholder="请输入运单号,快递公司,同时填入" size="small"></el-input>
              </el-form-item>
              <el-form-item label="快递公司" prop="shop_transport_type" v-if="form.transport_type == '0'">
                <el-select
                  v-model="form.shop_transport_type"
                  filterable
                  remote
                  reserve-keyword
                  placeholder="请选择快递公司,运单号,同时填入"
                  :remote-method="querySearch"
                  :loading="loading"
                  @change="handleSelect"
                  size="small"
                  style="width: 100%"
                >
                  <el-option v-for="item in shop_transport_typeList" :key="item.id" :label="item.label" :value="item.value"> </el-option>
                </el-select>
              </el-form-item>

              <el-form-item>
                <el-col>
                  <el-button type="primary" @click="onSubmit('form', '2')" v-if="(form.status == '1' || form.status == '2-') && status == '0'">
                    保存
                  </el-button>
                  <el-button type="primary" @click="onSubmit('form', '2-')" v-if="(form.status == '1' || form.status == '2-') && status == '1'">
                    添加保存
                  </el-button>
                  <el-button type="primary" @click="onSubmit('form', '2')" v-if="form.status == '2'"> 保存 </el-button>
                  <el-button type="warning" @click="onConfirm('3', '是否确认收货')" v-if="form.status == '2'"> 确认收货 </el-button>
                  <el-button type="warning" @click="onConfirm('2', '是否确认全部发货')" v-if="form.status == '2-'"> 确认全部发货 </el-button>
                </el-col>
              </el-form-item>
            </el-form>
          </el-col>
        </el-col>
        <!-- 物流信息 -->
        <el-col :span="9" class="one" v-if="form.transport_type == '0' && logList.length != 0">
          <el-col :span="24">{{ activit.is_check || '暂无快递信息' }}</el-col>
          <el-col :span="24">{{ shop.shop_transport_name || '暂无快递信息' }}: {{ activit.no || '暂无快递信息' }}</el-col>
          <el-col :span="24">
            <p>收货地址：{{ address.province }} , {{ address.city }} , {{ address.area }} , {{ address.address }}</p>
          </el-col>
          <el-col :span="24">
            <el-timeline :reverse="reverse">
              <el-timeline-item v-for="(activity, index) in logList" :key="index" :timestamp="activity.time" :color="activity.color">
                {{ activity.context }}
              </el-timeline-item>
            </el-timeline>
          </el-col>
        </el-col>
        <el-col :span="9" class="one" v-else-if="form.transport_type == '1'"> 本单由商家派送 </el-col>
      </el-col>
    </el-row>
    <e-dialog :dialog="dialog" @toClose="toClose">
      <template v-slot:info>
        <el-form :model="fieldform" ref="fieldform" label-width="100px" class="demo-ruleForm">
          <el-form-item label="发货商品" prop="goods_name" v-if="status == '1' && form.transport_type == '0'">
            <el-select v-model="fieldform.goods_id" clearable filterable placeholder="请选择发货商品" size="small" style="width: 100%" @change="goodsChange">
              <el-option v-for="i in list" :key="i._id" :label="getName(i)" :value="i._id"> </el-option>
            </el-select>
            <p>
              <span style="color: red">用于部分发货 </span>
            </p>
          </el-form-item>
          <el-form-item label="发货商品数量" prop="num" v-if="status == '1' && form.transport_type == '0'">
            <el-input v-model="fieldform.num" placeholder="请输入发货商品数量" size="small" @input="toNum"></el-input>
            <p>
              用于部分发货：发货最大数量不得超过购买商品最大数量 <span style="color: red"> {{ buy_num }}</span>
            </p>
          </el-form-item>
          <el-form-item>
            <el-col>
              <el-button type="primary" @click="toSave()"> 保存 </el-button>
            </el-col>
          </el-form-item>
        </el-form>
      </template>
    </e-dialog>
  </div>
</template>

<script>
const moment = require('moment');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('orderDetail');
export default {
  name: 'card-2',
  props: {
    transport: { type: Array },
    list: { type: Array },
    shop_transport_typeList: { type: Array },
    activities: { type: Array },
    transport_typeList: { type: Array },
    form: { type: Object },
    address: { type: Object },
  },
  components: {},
  data: function () {
    return {
      activit: {},
      logList: [],
      shop: {},
      status: 0,
      // 物流
      reverse: false,
      // 加载
      loading: false,
      rules: {
        shop_transport_no: [{ required: true, message: '请输入快递单号', trigger: 'blur' }],
        shop_transport_type: [{ required: true, message: '请选择快递公司', trigger: 'change' }],
        transport_type: [{ required: true, message: '请选择快递类型', trigger: 'change' }],
      },
      listForm: {},
      buy_num: '',
      // 商品
      goodsList: [],
      // 添加发货商品表单
      fieldform: {},
      // 底部菜单
      opera: [
        { label: '修改', method: 'edit' },
        { label: '删除', method: 'del', confirm: true, type: 'danger' },
      ],
      fieldsList: [
        { label: '商品名称', model: 'goods_id', format: (i) => this.getNames(i) },
        { label: '发货数量', model: 'num' },
      ],
      // 弹框
      dialog: { title: '信息管理', show: false, type: '1' },
    };
  },
  created() {},
  methods: {
    ...mapActions(['query', 'fetch', 'create', 'update']),
    search() {
      this.$emit('search');
    },
    // 远程查询
    async querySearch(value) {
      this.loading = true;
      this.$emit('querySearch', value);
      this.loading = false;
    },
    handleSelect(value) {},
    // 添加
    async toAdd() {
      this.dialog = { title: '信息管理', show: true, type: '1' };
    },
    // 关闭
    toClose() {
      this.fieldform = {};
      this.dialog = { title: '信息管理', show: false, type: '1' };
    },
    // 修改
    toEdit(index, data) {
      this.$set(this, `fieldform`, index.data);
      this.dialog = { title: '信息管理', show: true, type: '1' };
    },
    // 添加发货商品提交
    toSave() {
      let data = this.fieldform;
      data.id = moment(new Date()).valueOf();
      let info = this.goodsList.find((f) => f.id == data.id);
      if (info == '' || info == undefined) this.goodsList.push(data);
      this.toClose();
    },
    onstatus(status) {
      this.status = status;
    },
    // 选择商品
    goodsChange(value) {
      let name = this.form.goods.find((i) => i._id == value);
      if (name.set_num) this.$set(this, `buy_num`, name.set_num);
      else if (name.buy_num) this.$set(this, `buy_num`, name.buy_num);
    },
    getName(i) {
      let name = '';
      if (i.name && i.goods.name) name = i.name + ',' + i.goods.name;
      else if (i.name) name = i.name;
      else name = '套装名称';
      return name;
    },
    getNames(val) {
      let goods = this.form.goods.find((i) => i._id == val);
      let name = '';
      if (goods.name && goods.goods.name) name = goods.name + ',' + goods.goods.name;
      else if (goods.name) name = goods.name;
      else name = '套装名称';
      return name;
    },
    // 删除
    toDelete({ data }) {
      let newList = this.goodsList.filter((i) => i.id != data.id);
      this.$set(this, `goodsList`, newList);
    },
    // 发货最大数量
    toNum(val) {
      if (val > this.buy_num) {
        this.$message.error('超过发货最大数量');
        this.$set(this.form, `num`, '');
      }
    },
    // 修改快递信息
    // toExpre(data) {
    //   this.$set(this.form, `shop_transport_no`, data.shop_transport_no);
    //   this.querySearch(data.shop_transport_type);
    //   this.$set(this.form, `shop_transport_type`, data.shop_transport_type);
    //   if (data.goods) this.$set(this, `status`, 1);
    //   this.$set(this, `goodsList`, data.goods);
    // },
    // 查看物流
    toLog(value) {
      this.$set(this, `shop`, value);
      let info = this.activities.find((f) => f.no == value.shop_transport_no);
      if (info) {
        info.list[0].color = '#0bbd87';
        this.$set(this, `logList`, info.list);
        this.$set(this, `activit`, info);
      }
    },
    // 删除快递信息
    toDel(data) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        let list = this.transport;
        let form = this.form;
        let newList = list.filter((i) => i.id != data.id);
        form.transport = newList;
        let res;
        if (form.id) res = await this.update(form);
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `维护信息成功` });
          this.search();
        }
      });
    },
    // 确认收货-确认全部发货
    async onConfirm(val, text = '是否确认') {
      this.$confirm(text, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        var form = this.form;
        var res;
        form.status = val;
        if (form._id) res = await this.update(form);
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `维护信息成功` });
          this.search();
        }
      });
    },
    // 提交
    async onSubmit(formName, val) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.$confirm('是否确认保存', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(async () => {
            var form = this.form;
            if (form.transport_type == '0') {
              var shop_transport_no = form.shop_transport_no;
              var shop_transport_type = form.shop_transport_type;
              if (form.transport != undefined && val == '2-') {
                let goods = this.goodsList;
                form.transport.push({ id: moment(new Date()).valueOf(), goods, shop_transport_no, shop_transport_type });
              } else if (form.transport == undefined && val == '2-') {
                let goods = this.goodsList;
                let transport = [];
                transport.push({ id: moment(new Date()).valueOf(), goods, shop_transport_no, shop_transport_type });
                form.transport = transport;
              } else if (val == '2' && !form.transport && form.transport_type == '0') {
                let transport = [];
                transport.push({ shop_transport_no, shop_transport_type });
                form.transport = transport;
              } else if (val == '2' && form.transport && form.transport_type == '0' && this.status == '1') {
                let goods = this.goodsList;
                form.transport.push({ id: moment(new Date()).valueOf(), goods, shop_transport_no, shop_transport_type });
              } else if (val == '2' && form.transport && form.transport_type == '0' && this.status == '0') {
                let transport = [];
                transport.push({ shop_transport_no, shop_transport_type });
                form.transport = transport;
              }
            } else if (form.transport_type == '1' && form.transport) delete form.transport;
            var res;
            if (val != undefined || val != '') {
              if (this.goodsList.length == 0) form.status = '2';
              else form.status = val;
            }
            if (form._id) res = await this.update(form);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `维护信息成功` });
              this.goodsList = [];
              this.search();
            }
          });
        } else {
          return false;
        }
      });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    margin: 10px 0 0 2%;
    padding: 5px;
    .add {
      border-bottom: 2px dashed #ccc;
      margin: 0 0 5px 0;
      padding: 5px 0;
    }
    .shop {
      padding: 4px 0;
      font-size: 18px;
      border-bottom: 1px solid #ccc;
    }
    .goods {
      padding: 10px 0;
      .money {
        text-align: right;
      }
    }
    .transport {
      margin: 50px;
      padding: 5px;
      border: 1px solid rgb(254, 195, 195);
    }
    .other {
      text-align: right;
      word-break: break-all;
      p {
        color: red;
      }
      .spanfour {
        color: blue;
      }
    }
    .el-col {
      margin: 3px 0;
    }
    .bode {
      border-bottom: 1px solid rgb(238, 237, 237);
    }
    .form {
      margin: 20px 0 0 0;
    }
  }
}

.el-radio.is-bordered.is-checked {
  border-color: rgb(24, 145, 243);
}
/deep/.el-timeline {
  margin: 20px 0 0 0;
  font-size: 16px;
}
/deep/.el-timeline-item__node:nth-child(1) {
  background-color: green;
}
</style>
