<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="visit_count" /> -->
      <el-table-column label="发布时间" prop="createTime" width="180">
        <template #default="{ row }">
          <span>{{ row.createTime }}</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 == 2 ? 'Upload' : 'Download'"
            @click="handleShelf(row)"
            >{{ row.shelfStatus == 2 ? "上架" : "下架" }}</el-button
          >
          <el-button
            v-if="queryParams.status != 1"
            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.id)"
            >查看匹配结果</el-button
          >
          <el-button
            v-if="queryParams.status == 1"
            size="small"
            type="text"
            icon="View"
            @click="checkEnterpriseInfo(row.id)"
            >浏览企业信息</el-button
          >
          <el-button
            v-if="queryParams.status == 0"
            size="small"
            type="text"
            icon="Close"
            @click="releaseCancel(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="Product">
import {
  getEnterpriseProduct,
  deleteEnterpriseProduct,
  updateEnterpriseProduct,
} from "@/api/admin/enterprise/product";
import { ElMessage, ElMessageBox } from "element-plus";
import { onActivated } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();

const dataList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const dateRange = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 0, // 状态(0-待审核1-通过2-驳回3-草稿)
  },
});

const { queryParams } = toRefs(data);

/** 查询列表 */
const getList = async () => {
  loading.value = true;
  const resp = await getEnterpriseProduct(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  loading.value = false;
};

// 处理删除
const handleDelete = (id) => {
  ElMessageBox.confirm('是否确认删除数据编号为"' + id + '"的产品项？')
    .then(async () => {
      await deleteEnterpriseProduct(id);
      ElMessage.success("删除成功");
      getList();
    })
    .catch((error) => {
      console.log("已取消");
    });
};

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
// 取消发布
const releaseCancel = (id) => {
  ElMessageBox.confirm('确认要取消发布id为"' + id + '"的产品吗?')
    .then(async () => {
      await updateEnterpriseProduct({ id, status: 3 });
      ElMessage.success("取消发布成功");
      getList();
    })
    .catch((err) => {
      console.log(err);
    });
};
// 下架和上架
const handleShelf = (row) => {
  let text = row.shelfStatus == 2 ? "上架" : "下架";
  ElMessageBox.confirm('确认要"' + text + '""' + row.id + '"的产品吗?')
    .then(function () {
      let status = row.shelfStatus == 1 ? 2 : 1;
      return updateEnterpriseProduct({ id: row.id, shelfStatus: status });
    })
    .then(() => {
      getList();
      ElMessage.success(text + "成功");
    })
    .catch(() => {});
};
const handleResults = (id) => {
  router.push({ path: "./results" });
};
const checkEnterpriseInfo = (id) => {
  router.push({ path: "./business" });
};

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