<script setup lang="ts">
import type { RecordItem } from '../types'
import { Upload, Promotion } from '@element-plus/icons-vue'
import AppList from '@/components/base/AppList.vue'
import { getExperimentRecordList, uploadCheckExperimentRecord } from '../api'
import { useFilterList } from '../composables/useFilterList'
import { useFileDialog } from '@vueuse/core'
const ScoreDialog = defineAsyncComponent(() => import('../components/ScoreDialog.vue'))

const { courses, experiments, specialties, classes } = useFilterList()

const LAB_URL = import.meta.env.VITE_LAB_URL

const appList = $ref<InstanceType<typeof AppList> | null>(null)

// 列表配置
const listOptions = $computed(() => {
  return {
    remote: {
      httpRequest: getExperimentRecordList,
      params: { student_name: '', course_id: '', experiment_id: '', specialty_id: '', class_id: '' }
    },
    filterForm: { labelWidth: 100 },
    filters: [
      {
        type: 'select',
        prop: 'course_id',
        label: '实验课程名称',
        placeholder: '请选择实验课程名称',
        options: courses.value,
        labelKey: 'name',
        valueKey: 'id'
      },
      {
        type: 'select',
        prop: 'experiment_id',
        label: '实验名称',
        placeholder: '请选择实验名称',
        options: experiments.value,
        labelKey: 'name',
        valueKey: 'id'
      },
      {
        type: 'select',
        prop: 'specialty_id',
        label: '专业',
        placeholder: '请选择专业',
        options: specialties.value,
        labelKey: 'name',
        valueKey: 'id'
      },
      {
        type: 'select',
        prop: 'class_id',
        label: '班级',
        placeholder: '请选择班级',
        options: classes.value,
        labelKey: 'name',
        valueKey: 'id'
      },
      { type: 'input', prop: 'student_name', label: '学生姓名', placeholder: '请输入学生姓名' }
    ],
    columns: [
      { label: '序号', type: 'index', width: 60 },
      { label: '所属专业', prop: 'specialty_name' },
      { label: '所属班级', prop: 'class_name' },
      { label: '姓名', prop: 'student_name' },
      { label: '学号', prop: 'sno_number' },
      { label: '实验名称', prop: 'experiment_name' },
      { label: '提交状态', prop: 'status_name', slots: 'table-status' },
      { label: '实验成绩', prop: 'score', slots: 'table-score' },
      { label: '更新时间', prop: 'commit_time' },
      { label: '操作', slots: 'table-x', width: 100 }
    ]
  }
})

// 批量导入
const { files, open } = useFileDialog()
function handleImport() {
  open({
    accept: '.csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel',
    multiple: false
  })
}
watchEffect(() => {
  if (!files.value?.length) return
  const [file] = files.value
  uploadCheckExperimentRecord({ file }).then(() => {
    appList?.refetch()
  })
})

let dialogVisible = $ref(false)
const rowData = ref<RecordItem>()
// 评分
function handleScore(row: RecordItem) {
  rowData.value = row
  dialogVisible = true
}
function onUpdateSuccess() {
  appList?.refetch()
}
</script>

<template>
  <AppCard title="实验成绩管理">
    <AppList v-bind="listOptions" ref="appList">
      <template #header-buttons>
        <el-row justify="space-between">
          <el-button type="primary" round :icon="Upload" @click="handleImport" v-permission="'v1-teacher-record-upload'"
            >批量导入</el-button
          >
          <a :href="LAB_URL" target="_blank">
            <el-button type="primary" round :icon="Promotion">进入实验室</el-button>
          </a>
        </el-row>
      </template>
      <template #table-status="{ row }">
        <span :class="{ 'is-success': row.status !== 0 }">{{ row.status_name }}</span>
      </template>
      <template #table-score="{ row }">
        <span :class="{ 'is-info': row.score !== '--' }">{{ row.score }}</span>
      </template>
      <template #table-x="{ row }">
        <el-button
          text
          type="primary"
          v-if="row.status === 1"
          @click="handleScore(row)"
          v-permission="'v1-teacher-record-check'"
          >打分</el-button
        >
      </template>
    </AppList>
  </AppCard>
  <ScoreDialog
    v-model="dialogVisible"
    :data="rowData"
    @update="onUpdateSuccess"
    v-if="dialogVisible && rowData"
  ></ScoreDialog>
</template>

<style lang="scss" scoped>
.is-success {
  color: #63a103;
}
.is-info {
  color: #b18862;
}
</style>
