<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="90px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="清单名称" prop="KEYWORDS">
              <el-input
                v-model="searchForm.KEYWORDS"
                placeholder="请输入关键字"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="人员" prop="USERNAME">
              <el-input
                v-model="searchForm.USERNAME"
                placeholder="请输入关键字"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门" prop="DEPTIDS">
              <layout-department
                v-model="searchForm.DEPTIDS"
                multiple
                show-checkbox
                collapse-tags
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="清单类型" prop="TYPE">
              <el-select v-model="searchForm.TYPE">
                <el-option
                  v-for="item in inventoryTypeList"
                  :key="item.BIANMA"
                  :label="item.NAME"
                  :value="item.BIANMA"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="排查类型" prop="RISKCHECKLISTTYPE">
              <el-select v-model="searchForm.RISKCHECKLISTTYPE">
                <el-option
                  v-for="item in troubleshootingTypeList"
                  :key="item.BIANMA"
                  :label="item.NAME"
                  :value="item.BIANMA"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="清单级别" prop="LISTINGLEVEL">
              <el-select v-model="searchForm.LISTINGLEVEL">
                <el-option
                  v-for="item in [
                    { BIANMA: '1', NAME: '公司' },
                    ...inventoryLevelList,
                  ]"
                  :key="item.BIANMA"
                  :label="item.NAME"
                  :value="item.BIANMA"
                />
              </el-select>
            </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="fnResetPaginationTransfer">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        :data="list"
        v-model:pagination="pagination"
        @get-data="fnGetDataTransfer"
        row-key="LISTMANAGER_ID"
      >
        <el-table-column reserve-selection type="selection" width="55" />
        <el-table-column label="序号" width="70">
          <template v-slot="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="NAME" label="清单名称" />
        <el-table-column prop="DEPARTMENT_NAME_ALL" label="部门" />
        <el-table-column prop="POST_NAME" label="岗位" />
        <el-table-column
          prop="USER_NAME"
          label="人员"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          prop="PERIODNAME"
          label="排查周期"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          prop="SCREENTYPENAME"
          label="排查类型"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          prop="TYPENAME"
          label="清单类型"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          v-if="SPECIAL_ENTERPRISES.includes(CORPINFO_ID)"
          prop="TASK_TYPE_NAME"
          label="任务类型"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column label="状态" width="100">
          <template #default> 已删除 </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/hazard_investigation/delete_inventory_management/view',
                  query: { LISTMANAGER_ID: row.LISTMANAGER_ID },
                })
              "
            >
              查看
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="fnRecovery(row.LISTMANAGER_ID)"
            >
              启用
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button
            v-if="buttonJurisdiction.del"
            type="danger"
            @click="fnBatchDelete"
          >
            彻底删除
          </el-button>
        </template>
      </layout-table>
    </layout-card>
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import LayoutDepartment from "@/components/department/index.vue";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import {
  layoutFnGetInventoryLevel,
  layoutFnGetInventoryType,
  layoutFnGetTroubleshootingType,
} from "@/assets/js/data_dictionary.js";
import { SPECIAL_ENTERPRISES } from "@/assets/js/constant.js";
import { useUserStore } from "@/pinia/user.js";
import {
  getDeleteInventoryManagementList,
  setDeleteInventoryManagementRecovery,
  setInventoryManagementBatchDelete,
} from "@/request/hazard_investigation.js";
import { useRouter } from "vue-router";

const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getDeleteInventoryManagementList);
const userStore = useUserStore();
const CORPINFO_ID = userStore.getUserInfo.CORPINFO_ID;
const buttonJurisdiction = await useButtonJurisdiction("stoplistmanager");
const inventoryTypeList = await layoutFnGetInventoryType();
const troubleshootingTypeList = await layoutFnGetTroubleshootingType();
const inventoryLevelList = await layoutFnGetInventoryLevel();
const fnGetDataTransfer = () => {
  fnGetData({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnRecovery = debounce(
  1000,
  async (LISTMANAGER_ID) => {
    await ElMessageBox.confirm("确定要恢复吗？", { type: "warning" });
    await setDeleteInventoryManagementRecovery({ LISTMANAGER_ID });
    ElMessage.success("恢复成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
const fnBatchDelete = debounce(
  1000,
  async () => {
    const selectionData = tableRef.value.getSelectionRows();
    if (selectionData.length === 0) {
      ElMessage.warning("请选中要删除的项");
      return;
    }
    await ElMessageBox.confirm(
      "删除之后该清单无法恢复，确认要删除选中的数据吗？",
      { type: "warning" }
    );
    const DATA_IDS = selectionData.map((item) => item.LISTMANAGER_ID).join(",");
    await setInventoryManagementBatchDelete({ DATA_IDS });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
</script>

<style scoped></style>
