<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryRef"
      :inline="true"
      :model="queryParams"
      @submit.prevent
    >
      <el-form-item
        :label="t('admin.form.name', { type: t('admin.common.demand') })"
        prop="title"
      >
        <el-input
          v-model="queryParams.title"
          clearable
          :placeholder="
            t('admin.form.placeholder', { type: t('admin.common.demand') })
          "
          size="small"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          icon="Search"
          size="small"
          type="primary"
          @click="handleQuery"
        >
          {{ t("admin.common.search") }}
        </el-button>
        <el-button icon="Refresh" size="small" @click="resetQuery">
          {{ t("admin.common.reset") }}
        </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="t('admin.common.dataNumber')"
        prop="id"
      />
      <el-table-column
        align="center"
        :label="t('admin.form.name', { type: t('admin.common.demand') })"
        prop="title"
      />
      <el-table-column
        align="center"
        :label="t('admin.form.category', { type: t('admin.common.demand') })"
        prop="kind"
      />
      <el-table-column align="center" :label="t('common.status')" prop="status">
        <template #default="{ row }">
          <dict-tag :options="serviceDemandStatusDict" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="t('admin.form.contact')"
        prop="name"
      />
      <el-table-column
        align="center"
        :label="t('admin.form.mobile')"
        prop="mobile"
      />
      <el-table-column align="center" :label="t('admin.table.publishTime')">
        <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="t('admin.common.operation')"
      >
        <template #default="{ row }">
          <!--          <el-button icon="FolderRemove" size="small" type="text" @click=""-->
          <!--            >{{ t("admin.table.cancelPublish") }}-->
          <!--          </el-button>-->
          <el-button
            icon="view"
            size="small"
            type="text"
            @click="handleDetail(row.id)"
            >{{ t("admin.common.view") }}
          </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 modal from "@/plugins/modal";
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";

import { useI18n } from "vue-i18n";

const { t } = useI18n();
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) {
  modal
    .confirm(
      t("admin.common.confirmAction", {
        type: t("admin.common.patent"),
        action: t("admin.common.delete"),
        number: id,
      })
    )
    .then(function () {
      return deleteLabPatentByIds(id);
    })
    .then(() => {
      getList();
      ElMessage.success(t("admin.common.deleteSuccess"));
    })
    .catch((err) => {
      console.log(err);
    });
}

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

getList();
</script>
