<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link to="./release">
          <el-button size="small" type="primary"
            >{{ t("admin.table.publishAchievement") }}
          </el-button>
        </router-link>
      </el-col>
    </el-row>

    <el-radio-group
      v-model="queryParams.status"
      size="small"
      @change="handleQuery"
    >
      <el-radio-button :label="0"
        >{{ t("admin.table.pendingReview") }}
      </el-radio-button>
      <el-radio-button :label="1"
        >{{ t("admin.table.published") }}
      </el-radio-button>
      <el-radio-button :label="2"
        >{{ t("admin.table.rejected") }}
      </el-radio-button>
      <el-radio-button :label="3"
        >{{ t("admin.table.draftBox") }}
      </el-radio-button>
    </el-radio-group>

    <el-table v-loading="loading" :data="dataList" style="margin-top: 20px">
      <el-table-column :label="t('admin.common.dataNumber')" prop="id" />
      <el-table-column
        :label="t('admin.form.name', { type: t('admin.common.achievement') })"
        prop="title"
      />
      <el-table-column :label="t('admin.form.keywords')" prop="keyword" />
      <!-- <el-zh-column label="浏览量" prop="visitCount" /> -->
      <el-table-column :label="t('admin.form.industry')" prop="industryStr" />
      <el-table-column
        :label="t('admin.table.publishTime')"
        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
        v-if="false"
        :label="t('admin.common.operation')"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <router-link
            v-if="queryParams.status == 3"
            :to="{ path: './release', query: { id: row.id } }"
          >
            <el-button icon="Edit" size="small" type="text"
              >{{ t("admin.common.edit") }}
            </el-button>
          </router-link>

          <el-button
            v-if="queryParams.status == 1"
            :icon="row.shelfStatus == 1 ? `Download` : `Upload`"
            size="small"
            type="text"
            @click="handleShelf(row)"
            >{{
              row.shelfStatus == 1
                ? t("admin.table.putOn")
                : t("admin.table.putOff")
            }}
          </el-button>
          <el-button
            v-if="queryParams.status != 0"
            icon="Delete"
            size="small"
            type="text"
            @click="handleDelete(row.id)"
          >
            {{ t("admin.common.delete") }}
          </el-button>
          <el-button
            v-if="queryParams.status == 1"
            icon="View"
            size="small"
            type="text"
            @click="handleResults(row)"
            >{{ t("webSearch.viewMatchingResults") }}
          </el-button>
          <el-button
            v-if="queryParams.status == 0"
            icon="Close"
            size="small"
            type="text"
            @click="handleCancelPublish(row.id)"
            >{{ t("admin.table.cancelPublish") }}
          </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 name="Achievement" setup>
import {
  deleteAchievementByIds,
  expertAchievementList,
  updateExpertAchievement,
} from "@/api/admin/expert/achievement";
import { ElMessage } from "element-plus";
import modal from "@/plugins/modal"; // import { update } from "lodash-unified";
import { useRouter } from "vue-router";
import { onActivated, reactive } from "vue";
import { useI18n } from "vue-i18n";
import dayjs from "dayjs";

const router = useRouter();
const dataList = ref([]);
const loading = ref(true);
const total = ref(0);
const { t } = useI18n();
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) => {
  modal
    .confirm(
      t("admin.common.confirmAction", {
        number: id,
        action: t("admin.common.delete"),
        type: t("admin.common.achievement"),
      })
    )
    .then(async () => {
      await deleteAchievementByIds({ ids: [id] });
      getList();
      ElMessage.success(t("admin.common.deleteSuccess"));
    });
};

// 上架和下架
function handleShelf(row) {
  let text =
    row.shelfStatus == 2 ? t("admin.table.putOn") : t("admin.table.putOff");
  modal
    // .confirm('确认要"' + text + '""' + row.id + '"的成果吗?')
    .confirm(
      t("admin.common.confirmAction", {
        number: row.id,
        action: text,
        type: t("admin.common.achievement"),
      })
    )
    .then(async () => {
      const shelfStatus = row.shelfStatus == 1 ? 2 : 1;
      await updateExpertAchievement({ id: row.id, shelfStatus });
      getList();
      ElMessage.success(
        t("admin.common.success", {
          action: text,
        })
      );
    });
}

// 取消发布
const handleCancelPublish = (id) => {
  modal
    // .confirm(`确认要取消发布编号为"${id}"的成果吗?`)
    .confirm(
      t("admin.common.confirmAction", {
        number: id,
        action: t("admin.table.cancelPublish"),
        type: t("admin.common.achievement"),
      })
    )
    .then(async () => {
      await updateExpertAchievement({ id, status: "3" });
      ElMessage.success(t("tips.cancelReleaseSuccess"));
      getList();
    });
  // queryParams.value.status = "3";
  // handleQuery();
};

function handleResults(row) {
  router.push({
    path: "./results",
    query: {
      keyword: row.title,
    },
  });
}

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