<template>
  <div class="app-container">
    <!-- 顶部搜索区域 -->
    <el-form :model="queryData" :inline="true">
      <el-form-item label="企业名称">
        <el-input
          style="width: 150px"
          v-model="queryData.name"
          placeholder="请输入关键字"
        ></el-input>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select
          style="width: 100px"
          v-model="queryData.examineStatus"
          class="m-2"
          placeholder="请选择"
        >
          <el-option label="已审核" :value="1" />
          <el-option label="已拒绝" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="企业类型">
        <el-select
          style="width: 100px"
          v-model="queryData.kind"
          class="m-2"
          placeholder="请选择"
        >
          <el-option :value="101" label="上市企业" />
          <el-option :value="102" label="优质企业" />
          <el-option :value="103" label="普通企业" />
        </el-select>
      </el-form-item>
      <el-form-item label="站点">
        <SiteOptions
          v-model="queryData.tenantId"
          :site-list="siteList"
          style="width: 160px"
        />
      </el-form-item>

      <el-form-item style="float: right">
        <el-button
          type="primary"
          icon="search"
          size="default"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="refresh" size="default" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!--表格区域-->
    <el-table :data="dataList" style="width: 100%" border>
      <el-table-column prop="name" label="企业名称" align="center" />
      <el-table-column prop="code" label="统一社会信用代码" align="center" />
      <el-table-column label="所属领域" align="center">
        <template #default="{ row }">
          <div>{{ row.industrys[row.industrys.length - 1] }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="" label="站点" align="center">
        <template #default="{ row }">
          {{ siteList.find((el) => el.id === row.tenantId)?.name ?? "无" }}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="所在地" align="center" />
      <el-table-column prop="examineStatus" label="审核状态" align="center">
        <template #default="{ row }">
          {{ examineStatusDict[row.examineStatus] }}
        </template>
      </el-table-column>
      <el-table-column prop="kind" label="企业类型" align="center">
        <template #default="{ row }">
          <div>
            {{ enterpriseOptions.find((e) => row.kind == e.key).value }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="申请时间" align="center" />
      <el-table-column prop="" label="操作" width="200px" align="center">
        <template #default="{ row }">
          <el-button size="small" type="text" @click="handleDetail(row.id)"
            >审核</el-button
          >
          <el-button size="small" type="text" @click="delList(row.id)"
            >删除</el-button
          >
          <el-button size="small" type="text" @click="setType(row)"
            >设置企业类型</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryData.pageNum"
      v-model:limit="queryData.page_size"
      @pagination="getList"
    />

    <!-- 设置企业类型弹框 -->
    <el-dialog title="设置企业类型" v-model="isShow">
      <el-row type="flex" justify="center" style="height: 100px">
        <el-radio-group v-model="radioList">
          <el-radio label="101">上市企业</el-radio>
          <el-radio label="102">优质企业</el-radio>
          <el-radio label="103">普通企业</el-radio>
        </el-radio-group>
      </el-row>
      <el-row type="flex" justify="end">
        <el-button type="" @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="btnType">确认</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script setup>
import SiteOptions from "@/views/components/SiteOptions";
import { enterpriseList, editType, delItem } from "@/api/dataList/list";
import { tenantSelect } from "@/api/subPlatform/tenant";
import { enterpriseOptions } from "@/utils/parameter";
import { reactive, ref } from "vue";
import { ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
const data = reactive({
  queryData: {
    pageNum: 1,
    pageSize: 10,
    examineStatus: "",
  },
});
const router = useRouter();
const total = ref(0);
const dataList = ref([]);
const { queryData } = toRefs(data);
const siteList = ref([]);
const isShow = ref(false);
const radioList = ref("");
const examineStatusDict = {
  0: "审核中",
  1: "已通过",
  2: "已拒绝",
};
// 临时id
const id = ref("");
// 获取企业列表
const getList = async () => {
  const res = await enterpriseList(queryData.value);
  dataList.value = res.rows;
  total.value = res.total;
};
// 获取站点列表
const getSiteList = async () => {
  const resp = await tenantSelect();
  siteList.value = resp.rows;
};
// 搜索查询
const handleQuery = () => {
  getList(queryData.value);
};
// 重置按钮
const resetQuery = () => {
  queryData.value = {
    pageNum: 1,
    pageSize: 10,
    examineStatus: "",
  };
  getList();
};
// 企业类型弹框
const setType = (row) => {
  radioList.value = row.kind;
  isShow.value = true;
  id.value = row.id;
};
// 发送修改类型请求
const btnType = async () => {
  await editType({
    kind: radioList.value,
    id: id.value,
  });
  getList();
  isShow.value = false;
};
// 删除按钮
const delList = async (id) => {
  ElMessageBox.confirm(`是否确认删除编号为${id}的数据项？`)
    .then(async () => {
      await delItem(id);
      getList();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
  getList();
};
// 跳转详情页
const handleDetail = (id) => {
  router.push({
    path: "/approval/dataList/add",
    query: { id },
  });
};
getList();
getSiteList();
</script>
<style></style>
