<template>
  <el-dialog v-model="educationDialogFromVisible" width="600" :title="waitShenheForm.title" append-to-body>

    <!--2.1 教师资格证-->
    <el-form v-if="waitShenheForm.a" ref="teacherCertificateFormRef" :model="waitShenheForm.form" :rules="teacherCertificateRules" label-width="120px">
      <el-form-item label="类型" prop="certificateConfId">
        <el-select v-model="waitShenheForm.form.certificateConfId" placeholder="请选择类型" style="width: 100%">
          <el-option
            v-for="item in certificateTypeList"
            :key="item.id"
            :label="item.cretificateName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="证书编号" prop="certificateNumber">
        <el-input 
          v-model="waitShenheForm.form.certificateNumber" 
          placeholder="请输入证书编号（仅支持英文和数字）"
          show-word-limit
          maxlength="64"
          @input="handleCertificateNumberInput"
        />
      </el-form-item>
      
      <el-form-item label="证明材料">
        <el-upload
          :headers="headers"
          :limit="1"
          :action="materialUrlFrom.url"
          :file-list="materialUrlFrom.fileListA"
          :on-success="materiaUploadSuccessA"
          :accept="'.jpg,.jpeg,.png,.pdf'"
        >
          <div>
            <el-button size="small" type="primary">点击上传</el-button>
            <div style="margin-top: 8px;">
              <el-tag>仅支持jpg,jpeg,png,pdf后缀的文件上传</el-tag>
            </div>
          </div>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <div style="text-align: right; width: 100%;">
          <el-button type="primary" @click="dialogSubmit(0)">提交</el-button>
          <el-button @click="educationDialogFromVisible = false">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
    <!--2.2 学历-->
    <el-form v-if="waitShenheForm.b" ref="educationFormRef" :model="waitShenheForm.form" :rules="educationRules" label-width="120px">
      <el-form-item label="毕业时间" prop="graduateTime">
        <el-date-picker
          v-model="waitShenheForm.form.graduateTime"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 100%"
        />
      </el-form-item>
      
      <el-form-item label="教育类型" prop="type">
        <el-select v-model="waitShenheForm.form.type" placeholder="请选择教育类型" style="width: 100%">
          <el-option
            v-for="item in educationTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="学历" prop="qualificationConfigId">
        <el-select v-model="waitShenheForm.form.qualificationConfigId" placeholder="请选择学历" style="width: 100%">
          <el-option
            v-for="item in qualificationList"
            :key="item.id"
            :label="item.qualificationName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="学位" prop="degreeConfigId">
        <el-select v-model="waitShenheForm.form.degreeConfigId" placeholder="请选择学位" style="width: 100%">
          <el-option
            v-for="item in degreeList"
            :key="item.id"
            :label="item.degreeName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="毕业学校" prop="graduateSchool">
        <el-input v-model="waitShenheForm.form.graduateSchool" placeholder="请输入毕业学校" />
      </el-form-item>
      
      <el-form-item label="所学专业" prop="major">
        <el-input v-model="waitShenheForm.form.major" placeholder="请输入所学专业" />
      </el-form-item>
      
      <el-form-item label="证书编码" prop="certificateNumber">
        <el-input 
          v-model="waitShenheForm.form.certificateNumber" 
          placeholder="请输入证书编码（仅支持英文和数字）"
          show-word-limit
          maxlength="100"
          @input="handleCertificateNumberInput"
        />
      </el-form-item>
      
      <el-form-item label="学历证书">
        <el-upload
          :headers="headers"
          :limit="1"
          :action="materialUrlFrom.url"
          :file-list="materialUrlFrom.fileListA"
          :on-success="materiaUploadSuccessA"
          :accept="'.jpg,.jpeg,.png,.pdf'"
        >
          <div>
            <el-button size="small" type="primary">点击上传</el-button>
            <div style="margin-top: 8px;">
              <el-tag>仅支持jpg,jpeg,png,pdf后缀的文件上传</el-tag>
            </div>
          </div>
        </el-upload>
      </el-form-item>

      <el-form-item label="学位证书">
        <el-upload
          :headers="headers"
          :limit="1"
          :action="materialUrlFrom.url"
          :on-success="materiaUploadSuccessB"
          :file-list="materialUrlFrom.fileListB"
          :accept="'.jpg,.jpeg,.png,.pdf'"
        >
          <div>
            <el-button size="small" type="primary">点击上传</el-button>
            <div style="margin-top: 8px;">
              <el-tag>仅支持jpg,jpeg,png,pdf后缀的文件上传</el-tag>
            </div>
          </div>
        </el-upload>
      </el-form-item>
      
      <el-form-item>
        <div style="text-align: right; width: 100%;">
          <el-button type="primary" @click="dialogSubmit(1)">提交</el-button>
          <el-button @click="educationDialogFromVisible = false">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
    <!--2.3 职称-->
    <el-form v-if="waitShenheForm.c" ref="proFormRef" :model="waitShenheForm.form" :rules="proRules" label-width="120px">
      <el-form-item label="职称等级" prop="professionalTitleConfigId">
        <el-select v-model="waitShenheForm.form.professionalTitleConfigId" placeholder="请选择职称等级" style="width: 100%">
          <el-option
            v-for="item in baseInfoAbout.proTitleList"
            :key="item.id"
            :label="item.professionalTitle"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="专业技术职务" prop="majorStation">
        <el-select v-model="waitShenheForm.form.majorStation" placeholder="请选择专业技术职务" style="width: 100%">
          <el-option
            v-for="item in baseInfoAbout.majorStationList"
            :key="item.id"
            :label="item.majorStationName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="取证时间" prop="certificateTime">
        <el-date-picker
          v-model="waitShenheForm.form.certificateTime"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 100%"
        />
      </el-form-item>
      
      <el-form-item label="职称任职时间" prop="inOfficeDate">
        <el-date-picker
          v-model="waitShenheForm.form.inOfficeDate"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 100%"
        />
      </el-form-item>
      
      <el-form-item label="证书编号" prop="certificateNumber">
        <el-input 
          v-model="waitShenheForm.form.certificateNumber" 
          placeholder="请输入证书编号（仅支持英文和数字）"
          show-word-limit
          maxlength="32"
          @input="handleCertificateNumberInput"
        />
      </el-form-item>
      
      <el-form-item label="证明材料">
        <el-upload
          :headers="headers"
          :limit="1"
          :action="materialUrlFrom.url"
          :file-list="materialUrlFrom.fileListA"
          :on-success="materiaUploadSuccessA"
          :accept="'.jpg,.jpeg,.png,.pdf'"
        >
          <div>
            <el-button size="small" type="primary">点击上传</el-button>
            <div style="margin-top: 8px;">
              <el-tag>仅支持jpg,jpeg,png,pdf后缀的文件上传</el-tag>
            </div>
          </div>
        </el-upload>
      </el-form-item>
      
      <el-form-item>
        <div style="text-align: right; width: 100%;">
          <el-button type="primary" @click="dialogSubmit(2)">提交</el-button>
          <el-button @click="educationDialogFromVisible = false">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
    <!--2.4 职业-->
    <el-form v-if="waitShenheForm.d" ref="workFormRef" :model="waitShenheForm.form" :rules="workRules" label-width="120px">
      <el-form-item label="职业工种" prop="worker">
        <el-select v-model="waitShenheForm.form.worker" filterable placeholder="请选择职业工种" style="width: 100%">
          <el-option
            v-for="item in baseInfoAbout.workTypeList"
            :key="item.id"
            :label="item.workName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="等级" prop="qualificationConfigId">
        <el-select v-model="waitShenheForm.form.qualificationConfigId" placeholder="请选择等级" style="width: 100%">
          <el-option
            v-for="item in baseInfoAbout.qualificationList"
            :key="item.id"
            :label="item.levelName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="取证时间" prop="certificateTime">
        <el-date-picker
          v-model="waitShenheForm.form.certificateTime"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 100%"
        />
      </el-form-item>
      
      <el-form-item label="证书编号" prop="certificateNumber">
        <el-input 
          v-model="waitShenheForm.form.certificateNumber" 
          placeholder="请输入证书编号（仅支持英文和数字）"
          show-word-limit
          maxlength="32"
          @input="handleCertificateNumberInput"
        />
      </el-form-item>
      
      <el-form-item label="材料1">
        <el-upload
          :headers="headers"
          :limit="1"
          :action="materialUrlFrom.url"
          :file-list="materialUrlFrom.fileListA"
          :on-success="materiaUploadSuccessA"
          :accept="'.jpg,.jpeg,.png,.pdf'"
        >
          <div>
            <el-button size="small" type="primary">点击上传</el-button>
            <div style="margin-top: 8px;">
              <el-tag>仅支持jpg,jpeg,png,pdf后缀的文件上传</el-tag>
            </div>
          </div>
        </el-upload>
      </el-form-item>
      
      <el-form-item>
        <div style="text-align: right; width: 100%;">
          <el-button type="primary" @click="dialogSubmit(3)">提交</el-button>
          <el-button @click="educationDialogFromVisible = false">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
    <!--2.5 综合表彰-->
    <el-form v-if="waitShenheForm.e" ref="honorFormRef" :model="waitShenheForm.form" :rules="honorRules" label-width="120px">
      <el-form-item label="荣誉" prop="honor">
        <el-input v-model="waitShenheForm.form.honor" placeholder="请填写荣誉" />
      </el-form-item>
      
      <el-form-item label="表彰单位" prop="honorCompany">
        <el-input v-model="waitShenheForm.form.honorCompany" placeholder="请填写表彰单位" />
      </el-form-item>
      
      <el-form-item label="年份" prop="year">
        <el-date-picker
          v-model="waitShenheForm.form.year"
          type="year"
          placeholder="选择年份"
          format="yyyy"
          value-format="yyyy"
          style="width: 100%"
        />
      </el-form-item>
      
      <el-form-item label="证明材料">
        <el-upload
          :headers="headers"
          :limit="1"
          :action="materialUrlFrom.url"
          :on-success="materiaUploadSuccessA"
          :file-list="materialUrlFrom.fileListA"
          :accept="'.jpg,.jpeg,.png,.pdf'"
        >
          <div>
            <el-button size="small" type="primary">点击上传</el-button>
            <div style="margin-top: 8px;">
              <el-tag>仅支持jpg,jpeg,png,pdf后缀的文件上传</el-tag>
            </div>
          </div>
        </el-upload>
      </el-form-item>
      
      <el-form-item>
        <div style="text-align: right; width: 100%;">
          <el-button type="primary" @click="dialogSubmit(4)">提交</el-button>
          <el-button @click="educationDialogFromVisible = false">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
    <!--2.6 人员调动-->
    <el-form v-if="waitShenheForm.f" ref="stationChangeFormRef" :model="waitShenheForm.form" :rules="stationChangeRules" label-width="120px">
      <el-form-item label="工号">
        <el-tag>{{waitShenheForm.form.teacherNo}}</el-tag>
      </el-form-item>
      
      <el-form-item label="姓名">
        <el-tag>{{waitShenheForm.form.realName}}</el-tag>
      </el-form-item>

      <el-form-item label="原部门名称">
        <el-tag disabled>{{waitShenheForm.form.deptName}}</el-tag>
      </el-form-item>
      
      <el-form-item label="现二级部门" prop="newDeptCode">
        <el-select v-model="newSecDeptCode" @change="getDeptListByParent" placeholder="请选择二级部门" style="width: 100%">
          <el-option v-for="item in secDeptList"
                     :key="item.deptCode"
                     :label="item.deptName"
                     :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="现子级部门">
        <el-select v-model="newSecChildDeptCode" placeholder="请选择下级部门" clearable style="width: 100%">
          <el-option v-for="item in newSecChildDeptCodeList"
                     :key="item.deptCode"
                     :label="item.deptName"
                     :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="调令日期" prop="changeDate">
        <el-date-picker
          v-model="waitShenheForm.form.changeDate"
          type="date"
          placeholder="选择日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      
      <el-form-item label="岗位类型" prop="pos">
        <el-select v-model="waitShenheForm.form.pos" placeholder="请选择岗位类型" style="width: 100%">
          <el-option
            v-for="item in global.STATION_TYPE"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="waitShenheForm.form.remarks" type="textarea" :rows="3" placeholder="请输入备注" />
      </el-form-item>
      
      <el-form-item>
        <div style="text-align: right; width: 100%;">
          <el-button type="primary" @click="dialogSubmit(5)">提交</el-button>
          <el-button @click="educationDialogFromVisible = false">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
    <!--2.7 党员调动-->
    <el-form v-if="waitShenheForm.g" ref="partChangeFormRef" :model="waitShenheForm.form" :rules="partChangeRules" label-width="120px">
      <el-form-item label="工号">
        <el-tag>{{waitShenheForm.form.teacherNo}}</el-tag>
      </el-form-item>

      <el-form-item label="用户名">
        <el-tag>{{waitShenheForm.form.realName}}</el-tag>
      </el-form-item>

      <el-form-item label="原支部名">
        <el-select disabled v-model="waitShenheForm.form.oldBranchName" style="width: 100%">
          <el-option
            v-for="item in baseInfoAbout.partBranchList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="现支部名" prop="branchName">
        <el-select clearable v-model="waitShenheForm.form.branchName" placeholder="请选择现支部" style="width: 100%">
          <el-option
            v-for="item in baseInfoAbout.partBranchList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="党费" prop="partyFee">
        <el-input-number v-model="waitShenheForm.form.partyFee" :precision="2" :min="0" style="width: 100%" />
      </el-form-item>
      
      <el-form-item label="变动时间" prop="changeTime">
        <el-date-picker
          v-model="waitShenheForm.form.changeTime"
          type="date"
          placeholder="选择日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="waitShenheForm.form.remarks" placeholder="请输入备注" />
      </el-form-item>
      
      <el-form-item>
        <div style="text-align: right; width: 100%;">
          <el-button type="primary" @click="dialogSubmit(6)">提交</el-button>
          <el-button @click="educationDialogFromVisible = false">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup lang="ts">
  import { ref, reactive, computed } from 'vue'
  import { useMessage, useMessageBox } from '/@/hooks/message'
  import { updateOtherInfo, getMyTeacherNo, getAllInfoAboutList } from '/@/api/professional/professionaluser/teacherbase'
  import { checkLocked } from '/@/api/professional/professionalstatuslock'
  import { getDeptListByLevelTwo, getDeptListByParent as getDeptListByParentApi } from '/@/api/basic/basicdept'
  import { getTeacherCertificateList } from '/@/api/professional/rsbase/professionalteachercertificateconf'
  import { getAllTypeList } from '/@/api/professional/rsbase/professionalacademiceducationtypeconfig'
  import { getQualificationList } from '/@/api/professional/rsbase/academicqualificationsconfig'
import { getDegreeList } from '/@/api/professional/rsbase/professionalacademicdegreeconfig'
import { addObj as addStationChangeObj } from '/@/api/professional/professionaluser/professionalteacherstationchange'
import { addObj as addPartyChangeObj } from '/@/api/professional/professionaluser/professionalpartychange'
  import global from '/@/components/tools/commondict.vue'
  import { Session } from '/@/utils/storage'

  // 字典数据
  const certificateTypeList = ref<any[]>([])
  const educationTypeList = ref<any[]>([])
  const qualificationList = ref<any[]>([])
  const degreeList = ref<any[]>([])
  
  // 表单 ref
  const teacherCertificateFormRef = ref()
  const educationFormRef = ref()
  const proFormRef = ref()
  const workFormRef = ref()
  const honorFormRef = ref()
  const stationChangeFormRef = ref()
  const partChangeFormRef = ref()
  
  // 表单验证规则
  const teacherCertificateRules = {
    certificateConfId: [{ required: true, message: '请选择类型', trigger: 'change' }],
    certificateNumber: [
      { required: true, message: '请输入证书编号', trigger: 'blur' },
      { pattern: /^[A-Za-z0-9]+$/, message: '证书编号只能包含英文和数字', trigger: 'blur' }
    ]
  }
  
  const educationRules = {
    graduateTime: [{ required: true, message: '请输入毕业时间', trigger: 'change' }],
    type: [{ required: true, message: '请选择教育类型', trigger: 'change' }],
    graduateSchool: [{ required: true, message: '请输入毕业学校', trigger: 'blur' }],
    major: [{ required: true, message: '请输入所学专业', trigger: 'blur' }],
    certificateNumber: [
      { required: true, message: '请输入证书编码', trigger: 'blur' },
      { pattern: /^[A-Za-z0-9]+$/, message: '证书编码只能包含英文和数字', trigger: 'blur' }
    ]
  }
  
  const proRules = {
    professionalTitleConfigId: [{ required: true, message: '请选择职称等级', trigger: 'change' }],
    majorStation: [{ required: true, message: '请选择专业技术职务', trigger: 'change' }],
    certificateTime: [{ required: true, message: '请选择取证时间', trigger: 'change' }],
    certificateNumber: [
      { required: true, message: '请输入证书编号', trigger: 'blur' },
      { pattern: /^[A-Za-z0-9]+$/, message: '证书编号只能包含英文和数字', trigger: 'blur' }
    ]
  }
  
  const workRules = {
    worker: [{ required: true, message: '请选择职业工种', trigger: 'change' }],
    qualificationConfigId: [{ required: true, message: '请选择等级', trigger: 'change' }],
    certificateTime: [{ required: true, message: '请选择取证时间', trigger: 'change' }],
    certificateNumber: [
      { required: true, message: '请输入证书编号', trigger: 'blur' },
      { pattern: /^[A-Za-z0-9]+$/, message: '证书编号只能包含英文和数字', trigger: 'blur' }
    ]
  }
  
  const honorRules = {
    honor: [{ required: true, message: '请填写荣誉', trigger: 'blur' }],
    honorCompany: [{ required: true, message: '请填写表彰单位', trigger: 'blur' }],
    year: [{ required: true, message: '请输入年份', trigger: 'change' }]
  }
  
  const stationChangeRules = {
    newDeptCode: [{ required: true, message: '请选择要调入的二级部门', trigger: 'change' }],
    changeDate: [{ required: true, message: '请选择调令日期', trigger: 'change' }],
    pos: [{ required: true, message: '请选择岗位类型', trigger: 'change' }]
  }
  
  const partChangeRules = {
    branchName: [{ required: true, message: '请选择现支部', trigger: 'change' }],
    changeTime: [{ required: true, message: '请选择变动时间', trigger: 'change' }]
  }
  
  // 获取字典数据
  const loadCertificateTypeList = async () => {
    try {
      const res = await getTeacherCertificateList()
      certificateTypeList.value = res.data || []
    } catch (err) {
      // 获取证书类型列表失败
    }
  }
  
  const loadEducationTypeList = async () => {
    try {
      const res = await getAllTypeList()
      educationTypeList.value = res.data || []
    } catch (err) {
      // 获取教育类型列表失败
    }
  }
  
  const loadQualificationList = async () => {
    try {
      const res = await getQualificationList()
      qualificationList.value = res.data || []
    } catch (err) {
      // 获取学历列表失败
    }
  }
  
  const loadDegreeList = async () => {
    try {
      const res = await getDegreeList()
      degreeList.value = res.data || []
    } catch (err) {
      // 获取学位列表失败
    }
  }

  // Props
  const props = defineProps<{
    page: any
    nowRow: any
  }>()

  // Emits
  const emit = defineEmits(['getList'])

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

  // 响应式数据
  const educationDialogFromVisible = ref(false)
  const waitShenheForm = reactive({
    form: {
      newDeptCodeList: [] as any[],
      deptCodeList: [] as any[]
    } as any,
    title: '',
    a: false,
    b: false,
    c: false,
    d: false,
    e: false,
    f: false,
    g: false
  })
  
  const materialUrlFrom = reactive({
    url: '/professional/file/teacherAboutInfoUpload',
    fileListA: [] as any[],
    fileListB: [] as any[],
    fileListC: [] as any[]
  })
  
  const baseInfoAbout = reactive({
    stationTypeList: [] as any[],
    atStationList: [] as any[],
    teacherTypeList: [] as any[],
    partBranchList: [] as any[],
    employmentNatureList: [] as any[],
    stationLevelList: [] as any[],
    stationDutyLevelList: [] as any[],
    workTypeList: [] as any[],
    proTitleList: [] as any[],
    majorStationList: [] as any[],
    qualificationList: [] as any[]
  })
  
  const secDeptList = ref<any[]>([])
  const newSecChildDeptCodeList = ref<any[]>([])
  const newSecDeptCode = ref('')
  const newSecChildDeptCode = ref('')
  const teacherNo = ref('')

  // Computed
  const headers = computed(() => {
          return {
            "Authorization": 'Bearer ' + Session.getToken()
          }
  })
  // 方法定义
  const initDicData = async () => {
    try {
      const response = await getAllInfoAboutList()
      const map = response.data
      baseInfoAbout.stationTypeList = map['stationTypeList'] || []
      baseInfoAbout.atStationList = map['atStationList'] || []
      baseInfoAbout.teacherTypeList = map['teacherTypeList'] || []
      baseInfoAbout.employmentNatureList = map['employmentNatureList'] || []
      baseInfoAbout.stationLevelList = map['stationLevelList'] || []
      baseInfoAbout.stationDutyLevelList = map['stationDutyLevelList'] || []
      baseInfoAbout.workTypeList = map['workTypeList'] || []
      baseInfoAbout.proTitleList = map['proTitleList'] || []
      baseInfoAbout.majorStationList = map['majorStationList'] || []
      baseInfoAbout.qualificationList = map['qualificationList'] || []
      baseInfoAbout.partBranchList = map['partBranchList'] || []
    } catch (error) {
      // 获取基础信息失败
    }
    
    // 加载字典数据
    loadCertificateTypeList()
    loadEducationTypeList()
    loadQualificationList()
    loadDegreeList()
  }

  const init = async (val: number) => {
    await initDicData()
    if (val === 5 || val === 6) {
      teacherNo.value = props.nowRow.teacherNo
      handleWaitExam(val)
    } else {
      // 重置表单数据
      Object.keys(waitShenheForm.form).forEach(key => {
        if (key !== 'newDeptCodeList' && key !== 'deptCodeList') {
          (waitShenheForm.form as any)[key] = ''
        }
      })
      waitShenheForm.form.newDeptCodeList = []
      waitShenheForm.form.deptCodeList = []
      materialUrlFrom.fileListA = []
      materialUrlFrom.fileListB = []
      materialUrlFrom.fileListC = []
      materialUrlFrom.url = '/professional/file/teacherAboutInfoUpload'
      
      // 状态码映射
      const statusCodeMap: Record<number, string> = {
        0: "teacherTitle",
        1: "acade",
        2: "title",
        3: "job",
        4: "remix"
      }
      const statusCode = statusCodeMap[val]
      
      if (statusCode) {
        try {
          const lockResponse = await checkLocked(statusCode)
          if (lockResponse.data) {
            message.warning("新增功能已锁定，暂不允许操作")
            return
          }
          
          const response = await getMyTeacherNo()
          teacherNo.value = response.data
          handleWaitExam(val)
        } catch (error) {
          message.error('操作失败')
        }
      } else {
        handleWaitExam(val)
      }
          }
  }
  const handleWaitExam = (val: number) => {
    materialUrlFrom.url = '/professional/file/teacherAboutInfoUpload'
    waitShenheForm.a = false
    waitShenheForm.b = false
    waitShenheForm.c = false
    waitShenheForm.d = false
    waitShenheForm.e = false
    waitShenheForm.f = false
    waitShenheForm.g = false
    
    // 表单类型配置
    const formConfig: Record<number, { title: string; field: 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' }> = {
      0: { title: "教师资格证", field: 'a' },
      1: { title: "学历更新", field: 'b' },
      2: { title: "职称更新", field: 'c' },
      3: { title: "职业更新", field: 'd' },
      4: { title: "综合表彰", field: 'e' },
      5: { title: "人员调动", field: 'f' },
      6: { title: "党员调动", field: 'g' }
    }
    
    const config = formConfig[val]
    if (config) {
      waitShenheForm.title = config.title
      ;(waitShenheForm as any)[config.field] = true
      
      // 特殊处理
      if (val === 4) {
        // 综合表彰：重置相关字段
        materialUrlFrom.fileListA = []
        waitShenheForm.form.honor = ''
        waitShenheForm.form.honorCompany = ''
        waitShenheForm.form.year = ''
        waitShenheForm.form.attachment = ''
      } else if (val === 5) {
        // 人员调动：加载部门数据
        waitShenheForm.form = { ...props.nowRow }
        waitShenheForm.form.newDeptCodeList = []
        waitShenheForm.form.deptCodeList = []
        newSecDeptCode.value = ''
        newSecChildDeptCode.value = ''
        getDeptListByLevelTwo().then((res: any) => {
          secDeptList.value = res.data
          educationDialogFromVisible.value = true
        }).catch(() => {
          message.error('获取部门列表失败')
        })
      } else if (val === 6) {
        // 党员调动：设置原支部
        waitShenheForm.form = { ...props.nowRow }
        waitShenheForm.form.oldBranchName = waitShenheForm.form.oldBranchId
      }
    }
    materialUrlFrom.url = `${materialUrlFrom.url}?teacherNo=${teacherNo.value}&type=${val}`
    if (val !== 5) {
      educationDialogFromVisible.value = true
    }
  }
  // 证书编号输入处理（只允许英文和数字）
  const handleCertificateNumberInput = (value: string) => {
    waitShenheForm.form.certificateNumber = value.replace(/[^A-Za-z0-9]/g, '')
  }

  // 文件上传成功处理
  const materiaUploadSuccessA = (response: any) => {
    if (response.data?.code === "-1") {
      message.error("当前不允许上传文件")
      return
    }
    waitShenheForm.form.mateA = response.data.url
  }
  
  const materiaUploadSuccessB = (response: any) => {
    if (response.data?.code === "-1") {
      message.error("当前不允许上传文件")
      return
    }
    waitShenheForm.form.mateB = response.data.url
  }
  const dialogSubmit = async (val: number) => {
    waitShenheForm.form.type = val
    waitShenheForm.form.teacherNo = teacherNo.value

    // 表单验证配置
    const formRefMap: Record<number, any> = {
      0: teacherCertificateFormRef.value,
      1: educationFormRef.value,
      2: proFormRef.value,
      3: workFormRef.value,
      4: honorFormRef.value,
      5: stationChangeFormRef.value,
      6: partChangeFormRef.value
    }
    
    const formRef = formRefMap[val]
    
    // 材料验证
    if (val === 0 || val === 2 || val === 3 || val === 4) {
      // 需要上传材料A的表单
      if (!waitShenheForm.form.mateA) {
        message.info("请上传资料")
        return
      }
    } else if (val === 1) {
      // 学历：需要上传材料A或材料B
      if (!waitShenheForm.form.mateA && !waitShenheForm.form.mateB) {
        message.info("请上传学历或学位证书")
        return
      }
    } else if (val === 5) {
      // 人员调动：需要选择部门
      if (!newSecDeptCode.value) {
        message.info("请选择要调入的部门")
        return
      }
      waitShenheForm.form.newDeptCode = newSecDeptCode.value
      waitShenheForm.form.newSecDeptCode = newSecChildDeptCode.value || newSecDeptCode.value
    }

    // 验证表单
    if (formRef) {
      try {
        await formRef.validate()
      } catch (err) {
        return
      }
    }

    try {
      await messageBox.confirm('确认提交?')
      let res
      if (val === 5) {
        // 人员调动：使用人员调动接口
        res = await addStationChangeObj(waitShenheForm.form)
      } else if (val === 6) {
        // 党员调动：使用党员调动接口
        res = await addPartyChangeObj(waitShenheForm.form)
      } else {
        // 其他类型：走通用“其他信息”更新接口
        res = await updateOtherInfo(waitShenheForm.form)
      }
      if (res.data === '-1') {
        message.warning("当前不允许提交")
      } else {
        message.success("提交成功")
      }
      emit("getList", props.page)
      educationDialogFromVisible.value = false
    } catch (err) {
      // 用户取消
    }
  }

  const getDeptListByParent = async () => {
    newSecChildDeptCode.value = ''
    newSecChildDeptCodeList.value = []
    try {
      const res = await getDeptListByParentApi(newSecDeptCode.value)
      newSecChildDeptCodeList.value = res.data
    } catch (error) {
      message.error('获取部门列表失败')
    }
  }

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

<style scoped>

</style>
