<template>
  <div>
    <el-form ref="trailerForm" :rules="rules" :model="trailerObj" label-width="120px">
      <el-form-item :label="$t('状态')">
        <el-select v-model="trailerObj.tlContainerStatus" :placeholder="$t('请选择状态')">
          <el-option v-for="type in this.getDictDatas(DICT_TYPE.BOX_SHIPPING_TRAILER_STATUS)" :key="type.value" :label="type.label" :value="type.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('拖车公司')">
        <supplierSelect v-model="trailerObj.tlCompanyId" :companyType="'4'" :placeholder="$t('请选择拖车公司')" :allSupplier="this.$attrs.allSupplier" />
      </el-form-item>
      <el-form-item :label="$t('拖车时间')" prop="tlTime">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" value-format="yyyy-MM-dd HH:mm:ss" v-model="trailerObj.tlTime"></el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('车牌')">
        <el-input v-model="trailerObj.tlLicensePlate	" :placeholder="$t('请输入车牌')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('司机')">
        <el-input v-model="trailerObj.tlDriver" :placeholder="$t('请输入司机')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('司机联系方式')">
        <el-input v-model="trailerObj.tlDriverContact" :placeholder="$t('请输入司机联系方式')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('货柜号')">
        <el-input v-model="trailerObj.tlContainerNo" :placeholder="$t('请输入货柜号')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('封条')">
        <el-input v-model="trailerObj.tlStripSeal" :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 { trailer } from "@/api/ecw/boxSea";
import supplierSelect from "./common/supplierSelect.vue";
import {
  formatStringNumber,
  formatDateStr,
  formatNumberString,
  serviceMsg,
} from "../utils";
/**
 * 拖车
 */
export default {
  inheritAttrs: false,
  name: "trailer",
  components: { supplierSelect },
  data() {
    return {
      // 拖车对象
      trailerObj: {},
      // 校验
      rules: {
        tlTime: [{ required: true, message: this.$t("必填"), trigger: "change" }],
      },
    };
  },
  created() {
    const { currNode, shipmentObj } = this.$attrs;
    const voName = currNode.voName;
    let oldData = { ...shipmentObj[voName] };
    oldData = formatNumberString(oldData, ["tlContainerStatus"]);
    oldData = formatStringNumber(oldData, ["tlCompanyId"]);
    oldData = formatDateStr(oldData, ["tlTime"]);
    this.trailerObj = oldData;
  },
  methods: {
    /** 提交 */
    onSubmit(operateType) {
      this.$refs["trailerForm"].validate((valid) => {
        if (valid) {
          trailer({
            ...this.trailerObj,
            shipmentId: this.$attrs.shipmentObj.id,
            operateType,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
  },
};
</script>
