<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="50px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item
              label="风险点（单元）"
              prop="KEYWORDS"
              label-width="120px"
            >
              <el-input
                v-model="searchForm.KEYWORDS"
                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="Y"
              />
            </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-col :span="6">
            <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="fnGetDataTransfer"
        row-key="RISKUNIT_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="DEPT_NAME_ALL" label="所属部门" />
        <el-table-column prop="RISKUNITNAME" label="风险点（单元）" />
        <el-table-column label="操作" width="200">
          <template v-slot="{ row }">
            <el-button type="primary" text link @click="fnQrCode(row)">
              二维码
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/risk_control/risk_point/view',
                  query: {
                    RISKUNIT_ID: row.RISKUNIT_ID,
                    RISKUNITNAME: row.RISKUNITNAME,
                    DEPT_NAME: row.DEPT_NAME,
                  },
                })
              "
            >
              查看
            </el-button>
            <el-button
              v-if="buttonJurisdiction.edit"
              type="primary"
              text
              link
              @click="fnAddOrEdit(row.RISKUNIT_ID, 'edit')"
            >
              编辑
            </el-button>
            <el-button
              v-if="buttonJurisdiction.del"
              type="primary"
              text
              link
              @click="fnDelete(row.RISKUNIT_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/riskunitExcelTemplate.xls"
      @submit="fnSubmitImport"
    />
    <add
      v-model:visible="data.addOrEditDialog.visible"
      v-model:form="data.addOrEditDialog.form"
      :type="data.addOrEditDialog.type"
      @get-data="fnResetPaginationTransfer"
    />
    <qr-code
      v-model:visible="data.qrCodeDialog.visible"
      :list="data.qrCodeDialog.list"
    />
  </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 {
  getRiskPointList,
  getRiskPointView,
  setRiskPointBatchDelete,
  setRiskPointDelete,
  setRiskPointImport,
} 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 useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";

const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getRiskPointList);
const data = reactive({
  importDialogVisible: false,
  addOrEditDialog: {
    visible: false,
    type: "",
    form: {
      DEPARTMENT_ID: "",
      RISKUNITNAME: "",
    },
  },
  qrCodeDialog: {
    visible: false,
    list: [],
  },
});
const buttonJurisdiction = await useButtonJurisdiction("riskunit");
const fnGetDataTransfer = () => {
  fnGetData({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnDelete = debounce(
  1000,
  async (RISKUNIT_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    await setRiskPointDelete({ RISKUNIT_ID });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
const fnImportDialogChangeShow = () => {
  data.importDialogVisible = !data.importDialogVisible;
};
const fnSubmitImport = async (formData) => {
  const resData = await setRiskPointImport(formData);
  if (resData.resultStr) {
    ElMessage({
      dangerouslyUseHTMLString: true,
      message: resData.resultStr,
      type: resData.resultType,
    });
  }
  fnImportDialogChangeShow();
  fnResetPaginationTransfer();
};
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.RISKUNIT_ID).join(",");
  window.location.href =
    import.meta.env[import.meta.env.DEV ? "VITE_PROXY" : "VITE_BASE_URL"] +
    "/riskunit/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.RISKUNIT_ID).join(",");
    await setRiskPointBatchDelete({ DATA_IDS });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
const fnAddOrEdit = async (RISKUNIT_ID, type) => {
  data.addOrEditDialog.visible = true;
  await nextTick();
  data.addOrEditDialog.type = type;
  if (type === "edit") {
    const resData = await getRiskPointView({ RISKUNIT_ID });
    data.addOrEditDialog.form = resData.pd;
  }
};
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>
