<template>
  <div>
    <el-form ref="subMaterialForm" :model="subMaterialObj" label-width="140px">
      <el-form-item :label="$t('代理商AGENT')">
        <supplierSelect v-model="subMaterialObj.agentId" :areaType="1" :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-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('托运人')">
        <ladingSelect v-model="subMaterialObj.shipperId" :placeholder="$t('请选择托运人')" :allUsers="this.$attrs.allLading" />
      </el-form-item>
      <el-form-item :label="$t('收货人')">
        <el-input v-model="subMaterialObj.consigneeName" disabled></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" disabled></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>
        <dict-selector v-model="subMaterialObj.packageUnit" type="goods_package_type" />
      </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" type="textarea" autosize :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-button :disabled="shipmentObj.ladingBillInfo == null" @click="download">{{$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 ladingSelect from "./common/ladingSelect.vue";
import { getUnitList } from "@/api/ecw/unit";
import { getCabinetPage } from "@/api/ecw/cabinet";
import { billCreate, getBoxLadingBillPdf } from "@/api/ecw/boxSea";
import { getSupplier } from "@/api/ecw/supplier";
import { formatNumberString, constantDict, serviceMsg } from "../utils";
import FileSaver from 'file-saver'

/**
 * 提单补料
 */
export default {
  name: "subMaterial",
  inheritAttrs: false,
  components: { supplierSelect, dockSelect, userSelect, ladingSelect },
  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;
          this.$set(this.subMaterialObj,'cbm',cabinetItem.ladingBill)
        }
      }
    });
    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,
    };
    if(!this.shipmentObj.ladingBillInfo){
      
      this.$set(this.subMaterialObj,'agentId',this.shipmentObj.agentInfo?.agentId||undefined)
      this.$set(this.subMaterialObj,'soNo',this.shipmentObj.bookSeaInfo?.sono||undefined)
      this.$set(this.subMaterialObj,'blMblNo',this.shipmentObj.bookSeaInfo?.sono||undefined)
      this.$set(this.subMaterialObj,'markNo',"N/M")
      this.$set(this.subMaterialObj,'startPortId',this.shipmentObj.bookSeaInfo?.bigPortId||undefined)
      this.$set(this.subMaterialObj,'shipCompanyId',Number(this.shipmentObj.bookSeaInfo?.shipCompanyType)||undefined)
      this.$set(this.subMaterialObj,'destPortId',this.shipmentObj.bookSeaInfo?.destPortId||undefined)
      this.$set(this.subMaterialObj,'kgs',this.shipmentObj.customsInfo?.dcGoodsWgt||undefined)
      this.$set(this.subMaterialObj,'containerSealNo',(this.shipmentObj.trailerInfo?.tlContainerNo||'')+'/'+(this.shipmentObj.trailerInfo?.tlStripSeal||'/'))
      this.subMaterialObj.containerSealNo = this.subMaterialObj.containerSealNo.replace('//','')
      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)
          var goodName = '',baNo = '',fromNo = '',ctnNo = ''
          if(response.data.externalBackVO.goodName && response.data.externalBackVO.goodName != '') goodName = response.data.externalBackVO.goodName
          if(response.data.externalBackVO.fromNo && response.data.externalBackVO.fromNo != '') fromNo = (goodName!=''?'\n':'')+'FROM M NO:'+response.data.externalBackVO.fromNo
          if(response.data.externalBackVO.baNo && response.data.externalBackVO.baNo != '') baNo = '\nBA NO:'+response.data.externalBackVO.baNo
          if(response.data.externalBackVO.ctnNo && response.data.externalBackVO.ctnNo != '') ctnNo = '\nCTN NO:'+response.data.externalBackVO.ctnNo
          this.$set(this.subMaterialObj,'goodsDesc',goodName+fromNo+baNo+ctnNo)
          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(() => {
              if(operateType == 2) this.download()
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
    download() {
      getBoxLadingBillPdf({shipmentId: this.shipmentObj.id,type:1}).then(res=>{
        let blob = new Blob([res], {type: "application/vnd.ms-excel"})
        FileSaver.saveAs(blob, this.$t('提单确认件.xlsx')); 
      })
    }
  },
};
</script>

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