<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="130px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <el-row>
          <el-col :span="5">
            <el-form-item label="清单名称" prop="KEYWORDS">
              <el-input
                v-model="searchForm.KEYWORDS"
                placeholder="请输入关键字"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="清单种类" prop="LIST_TYPE">
              <el-select v-model="searchForm.LIST_TYPE">
                <el-option label="风险管控清单" value="1" />
                <el-option label="隐患排查清单" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <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="5">
            <el-form-item label="是否存在隐患项" prop="HASHIDDEN">
              <el-select v-model="searchForm.HASHIDDEN">
                <el-option label="无隐患" value="0" />
                <el-option label="有隐患" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="是否有超期记录" prop="OVERTIME">
              <el-select v-model="searchForm.OVERTIME">
                <el-option label="无超期" value="0" />
                <el-option label="有超期" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="人员" prop="USERNAME">
              <el-input
                v-model="searchForm.USERNAME"
                placeholder="请输入关键字"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <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="5">
            <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="5">
            <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="4">
            <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="5">
            <el-form-item label="清单状态" prop="ISDELETE">
              <el-select v-model="searchForm.ISDELETE">
                <el-option label="启用" value="0" />
                <el-option label="禁用" value="1" />
                <el-option label="已删除" value="2" />
                <el-option label="彻底删除" value="-1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="在岗状态" prop="WORKSTATUS">
              <el-select v-model="searchForm.WORKSTATUS">
                <el-option label="在岗" value="0" />
                <el-option label="离岗" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <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
        v-model:pagination="pagination"
        :data="list"
        @get-data="fnGetDataTransfer"
      >
        <el-table-column label="序号" width="70">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="NAME" label="清单名称" />
        <el-table-column label="清单种类" width="100">
          <template #default="{ row }">
            <span v-if="row.list_type === '1'">风险管控清单</span>
            <span v-if="row.list_type === '2'">隐患排查清单</span>
          </template>
        </el-table-column>
        <el-table-column prop="DEPARTMENT_NAME_ALL" label="部门" />
        <el-table-column prop="POST_NAME" label="岗位" />
        <el-table-column
          prop="USER_NAME"
          label="人员"
          width="90"
          show-overflow-tooltip
        />
        <el-table-column label="在岗状态" width="90">
          <template #default="{ row }">
            {{ fnGetStatus(row.STARTTIME, row.ENDTIME, row.WORKSTATUS) }}
          </template>
        </el-table-column>
        <el-table-column prop="PERIODNAME" label="排查周期" width="90" />
        <el-table-column label="周期" width="260">
          <template #default="{ row }">
            <span v-if="row.DATESTART && row.DATEEND">
              {{ row.DATESTART }}-{{ row.DATEEND }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="count" label="检查次数" width="90" />
        <el-table-column prop="hiddenCount" label="隐患数" width="90" />
        <el-table-column label="周期内次数" width="120" show-overflow-tooltip>
          <template #default="{ row }">
            <span :class="{ 'text-red': row.checkCount === 0 }">
              {{ row.checkCount }}/1
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="TYPENAME" label="清单类型" width="90" />
        <el-table-column
          v-if="data.WHETHER_HAZARDS === 1"
          label="是否包保责任人任务"
        >
          <template #default="{ row }">
            <span v-if="row.BAO_BAO_TYPE !== '1'"> 否 </span>
            <span v-if="row.BAO_BAO_TYPE === '1'"> 是 </span>
          </template>
        </el-table-column>
        <el-table-column label="清单状态" width="90">
          <template #default="{ row }">
            <span v-if="row.ISDELETE === '0'" class="text-green">启用</span>
            <span v-else-if="row.ISDELETE === '1'" class="text-red">禁用</span>
            <span v-else-if="row.ISDELETE === '2'"> 已删除 </span>
            <span v-else-if="row.ISDELETE === '-1'"> 彻底删除 </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/hazard_investigation/checklist_inspection_status/inspection_record',
                  query: {
                    LIST_ID: row.LISTMANAGER_ID,
                    LIST_TYPE: row.list_type,
                  },
                })
              "
            >
              检查记录
            </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,
  layoutFnGetInventoryType,
  layoutFnGetTroubleshootingCycle,
} from "@/assets/js/data_dictionary.js";
import { getRiskStandardStatusList } from "@/request/hazard_investigation.js";
import { useRouter } from "vue-router";
import dayjs from "dayjs";
import { reactive } from "vue";
import { getEnterpriseInfo } from "@/request/enterprise_management.js";

const data = reactive({
  WHETHER_HAZARDS: "",
});
const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getRiskStandardStatusList);
const troubleshootingCycleList = await layoutFnGetTroubleshootingCycle();
const inventoryLevelList = await layoutFnGetInventoryLevel();
const inventoryTypeList = await layoutFnGetInventoryType();
const fnGetCorpInfo = async () => {
  const resData = await getEnterpriseInfo();
  data.WHETHER_HAZARDS = resData.pd.WHETHER_HAZARDS;
};
fnGetCorpInfo();
const fnGetDataTransfer = () => {
  fnGetData({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnGetStatus = (STARTTIME, ENDTIME, WORKSTATUS) => {
  if (!STARTTIME || !ENDTIME) {
    return "在岗";
  }
  if (
    dayjs(dayjs().format("YYYY-MM-DD")).diff(dayjs(STARTTIME), "days") >= 0 &&
    dayjs(dayjs().format("YYYY-MM-DD")).diff(dayjs(ENDTIME), "days") <= 0
  ) {
    return "离岗";
  } else {
    if (WORKSTATUS === 2) return "离岗";
    return "在岗";
  }
};
</script>

<style scoped></style>
