<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="考试编号" prop="examCode">
        <el-input v-model="queryParams.examCode" placeholder="请输入考试编号" clearable style="width: 200px" />
      </el-form-item>
      <el-form-item label="考试名称" prop="examName">
        <el-input v-model="queryParams.examName" placeholder="请输入考试名称" clearable style="width: 200px" />
      </el-form-item>
      <el-form-item label="考试人姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入考试人姓名" clearable style="width: 200px" />
      </el-form-item>
      <el-form-item label="考试时间" prop="examTime">
        <el-date-picker v-model="queryParams.examTime" type="daterange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="YYYY-MM-DD" style="width: 240px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="info" plain icon="Upload" @click="handleImport">导入</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="trainingPerformanceList" >
      <el-table-column label="考试编号" align="center" prop="examCode"  />
      <el-table-column label="考试名称" align="center" prop="examName"  />
      <el-table-column label="考试人编号" align="center" prop="userCode" />
      <el-table-column label="考试人姓名" align="center" prop="userName" />
      <el-table-column label="考试时间" align="center" prop="examTime" >
        <template #default="scope">
          <span>{{ parseTime(scope.row.examTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考试成绩" align="center" prop="examScore"  />
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
      <el-upload ref="uploadRef" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <template #tip>
          <div class="el-upload__tip text-center">
              <span>仅允许导入xls、xlsx格式文件。</span>
              <el-link type="primary" :underline="false" style="font-size:16px;vertical-align: baseline;" @click="handleDownloadTemplate">下载模板</el-link>
            </div>
        </template>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TrainingPerformance">
import { getGrowthList, importData, downloadTemplate } from "@/api/examManage/trainingPerformance"
import { getToken } from "@/utils/auth"
import { addDateRange, parseTime } from "@/utils/ruoyi"
import { UploadFilled } from '@element-plus/icons-vue'

const { proxy } = getCurrentInstance()

const trainingPerformanceList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const dateRange = ref([])

// 导入参数
const upload = reactive({
  // 是否显示弹出层
  open: false,
  // 弹出层标题
  title: "",
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/system/growth/importData"
})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    examCode: undefined,
    examName: undefined,
    userName: undefined,
    examTime: []
  }
})

const { queryParams } = toRefs(data)

/** 查询培训绩效列表 */
function getList() {
  loading.value = true;
  let params = {
    ...queryParams.value,
    startTime: queryParams.value.examTime.length > 0 ? queryParams.value.examTime[0] : undefined,
    endTime: queryParams.value.examTime.length > 0 ? queryParams.value.examTime[1] : undefined
  }
  delete params.examTime
  getGrowthList(params).then(response => {
    trainingPerformanceList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 导入按钮操作 */
function handleImport() {
  upload.title = "导入"
  upload.open = true
}

/** 下载模板操作 */
function handleDownloadTemplate() {

  proxy.download("/system/growth/importTemplate", {}, `record_template_${new Date().getTime()}.xlsx`)
}

/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
  upload.isUploading = true
}

/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
  upload.open = false
  upload.isUploading = false
  proxy.$refs["uploadRef"].handleRemove(file)
  if (response.code === 200) {
    proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true })
    getList()
  } else {
    proxy.$modal.msgError(response.msg)
  }
}

/** 提交上传文件 */
function submitFileForm() {
  proxy.$refs["uploadRef"].submit()
}

getList()
</script>
