<template>
  <div>
    <el-form ref="airShipmentForm" :model="airShipmentObj" :rules="rules" label-width="100px">
      <el-form-item :label="$t('')" prop="deliverType">
        <el-radio-group v-model="airShipmentObj.deliverType" @change="handleRadioChange">
          <el-radio v-for="item in deliverTypes" :key="item.value" :label="item.value">{{item.label}}</el-radio>
        </el-radio-group>
      </el-form-item>

      <div v-show="airShipmentObj.deliverType === '1'">

        <el-form-item :label="$t('送货时间')">
              <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="airShipmentObj.deliverTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>

        <el-form-item :label="$t('送货地址')">
            <el-input v-model="airShipmentObj.deliverAddress" :placeholder="$t('请输入送货地址')"></el-input>
        </el-form-item>

        <el-form-item :label="$t('车牌')">
            <el-input v-model="airShipmentObj.licensePlate" :placeholder="$t('请输入车牌')"></el-input>
        </el-form-item>

        <el-form-item :label="$t('司机')">
            <el-input v-model="airShipmentObj.driver" :placeholder="$t('请输入司机')"></el-input>
        </el-form-item>

        <el-form-item :label="$t('司机联系方式')">
            <el-input v-model="airShipmentObj.driverPhone" :placeholder="$t('请输入司机联系方式')"></el-input>
        </el-form-item>

      </div>

      <div v-show="airShipmentObj.deliverType === '2'">

            <el-form-item :label="$t('自提时间')">
                  <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="airShipmentObj.deliverTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>

            <el-form-item :label="$t('自提地址')">
                <el-input v-model="airShipmentObj.deliverAddress" :placeholder="$t('请输入自提地址')"></el-input>
            </el-form-item>

            <el-form-item :label="$t('车牌')">
                <el-input v-model="airShipmentObj.licensePlate" :placeholder="$t('请输入车牌')"></el-input>
            </el-form-item>

            <el-form-item :label="$t('司机')">
                <el-input v-model="airShipmentObj.driver" :placeholder="$t('请输入司机')"></el-input>
            </el-form-item>

            <el-form-item :label="$t('司机联系方式')">
                <el-input v-model="airShipmentObj.driverPhone" :placeholder="$t('请输入司机联系方式')"></el-input>
            </el-form-item>

      </div>

    </el-form>

    <el-row class="operate-button">
      <el-button v-if="airShipmentApprovalInfo && airShipmentApprovalInfo.approvalStatus == 1" type="primary"  @click="$router.push({path: '/bpm/process-instance/detail', query: {id: airShipmentApprovalInfo.bpmProcessId}})">{{$t('出货审核中')}}</el-button>
      <el-button v-if="airShipmentApprovalInfo && airShipmentApprovalInfo.approvalStatus == 1" type="primary" @click="canclAudit">{{$t('取消出货审核')}}</el-button>
      <el-button type="primary" @click="onSubmit(1)">{{$t('保存')}}</el-button>
      <el-button :disabled="$attrs.shipmentObj.approvaling" v-if="!airShipmentApprovalInfo || airShipmentApprovalInfo.approvalStatus != 1" type="success" @click="onSubmit(2)">{{$t('提交')}}</el-button>
      <el-button @click="cancel">{{$t('关闭')}}</el-button>
    </el-row>
  </div>
</template>

<script>
import { airShipmentCreate, approvalCancel } from "@/api/ecw/boxSea";
import userSelect from "./common/userSelect.vue";
import { constantDict, formatDateStr, formatNumberString, serviceMsg } from "../utils";
import {listByIds} from "@/api/ecw/region";

/**
 * 驳船
 */
export default {
  name: "shipment",
  inheritAttrs: false,
  components: { userSelect },
  data() {
    return {
      // 空运出货对象
      airShipmentObj: {
        deliverTime:null,
        deliverType:"1",
        deliverAddress:null,
        licensePlate:null,
        driver:null,
        driverPhone:null
      },
      region:'',
      //初始地址
      oldAddress: "",
      //订单对象
      shipmentObj:{},
      //所有供应商
      suppliers: [],
      // 送货方式
      deliverTypes: constantDict.deliverType,
      // 校验
      rules: {
        deliverType: [{ required: true, message: this.$t("必填"), trigger: "change" }],
      },
      airShipmentApprovalInfo: {}
    };
  },
  created() {
    const voName = this.$attrs.currNode.voName;
    this.shipmentObj = { ...this.$attrs.shipmentObj };
    this.suppliers= this.$attrs.allSupplier;
    let oldData = { ...this.$attrs.shipmentObj[voName] };
    oldData = formatDateStr(oldData, ["deliverTime"]);
    oldData = formatNumberString(oldData, ["deliverType"]);
    if (Object.keys(oldData).length > 0){
      this.airShipmentObj = oldData;
      this.oldAddress=this.airShipmentObj.deliverAddress
    }
    //判断是否有地址数据，没有就带出默认地址
    if (!this.oldAddress) {
      this.handleRadioChange(this.airShipmentObj.deliverType);
    }
    this.airShipmentApprovalInfo = this.$attrs.shipmentObj.airShipmentApprovalInfo
  },
  methods: {
    /** 提交 */
    onSubmit(operateType) {
      this.$refs["airShipmentForm"].validate((valid) => {
        if (valid) {
          airShipmentCreate({
            ...this.airShipmentObj,
            shipmentId: this.$attrs.shipmentObj.id,
            operateType,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /* 取消审核 */
    canclAudit() {
      console.log(this.shipmentObj)
      approvalCancel({
        applyReason: this.$t("取消审核"),
        id: this.airShipmentApprovalInfo.id,
        shipmentId: this.$attrs.shipmentObj.id,
      }).then((res) => {
        serviceMsg(res, this).then(() => {
          // 触发外层重新查询出货信息
          this.cancel("close");
          this.$emit("getBoxInfo");
        });
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
    handleRadioChange: function (value) {
      if (this.oldAddress){
        return
      }
      console.log(1)
      if (value === "1") {
        this.airShipmentObj.deliverAddress="";
        this.shipmentObj.bookAirInfo.shipperId;
        this.suppliers.forEach((item) => {
          if (item.id === this.shipmentObj.bookAirInfo.shipperId) {
            listByIds({ids: [item.country, item.province, item.city].join(',')}).then(res => {
              console.log('listById', res)
              let region = ''
              res.data.forEach(item => {
                region += " " + item.titleZh
              })
              this.region = region
              this.airShipmentObj.deliverAddress =this.region+item.address;
            })
          }
        });
      }
      if (value === "2") {
        this.airShipmentObj.deliverAddress="";
        this.airShipmentObj.deliverAddress = this.$i18n.locale === 'zh_CN'?this.shipmentObj.startWarehouseResp.addressZh:this.shipmentObj.startWarehouseResp.addressEn;
      }
    }
  },
};
</script>

<style lang="scss" scoped>
</style>
