<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="KEYWORDS">
              <el-input v-model="searchForm.KEYWORDS" />
            </el-form-item>
          </el-col>
          <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="至"
                :disabled="searchForm.TYPE === '2'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="检查状态" prop="TYPE">
              <el-select
                v-model="searchForm.TYPE"
                @change="searchForm.dates = []"
              >
                <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-col :span="2">
            <el-form-item label-width="10px" class="end">
              <el-button @click="fnBatchPrinting"> 批量打印 </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        v-model:pagination="pagination"
        :data="list"
        row-key="CHECKRECORD_ID"
        @get-data="fnGetDataTransfer"
      >
        <el-table-column reserve-selection type="selection" width="55" />
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="LIST_NAME" label="清单名称">
          <template #default="{ 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 #default="{ 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 #default="{ 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/inspection_record/view',
                    query: {
                      CHECKRECORD_ID: row.CHECKRECORD_ID,
                      LIST_TYPE: row.LIST_TYPE,
                    },
                  })
                "
              >
                检查记录
              </el-button>
              <el-button
                v-if="buttonJurisdiction.del"
                type="primary"
                text
                link
                @click="fnDelete(row.LIST_TYPE, row.CHECKRECORD_ID)"
              >
                删除
              </el-button>
            </template>
            <template v-else>
              <el-button
                type="primary"
                text
                link
                @click="
                  router.push({
                    path: '/hazard_investigation/inspection_record/supplementary_recording',
                    query: {
                      CHECKRECORD_ID: row.CHECKRECORD_ID,
                      DATESTART: row.DATESTART,
                      DATEEND: row.DATEEND,
                      ID: row.LISTMANAGER_ID,
                      LIST_TYPE: row.LIST_TYPE,
                    },
                  })
                "
              >
                补录
              </el-button>
            </template>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { serialNumber } from "@/assets/js/utils";
import {
  getInspectionRecordList,
  setChecklistInspectionStatusRecordDelete,
  setDeleteCustommCheckrecord,
} 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";
import { useUserStore } from "@/pinia/user.js";

const router = useRouter();
const userStore = useUserStore();
const info = ref({});
const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getInspectionRecordList, {
    otherParams: { USER_ID: userStore.getUserInfo.USER_ID },
    defaultSearchForm: { TYPE: "1" },
    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 (LIST_TYPE, CHECKRECORD_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    LIST_TYPE === "1"
      ? await setChecklistInspectionStatusRecordDelete({ CHECKRECORD_ID })
      : await setDeleteCustommCheckrecord({ CHECKRECORD_ID });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
const fnBatchPrinting = () => {
  const selectionData = tableRef.value.getSelectionRows();
  if (selectionData.length === 0) {
    ElMessage.warning("请选择要打印的数据");
    return;
  }
  const DATA_IDS = selectionData.map((item) => item.CHECKRECORD_ID).join(",");
  const LIST_TYPE = selectionData.map((item) => item.LIST_TYPE).join(",");
  router.push({
    path: "/hazard_investigation/inspection_record/batch_printing",
    query: {
      DATA_IDS,
      LIST_TYPE,
    },
  });
};
</script>

<style scoped></style>
