<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="130px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="受限空间编号/名称" prop="KEYWORDS">
              <el-input v-model="searchForm.KEYWORDS" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPagination">
                重置
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="10px" class="end">
              <el-button @click="fnImportDialogChangeShow"> 导入 </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        :data="list"
        @get-data="fnGetData"
        v-model:pagination="pagination"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="NUMBER" label="受限空间编号" />
        <el-table-column prop="NAME" label="受限空间名称" />
        <el-table-column prop="LIMITSPACETYPENAME" label="受限空间类型" />
        <el-table-column prop="RISKGRADENAME" label="风险等级" />
        <el-table-column prop="INFORMANTNAME" label="填报人" />
        <el-table-column prop="PRINCIPALNAME" label="主要负责人" />
        <el-table-column prop="COMPILETIME" label="填报时间" />
        <el-table-column label="操作" width="100">
          <template v-slot="{ row }">
            <el-button
              v-if="buttonJurisdiction.edit"
              type="primary"
              text
              link
              @click="fnAddOrEdit(row, 'edit')"
            >
              修改
            </el-button>
            <el-button
              v-if="buttonJurisdiction.del"
              type="primary"
              text
              link
              @click="fnDelete(row.LIMITSPACE_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button
            v-if="buttonJurisdiction.add"
            type="primary"
            @click="fnAddOrEdit({}, 'add')"
          >
            新增
          </el-button>
        </template>
      </layout-table>
    </layout-card>
    <layout-import-file
      v-model:visible="data.importDialogVisible"
      template-url="/template/limitSpace.xls"
      @submit="fnSubmitImport"
    />
    <add
      v-model:visible="data.addOrEditDialog.visible"
      v-model:form="data.addOrEditDialog.form"
      :type="data.addOrEditDialog.type"
      @get-data="fnResetPagination"
    />
  </div>
</template>

<script setup>
import { nextTick, reactive } from "vue";
import { serialNumber } from "@/assets/js/utils";
import {
  getLedgerList,
  setLedgerDelete,
  setLedgerImport,
} from "@/request/confined_space.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { debounce } from "throttle-debounce";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import { cloneDeep } from "lodash-es";
import useListData from "@/assets/js/useListData.js";
import LayoutImportFile from "@/components/import_file/index.vue";
import Add from "./components/add.vue";

const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getLedgerList);
const data = reactive({
  importDialogVisible: false,
  addOrEditDialog: {
    visible: false,
    type: "",
    form: {
      NUMBER: "",
      NAME: "",
      LIMITSPACETYPE: "",
      POSITIONSCOPE: "",
      PRIMARYHAZARD: "",
      RISKGRADE: "",
      MAXPERSON: "",
      HASINSTRUCTOR: "1",
      INFORMANT: "",
      PRINCIPAL: "",
      COMPILETIME: "",
      DESCR: "",
    },
  },
});
const buttonJurisdiction = await useButtonJurisdiction("limitspace");
const fnAddOrEdit = async (row, type) => {
  data.addOrEditDialog.visible = true;
  await nextTick();
  data.addOrEditDialog.type = type;
  if (type === "edit") data.addOrEditDialog.form = cloneDeep(row);
};
const fnDelete = debounce(
  1000,
  async (LIMITSPACE_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    await setLedgerDelete({ LIMITSPACE_ID });
    ElMessage.success("删除成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnImportDialogChangeShow = () => {
  data.importDialogVisible = !data.importDialogVisible;
};
const fnSubmitImport = async (formData) => {
  const resData = await setLedgerImport(formData);
  ElMessage.success(resData.msg);
  fnImportDialogChangeShow();
  fnResetPagination();
};
</script>

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