<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="企业名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入企业名称"
          clearable
          size="default"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属站点" prop="tenantId">
        <el-select
          v-model="queryParams.tenantId"
          placeholder="请选择，支持搜索"
          size="default"
          :loading="siteList.length === 0"
          filterable
          clearable
        >
          <el-option
            v-for="item in siteList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          size="default"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="upload" size="default" @click=""
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="download"
          size="default"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="数据编号" align="center" prop="id" />
      <el-table-column label="企业名称" align="center" prop="name" />
      <el-table-column label="统一社会信用代码" align="center" prop="code" />
      <el-table-column label="所属领域" align="center" show-overflow-tooltip>
        <template #default="{ row }">
          <div>{{ row.industrys[row.industrys.length - 1] }}</div>
        </template>
      </el-table-column>
      <el-table-column label="站点" align="center">
        <template #default="{ row }">
          {{ siteList.find((el) => el.id === row.tenantId)?.name || "无" }}
        </template>
      </el-table-column>
      <el-table-column label="所在地" align="center" prop="address" />
      <el-table-column label="企业类型" align="center" prop="kind">
        <template #default="{ row }">
          <div>
            {{ enterpriseOptions.find((e) => row.kind == e.key).value }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center">
        <template #default="{ row }">
          <span>{{ row.createdAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button
            size="small"
            type="text"
            icon="edit"
            @click="handleDetail(row.id)"
            >修改</el-button
          >
          <el-button
            size="small"
            type="text"
            icon="delete"
            @click="handleDelete(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.page_size"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Enterprise">
import {
  companyList,
  companyExport,
  companyDelete,
} from "@/api/dataList/enterprise";
import { download } from "@/utils/request";
import dayjs from "dayjs";
import { tenantSelect } from "@/api/subPlatform/tenant";
import { reactive, ref, toRefs } from "vue";
import { useRouter } from "vue-router";
import { enterpriseOptions } from "@/utils/parameter";
import { ElMessageBox } from "element-plus";
const router = useRouter();
const data = reactive({
  queryParams: {
    pageNum: 1,
    page_size: 10,
    examine_status: 2,
    name: undefined,
    tenantId: undefined,
  },
});
const { queryParams } = toRefs(data);
const showSearch = ref(true);
const siteList = ref([]);
const loading = ref(true);
const total = ref(0);
const dataList = ref([]);

/** 查询企业列表 */
const getList = async () => {
  loading.value = true;
  const resp = await companyList(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  loading.value = false;
};
// 获取站点列表
const getSiteList = async () => {
  const resp = await tenantSelect();
  siteList.value = resp.rows;
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [];
  if (queryFormRef.value) {
    queryFormRef.value.resetFields();
  }
  handleQuery();
};
const handleAdd = () => {
  router.push({
    path: "/dataList/enterprise/add",
  });
};
const handleDetail = (id) => {
  router.push({
    path: "/dataList/enterprise/add",
    query: { id },
  });
};
const handleDelete = async (id) => {
  ElMessageBox.confirm(`是否确认删除编号为${id}的数据项？`)
    .then(async () => {
      await companyDelete(id);
      getList();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
};
const handleExport = () => {
  ElMessageBox.confirm(`是否确认企业列表？`)
    .then(() => {
      download("/business/enterprise/export", {}, "res.xls");
      // companyExport();
    })
    .catch(() => {});
};
getList();
getSiteList();
</script>
