<template>
  <el-dialog 
    :title="form.id ? '编辑' : '批量新增异动'" 
    v-model="visible"
    :close-on-click-modal="false" 
    draggable
    width="900px">
    <el-form 
      ref="dataFormRef" 
      :model="form" 
      :rules="dataRules" 
      label-width="120px" 
      v-loading="loading"
      :key="form.turnoverType">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="学年" prop="schoolYear">
            <el-select 
              v-model="form.schoolYear" 
              placeholder="请选择学年" 
              clearable
              filterable
              style="width: 100%">
              <el-option
                v-for="item in schoolYearList"
                :key="item.year"
                :label="item.year"
                :value="item.year">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="学期" prop="schoolTerm">
            <el-select 
              v-model="form.schoolTerm" 
              placeholder="请选择学期" 
              clearable
              style="width: 100%">
              <el-option
                v-for="item in schoolTermList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20" v-if="!isDropoutType">
          <el-form-item label="原班级" prop="oldClassCode">
            <el-select 
              v-model="form.oldClassCode" 
              placeholder="请选择原班级" 
              clearable
              filterable
              style="width: 100%"
              @change="handleOldClassChange">
              <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" v-if="!isDropoutType">
          <el-form-item label="现班级" prop="newClassCode">
            <el-select 
              v-model="form.newClassCode" 
              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="turnoverType">
            <el-select 
              v-model="form.turnoverType" 
              placeholder="请选择异动类型" 
              clearable
              style="width: 100%"
              @change="handleTurnoverTypeChange">
              <el-option
                v-for="item in turnoverTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="转制类型" prop="turnYear">
            <el-select 
              v-model="form.turnYear" 
              placeholder="请选择转制类型" 
              clearable
              style="width: 100%">
              <el-option
                v-for="item in turnYearList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="异动时间" prop="turnoverDate">
            <el-date-picker
              v-model="form.turnoverDate"
              type="datetime"
              placeholder="选择异动时间"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
              style="width: 100%" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="选择学生" prop="selectedStudents">
            <el-input
              v-model="selectedStudentsText"
              placeholder="请选择学生"
              readonly
              style="width: 100%"
              @click="openStudentDialog">
              <template #suffix>
                <el-button 
                  icon="Search" 
                  circle 
                  @click.stop="openStudentDialog" />
              </template>
            </el-input>
            <span v-if="form.selectedStudents.length > 0" class="ml5" style="color: #409eff;">
              已选 {{ form.selectedStudents.length }} 人
            </span>
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="异动原因" prop="remarks">
            <el-input 
              v-model="form.remarks" 
              type="textarea" 
              :rows="4"
              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>

  <!-- 学生选择弹窗 -->
  <el-dialog 
    title="选择学生" 
    v-model="studentDialogVisible"
    :close-on-click-modal="false" 
    draggable
    width="900px">
    <!-- 搜索表单 -->
    <el-form :model="studentSearchForm" :inline="true" @keyup.enter="handleStudentSearch">
      <el-form-item label="班号" prop="classNo">
        <el-input 
          v-model="studentSearchForm.classNo" 
          placeholder="请输入班号" 
          clearable
          style="width: 200px" />
      </el-form-item>
      <el-form-item label="学号" prop="stuNo">
        <el-input 
          v-model="studentSearchForm.stuNo" 
          placeholder="请输入学号" 
          clearable
          style="width: 200px" />
      </el-form-item>
      <el-form-item label="姓名" prop="realName">
        <el-input 
          v-model="studentSearchForm.realName" 
          placeholder="请输入姓名" 
          clearable
          style="width: 200px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain icon="Search" @click="handleStudentSearch">查询</el-button>
        <el-button icon="Refresh" @click="handleStudentReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 学生表格 -->
    <el-table 
      ref="studentTableRef"
      :data="studentTableData" 
      v-loading="studentTableLoading"
      border
      @selection-change="handleStudentSelectionChange"
      style="margin-top: 20px; max-height: 400px; overflow-y: auto;">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column prop="classNo" label="班号" show-overflow-tooltip align="center" />
      <el-table-column prop="stuNo" label="学号" show-overflow-tooltip align="center" />
      <el-table-column prop="realName" label="姓名" show-overflow-tooltip align="center" />
    </el-table>

    <!-- 分页 -->
    <pagination 
      @size-change="studentSizeChangeHandle" 
      @current-change="studentCurrentChangeHandle" 
      v-bind="studentPagination"
      style="margin-top: 20px" />

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="studentDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmStudentSelection">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="StuTurnoverFormDialog">
import { ref, reactive, nextTick, onMounted, computed } from 'vue'
import { useMessage } from '/@/hooks/message'
import { addObj, editObj, getDetail } from '/@/api/stuwork/stuturnover'
import { queryAllSchoolYear } from '/@/api/basic/basicyear'
import { getDicts } from '/@/api/admin/dict'
import { list as getClassList } from '/@/api/basic/basicclass'
import { fetchList as getStudentList } from '/@/api/basic/basicstudent'

const emit = defineEmits(['refresh'])

// 定义变量内容
const dataFormRef = ref()
const studentTableRef = ref()
const visible = ref(false)
const loading = ref(false)
const operType = ref('add') // add 或 edit
const schoolYearList = ref<any[]>([])
const schoolTermList = ref<any[]>([])
const classList = ref<any[]>([])
const turnoverTypeList = ref<any[]>([])
const turnYearList = ref<any[]>([])

// 学生选择弹窗相关
const studentDialogVisible = ref(false)
const studentTableLoading = ref(false)
const studentTableData = ref<any[]>([])
const selectedStudentRows = ref<any[]>([]) // 表格中选中的行
const studentSearchForm = reactive({
  classCode: '',
  classNo: '',
  stuNo: '',
  realName: ''
})
const studentPagination = reactive({
  total: 0,
  pageSize: 10,
  currentPage: 1
})

// 提交表单数据
const form = reactive({
  id: '',
  schoolYear: '',
  schoolTerm: '',
  oldClassCode: '',
  newClassCode: '',
  turnoverType: '',
  turnYear: '',
  turnoverDate: '',
  remarks: '',
  selectedStudents: [] as any[] // 选中的学生对象数组
})

// 已选学生文本显示
const selectedStudentsText = computed(() => {
  if (!form.selectedStudents || form.selectedStudents.length === 0) {
    return ''
  }
  return `已选择 ${form.selectedStudents.length} 名学生`
})

// 判断是否为退学类型
const isDropoutType = computed(() => {
  if (!form.turnoverType) return false
  // 查找异动类型字典中 label 包含"退学"的项
  const dropoutItem = turnoverTypeList.value.find((item: any) => {
    const label = item.label || item.dictLabel || item.name || ''
    return label.includes('退学')
  })
  if (dropoutItem) {
    return dropoutItem.value === form.turnoverType || dropoutItem.dictValue === form.turnoverType || dropoutItem.code === form.turnoverType
  }
  return false
})

// 定义校验规则（动态）
const dataRules = computed(() => {
  const rules: any = {
    schoolYear: [
      { required: true, message: '请选择学年', trigger: 'change' }
    ],
    schoolTerm: [
      { required: true, message: '请选择学期', trigger: 'change' }
    ],
    turnoverType: [
      { required: true, message: '请选择异动类型', trigger: 'change' }
    ],
    turnoverDate: [
      { required: true, message: '请选择异动时间', trigger: 'change' }
    ],
    selectedStudents: [
      { required: true, message: '请至少选择一个学生', trigger: 'change', type: 'array', min: 1 }
    ]
  }
  
  // 如果不是退学类型，原班级和现班级为必填
  if (!isDropoutType.value) {
    rules.oldClassCode = [
      { required: true, message: '请选择原班级', trigger: 'change' }
    ]
    rules.newClassCode = [
      { required: true, message: '请选择现班级', trigger: 'change' }
    ]
  }
  
  return rules
})

// 原班级变化时，更新学生搜索条件
const handleOldClassChange = () => {
  studentSearchForm.classCode = form.oldClassCode
  // 清空已选学生
  form.selectedStudents = []
}

// 异动类型变化时处理
const handleTurnoverTypeChange = () => {
  // 如果切换为退学类型，清空原班级和现班级
  if (isDropoutType.value) {
    form.oldClassCode = ''
    form.newClassCode = ''
    // 清空已选学生（因为退学不需要原班级，学生选择逻辑会受影响）
    form.selectedStudents = []
  }
  // 重新验证表单
  nextTick(() => {
    dataFormRef.value?.clearValidate(['oldClassCode', 'newClassCode'])
  })
}

// 打开学生选择弹窗
const openStudentDialog = () => {
  // 如果是退学类型，不需要原班级
  if (!isDropoutType.value && !form.oldClassCode) {
    useMessage().warning('请先选择原班级')
    return
  }
  studentDialogVisible.value = true
  studentSearchForm.classCode = form.oldClassCode || ''
  studentSearchForm.classNo = ''
  studentSearchForm.stuNo = ''
  studentSearchForm.realName = ''
  selectedStudentRows.value = []
  studentPagination.currentPage = 1
  handleStudentSearch()
}

// 学生搜索
const handleStudentSearch = async () => {
  studentTableLoading.value = true
  try {
    const params: any = {
      current: studentPagination.currentPage,
      size: studentPagination.pageSize,
      stuNo: studentSearchForm.stuNo || undefined,
      realName: studentSearchForm.realName || undefined
    }
    // 如果不是退学类型且有原班级，才添加 classCode 条件
    if (!isDropoutType.value && studentSearchForm.classCode) {
      params.classCode = studentSearchForm.classCode
    }
    const res = await getStudentList(params)
    if (res.data && res.data.records) {
      studentTableData.value = res.data.records
      studentPagination.total = res.data.total || 0
    } else {
      studentTableData.value = []
      studentPagination.total = 0
    }
    
    // 恢复之前选中的学生
    nextTick(() => {
      if (form.selectedStudents.length > 0) {
        studentTableData.value.forEach((row: any) => {
          const isSelected = form.selectedStudents.some((s: any) => s.stuNo === row.stuNo)
          if (isSelected) {
            studentTableRef.value?.toggleRowSelection(row, true)
          }
        })
      }
    })
  } catch (err) {
    studentTableData.value = []
  } finally {
    studentTableLoading.value = false
  }
}

// 学生搜索重置
const handleStudentReset = () => {
  studentSearchForm.classNo = ''
  studentSearchForm.stuNo = ''
  studentSearchForm.realName = ''
  handleStudentSearch()
}

// 学生表格选择变化
const handleStudentSelectionChange = (selection: any[]) => {
  selectedStudentRows.value = selection
}

// 确认学生选择
const confirmStudentSelection = () => {
  if (selectedStudentRows.value.length === 0) {
    useMessage().warning('请至少选择一个学生')
    return
  }
  
  // 合并已选学生（避免重复）
  const newStudents = selectedStudentRows.value.map((row: any) => ({
    stuNo: row.stuNo,
    realName: row.realName
  }))
  
  // 合并到已选列表，去重
  const existingStuNos = form.selectedStudents.map((s: any) => s.stuNo)
  newStudents.forEach((student: any) => {
    if (!existingStuNos.includes(student.stuNo)) {
      form.selectedStudents.push(student)
    }
  })
  
  studentDialogVisible.value = false
  useMessage().success(`已选择 ${form.selectedStudents.length} 名学生`)
}

// 学生分页变化
const studentSizeChangeHandle = (size: number) => {
  studentPagination.pageSize = size
  studentPagination.currentPage = 1
  handleStudentSearch()
}

const studentCurrentChangeHandle = (current: number) => {
  studentPagination.currentPage = current
  handleStudentSearch()
}

// 打开弹窗
const openDialog = async (type: string = 'add', row?: any) => {
  visible.value = true
  operType.value = type
  
  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields()
    form.id = ''
    form.schoolYear = ''
    form.schoolTerm = ''
    form.oldClassCode = ''
    form.newClassCode = ''
    form.turnoverType = ''
    form.turnYear = ''
    form.turnoverDate = ''
    form.remarks = ''
    form.selectedStudents = []
    
    // 编辑时填充数据
    if (type === 'edit' && row) {
      form.id = row.id
      form.schoolYear = row.schoolYear || ''
      form.schoolTerm = row.schoolTerm || ''
      form.oldClassCode = row.oldClassCode || ''
      form.newClassCode = row.newClassCode || ''
      form.turnoverType = row.turnoverType || ''
      form.turnYear = row.turnYear || ''
      form.turnoverDate = row.turnoverDate || ''
      form.remarks = row.remarks || ''
      
      // 编辑时，学生列表只有当前学生
      if (row.stuNo && row.realName) {
        form.selectedStudents = [{
          stuNo: row.stuNo,
          realName: row.realName
        }]
      }
    }
  })
}

// 提交表单
const onSubmit = async () => {
  if (!dataFormRef.value) return
  
  await dataFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    
    if (!form.selectedStudents || form.selectedStudents.length === 0) {
      useMessage().error('请至少选择一个学生')
      return
    }
    
    loading.value = true
    try {
      const submitData: any = {
        schoolYear: form.schoolYear,
        schoolTerm: form.schoolTerm,
        turnoverType: form.turnoverType,
        turnYear: form.turnYear || '',
        turnoverDate: form.turnoverDate,
        remarks: form.remarks || '',
        stuList: form.selectedStudents.map((s: any) => ({
          stuNo: s.stuNo,
          realName: s.realName
        }))
      }
      
      // 如果不是退学类型，才添加原班级和现班级
      if (!isDropoutType.value) {
        submitData.oldClassCode = form.oldClassCode
        submitData.newClassCode = form.newClassCode
      }
      
      // 编辑时需要包含id和单个学生信息
      if (operType.value === 'edit') {
        submitData.id = form.id
        submitData.stuNo = form.selectedStudents[0]?.stuNo
        submitData.realName = form.selectedStudents[0]?.realName
        // 编辑时不需要stuList
        delete submitData.stuList
      }
      
      if (operType.value === 'add') {
        await addObj(submitData)
        useMessage().success('批量新增成功')
      } else {
        await editObj(submitData)
        useMessage().success('编辑成功')
      }
      visible.value = false
      emit('refresh')
    } catch (err: any) {
      useMessage().error(err.msg || (operType.value === 'add' ? '批量新增失败' : '编辑失败'))
    } finally {
      loading.value = false
    }
  })
}

// 获取学年列表
const getSchoolYearList = async () => {
  try {
    const res = await queryAllSchoolYear()
    if (res.data) {
      schoolYearList.value = Array.isArray(res.data) ? res.data : []
    }
  } catch (err) {
    schoolYearList.value = []
  }
}

// 获取学期字典
const getSchoolTermDict = async () => {
  try {
    const res = await getDicts('school_term')
    if (res.data) {
      schoolTermList.value = Array.isArray(res.data) ? res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      })) : []
    }
  } catch (err) {
    schoolTermList.value = []
  }
}

// 获取班级列表
const getClassListData = async () => {
  try {
    const res = await getClassList()
    if (res.data) {
      classList.value = Array.isArray(res.data) ? res.data : []
    }
  } catch (err) {
    classList.value = []
  }
}

// 获取异动类型字典
const getTurnoverTypeDict = async () => {
  try {
    const res = await getDicts('turnover_type')
    if (res.data) {
      turnoverTypeList.value = Array.isArray(res.data) ? res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      })) : []
    }
  } catch (err) {
    turnoverTypeList.value = []
  }
}

// 获取转制类型字典
const getTurnYearDict = async () => {
  try {
    const res = await getDicts('turn_year_type')
    if (res.data) {
      turnYearList.value = Array.isArray(res.data) ? res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      })) : []
    }
  } catch (err) {
    turnYearList.value = []
  }
}

// 初始化
onMounted(() => {
  getSchoolYearList()
  getSchoolTermDict()
  getClassListData()
  getTurnoverTypeDict()
  getTurnYearDict()
})

// 暴露方法给父组件
defineExpose({
  openDialog
})
</script>

