<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
          <!-- <el-form-item label="账号" prop="userName">
        <el-select
          v-model="queryParams.userName"
          placeholder="请选择账号"
          clearable
          filterable
          class="!w-240px"
        >
          <el-option
            v-for="user in users"
            :key="user.id"
            :label="user.nickname"
            :value="user.nickname"
          />
        </el-select>
      </el-form-item> -->

      <el-form-item label="成果名称" prop="achievementName">
        <el-input
          v-model="queryParams.achievementName"
          placeholder="请输入成果名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="成果类型" prop="achievementType">
        <el-select
          v-model="queryParams.achievementType"
          placeholder="请选择成果类型"
          clearable
          class="!w-240px"
        >
          <el-option :label="'北大中文核心期刊论文'" :value="1" />
          <el-option :label="'SCI、EI、SSCI、A&HCI国际学术期刊论文'" :value="2" />
          <el-option :label="'SCI、EI、ISIP会议论文'" :value="3" />
          <el-option :label="'专利'" :value="4" />
          <el-option :label="'著作'" :value="5" />
          <el-option :label="'竞赛'" :value="6" />
        </el-select>
      </el-form-item>
      <el-form-item label="署名" prop="sort">
        <el-select
          v-model="queryParams.sort"
          placeholder="请选择署名"
          clearable
          class="!w-240px"
        >
          <el-option :label="'第一作者'" :value="1" />
          <el-option :label="'第二作者'" :value="2" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!-- <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:user-achievement:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button> -->
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:user-achievement:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column type="index" width="50" />
      <el-table-column label="Id" align="center" prop="id" />
      <el-table-column label="用户 Id" align="center" prop="userId" v-if="false" />
      <!-- <el-table-column label="姓名" align="center" prop="userName" /> -->
      <el-table-column label="成果名称" align="center" prop="achievementName" />
      <el-table-column label="成果类型" align="center" prop="achievementType">
        <template #default="rowData">
          <span>
            {{
              rowData.row.achievementType == 1
               ? '北大中文核心期刊论文'
                : rowData.row.achievementType == 2
               ? 'SCI、EI、SSCI、A&HCI国际学术期刊论文'
                : rowData.row.achievementType == 3
               ? 'SCI、EI、ISIP会议论文'
                : rowData.row.achievementType ==4
               ? '专利'
                : rowData.row.achievementType == 5
               ? '著作'
                : rowData.row.achievementType == 6
               ? '竞赛'
                : '未知类型'
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="署名" align="center" prop="sort">
        <template #default="rowData">
          <span>
            {{ 
              rowData.row.sort == 1
               ? '第一作者'
                : rowData.row.sort == 2
               ? '第二作者'
                : '其他'
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否达成毕业条件" align="center" prop="isGraduate">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.SYSTEM_STUDENT_GRADUATE_STATUS" :value="scope.row.isGraduate" />  
            </template>
          </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="图片材料" align="center">
        <!-- #default获取当前·行数据 -->
        <template #default="{ row }">
          <a :href="row.detail" target="_blank" v-if="row.detail">{{ row.detail }}</a>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" min-width="120px">
        <template #default="rowData">
          <el-button
            link
            type="primary"
            @click="openForm('update', rowData.row.id)"
            v-hasPermi="['system:user-achievement:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(rowData.row.id)"
            v-hasPermi="['system:user-achievement:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <UserAchievementForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { UserAchievementApi, UserAchievementVO } from '@/api/system/userAchievement'
import UserAchievementForm from './UserAchievementForm.vue'
import * as UserApi from '@/api/system/user'
import { useRoute } from 'vue-router'

/** 成果 列表 */
defineOptions({ name: 'UserAchievement' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const route = useRoute()
const id = route.query.id 

const loading = ref(true) // 列表的加载中
const list = ref<UserAchievementVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: id,
  achievementName: undefined,
  achievementType: undefined,
  userName: undefined,
  createTime: [],
  detail: undefined,
  sort: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const users = ref()
const getDeptUser = async () => {
  try {
    const response = await UserApi.getDeptUser()
    users.value = response
  } catch (error) {
    console.error('Error fetching user data:', error)
  }
}

const getList = async () => {
  loading.value = true
  try {
    const data = await UserAchievementApi.getUserAchievementPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

const handleDelete = async (id: number) => {
  try {
    await message.delConfirm()
    await UserAchievementApi.deleteUserAchievement(id)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const handleExport = async () => {
  try {
    await message.exportConfirm()
    exportLoading.value = true
    const data = await UserAchievementApi.exportUserAchievement(queryParams)
    download.excel(data, '成果.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

onMounted(() => {
  getList()
  getDeptUser()
})
</script>