<script setup lang='ts'>
import request from '~/utils/request'

const props = defineProps<{
  disabled: boolean
}>()
let dialogVisible = $ref(false)
const _ykl_lc_ = JSON.parse(sessionStorage.getItem('ykl_lc')!)

let data = $ref([])
async function handleClickLink() {
  if (props.disabled)
    return
  await handleQuery()
  dialogVisible = true
}

async function handleQuery() {
  try {
    const res = await request({
      url: '/yzy/kcgl/get_qk_stu',
      data: {
        ze_id: _ykl_lc_.ze_id,
      },
    })
    if (res.code === '1') {
      data = res?.data?.list
      dialogVisible = true
    }
    else {
      ElMessage({
        message: '获取缺考学生失败',
        type: 'success',
        grouping: true,
      })
    }
  }
  catch (error) {
    console.error(error)
    ElMessage({
      message: '获取缺考学生失败',
      type: 'success',
      grouping: true,
    })
  }
}

function handleClickSign(id?: string) {
  request({
    url: '/yzy/kcgl/onekey2fill',
    data: {
      ze_id: _ykl_lc_.ze_id,
      stuid: id,
    },
  }).then((res) => {
    if (res.code === '1') {
      ElMessage({
        message: res.msg,
        type: 'success',
        grouping: true,
      })
      handleQuery()
    }
    else {
      ElMessage({
        message: res.msg,
        type: 'error',
        grouping: true,
      })
    }
  }).catch((err) => {
    console.error(err)
    ElMessage({
      message: '标记缺考失败',
      type: 'success',
      grouping: true,
    })
  })
}
</script>

<template>
  <el-link type="primary" :disabled="disabled" @click="handleClickLink">
    标记缺考
  </el-link>
  <el-dialog v-model="dialogVisible" title="标记缺考" width="1200px">
    <el-table :data="data" style="width: 100%" max-height="550">
      <el-table-column prop="sm_name" label="学校" width="auto" />
      <el-table-column prop="cm_name" label="班级" width="210" />
      <el-table-column prop="student_no" label="学号" width="170" />
      <el-table-column prop="user_realname" label="姓名" width="130" />
      <el-table-column label="成绩" width="80">
        <template #default="{ row }">
          {{ row.score.ysk_cj }}
        </template>
      </el-table-column>
      <el-table-column label="主观题得分" width="100">
        <template #default="{ row }">
          {{ row.score.ysk_cj_zg }}
        </template>
      </el-table-column>
      <el-table-column label="客观题得分" width="100">
        <template #default="{ row }">
          {{ row.score.ysk_cj_kg }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="操作" width="120">
        <template #default="{ row }">
          <el-button
            :type="row.dtk.ysk_id === '0' ? 'primary' : 'danger'" size="small"
            @click="handleClickSign(row.user_id)"
          >
            {{ row.dtk.ysk_id === '0' ? '标记为缺考' : '取消标记缺考' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <template #footer>
      <div class="w-full flex justify-center">
        <el-button type="primary" @click="handleClickSign(undefined)">
          一键标记缺考
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
