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

    <el-row class="operate-button">
      <el-button type="primary" @click="onSubmit(1)">保存</el-button>
      <el-button type="success" @click="onSubmit(2)">提交</el-button>
      <el-button @click="cancel">关闭</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 { 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,
    };
  },
  methods: {
    /** 提交 */
    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>
