<template>
  <div>
    <el-form ref="subMaterialForm" :model="subMaterialObj" label-width="140px">
      <el-form-item :label="$t('代理商AGENT')">
        <supplierSelect v-model="subMaterialObj.agentId" :placeholder="$t('请选择供应商')" :allSupplier="this.$attrs.allSupplier" disabled />
      </el-form-item>
      <el-form-item :label="$t('提单号M/BL NO.')" class="two-element">
        <el-input v-model="subMaterialObj.blMblNo" :placeholder="$t('请输入提单号')"></el-input>
        <el-button type="primary">{{$t('提单确认件')}}</el-button>
      </el-form-item>
      <el-form-item label="SO NO.">
        <el-input v-model="subMaterialObj.soNo" :placeholder="$t('请输入SO NO')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('装货港')">
        <dockSelect v-model="subMaterialObj.startPortId" :placeholder="$t('请选择装货港')" :allDocks="this.$attrs.allDocks" />
      </el-form-item>
      <el-form-item :label="$t('目的港')">
        <dockSelect v-model="subMaterialObj.destPortId" :placeholder="$t('请选择目的港')" :allDocks="this.$attrs.allDocks" />
      </el-form-item>
      <el-form-item :label="$t('船公司')">
        <supplierSelect v-model="subMaterialObj.shipCompanyId" :companyType="'9'" :placeholder="$t('请选择船公司')" :allSupplier="this.$attrs.allSupplier" />
      </el-form-item>
      <el-form-item :label="$t('托运人')">
        <userSelect v-model="subMaterialObj.shipperId" :placeholder="$t('请选择托运人')" :allUsers="this.$attrs.allUsers" />
      </el-form-item>
      <el-form-item :label="$t('收货人')">
        <el-input v-model="subMaterialObj.consigneeName"></el-input>
        <!-- <userSelect v-model="subMaterialObj.consigneeId" :placeholder="$t('请选择收货人')" :allUsers="this.$attrs.allUsers" /> -->
      </el-form-item>
      <el-form-item :label="$t('通知方')">
        <el-input v-model="subMaterialObj.notifyingName"></el-input>
        <!-- <userSelect v-model="subMaterialObj.notifyingId" :placeholder="$t('请选择通知方')" :allUsers="this.$attrs.allUsers" /> -->
      </el-form-item>
      <el-form-item label="Mark & No.">
        <el-input v-model="subMaterialObj.markNo" placeholder="请输入Mark & No."></el-input>
      </el-form-item>
      <el-form-item :label="$t('包装数量与单位')" class="two-element">
        <el-input v-model="subMaterialObj.packageNum" :placeholder="$t('请输入包装数量')"></el-input>
        <!-- <el-select v-model="subMaterialObj.packageUnit" :placeholder="$t('请选择单位')">
          <el-option v-for="unit in units" :key="unit.id" :value="unit.id" :label="$l(unit, 'title')"></el-option>
        </el-select> -->
        <el-input v-model="subMaterialObj.packageUnit" :placeholder="$t('请输入包装单位')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('毛重(KGS)')">
        <el-input v-model="subMaterialObj.kgs" :placeholder="$t('请输入毛重') + '(KGS)'"></el-input>
      </el-form-item>
      <el-form-item :label="$t('立方数(CBM)')">
        <el-input v-model="subMaterialObj.cbm" :placeholder="$t('请输入立方数') + '(CBM)'"></el-input>
      </el-form-item>
      <el-form-item :label="$t('货物描述')">
        <el-input v-model="subMaterialObj.goodsDesc" :placeholder="$t('请输入货物描述')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('柜号') + '/' + $t('封条')">
        <el-input v-model="subMaterialObj.containerSealNo" :placeholder="$t('请输入柜号') + '/' + $t('封条')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('柜型')">
        {{cabinetType}}
      </el-form-item>
      <el-form-item :label="$t('出单方式')">
        <el-radio-group v-model="subMaterialObj.issueType">
          <el-radio v-for="item in method" :key="item.value" :label="item.value">{{item.label}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('备注')">
        <el-input v-model="subMaterialObj.remarks" type="textarea" rows="2" :placeholder="$t('请输入备注')"></el-input>
      </el-form-item>
    </el-form>

    <el-row class="operate-button">
      <el-button type="primary" @click="onSubmit(1)">{{$t('保存')}}</el-button>
      <el-button type="success" @click="onSubmit(2)">{{$t('提交')}}</el-button>
      <el-button @click="cancel">{{$t('关闭')}}</el-button>
    </el-row>
  </div>
</template>

<script>
import supplierSelect from "./common/supplierSelect.vue";
import dockSelect from "./common/dockSelect.vue";
import userSelect from "./common/userSelect.vue";
import { getUnitList } from "@/api/ecw/unit";
import { getCabinetPage } from "@/api/ecw/cabinet";
import { billCreate } from "@/api/ecw/boxSea";
import { getSupplier } from "@/api/ecw/supplier";
import { formatNumberString, constantDict, serviceMsg } from "../utils";

/**
 * 提单补料
 */
export default {
  name: "subMaterial",
  inheritAttrs: false,
  components: { supplierSelect, dockSelect, userSelect },
  props: {
    shipmentObj: Object,
  },
  data() {
    return {
      // 提单补料对象
      subMaterialObj: {
        markNo: "N/M",
      },
      // 出单方式
      method: constantDict.billingMethod,
      // 单位
      units: [],
      // 柜型
      cabinetType: "/",
    };
  },
  created() {
    // 获取单位
    getUnitList().then((res) => {
      const { data } = res;
      this.units = data ?? [];
    });
    // 柜型
    getCabinetPage(null).then((response) => {
      const cabinetList = response.data.list;
      for (let index in cabinetList) {
        let cabinetItem = cabinetList[index];
        if (cabinetItem.id == this.shipmentObj.cabinetId) {
          this.cabinetType = cabinetItem.name;
        }
      }
    });
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.shipmentObj[voName] };
    oldData = formatNumberString(oldData, ["issueType"]);
    this.subMaterialObj = {
      ...oldData,
      packageUnit: oldData.packageUnit === 0 ? undefined : oldData.packageUnit,
    };
    this.$set(this.subMaterialObj,'agentId',this.shipmentObj.agentInfo?.agentId||undefined)
    this.getSup(this.subMaterialObj.agentId)
  },
  methods: {
    getSup(id) {
      getSupplier(id).then(response => {
        if(response.data.externalBackVO){
          this.$set(this.subMaterialObj,'consigneeName',response.data.externalBackVO?.receiveCompanyName||'/')
          this.$set(this.subMaterialObj,'notifyingName',response.data.externalBackVO?.notifyCompanyName||'/')
          this.$set(this.subMaterialObj,'packageNum',response.data.externalBackVO?.packNum)
          this.$set(this.subMaterialObj,'packageUnit',response.data.externalBackVO?.packUnit)
          this.$set(this.subMaterialObj,'goodsDesc',response.data.externalBackVO?.goodName+response.data.externalBackVO?.fromNo+response.data.externalBackVO?.baNo)
          if(this.subMaterialObj.goodsDesc == null){
            this.$set(this.subMaterialObj,'goodsDesc','/')
          }
        }
      })
    },
    /** 提交 */
    onSubmit(operateType) {
      this.$refs["subMaterialForm"].validate((valid) => {
        if (valid) {
          billCreate({
            ...this.subMaterialObj,
            shipmentId: this.shipmentObj.id,
            operateType,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
  },
};
</script>

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