<template>
  <el-dialog 
    :title="form.id ? '编辑' : '新增'" 
    v-model="visible" 
    :width="800" 
    :close-on-click-modal="false" 
    draggable>
    <el-form 
      ref="dataFormRef" 
      :model="form" 
      :rules="dataRules" 
      label-width="120px" 
      v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="班号" prop="classCode">
            <el-select 
              v-model="form.classCode" 
              placeholder="请选择班号" 
              clearable
              filterable
              style="width: 100%">
              <el-option
                v-for="item in classList"
                :key="item.classCode"
                :label="item.classNo"
                :value="item.classCode">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="总人数" prop="totalCnt">
            <el-input-number
              v-model="form.totalCnt"
              :min="0"
              placeholder="请输入总人数"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="男生人数" prop="boyCnt">
            <el-input-number
              v-model="form.boyCnt"
              :min="0"
              placeholder="请输入男生人数"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="女生人数" prop="girlCnt">
            <el-input-number
              v-model="form.girlCnt"
              :min="0"
              placeholder="请输入女生人数"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="男(住宿)" prop="boyDormCnt">
            <el-input-number
              v-model="form.boyDormCnt"
              :min="0"
              placeholder="请输入男(住宿)人数"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="女(住宿)" prop="girlDormCnt">
            <el-input-number
              v-model="form.girlDormCnt"
              :min="0"
              placeholder="请输入女(住宿)人数"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="留校察看人数" prop="keepSchoolCnt">
            <el-input-number
              v-model="form.keepSchoolCnt"
              :min="0"
              placeholder="请输入留校察看人数"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="记过人数" prop="gigCnt">
            <el-input-number
              v-model="form.gigCnt"
              :min="0"
              placeholder="请输入记过人数"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="严重警告人数" prop="seriousWarningCnt">
            <el-input-number
              v-model="form.seriousWarningCnt"
              :min="0"
              placeholder="请输入严重警告人数"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="警告人数" prop="warningCnt">
            <el-input-number
              v-model="form.warningCnt"
              :min="0"
              placeholder="请输入警告人数"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="撤销处分人数" prop="revokeCnt">
            <el-input-number
              v-model="form.revokeCnt"
              :min="0"
              placeholder="请输入撤销处分人数"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="退学人数" prop="dropCnt">
            <el-input-number
              v-model="form.dropCnt"
              :min="0"
              placeholder="请输入退学人数"
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="总结报告" prop="summary">
            <Editor
              v-model:getHtml="form.summary"
              :height="'400'"
              placeholder="请输入总结报告" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="ClassSummaryFormDialog">
import { ref, reactive, nextTick, onMounted } from 'vue'
import { useMessage } from '/@/hooks/message'
import { addObj, editObj, getDetail } from '/@/api/stuwork/classsummary'
import { getClassListByRole } from '/@/api/basic/basicclass'
import Editor from '/@/components/Editor/index.vue'

const emit = defineEmits(['refresh'])

// 定义变量内容
const dataFormRef = ref()
const visible = ref(false)
const loading = ref(false)
const operType = ref('add')
const classList = ref<any[]>([])

// 提交表单数据
const form = reactive({
  id: '',
  classCode: '',
  totalCnt: 0,
  boyCnt: 0,
  girlCnt: 0,
  boyDormCnt: 0,
  girlDormCnt: 0,
  keepSchoolCnt: 0,
  gigCnt: 0,
  seriousWarningCnt: 0,
  warningCnt: 0,
  revokeCnt: 0,
  dropCnt: 0,
  summary: ''
})

// 定义校验规则
const dataRules = {
  classCode: [
    { required: true, message: '请选择班号', trigger: 'change' }
  ],
  totalCnt: [
    { required: true, message: '请输入总人数', trigger: 'blur' }
  ],
  boyCnt: [
    { required: true, message: '请输入男生人数', trigger: 'blur' }
  ],
  girlCnt: [
    { required: true, message: '请输入女生人数', trigger: 'blur' }
  ],
  boyDormCnt: [
    { required: true, message: '请输入男(住宿)人数', trigger: 'blur' }
  ],
  girlDormCnt: [
    { required: true, message: '请输入女(住宿)人数', trigger: 'blur' }
  ],
  keepSchoolCnt: [
    { required: true, message: '请输入留校察看人数', trigger: 'blur' }
  ],
  gigCnt: [
    { required: true, message: '请输入记过人数', trigger: 'blur' }
  ],
  seriousWarningCnt: [
    { required: true, message: '请输入严重警告人数', trigger: 'blur' }
  ],
  warningCnt: [
    { required: true, message: '请输入警告人数', trigger: 'blur' }
  ],
  revokeCnt: [
    { required: true, message: '请输入撤销处分人数', trigger: 'blur' }
  ],
  dropCnt: [
    { required: true, message: '请输入退学人数', trigger: 'blur' }
  ],
  summary: [
    { required: true, message: '请输入总结报告', trigger: 'blur' }
  ]
}

// 打开弹窗
const openDialog = async (type: string = 'add', row?: any) => {
  visible.value = true
  operType.value = type
  
  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields()
    form.id = ''
    form.classCode = ''
    form.totalCnt = 0
    form.boyCnt = 0
    form.girlCnt = 0
    form.boyDormCnt = 0
    form.girlDormCnt = 0
    form.keepSchoolCnt = 0
    form.gigCnt = 0
    form.seriousWarningCnt = 0
    form.warningCnt = 0
    form.revokeCnt = 0
    form.dropCnt = 0
    form.summary = ''
    
    // 编辑时填充数据
    if (type === 'edit' && row) {
      form.id = row.id
      form.classCode = row.classCode || ''
      form.totalCnt = row.totalCnt || 0
      form.boyCnt = row.boyCnt || 0
      form.girlCnt = row.girlCnt || 0
      form.boyDormCnt = row.boyDormCnt || 0
      form.girlDormCnt = row.girlDormCnt || 0
      form.keepSchoolCnt = row.keepSchoolCnt || 0
      form.gigCnt = row.gigCnt || 0
      form.seriousWarningCnt = row.seriousWarningCnt || 0
      form.warningCnt = row.warningCnt || 0
      form.revokeCnt = row.revokeCnt || 0
      form.dropCnt = row.dropCnt || 0
      form.summary = row.summary || ''
      
      // 如果需要获取详情
      if (row.id && !row.summary) {
        loading.value = true
        getDetail(row.id).then((res: any) => {
          if (res.data) {
            form.summary = res.data.summary || ''
          }
        }).finally(() => {
          loading.value = false
        })
      }
    }
  })
}

// 提交表单
const onSubmit = async () => {
  if (!dataFormRef.value) return
  
  await dataFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    
    loading.value = true
    try {
      const submitData = {
        classCode: form.classCode,
        totalCnt: form.totalCnt,
        boyCnt: form.boyCnt,
        girlCnt: form.girlCnt,
        boyDormCnt: form.boyDormCnt,
        girlDormCnt: form.girlDormCnt,
        keepSchoolCnt: form.keepSchoolCnt,
        gigCnt: form.gigCnt,
        seriousWarningCnt: form.seriousWarningCnt,
        warningCnt: form.warningCnt,
        revokeCnt: form.revokeCnt,
        dropCnt: form.dropCnt,
        summary: form.summary
      }
      
      if (operType.value === 'add') {
        await addObj(submitData)
        useMessage().success('新增成功')
      } else {
        await editObj({
          id: form.id,
          ...submitData
        })
        useMessage().success('编辑成功')
      }
      visible.value = false
      emit('refresh')
    } catch (err: any) {
      useMessage().error(err.msg || (operType.value === 'add' ? '新增失败' : '编辑失败'))
    } finally {
      loading.value = false
    }
  })
}

// 获取班号列表
const getClassListData = async () => {
  try {
    const res = await getClassListByRole()
    if (res.data && Array.isArray(res.data)) {
      classList.value = res.data
    }
  } catch (err) {
  }
}

// 初始化
onMounted(() => {
  getClassListData()
})

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

<style scoped lang="scss">
.mb20 {
  margin-bottom: 20px;
}
</style>

