<template>
  <div>
    <el-form ref="settlementForm" :model="settlementObj" label-width="80px">
      <el-form-item :label="$t('可结算')">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="settlementObj.slSettlementTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('已结算')">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="settlementObj.slSettledTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </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 { settlementCreate } from "@/api/ecw/boxSea";
import { formatDateStr, serviceMsg } from "../utils";
/**
 * 结算
 */
export default {
  name: "settlement",
  inheritAttrs: false,
  data() {
    return {
      // 结算对象
      settlementObj: {},
    };
  },
  created() {
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.$attrs.shipmentObj[voName] };
    oldData = formatDateStr(oldData, ["slSettlementTime", "slSettledTime"]);
    // 2024-12-13,可结算和已结算默认赋值当前完整时间
    // 获取当前时间精确至[年、月、日、时、分、秒]
    let nTime = new Date();
    let nYear = nTime.getFullYear();
    let nMonth = nTime.getMonth() + 1;
    let nDay = nTime.getDate();
    let nHour = nTime.getHours();
    let nMin = nTime.getMinutes();
    let nSecond = nTime.getSeconds();
    if(oldData['slSettlementTime'] == undefined){
      oldData['slSettlementTime'] = nYear+'-'+nMonth+'-'+nDay+' '+nHour+':'+nMin+':'+nSecond
    }
    if(oldData['slSettledTime'] == undefined){
      oldData['slSettledTime'] = nYear+'-'+nMonth+'-'+nDay+' '+nHour+':'+nMin+':'+nSecond
    }
    this.settlementObj = oldData;
  },
  methods: {
    /** 提交 */
    onSubmit(operateType) {
      this.$refs["settlementForm"].validate((valid) => {
        if (valid) {
          settlementCreate({
            ...this.settlementObj,
            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>
