<template>
  <div>
    <el-form ref="bargeForm" :model="bargeObj" :rules="rules" label-width="80px">
      <el-form-item :label="$t('驳船')" prop="bgExmtStatus">
        <el-radio-group v-model="bargeObj.bgExmtStatus">
          <el-radio v-for="item in bargeStatus" :key="item.value" :label="item.value">{{item.label}}</el-radio>
        </el-radio-group>
      </el-form-item>

      <div v-show="bargeObj.bgExmtStatus === '2'">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('驳船预计开船时间')" label-width="130px">
              <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="bargeObj.bgEstShipTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('通知人')" label-width="80px">
              <userSelect v-model="bargeObj.notifyUser" :placeholder="$t('请选择通知人')" :allUsers="this.$attrs.allUsers" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item :label="$t('驳船实际开船时间')" label-width="130px">
          <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="bargeObj.bgRealShipTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>
      </div>
    </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 { bargeCreate } from "@/api/ecw/boxSea";
import userSelect from "./common/userSelect.vue";
import { constantDict, formatDateStr, formatNumberString, serviceMsg } from "../utils";

/**
 * 驳船
 */
export default {
  name: "barge",
  inheritAttrs: false,
  components: { userSelect },
  data() {
    return {
      // 驳船对象
      bargeObj: {},
      // 驳船状态
      bargeStatus: constantDict.bgExmtStatus,
      // 校验
      rules: {
        bgExmtStatus: [{ required: true, message: this.$t("必填"), trigger: "change" }],
      },
    };
  },
  created() {
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.$attrs.shipmentObj[voName] };
    oldData = formatDateStr(oldData, ["bgEstShipTime", "bgRealShipTime"]);
    oldData = formatNumberString(oldData, ["bgExmtStatus"]);
    this.bargeObj = oldData;
  },
  methods: {
    /** 提交 */
    onSubmit(operateType) {
      this.$refs["bargeForm"].validate((valid) => {
        if (valid) {
          bargeCreate({
            ...this.bargeObj,
            shipmentId: this.$attrs.shipmentObj.id,
            operateType,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
  },
};
</script>

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