<template>
  <div>
    <el-dialog
      title="修改快递信息"
      :visible.sync="editShow"
      width="500px"
      :before-close="close"
    >
      <el-form
        :model="shipForm"
        ref="shipForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item 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-item label="快递单号：" prop="deliveryFlowId">
          <el-input
            v-model="shipForm.deliveryFlowId"
            placeholder="输入快递单号"
            clearable
            size="small"
            style="width: 300px"
          />
        </el-form-item>
      </el-form>
      <!-- 按钮 -->
      <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,
  channelOrderShipUpdate,
} from "@/api/business/order";
export default {
  mixins: [CustomFieldsMixin],
  props: {
    // 发货弹框显示
    editShow: {
      type: Boolean,
      default: false,
    },
    // 订单详情
    goodsInfo: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      loading: false,
      shipForm: {
        deliveryId: "",
        deliveryFlowId: "",
      },
      // 快递下拉列表
      companyData: [],
      rules: {
        deliveryId: [
          { required: true, message: "请选择快递公司", trigger: "change" },
        ],
        deliveryFlowId: [
          { required: true, message: "请输入快递单号", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getCompanyList();
    this.getFormData()
  },
  methods: {
    getFormData(){
      this.shipForm.deliveryId = this.goodsInfo.deliveryId
      this.shipForm.deliveryFlowId = this.goodsInfo.deliveryFlowId
    },
    
    // 关闭发货弹框
    close() {
      this.$emit("close");
      this.loading = false;
      this.$refs["shipForm"].resetFields();
    },

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

    // 确认
    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) {
      let data = {
        ...form,
        orderId: this.goodsInfo.orderId,
      };
      channelOrderShipUpdate(data)
        .then((res) => {
          if (res.code == 0) {
            this.msgSuccess("修改成功");
            this.close();
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>
<style scoped>
</style>