<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link to="./release">
          <el-button type="primary" size="small">发布成果</el-button>
        </router-link>
      </el-col>
    </el-row>

    <el-radio-group
      v-model="queryParams.status"
      size="small"
      @change="handleQuery"
    >
      <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-button :label="3">草稿箱</el-radio-button>
    </el-radio-group>

    <el-table v-loading="loading" :data="dataList" style="margin-top: 20px">
      <el-table-column label="数据编号" prop="id" />
      <el-table-column label="成果名称" prop="title" />
      <el-table-column label="成果领域" prop="industryStr" />
      <!-- <el-table-column label="浏览量" prop="visitCount" /> -->
      <el-table-column label="发布时间" prop="createTime" width="180">
        <template #default="{ row }">
          <span>
            {{ dayjs(row.createTime).format("YYYY-MM-DD HH:mm:ss") }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <router-link
            :to="{ path: './release', query: { id: row.id } }"
            v-if="queryParams.status == 3"
          >
            <el-button size="small" type="text" icon="Edit">编辑</el-button>
          </router-link>

          <el-button
            v-if="queryParams.status == 1"
            size="small"
            type="text"
            :icon="row.shelfStatus == 1 ? `Download` : `Upload`"
            @click="handleShelf(row)"
            >{{ row.shelfStatus == 1 ? "下架" : "上架" }}</el-button
          >
          <el-button
            v-if="queryParams.status != 0"
            size="small"
            type="text"
            icon="Delete"
            @click="handleDelete(row.id)"
            >删除</el-button
          >
          <el-button
            v-if="queryParams.status == 1"
            size="small"
            type="text"
            icon="View"
            @click="handleResults(row)"
            >查看匹配结果</el-button
          >
          <el-button
            v-if="queryParams.status == 0"
            size="small"
            type="text"
            icon="Close"
            @click="handleCancelPublish(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 name="Achievement">
import {
  expertAchievementList,
  deleteAchievementByIds,
} from "@/api/admin/expert/achievement";
import dayjs from "dayjs";
import { ElMessage, ElMessageBox } from "element-plus";
// import { update } from "lodash-unified";
import { useRouter } from "vue-router";
import { updateExpertAchievement } from "@/api/admin/expert/achievement";
import { onActivated } from "vue";
const router = useRouter();
const dataList = ref([]);
const loading = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 1,
  },
});

const { queryParams } = toRefs(data);

/** 查询列表 */
const getList = async () => {
  loading.value = true;
  const resp = await expertAchievementList(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  loading.value = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
// function resetQuery() {
//   if (queryRef.value) {
//     queryRef.value.resetFields();
//   }
//   handleQuery();
// }
/** 删除按钮操作 */
const handleDelete = (id) => {
  ElMessageBox.confirm('是否确认删除数据编号为"' + id + '"的成果项？').then(
    async () => {
      await deleteAchievementByIds({ ids: [id] });
      getList();
      ElMessage.success("删除成功");
    }
  );
};
// 上架和下架
function handleShelf(row) {
  let text = row.shelfStatus == 2 ? "上架" : "下架";
  ElMessageBox.confirm('确认要"' + text + '""' + row.id + '"的成果吗?').then(
    async () => {
      const shelfStatus = row.shelfStatus == 1 ? 2 : 1;
      await updateExpertAchievement({ id: row.id, shelfStatus });
      getList();
      ElMessage.success(text + "成功");
    }
  );
}
// 取消发布
const handleCancelPublish = (id) => {
  ElMessageBox.confirm(`确认要取消发布编号为"${id}"的成果吗?`).then(
    async () => {
      await updateExpertAchievement({ id, status: "3" });
      ElMessage.success("取消发布成功");
      getList();
    }
  );
  // queryParams.value.status = "3";
  // handleQuery();
};
function handleResults(row) {
  router.push({
    path: "./results",
    query: {
      keyword: row.title,
    },
  });
}

onActivated(() => {
  getList();
});
</script>
