<template>
  <div>
    <el-dialog
      title="订单发货"
      :visible.sync="sendShow"
      width="1000px"
      :before-close="close"
    >
      <el-form
        :model="shipForm"
        ref="shipForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="订单号：">
          <span>{{ goodsInfo.orderId }}</span>
        </el-form-item>
        <el-form-item label="收货地址：">
          <span>{{
            `${goodsInfo.receiver}，${goodsInfo.tel}，${goodsInfo.province}${goodsInfo.city}${goodsInfo.area}${goodsInfo.address}`
          }}</span>
        </el-form-item>
        <el-form-item label="配送方式：">
          <!--单选框-->
          <el-radio @change="radioDistribution" v-model="shipForm.deliveryType" label="1">快递发货</el-radio>
          <el-radio @change="radioDistribution" v-model="shipForm.deliveryType" label="2">无需物流</el-radio>
        </el-form-item>
        <el-form-item v-if="shipForm.deliveryType == 1" label="快递单号：" prop="deliveryFlowId">
          <el-input
            @blur="InputBlur()"
            v-model="shipForm.deliveryFlowId"
            placeholder="输入快递单号"
            clearable
            size="small"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item v-if="shipForm.deliveryType == 1" label="快递公司：" prop="deliveryId">
          <el-select
            v-model="shipForm.deliveryId"
            placeholder="请选择快递公司"
            clearable
            size="small"
            style="width: 300px"
          >
            <el-option
              v-for="item in companyData"
              :key="item.areaId"
              :label="item.companyName"
              :value="item.deliveryId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 商品 -->
      <el-table
        ref="table"
        :data="goodsInfo.items"
        @selection-change="handleSelectionGoods"
        class="ticket-table"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />
        <el-table-column label="商品名称" prop="title" />
        <el-table-column label="规格">
          <template slot-scope="{ row }">
            <div>{{ row.properties || "--" }}</div>
          </template>
        </el-table-column>
        <el-table-column label="数量">
          <template slot-scope="{ row }">
            <div>{{ row.goodsNum }}件</div>
          </template>
        </el-table-column>
        <el-table-column label="快递公司">
          <template slot-scope="{ row }">
            <div>{{ row.companyName || "--" }}</div>
          </template>
        </el-table-column>
        <el-table-column label="快递单号">
          <template slot-scope="{ row }">
            <div>{{ row.deliveryFlowId || "--" }}</div>
          </template>
        </el-table-column>
        <el-table-column label="发货状态">
          <template slot-scope="{ row }">
            <div>{{ row.deliveryTime ? "已发货" : "未发货" }}</div>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" :disabled="loading" @click="saveClick"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import CustomFieldsMixin from "@/mixins/CustomFields";
import { companyList, deliverOrderShip, automaticRecognition} from "@/api/business/order";
export default {
  mixins: [CustomFieldsMixin],
  props: {
    // 弹框显示
    sendShow: {
      type: Boolean,
      default: false,
    },
    // 订单详情
    goodsInfo: {
      type: Object,
      default: {},
    },
  },
  created() {
    this.getCompanyList();
  },
  data() {
    return {
      loading: false,
      shipForm: {
        deliveryId: "",
        deliveryFlowId: "",
        deliveryType: "1",
      },
      // 快递下拉列表
      companyData: [],
      // 选中sp
      goodsList: [],
      // 选中商品ID
      ids: [],
      rules: {
        deliveryId: [
          { required: true, message: "请选择快递公司", trigger: "change" },
        ],
        deliveryFlowId: [
          { required: true, message: "请输入快递单号", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 关闭发货弹框
    close() {
      this.shipForm.deliveryType = "1";
      this.$emit("close");
      this.loading = false;
      this.ids = [];
      this.$refs["shipForm"].resetFields();
    },

    //切换配送方式时清空另一个方式所选项
    radioDistribution(){
      if(this.shipForm.deliveryType == '2'){
        this.shipForm.deliveryId = '';
        this.shipForm.deliveryFlowId = '';
      }
    },

    // 快递下拉列表
    getCompanyList() {
      companyList({}).then((res) => {
        this.companyData = res.data;
      });
    },

    //快递单号失去焦点时
    InputBlur(){
      if (this.shipForm.deliveryFlowId){
        automaticRecognition(this.shipForm.deliveryFlowId).then((res)=>{
          if (res.code == 0 && res.data){
            this.shipForm.deliveryId = res.data.deliveryId
          }
        })
      }
    },

    // 选中商品
    handleSelectionGoods(e) {
      this.goodsList = e
    },

    // 确认
    saveClick() {
      this.loading = true;
      const subForm = this.$refs["shipForm"];
      subForm.validate((valid) => {
        if (valid) {
          this.submitForm(this.shipForm);
        } else {
          this.loading = false;
          // 提示第一个error
          this.getFormErrorMessage(subForm);
          return false;
        }
      });
    },

    // 提交
    submitForm(form) {
      if (!this.goodsList.length) {
        this.msgError("请选择发货的商品");
        this.loading = false;
        return;
      }
      let index = this.goodsList.findIndex(item=>{
        return item.deliveryTime
      })
      if(index != -1){
        this.msgError(`${ this.goodsList[index].title }已发货！`);
        this.loading = false;
        return
      }
      this.ids = this.goodsList.map((item) => item.itemId);
      let data = {
        ...form,
        orderId: this.goodsInfo.orderId,
        itemIds: this.ids,
      };
      deliverOrderShip(data)
        .then((res) => {
          if (res.code == 0) {
            this.msgSuccess("发货成功");
            this.close();
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>
<style>
</style>
