<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryRef"
      :inline="true"
      :model="queryParams"
      @submit.prevent
    >
      <el-form-item label="成果名称" prop="title">
        <el-input
          v-model="queryParams.title"
          clearable
          placeholder="请输入专利名称"
          size="small"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          icon="Search"
          size="small"
          type="primary"
          @click="handleQuery"
          >搜索
        </el-button>
        <el-button icon="Refresh" size="small" @click="resetQuery"
          >重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column align="center" label="数据编号" prop="id" />
      <el-table-column align="center" label="需求名称" prop="title" />
      <el-table-column align="center" label="需求类别" prop="kind" />
      <el-table-column align="center" label="状态" prop="status">
        <template #default="{ row }">
          <dict-tag :options="serviceDemandStatusDict" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="联系人" prop="name" />
      <el-table-column align="center" label="手机" prop="mobile" />
      <el-table-column align="center" label="发布时间">
        <template #default="{ row }">
          <span>{{ dayjs(row.createTime).format("YYYY-MM-DD") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        class-name="small-padding fixed-width"
        label="操作"
      >
        <template #default="{ row }">
          <el-button icon="FolderRemove" size="small" type="text" @click=""
            >取消发布
          </el-button>
          <el-button
            icon="view"
            size="small"
            type="text"
            @click="handleDetail(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 { deleteLabPatentByIds } from "@/api/admin/laboratory/patent";
import dayjs from "dayjs";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";
import { serviceDemandList } from "@/api/admin/laboratory/service-demand";
import { serviceDemandStatusDict } from "@/constant/dict";
import DictTag from "@/components/DictTag/index.vue";

const router = useRouter();

const { proxy } = getCurrentInstance();

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");

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

const { queryParams } = toRefs(data);

const queryRef = ref();

/** 查询数据列表 */
const getList = async () => {
  loading.value = true;
  serviceDemandList(queryParams.value)
    .then((resp) => {
      dataList.value = resp.rows;
      total.value = resp.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
function resetQuery() {
  queryRef.value.resetFields();
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(id) {
  ElMessageBox.confirm('是否确认删除数据编号为"' + id + '"的专利项?')
    .then(function () {
      return deleteLabPatentByIds(id);
    })
    .then(() => {
      getList();
      ElMessage.success("删除成功");
    })
    .catch((err) => {
      console.log(err);
    });
}

/**
 * 前往查看详情页面
 * @param id
 */
const handleDetail = (id) => {
  router.push({
    path: `/admin/research/service-demand/detail/${id}`,
  });
};

getList();
</script>
