<template>
  <div class="app-container">
    <!-- 顶部区域 -->

    <el-form ref="queryFormRef" :model="queryParams" inline>
      <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">
            <site-options 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 class="mb8">
      <el-button
          v-if="queryParams.status == 0"
          :disabled="!selectedIds.length"
          size="small"
          type="primary"
          @click="handleBatchApproval"
      >批量审核
      </el-button
      >
      <el-button
          :disabled="!selectedIds.length"
          size="small"
          type="danger"
          @click="handleDelete(...selectedIds)"
      >批量删除
      </el-button
      >
      <el-button
          size="small"
          type="primary"
          @click="
          userStore.resetUnApproval('achievement', 'unApprovedAchivement')
        "
      >未读归零
      </el-button
      >
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="80"></el-table-column>
      <el-table-column
          align="center"
          label="成果名称"
          prop="title"
      ></el-table-column>
      <el-table-column
          align="center"
          label="成果领域"
          prop="industryStr"
      ></el-table-column>
      <el-table-column
          align="center"
          label="来源类型"
      >
        <template #default="{row}">
          {{ comeFromDict.find((el) => el.value === row.comeFrom)?.label }}
        </template>
      </el-table-column>
      <!--      来源名称-->
      <el-table-column
          align="center"
          label="来源名称"
      >
        <template #default="{row}">
          {{ row[`${comeFromDict.find((el) => el.value === row.comeFrom)?.field}Name`] }}
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="所属研究机构"
        align="center"
        prop=""
      ></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
          align="center"
          label="申请时间"
          prop="createTime"
      ></el-table-column>
      <el-table-column align="center" label="操作">
        <template #default="{ row }">
          <el-button round size="small" type="primary" @click="approval(row.id)"
          >审批
          </el-button
          >
          <el-button
              round
              size="small"
              type="danger"
              @click="handleDelete(row.id)"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />
  </div>
</template>
<script setup>
// import SiteOptions from "@/views/components/SiteOptions";
// import {} from "../../../api/dataApproval/achivement";
import {reactive, ref, toRefs} from "vue";
import {batchAchievement, deleteAchievementByIds, expertAchievementList,} from "@/api/dataApproval/achivement";
import useUserStore from "@/store/modules/user";
import {useRouter} from "vue-router";
import {ElMessage, ElMessageBox} from "element-plus";
import {comeFromDict} from "../../../constant/dict";

const userStore = useUserStore();
const router = useRouter();
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 0,
  },
});

const {queryParams} = toRefs(data);

const dataList = ref([]);
const siteList = ref([]);
const total = ref(0);
// 获取站点列表
// const getSiteList = async () => {
//   const resp = await tenantSelect();
//   siteList.value = resp.rows;
// };
// getSiteList();

const getList = async () => {
  const resp = await expertAchievementList(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
};

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

// 重置按钮
const resetQuery = () => {
  queryData.value = {
    pageNum: 1,
    pageSize: 10,
  };
  getList();
};
const approval = (id) => {
  router.push({
    path: "/auditing/achievement/approval",
    query: {
      id,
    },
  });
};

handleQuery();

const selectedIds = ref([]);
// 选中状态改变
const handleSelectionChange = (val) => {
  selectedIds.value = val.map((el) => el.id);
};
const handleDelete = async (...ids) => {
  ElMessageBox.confirm(`是否确认删除编号为${ids.join(",")}的数据项？`)
      .then(async () => {
        await deleteAchievementByIds(ids);
        // userStore.getApprovalCount();
        userStore.getUnApprovedAchivement();
        getList();
        ElMessage.success("删除成功");
      })
      .catch(() => {
      });
};

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