<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
                root-disabled="N"
              />
            </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 v-slot="{ row }">
            <span v-if="row.ISDELETE === '0'">启用</span>
            <span v-else-if="row.ISDELETE === '1'">禁用</span>
            <span v-else-if="row.ISDELETE === '-99'">未开始</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="470">
          <template v-slot="{ row }">
            <el-button v-if="buttonJurisdiction.edit" type="primary" text link>
              打印
            </el-button>
            <el-button
              v-if="buttonJurisdiction.edit"
              type="primary"
              text
              link
              @click="fnListQRCode(row)"
            >
              清单二维码
            </el-button>
            <el-button
              v-if="
                (row.cdcount === 0 &&
                  (row.USER_ID === USER_ID || buttonJurisdiction.edit) &&
                  row.TYPE !== 'listType0006') ||
                row.ISDELETE === '-99'
              "
              type="primary"
              text
              link
            >
              修改
            </el-button>
            <el-button
              v-if="
                row.ISDELETE === '0' &&
                (row.USER_ID === USER_ID || buttonJurisdiction.edit)
              "
              type="primary"
              text
              link
              @click="fnDisable(row.LISTMANAGER_ID)"
            >
              禁用
            </el-button>
            <el-button
              v-if="
                row.ISDELETE === '1' &&
                (row.USER_ID === USER_ID || buttonJurisdiction.edit)
              "
              type="primary"
              text
              link
              @click="fnEnable(row.LISTMANAGER_ID)"
            >
              启用
            </el-button>
            <el-button type="primary" text link> 部位二维码 </el-button>
            <el-button type="primary" text link @click="fnInspectionRoute(row)">
              巡检路线
            </el-button>
            <el-button
              v-if="row.TASK_TYPE && row.TASK_TYPE !== ''"
              type="primary"
              text
              link
            >
              设置包保责任类型
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button v-if="buttonJurisdiction.add" type="primary">
            新增
          </el-button>
          <el-button
            v-if="buttonJurisdiction.del"
            type="danger"
            @click="fnBatchDelete"
          >
            批量删除
          </el-button>
          <el-button type="primary"> 继承清单 </el-button>
        </template>
      </layout-table>
    </layout-card>
    <qr-code
      :info="data.qrCodeDialog.info"
      v-model:visible="data.qrCodeDialog.visible"
    />
    <inspection-route
      v-model:visible="data.inspectionRouteDialog.visible"
      v-model:list="data.inspectionRouteDialog.list"
      :info="data.inspectionRouteDialog.info"
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils.js";
import { reactive } from "vue";
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 {
  getInventoryManagementList,
  getInventoryManagementRouteView,
  setInventoryManagementBatchDelete,
  setInventoryManagementBatchDeleteTemporary,
  setInventoryManagementDisable,
  setInventoryManagementEnable,
} from "@/request/hazard_investigation.js";
import QrCode from "./components/qr_code.vue";
import InspectionRoute from "./components/inspection_route.vue";

const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getInventoryManagementList);
const userStore = useUserStore();
const USER_ID = userStore.getUserInfo.USER_ID;
const CORPINFO_ID = userStore.getUserInfo.CORPINFO_ID;
const buttonJurisdiction = await useButtonJurisdiction("listmanager");
const inventoryTypeList = await layoutFnGetInventoryType();
const troubleshootingTypeList = await layoutFnGetTroubleshootingType();
const inventoryLevelList = await layoutFnGetInventoryLevel();
const data = reactive({
  qrCodeDialog: {
    visible: false,
    info: {},
  },
  inspectionRouteDialog: {
    visible: false,
    list: [],
    info: {},
  },
});
const fnGetDataTransfer = () => {
  fnGetData({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnListQRCode = (row) => {
  data.qrCodeDialog.visible = true;
  data.qrCodeDialog.info = row;
};
const fnInspectionRoute = async (row) => {
  const resData = await getInventoryManagementRouteView({
    LISTMANAGER_ID: row.LISTMANAGER_ID,
  });
  data.inspectionRouteDialog.list = resData.varList;
  data.inspectionRouteDialog.info = row;
  data.inspectionRouteDialog.visible = true;
};
const fnDisable = debounce(
  1000,
  async (LISTMANAGER_ID) => {
    await ElMessageBox.confirm("确定要禁用吗？", { type: "warning" });
    await setInventoryManagementDisable({ LISTMANAGER_ID });
    ElMessage.success("禁用成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
const fnEnable = debounce(
  1000,
  async (LISTMANAGER_ID) => {
    await ElMessageBox.confirm("确定要启用吗？", { type: "warning" });
    await setInventoryManagementEnable({ LISTMANAGER_ID });
    ElMessage.success("启用成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
const fnBatchDelete = debounce(
  1000,
  async () => {
    const selectionData = tableRef.value.getSelectionRows();
    if (selectionData.length === 0) {
      ElMessage.warning("请选中要删除的项");
      return;
    }
    const DATA_IDS1 = [];
    const DATA_IDS2 = [];
    selectionData.forEach((item) => {
      if (item.TYPE === "listType0006") {
        DATA_IDS1.push(item.LISTMANAGER_ID);
      } else {
        DATA_IDS2.push(item.LISTMANAGER_ID);
      }
    });
    if (DATA_IDS1.length > 0) {
      await ElMessageBox.confirm(
        "临时清单会彻底被删除不可恢复，确定要删除吗？",
        { type: "warning" }
      );
      await setInventoryManagementBatchDeleteTemporary({
        DATA_IDS: DATA_IDS1.join(","),
      });
    } else {
      await ElMessageBox.confirm("确定要删除选中的数据吗？", {
        type: "warning",
      });
    }
    await setInventoryManagementBatchDelete({ DATA_IDS: DATA_IDS2.join(",") });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
</script>

<style scoped></style>
