<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      @submit.prevent
    >
      <el-form-item label="成果名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入成果名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="postList">
      <el-table-column label="数据编号" prop="id" />
      <el-table-column label="成果名称" prop="title" />
      <el-table-column label="成果领域" prop="industrys" />
      <el-table-column label="所属专家" prop="industrys" />
      <el-table-column label="浏览量" prop="visit_count" />
      <el-table-column label="发布时间" prop="created_at" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.created_at) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="Download"
            @click="handleShelf(scope.row)"
            >下架</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="Delete"
            @click="handleDelete(scope.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="Post">
import {
  expertAchievement,
  achievementShelf,
  achievementDelete,
} from "@/api/admin/expert/technology";

const { proxy } = getCurrentInstance();
const router = useRouter();

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

const data = reactive({
  form: {},
  queryParams: {
    page_num: 1,
    page_size: 10,
    name: undefined,
  },
  rules: {
    name: [{ required: true, message: "公司名称不能为空", trigger: "blur" }],
    dutyParagraph: [
      { required: true, message: "公司税号不能为空", trigger: "blur" },
    ],
    bank: [{ required: true, message: "开户行不能为空", trigger: "blur" }],
    bankAccount: [
      { required: true, message: "开户行账号不能为空", trigger: "blur" },
    ],
    bankPhone: [
      { required: true, message: "开户行电话不能为空", trigger: "blur" },
    ],
    email: [{ required: true, message: "电子邮箱不能为空", trigger: "blur" }],
    phone: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
    address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
    username: [{ required: true, message: "联系人不能为空", trigger: "blur" }],
    userPhone: [
      { required: true, message: "联系人电话不能为空", trigger: "blur" },
    ],
    userAddress: [
      { required: true, message: "邮寄地址不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  expertAchievement(queryParams.value).then((response) => {
    postList.value = [1];
    // postList.value = response.data.data;
    // total.value = response.data.count;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 删除按钮操作 */
function handleDelete(id) {
  proxy.$modal
    .confirm('是否确认删除数据编号为"' + id + '"的成果项？')
    .then(function () {
      return achievementDelete({ id });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
function handleShelf(row) {
  let text = row.shelf_status == 2 ? "上架" : "下架";
  proxy.$modal
    .confirm('确认要"' + text + '""' + row.id + '"的成果吗?')
    .then(function () {
      let status = row.shelf_status == 1 ? 2 : 1;
      return achievementShelf({ id: row.id, status });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess(text + "成功");
    })
    .catch(() => {});
}
function handleResults(id) {
  router.push({ path: "./results" });
}
getList();
</script>
