<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" />
            </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-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        v-model:pagination="pagination"
        :data="list"
        row-key="RISKWARNING_ID"
        @get-data="fnGetData"
      >
        <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="NAME" label="文件名" />
        <el-table-column label="图片">
          <template #default="{ row }">
            <layout-tooltip-img :imgs="row.imgs" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="fnDownload(row.RISKWARNING_ID)"
            >
              下载
            </el-button>
            <el-button
              v-show="buttonJurisdiction.edit"
              type="primary"
              text
              link
              @click="fnAddOrEdit(row.RISKWARNING_ID, 'edit')"
            >
              编辑
            </el-button>
            <el-button
              v-show="buttonJurisdiction.del"
              type="primary"
              text
              link
              @click="fnDelete(row.RISKWARNING_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button
            v-show="buttonJurisdiction.add"
            type="primary"
            @click="fnAddOrEdit('', 'add')"
          >
            新增
          </el-button>
          <el-button
            v-show="buttonJurisdiction.del"
            type="danger"
            @click="fnBatchDelete"
          >
            批量删除
          </el-button>
        </template>
      </layout-table>
    </layout-card>
    <add
      v-model:visible="data.addOrEditDialog.visible"
      v-model:form="data.addOrEditDialog.form"
      :type="data.addOrEditDialog.type"
      :list-type="listType"
      @get-data="fnResetPagination"
    />
  </div>
</template>

<script setup>
import { addingPrefixToFile, serialNumber } from "@/assets/js/utils";
import useListData from "@/assets/js/useListData.js";
import LayoutTooltipImg from "@/components/tooltip_img/index.vue";
import {
  getList,
  getView,
  setBatchDelete,
  setDelete,
} from "@/request/risk_statement.js";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { debounce } from "throttle-debounce";
import { nextTick, reactive } from "vue";
import Add from "./components/add.vue";

const props = defineProps({
  listType: {
    type: Array,
    required: true,
    validator: (value) => {
      if (value.length === 2) {
        return true;
      } else {
        throw new Error("listType必须是长度为2的数组");
      }
    },
    default: () => ["1", "16"],
  },
});
const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getList, {
    otherParams: { TYPE: props.listType[0] },
  });
const data = reactive({
  addOrEditDialog: {
    visible: false,
    type: "",
    form: {
      NAME: "",
      file: [],
    },
  },
});
const buttonJurisdiction = await useButtonJurisdiction("riskwarning");
const fnDownload = async (RISKWARNING_ID) => {
  await ElMessageBox.confirm("确定要下载此文件吗？", {
    type: "warning",
  });
  window.location.href =
    import.meta.env[import.meta.env.DEV ? "VITE_PROXY" : "VITE_BASE_URL"] +
    "riskwarning/goDownload?RISKWARNING_ID=" +
    RISKWARNING_ID +
    "&TYPE=" +
    props.listType[1];
};
const fnDelete = debounce(
  1000,
  async (RISKWARNING_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", {
      type: "warning",
    });
    await setDelete({ RISKWARNING_ID, TYPE: props.listType[1] });
    ElMessage.success("删除成功");
    fnResetPagination();
  },
  { atBegin: true }
);
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.RISKWARNING_ID).join(",");
    await setBatchDelete({ DATA_IDS });
    ElMessage.success("删除成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnAddOrEdit = async (RISKWARNING_ID, type) => {
  data.addOrEditDialog.visible = true;
  await nextTick();
  data.addOrEditDialog.type = type;
  if (type === "edit") {
    const resData = await getView({
      RISKWARNING_ID,
      TYPE: props.listType[1],
    });
    data.addOrEditDialog.form = resData.pd;
    data.addOrEditDialog.form.file = addingPrefixToFile(resData.imgs);
  }
};
</script>

<style scoped></style>
