<template>
  <div>
    <el-dialog v-model="newStuCheckInDialog" width="600">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" disabled/>
        </el-form-item>

        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio v-for="item in genderData" :key="item.value" :label="item.value" :disabled="true">{{ item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="form.idNumber" disabled/>
        </el-form-item>

        <el-form-item label="报到状态" prop="backSchoolState">
          <el-select v-model="form.backSchoolState" filterable placeholder="请选择报到状态">
            <el-option
              v-for="item in checkInStatusData"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="回校备注" prop="backSchoolRemark">
          <el-input v-model="form.backSchoolRemark" :rows="2" type="textarea"/>
        </el-form-item>
        
      </el-form>
      <template #footer>
        <el-button @click="newStuCheckInDialog = false">取消</el-button>
        <el-button type="primary" @click="checkIn" :loading="submitLoading">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="backSchoolCheckin-stu-check-in">
import { ref, reactive } from 'vue'
import { useMessage } from '/@/hooks/message'
import { useDict } from '/@/hooks/dict'
import { putBackObj } from '/@/api/recruit/recruitstudentsignup'
import { getDicts } from '/@/api/admin/dict'

// 性别字典
const { sexy:genderData } = useDict('sexy')

// 消息提示 hooks
const message = useMessage()

// 定义 emits
const emit = defineEmits(['reload'])

// 引用
const formRef = ref()

// 状态
const newStuCheckInDialog = ref(false)
const checkInStatusData = ref<any[]>([])
const submitLoading = ref(false)
const page = ref<any>({})

// 表单数据
const form = reactive({
  id: '',
  name: '',
  backSchoolState: '',
  gender: '',
  idNumber: '',
  backSchoolRemark: ''
})

// 表单验证规则
const rules = reactive({
  name: [
    { required: true, message: '请输入姓名', trigger: ['blur', 'change'] }
  ],
  gender: [
    { required: true, message: '请输入性别', trigger: ['blur', 'change'] }
  ],
  idNumber: [
    { required: true, message: '请输入身份证号', trigger: ['blur', 'change'] }
  ],
  backSchoolState: [
    { required: true, message: '请输入报到状态', trigger: ['blur', 'change'] }
  ]
})

// 初始化表单
const initForm = () => {
  form.id = ''
  form.name = ''
  form.backSchoolState = ''
  form.gender = ''
  form.idNumber = ''
  form.backSchoolRemark = ''
}

// 初始化
const init = async (formData: any, pageData: any) => {
  initForm()
  page.value = pageData
  submitLoading.value = false
  newStuCheckInDialog.value = true
  checkInStatusData.value = []
  
  try {
    const dictData = await getDicts('check_in_status')
    checkInStatusData.value = dictData.data || []
  } catch (error) {
    console.error('获取字典数据失败', error)
  }
  
  Object.assign(form, formData)
}

// 报到
const checkIn = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    submitLoading.value = true
    await putBackObj(form)
    message.success('报到成功')
    emit('reload')
    newStuCheckInDialog.value = false
  } finally {
    submitLoading.value = false
  }
}

// 暴露方法
defineExpose({
  init
})
</script>

<style scoped>

</style>
