<template>
  <el-dialog
    v-model="visible"
    :title="type === 'edit' ? '修改' : '新增'"
    :before-close="fnClose"
  >
    <el-form ref="formRef" :rules="rules" :model="form" label-width="150px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="受限空间名称" prop="NAME">
            <el-input v-model="form.NAME" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="受限空间编号" prop="NUMBER">
            <el-input v-model="form.NUMBER" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="受限空间类型" prop="LIMITSPACETYPE">
            <el-select v-model="form.LIMITSPACETYPE">
              <el-option
                v-for="item in typeOfConfinedSpaceList"
                :key="item.BIANMA"
                :label="item.NAME"
                :value="item.BIANMA"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="位置及范围" prop="POSITIONSCOPE">
            <el-input v-model="form.POSITIONSCOPE" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="主要危险及有害因素" prop="PRIMARYHAZARD">
            <el-input v-model="form.PRIMARYHAZARD" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="风险等级" prop="RISKGRADE">
            <el-select v-model="form.RISKGRADE">
              <el-option
                v-for="item in riskLevelList"
                :key="item.BIANMA"
                :label="item.NAME"
                :value="item.BIANMA"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="最多作业人数" prop="MAXPERSON">
            <el-input v-model="form.MAXPERSON" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="应急指导书" prop="HASINSTRUCTOR">
            <el-radio-group v-model="form.HASINSTRUCTOR">
              <el-radio :label="'1'">有</el-radio>
              <el-radio :label="'0'">无</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="DESCR">
            <el-input
              v-model="form.DESCR"
              type="textarea"
              :autosize="{
                minRows: 3,
              }"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="填报人" prop="INFORMANT">
            <el-select v-model="form.INFORMANT">
              <el-option
                v-for="item in userList"
                :key="item.USER_ID"
                :label="item.USERNAME"
                :value="item.USER_ID"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="主要负责人" prop="PRINCIPAL">
            <el-select v-model="form.PRINCIPAL">
              <el-option
                v-for="item in userList"
                :key="item.USER_ID"
                :label="item.USERNAME"
                :value="item.USER_ID"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="填报时间" prop="COMPILETIME">
            <el-date-picker
              v-model="form.COMPILETIME"
              value-format="YYYY-MM-DD"
              format="YYYY-MM-DD"
              type="date"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="fnClose">取消</el-button>
      <el-button type="primary" @click="fnSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from "vue";
import { useVModels } from "@vueuse/core";
import {
  layoutFnGetRiskLevel,
  layoutFnGetTypeOfConfinedSpace,
} from "@/assets/js/data_dictionary.js";
import { getUserListAllByCorp } from "@/request/data_dictionary.js";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import { setLedgerAdd, setLedgerEdit } from "@/request/confined_space.js";
import { ElMessage } from "element-plus";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  type: {
    type: String,
    required: true,
    default: "",
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
});
const emits = defineEmits(["update:visible", "update:form", "get-data"]);
const { visible, form } = useVModels(props, emits);
const rules = {
  NUMBER: [{ required: true, message: "请输入受限空间编号", trigger: "blur" }],
  NAME: [{ required: true, message: "请输入受限空间名称", trigger: "blur" }],
  LIMITSPACETYPE: [
    { required: true, message: "请选择受限空间类型", trigger: "blur" },
  ],
  POSITIONSCOPE: [
    { required: true, message: "请输入位置及范围", trigger: "blur" },
  ],
  PRIMARYHAZARD: [
    { required: true, message: "请输入主要危险及有害因素", trigger: "blur" },
  ],
  RISKGRADE: [{ required: true, message: "请选择风险等级", trigger: "blur" }],
  MAXPERSON: [
    { required: true, message: "请输入最多作业人数", trigger: "blur" },
  ],
  HASINSTRUCTOR: [
    { required: true, message: "请输入应急指导书", trigger: "blur" },
  ],
  INFORMANT: [{ required: true, message: "请选择填报人", trigger: "blur" }],
  PRINCIPAL: [{ required: true, message: "请选择主要负责人", trigger: "blur" }],
  COMPILETIME: [{ required: true, message: "请选择填报时间", trigger: "blur" }],
};
const formRef = ref(null);
const typeOfConfinedSpaceList = await layoutFnGetTypeOfConfinedSpace();
const riskLevelList = await layoutFnGetRiskLevel();
const { userList } = await getUserListAllByCorp();
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    if (props.type === "add") await setLedgerAdd({ ...form.value });
    if (props.type === "edit") await setLedgerEdit({ ...form.value });
    fnClose();
    ElMessage.success("保存成功");
    emits("get-data");
  },
  { atBegin: true }
);
</script>

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