<template>
  <div class="app-container">
    <!-- 顶部区域 -->
    <el-form inline ref="queryFormRef" :model="queryParams">
      <el-row>
        <el-col>
          <el-form-item prop="status">
            <el-radio-group
                v-model="queryParams.status"
                size="small"
                @change="handleQuery"
            >
              <!-- <el-radio-button :label="1">已发布</el-radio-button> -->
              <el-radio-button :label="0">待审核</el-radio-button>
              <el-radio-button :label="1">已通过</el-radio-button>
              <el-radio-button :label="2">已驳回</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="所属站点" prop="tenantId">
            <SiteOptions v-model="queryParams.tenantId" :site-list="siteList"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button
              type="primary"
              icon="search"
              size="default"
              @click="handleQuery"
          >搜索
          </el-button
          >
          <el-button icon="refresh" size="default" @click="resetQuery"
          >重置
          </el-button
          >
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <el-button
          v-if="queryParams.status == 0"
          size="small"
          type="primary"
          :disabled="!selectedIds.length"
          @click="handleBatchApproval"
      >批量审核
      </el-button
      >
      <el-button
          size="small"
          type="danger"
          :disabled="!selectedIds.length"
          @click="handleDelete(...selectedIds)"
      >批量删除
      </el-button
      >
      <el-button
          size="small"
          type="primary"
          @click="userStore.resetUnApproval('product', 'unApprovedProduct')"
      >未读归零
      </el-button
      >
    </el-row>
    <!-- 表格区域 -->
    <el-table
        :data="dataList"
        :loading="loading"
        @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="80"></el-table-column>
      <el-table-column
          label="产品名称"
          align="center"
          prop="title"
      ></el-table-column>
      <el-table-column
          label="所属领域"
          align="center"
          prop="industryStr"
      ></el-table-column>
      <el-table-column
          label="所属企业"
          align="center"
          prop="enterpriseName"
      ></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="createTime"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button
              type="primary"
              round
              size="small"
              :disabled="queryParams.status != 0"
              @click="handleApproval(row.id)"
          >审批
          </el-button
          >
          <el-button
              type="danger"
              round
              size="small"
              @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.pageSize"
        @pagination="getList"
    />
  </div>
</template>
<script setup>
import {reactive, ref, toRefs} from "vue";
import SiteOptions from "@/views/components/SiteOptions";
import {tenantSelect} from "@/api/subPlatform/tenant";
import {
  enterpriseProductApprovalList,
  batchEnterpriseProduct,
  deleteEnterpriseProduct,
} from "@/api/dataApproval/enterpriseProduct";
import useUserStore from "@/store/modules/user";

import {useRouter} from "vue-router";
import {ElMessage, ElMessageBox} from "element-plus";

const userStore = useUserStore();
const router = useRouter();
const dataList = ref([]);
const siteList = ref([]);
const total = ref(0);
const loading = ref(true);
const queryFormRef = ref();
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 0,
  },
});
const {queryParams} = toRefs(data);

// const statusDic = {
//   0: "待审核",
//   1: "通过",
//   2: "驳回",
//   3: "草稿",
// };

const getList = async () => {
  loading.value = true;
  const resp = await enterpriseProductApprovalList(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  loading.value = false;
};

getList();
// 获取站点列表
const getSiteList = async () => {
  const resp = await tenantSelect();
  siteList.value = resp.rows;
};
getSiteList();

const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  if (queryFormRef.value) {
    queryFormRef.value.resetFields();
  }
  handleQuery();
};

const handleApproval = (id) => {
  router.push(`/auditing/Enterpriseproducts/approval?id=${id}`);
};

const selectedIds = ref([]);
// 选中状态改变
const handleSelectionChange = (val) => {
  selectedIds.value = val.map((el) => el.id);
};

const handleDelete = async (...ids) => {
  ElMessageBox.confirm(`是否确认删除编号为${ids.join(",")}的数据项？`)
      .then(async () => {
        await deleteEnterpriseProduct(ids);
        userStore.getUnApprovedProduct();
        getList();
        ElMessage.success("删除成功");
      })
      .catch(() => {
      });
};

const handleBatchApproval = async () => {
  ElMessageBox.confirm(
      `是否确认批量审核通过编号为${selectedIds.value.join(",")}的数据项？`
  )
      .then(async () => {
        await batchEnterpriseProduct(selectedIds.value.join(","), 1);
        userStore.getUnApprovedProduct();
        getList();
        ElMessage.success("批量审核成功");
      })
      .catch(() => {
      });
};
userStore.getUnApprovedProduct();
userStore.getProductTotal();
</script>
<style></style>
