<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 form.transport" :key="index" class="transport">
              <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="primary" @click="toLog(item)"> 查看物流 </el-button>
              </el-col>
            </el-col>
          </el-col>
          <el-col :span="24" class="form" v-if="form.status == '1' || 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 == '2'"> 保存 </el-button>
                  <el-button type="warning" @click="onConfirm('3', '是否确认收货')" 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>
  </div>
</template>

<script>
const moment = require('moment');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('groupOrder');
export default {
  name: 'card-2',
  props: {
    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' }],
      },
    };
  },
  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) {},
    // 查看物流
    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(val) {
      this.$confirm('是否确认删除快递信息', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        let form = this.form;
        let data = { _id: form._id, transport: [] };
        let res;
        if (data._id) res = await this.update(data);
        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 res;
        let data = { _id: this.form._id, status: val };
        if (data._id) res = await this.update(data);
        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 (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 == '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;
            let data;
            if (val) {
              if (val == '2') data = { _id: form._id, status: val, transport: form.transport, transport_type: form.transport_type };
              else data = { _id: form._id, status: val, transport_type: form.transport_type };
            }
            if (data._id) res = await this.update(data);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `维护信息成功` });
              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>
