<template>
  <el-dialog :visible.sync="visible" title="物流发货" width="510px" @closed="$emit('close')">
    <el-form v-loading="loading" :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="收货信息">
        <p>{{data.consignee}}，{{data.consigneePhone}}</p>
        <p>{{data.consigneeAddress}}</p>
      </el-form-item>
      <el-form-item label="物流公司" prop="logisticsCompanyCode">
        <el-select style="width:100%" placeholder="请选择" v-model="form.logisticsCompanyCode">
          <el-option label="顺丰速递" value="SF"></el-option>
          <el-option label="中通快递" value="STO"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="物流单号" prop="logisticCode">
        <dm-input-phone style="width:100%" v-model="form.logisticCode" placeholder="请输入"></dm-input-phone>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import fetch from '@/api/performance.js';
const { delivering } = fetch;

export default {
  props: {
    data: Object
  },
  data() {
    return {
      visible: false,
      successCount: 0,
      failedCount: 0,
      form: {
        logisticsCompanyCode: '',
        logisticCode: '',
      },
      rules: {
        logisticsCompanyCode: [ { required: true, message: '请选择物流公司', trigger: 'blur' } ],
        logisticCode: [ { required: true, message: '请填写物流单号', trigger: 'blur' } ]
      },
      loading: false
    };
  },
  mounted() {
    this.visible = true;
  },
  methods: {
    async handleConfirm() {
      let valid = await this.$refs.form.validate();
      if(valid) {
        let logisticsCompany = {
          'SF': '顺丰速递',
          'STO': '中通快递'
        };
        this.loading = true;
        await delivering({
          companyName: logisticsCompany[this.form.logisticsCompanyCode],
          companyCode: this.form.logisticsCompanyCode,
          logisticsNo: this.form.logisticCode,
          deliveryId: this.data.deliveryId,
        }).finally(() => this.loading = false);
        this.visible = false;
        this.$message.success('发货成功');
        this.$emit('refresh');
      }
    },
  }
};
</script>
