<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="'90%'"
    :visible="visible"
    :confirm-loading="loading"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '编辑结算' : '添加结算'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 6 }, sm: { span: 20 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 24 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-card title="填报人信息" :bordered="false">
        <a-row :gutter="16">
          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item label="填报人">
              <a-input disabled v-model:value="loginUser.username" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item label="填报时间">
              <a-input disabled v-model:value="now" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-card>
      <a-card title="租单信息" :bordered="false">
        <a-row :gutter="16">
          <a-col :md="8" :sm="24" :xs="24">
            <a-form-item label="项目名称" v-bind="validateInfos.projectId">
              <ProjectSelectModel
                :placeholder="`请选择项目`"
                v-model:value="form.projectName"
                @done="chooseProject"
              />
            </a-form-item>
            <a-form-item label="设备名称" v-bind="validateInfos.equipmentId">
              <TowerEquipmentModel
                :placeholder="`请选择设备名称`"
                v-model:value="form.equipmentName"
                @done="chooseEquipmentName"
              />
            </a-form-item>
            <a-form-item label="承租单位" v-bind="validateInfos.companyId">
              <SelectCompany
                :placeholder="`请选择承租单位`"
                v-model:value="form.companyName"
                @done="chooseCompany"
              />
            </a-form-item>
            <a-form-item label="报监编号">
              <a-input v-model:value="form.reportMonitorNo" size="small" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24" :xs="24">
            <a-form-item label="起租日期">
              <a-date-picker
                class="ele-fluid"
                placeholder="请选择起租日期"
                v-model:value="form.startDate"
                valueFormat="YYYY-MM-DD"
              />
            </a-form-item>
            <a-form-item label="停租日期">
              <a-date-picker
                class="ele-fluid"
                placeholder="请选择停租日期"
                v-model:value="form.stopDate"
                valueFormat="YYYY-MM-DD"
              />
            </a-form-item>
            <a-form-item label="检测日期">
              <a-date-picker
                class="ele-fluid"
                placeholder="请选择检测日期"
                v-model:value="form.checkDate"
                valueFormat="YYYY-MM-DD"
              />
            </a-form-item>
            <a-form-item label="租用状态" v-bind="validateInfos.status">
              <a-select placeholder="请选择租用状态" style="width: 250px" size="small"
                        v-model:value="form.status">
                <a-select-option v-for="(item, index) in rentRecordStatusList" :key="index" :value="index">
                  {{ item }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24" :xs="24">
            <a-form-item label="合同编号" v-bind="validateInfos.contractNo">
              <a-input v-model:value="form.contractNo" size="small" />
            </a-form-item>
            <a-form-item label="报停日期">
              <a-date-picker
                class="ele-fluid"
                placeholder="请选择报停日期"
                v-model:value="form.reportStopDate"
                valueFormat="YYYY-MM-DD"
              />
            </a-form-item>
            <a-form-item label="报停类型">
              <DictSelect
                dict-code="RentReportStopType"
                placeholder="请选择报停类型"
                v-model:value="form.reportStopType"
              />
            </a-form-item>
            <a-form-item label="复工日期">
              <a-date-picker
                class="ele-fluid"
                placeholder="请选择复工日期"
                v-model:value="form.restoreDate"
                valueFormat="YYYY-MM-DD"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-card>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
import { ref, reactive, watch, computed } from "vue";
import { Form, message } from "ant-design-vue";
import { assignObject, EleProTable } from "ele-admin-pro";
import { useUserStore } from "@/store/modules/user";
import dayjs from "dayjs";

import { RentRecord, rentRecordStatusList } from "@/api/tower/rentRecord/model";
import { addRentRecord, updateRentRecord } from "@/api/tower/rentRecord";
import { Company } from "@/api/system/company/model";

const userStore = useUserStore();
// 当前用户信息
const loginUser = computed(() => userStore.info ?? {});

const now = dayjs().format("YYYY-MM-DD HH:mm:ss");

// 是否是修改
const isUpdate = ref(false);
const useForm = Form.useForm;
const props = defineProps<{
  // 弹窗是否打开
  visible: boolean;
  // 修改回显的数据
  data?: RentRecord | null;
}>();

const emit = defineEmits<{
  (e: "done"): void;
  (e: "update:visible", visible: boolean): void;
}>();

// 提交状态
const loading = ref(false);
// 是否显示最大化切换按钮
const maxable = ref(true);
const activeKey = ref(0);

const chooseProject = (res) => {
  form.projectName = res.projectName;
  form.projectId = res.projectId;
};

const chooseEquipmentName = (res) => {
  form.equipmentId = res.equipmentId;
  form.equipmentName = res.name;
};

// 用户信息
const form = reactive<RentRecord>({
  rentRecordId: "",
  projectId: "",
  projectName: "",
  contractNo: "",
  equipmentId: "",
  equipmentName: "",
  startDate: "",
  stopDate: "",
  checkDate: "",
  reportStopDate: "",
  reportStopType: "",
  restoreDate: "",
  reportMonitorNo: "",
  companyId: "",
  companyName: "",
  status: "",
  userId: loginUser.value.userId
});

/* 更新visible */
const updateVisible = (value: boolean) => {
  emit("update:visible", value);
};

const chooseCompany = (data: Company) => {
  form.companyName = data.companyName;
  form.companyId = data.companyId;
};

// 表单验证规则
const rules = reactive({
  projectId: [
    {
      required: true,
      message: "请选择项目",
      trigger: "blur"
    }
  ],
  contractNo: [
    {
      required: true,
      message: "请输入合同编号",
      trigger: "blur"
    }
  ],
  equipmentId: [
    {
      required: true,
      message: "请选择设备",
      trigger: "blur"
    }
  ],
  companyId: [
    {
      required: true,
      message: "请选择承租单位",
      trigger: "blur"
    }
  ],
  status: [
    {
      required: true,
      message: "请选择租用状态",
      trigger: "blur"
    }
  ]
});
const { resetFields, validate, validateInfos } = useForm(form, rules);

/* 保存编辑 */
const save = () => {
  validate()
    .then(async () => {
      loading.value = true;
      const data = {
        ...form
      };
      const saveOrUpdate = isUpdate.value ? updateRentRecord : addRentRecord;
      const res = await saveOrUpdate(data).catch((e) => {
        loading.value = false;
        message.error(e.message);
      });
      message.success(res?.message);
      updateVisible(false);
      emit("done");
    })
    .catch(() => {
    });
};

watch(
  () => props.visible,
  (visible) => {
    if (visible) {
      if (props.data) {
        loading.value = false;
        assignObject(form, props.data);
        isUpdate.value = true;
      } else {
        isUpdate.value = false;
      }
    } else {
      resetFields();
    }
  }
);
</script>
<style lang="less">
.title {
  font-weight: bold;
  font-size: 1.1rem;
  padding-bottom: 10px;
  border-bottom: 1px solid #dcdfe6;
  color: #494949;
  margin-bottom: 10px;
}
</style>
