<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="开课率最小" prop="brcReportCourseMin">
        <el-input
          v-model="queryParams.brcReportCourseMin"
          placeholder="请输入开课率最小"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="开课率最大" prop="brcReportCourseMax">
        <el-input
          v-model="queryParams.brcReportCourseMax"
          placeholder="请输入开课率最大"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="互动最小" prop="brcReportQuestMin">
        <el-input
          v-model="queryParams.brcReportQuestMin"
          placeholder="请输入互动最小"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="互动最大" prop="brcReportQuestMax">
        <el-input
          v-model="queryParams.brcReportQuestMax"
          placeholder="请输入互动最大"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="ai问答次数最小" prop="brcReportAiCountMin">
        <el-input
          v-model="queryParams.brcReportAiCountMin"
          placeholder="请输入ai问答次数最小"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="ai问答次数最大" prop="brcReportAiCountMax">
        <el-input
          v-model="queryParams.brcReportAiCountMax"
          placeholder="请输入ai问答次数最大"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="等级" prop="brcReportLevel">
        <el-input
          v-model="queryParams.brcReportLevel"
          placeholder="请输入等级"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="评语" prop="brcReportComment">
        <el-input
          v-model="queryParams.brcReportComment"
          placeholder="请输入评语"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </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="['report:manage:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['report:manage: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 label="开课率最小" align="center" prop="brcReportCourseMin" />
      <el-table-column label="开课率最大" align="center" prop="brcReportCourseMax" />
      <el-table-column label="互动最小" align="center" prop="brcReportQuestMin" />
      <el-table-column label="互动最大" align="center" prop="brcReportQuestMax" />
      <el-table-column label="ai问答次数最小" align="center" prop="brcReportAiCountMin" />
      <el-table-column label="ai问答次数最大" align="center" prop="brcReportAiCountMax" />
      <el-table-column label="等级" align="center" prop="brcReportLevel" />
      <el-table-column label="评语" align="center" prop="brcReportComment" width="240px"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            v-if="scope.row.tenantId == getTenantId()"
            @click="openForm('update', scope.row.brcId)"
            v-hasPermi="['report:manage:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            v-if="scope.row.tenantId == getTenantId()"
            @click="handleDelete(scope.row.brcId)"
            v-hasPermi="['report:manage: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>

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

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
// 修复导入路径，使用 @ 别名代替 src/
import { ManageApi, ManageVO } from '@/api/bjdxReport/reportmanage'
import ManageForm from './ManageForm.vue'
import { getTenantId } from '@/utils/auth'
import { ElButton } from 'element-plus'

/** 评估报告-评语 列表 */
defineOptions({ name: 'ReportManage' })

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

const loading = ref(true) // 列表的加载中
const list = ref<ManageVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  brcReportCourseMin: undefined,
  brcReportCourseMax: undefined,
  brcReportQuestMin: undefined,
  brcReportQuestMax: undefined,
  brcReportAiCountMin: undefined,
  brcReportAiCountMax: undefined,
  brcReportLevel: undefined,
  brcReportComment: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ManageApi.getManagePage(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 ManageApi.deleteManage(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ManageApi.exportManage(queryParams)
    download.excel(data, '评估报告-评语.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
