<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="STATUS">
              <el-select v-model="searchForm.STATUS">
                <el-option label="未检查" value="0" />
                <el-option label="已检查" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="排查周期" prop="PERIOD">
              <el-select v-model="searchForm.PERIOD">
                <el-option
                  v-for="item in troubleshootingCycleList"
                  :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
        :data="list"
        v-model:pagination="pagination"
        @get-data="fnGetDataTransfer"
      >
        <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="140"
          show-overflow-tooltip
        />
        <el-table-column
          prop="PERIODNAME"
          label="排查周期"
          width="140"
          show-overflow-tooltip
        />
        <el-table-column label="检查次数" width="140" show-overflow-tooltip>
          <template v-slot="{ row }">
            <span :class="{ 'text-red': row.checkCount === 0 }">
              {{ row.checkCount }}/1
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="TYPENAME"
          label="清单类型"
          width="140"
          show-overflow-tooltip
        />
        <el-table-column label="操作" width="100">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="fnInspect(row.LISTMANAGER_ID)"
            >
              开始检查
            </el-button>
          </template>
        </el-table-column>
      </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 {
  layoutFnGetInventoryLevel,
  layoutFnGetTroubleshootingCycle,
} from "@/assets/js/data_dictionary.js";
import { getInventoryTroubleshootingList } from "@/request/hazard_investigation.js";
import { useRouter } from "vue-router";
import { ElMessageBox } from "element-plus";

const router = useRouter();
let ISREST = "";
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getInventoryTroubleshootingList, {
    callbackFn: (list, resData) => {
      ISREST = resData.ISREST;
    },
  });
const troubleshootingCycleList = await layoutFnGetTroubleshootingCycle();
const inventoryLevelList = await layoutFnGetInventoryLevel();
const fnGetDataTransfer = () => {
  fnGetData({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnInspect = (LISTMANAGER_ID) => {
  if (ISREST === "1") {
    ElMessageBox.alert("您处于离岗状态，无需检查清单");
    return;
  }
  router.push({
    path: "/hazard_investigation/inventory_troubleshooting/inspect",
    query: { LISTMANAGER_ID },
  });
};
</script>

<style scoped></style>
