<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="60px"
        @submit.prevent="fnResetPagination"
      >
        <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-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPagination">
                重置
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="10px" class="end">
              <el-button @click="fnBatchPrint">打印</el-button>
              <el-button @click="fnImportDialogChangeShow"> 导入 </el-button>
              <el-button @click="fnExport">导出</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="fnGetData"
        row-key="IDENTIFICATIONPARTS_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="RISKUNITNAME" label="风险点（单元）" />
        <el-table-column prop="PARTSNAME" label="辨识部位名称" />
        <el-table-column label="告知卡">
          <template v-slot="{ row }">
            <layout-tooltip-img :imgs="row.imgs" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280">
          <template v-slot="{ row }">
            <el-button type="primary" text link @click="fnQrCode(row)">
              二维码
            </el-button>
            <el-button
              v-if="buttonJurisdiction.edit"
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/risk_control/identifying_parts/resources_risk',
                  query: {
                    IDENTIFICATIONPARTS_ID: row.IDENTIFICATIONPARTS_ID,
                    PARTSNAME: row.PARTSNAME,
                    RISKUNITNAME: row.RISKUNITNAME,
                  },
                })
              "
            >
              匹配资源存在风险
            </el-button>
            <el-button
              v-if="buttonJurisdiction.edit"
              type="primary"
              text
              link
              @click="fnAddOrEdit(row.IDENTIFICATIONPARTS_ID, 'edit')"
            >
              编辑
            </el-button>
            <el-button
              v-if="buttonJurisdiction.del"
              type="primary"
              text
              link
              @click="fnDelete(row.IDENTIFICATIONPARTS_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button
            v-if="buttonJurisdiction.add"
            type="primary"
            @click="fnAddOrEdit('', 'add')"
          >
            新增
          </el-button>
          <el-button
            v-if="buttonJurisdiction.del"
            type="danger"
            @click="fnBatchDelete"
          >
            批量删除
          </el-button>
        </template>
      </layout-table>
    </layout-card>
    <layout-import-file
      v-model:visible="data.importDialogVisible"
      template-url="template/identificationpartsExcelTemplate.xls"
      @submit="fnSubmitImport"
    />
    <add
      v-model:visible="data.addOrEditDialog.visible"
      v-model:form="data.addOrEditDialog.form"
      :type="data.addOrEditDialog.type"
      @get-data="fnResetPagination"
    />
    <qr-code
      v-model:visible="data.qrCodeDialog.visible"
      :list="data.qrCodeDialog.list"
    />
  </div>
</template>

<script setup>
import { addingPrefixToFile, serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import {
  getIdentifyingPartsList,
  getIdentifyingPartsView,
  setIdentifyingPartsBatchDelete,
  setIdentifyingPartsDelete,
  setIdentifyingPartsImport,
} from "@/request/risk_control.js";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import { nextTick, reactive } from "vue";
import LayoutImportFile from "@/components/import_file/index.vue";
import Add from "./components/add.vue";
import { useRouter } from "vue-router";
import QrCode from "./components/qr_code.vue";
import LayoutTooltipImg from "@/components/tooltip_img/index.vue";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";

const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getIdentifyingPartsList);
const data = reactive({
  importDialogVisible: false,
  addOrEditDialog: {
    visible: false,
    type: "",
    form: {
      RISK_UNIT_ID: "",
      PARTSNAME: "",
      file: [],
    },
  },
  qrCodeDialog: {
    visible: false,
    list: [],
  },
});
const buttonJurisdiction = await useButtonJurisdiction("identificationparts");
const fnDelete = debounce(
  1000,
  async (IDENTIFICATIONPARTS_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    await setIdentifyingPartsDelete({ IDENTIFICATIONPARTS_ID });
    ElMessage.success("删除成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnImportDialogChangeShow = () => {
  data.importDialogVisible = !data.importDialogVisible;
};
const fnSubmitImport = async (formData) => {
  const resData = await setIdentifyingPartsImport(formData);
  if (resData.resultStr) {
    ElMessage({
      dangerouslyUseHTMLString: true,
      message: resData.resultStr,
      type: resData.resultType,
    });
  }
  fnImportDialogChangeShow();
  fnResetPagination();
};
const fnExport = async () => {
  const selectionData = tableRef.value.getSelectionRows();
  if (selectionData.length === 0) {
    ElMessage.warning("请选择需要导出至excel报表的记录信息");
    return;
  }
  await ElMessageBox.confirm("确定要导出到excel吗？", { type: "warning" });
  const DATA_IDS = selectionData
    .map((item) => item.IDENTIFICATIONPARTS_ID)
    .join(",");
  window.location.href =
    import.meta.env[import.meta.env.DEV ? "VITE_PROXY" : "VITE_BASE_URL"] +
    "identificationparts/excel?" +
    "&KEYWORDS=" +
    searchForm.value.KEYWORDS +
    "&DATA_IDS=" +
    DATA_IDS;
};
const fnBatchDelete = debounce(
  1000,
  async () => {
    const selectionData = tableRef.value.getSelectionRows();
    if (selectionData.length === 0) {
      ElMessage.warning("请选中要删除的项");
      return;
    }
    await ElMessageBox.confirm("确定要删除选中的数据吗？", { type: "warning" });
    const DATA_IDS = selectionData
      .map((item) => item.IDENTIFICATIONPARTS_ID)
      .join(",");
    await setIdentifyingPartsBatchDelete({ DATA_IDS });
    ElMessage.success("删除成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnAddOrEdit = async (IDENTIFICATIONPARTS_ID, type) => {
  data.addOrEditDialog.visible = true;
  await nextTick();
  data.addOrEditDialog.type = type;
  if (type === "edit") {
    const resData = await getIdentifyingPartsView({ IDENTIFICATIONPARTS_ID });
    data.addOrEditDialog.form = resData.pd;
    data.addOrEditDialog.form.file = addingPrefixToFile(resData.imgs);
  }
};
const fnQrCode = (row) => {
  data.qrCodeDialog.visible = true;
  data.qrCodeDialog.list = [row];
};
const fnBatchPrint = () => {
  const selectionData = tableRef.value.getSelectionRows();
  if (selectionData.length === 0) {
    ElMessage.warning("请选择需要打印的数据");
    return;
  }
  data.qrCodeDialog.list = selectionData;
  data.qrCodeDialog.visible = true;
};
</script>

<style scoped></style>
