<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.achievement') })"
        prop="name"
      >
        <el-input
          v-model="queryParams.name"
          clearable
          :placeholder="
            t('admin.form.placeholder', { type: t('admin.common.patent') })
          "
          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 :label="t('admin.common.dataNumber')" prop="id" />
      <el-table-column
        :show-overflow-tooltip="true"
        :label="
          t('admin.form.name', {
            type: t('admin.common.researchProject'),
          })
        "
        prop="title"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        :label="t('admin.common.type')"
        prop="kind"
      />
      <!--      <el-zh-column :label="t('admin.form.belongExpert')" prop="inventor" />-->
      <el-table-column :label="t('admin.table.topicRole')">
        <template #default="{ row }">
          {{
            subjectRoleDict.find((item) => item.value === row.role)?.[locale]
          }}
        </template>
      </el-table-column>
      <el-table-column :label="t('admin.form.topicLeader')" prop="director" />
      <el-table-column
        align="center"
        :label="t('admin.table.publishTime')"
        prop="applyAt"
      >
        <template #default="{ row }">
          <span>{{ dayjs(row.applyAt).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="download"-->
          <!--            size="small"-->
          <!--            type="text"-->
          <!--            @click="handleTakeOff(row.id)"-->
          <!--            >下架-->
          <!--          </el-button>-->
          <el-button
            icon="Delete"
            size="small"
            type="text"
            @click="handleDelete(row.id)"
          >
            {{ t("admin.common.delete") }}
          </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 dayjs from "dayjs";
import { ElMessage } from "element-plus";
import modal from "@/plugins/modal";
import { useRouter } from "vue-router";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";
import {
  deleteTechnologyProject,
  technologyProjectList,
} from "@/api/admin/laboratory/research-project";
import { subjectRoleDict } from "@/constant/dict";
import { useI18n } from "vue-i18n";

const { t, locale } = 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;
  technologyProjectList(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", {
        number: id,
        action: t("admin.common.delete"),
        type: t("admin.common.researchProject"),
      })
    )
    .then(function () {
      return deleteTechnologyProject(id);
    })
    .then(() => {
      getList();
      ElMessage.success(t("admin.common.deleteSuccess"));
    })
    .catch((err) => {
      console.log(err);
    });
}

/**
 * 下架
 * @param id
 */
const handleTakeOff = (id) => {};

getList();
</script>
