<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="80px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="检查周期" prop="dates">
              <el-date-picker
                v-model="searchForm.dates"
                type="daterange"
                value-format="YYYY-MM-DD"
                format="YYYY-MM-DD"
                range-separator="至"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="检查状态" prop="TYPE">
              <el-select v-model="searchForm.TYPE">
                <el-option label="已检查" value="1" />
                <el-option label="超期未检查" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <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"
        @get-data="fnGetDataTransfer"
        v-model:pagination="pagination"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="LIST_NAME" label="清单名称">
          <template v-slot="{ row }">
            {{ row.LIST_NAME ? row.LIST_NAME : info.NAME }}
          </template>
        </el-table-column>
        <el-table-column
          prop="PRINCIPALNAME"
          label="人员"
          width="90"
          show-overflow-tooltip
        />
        <el-table-column prop="DATESTART" label="检查周期">
          <template v-slot="{ row }">
            <template v-if="row.DATESTART && row.DATEEND">
              {{ row.DATESTART }}-{{ row.DATEEND }}
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="CHECK_USERS" label="检查人" width="90" />
        <el-table-column prop="CHECK_TIME" label="检查时间" />
        <el-table-column label="检查状态" width="120">
          <template v-slot="{ row }">
            <div v-if="row.TYPE === '2'">超期未检查</div>
            <div v-else>已检查</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template #default="{ row }">
            <template v-if="row.TYPE === '1'">
              <el-button
                type="primary"
                text
                link
                @click="
                  router.push({
                    path: '/hazard_investigation/checklist_inspection_status/inspection_record/view',
                    query: {
                      CHECKRECORD_ID: row.CHECKRECORD_ID,
                    },
                  })
                "
              >
                检查记录
              </el-button>
              <el-button
                v-if="buttonJurisdiction.del"
                type="primary"
                text
                link
                @click="fnDelete(row.CHECKRECORD_ID)"
              >
                删除
              </el-button>
            </template>
            <template v-else>
              <el-button
                type="primary"
                text
                link
                @click="
                  router.push({
                    path: '/hazard_investigation/checklist_inspection_status/inspection_record/supplementary_recording',
                    query: {
                      CHECKRECORD_ID: row.CHECKRECORD_ID,
                      DATESTART: row.DATESTART,
                      DATEEND: row.DATEEND,
                      LISTMANAGER_ID,
                    },
                  })
                "
              >
                补录
              </el-button>
            </template>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { serialNumber } from "@/assets/js/utils";
import {
  getChecklistInspectionStatusRecordList,
  setChecklistInspectionStatusRecordDelete,
} from "@/request/hazard_investigation.js";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import useListData from "@/assets/js/useListData.js";

const route = useRoute();
const router = useRouter();
const { LISTMANAGER_ID } = route.query;
const info = ref({});
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getChecklistInspectionStatusRecordList, {
    otherParams: { LISTMANAGER_ID },
    callbackFn: (list, resData) => {
      info.value = resData.ls;
    },
  });
const fnGetDataTransfer = () => {
  fnGetData({
    STARTTIME: searchForm.value.dates?.[0],
    ENDTIME: searchForm.value.dates?.[1],
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    STARTTIME: searchForm.value.dates?.[0],
    ENDTIME: searchForm.value.dates?.[1],
  });
};
const buttonJurisdiction = await useButtonJurisdiction("checkrecord");
const fnDelete = debounce(
  1000,
  async (CHECKRECORD_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    await setChecklistInspectionStatusRecordDelete({ CHECKRECORD_ID });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
</script>

<style scoped></style>
