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

/**
 * 驳船
 */
export default {
  name: "barge",
  inheritAttrs: false,
  components: { userSelect },
  data() {
    return {
      // 驳船对象
      bargeObj: {},
      // 驳船状态
      bargeStatus: constantDict.bgExmtStatus,
      // 校验
      rules: {
        bgExmtStatus: [{ required: true, message: "必填", 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>
