<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    v-model="visible"
    width="600px"
    destroy-on-close
    
  >
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmit"
             label-width="120px">

      <el-form-item label="招生计划" prop="groupId">
        <el-select v-model="dataForm.groupId" filterable placeholder="请选择招生计划">
          <el-option
            v-for="item in planList"
            :key="item.id"
            :label="item.groupName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="专业代码" prop="majorCode">
        <el-input v-model="dataForm.majorCode" placeholder="专业代码"></el-input>
      </el-form-item>
      <el-form-item label="专业名称" prop="majorName">
        <el-input v-model="dataForm.majorName" placeholder="专业名称"></el-input>
      </el-form-item>
<!--      <el-form-item label="专业规范名称" prop="zygfmc">-->
<!--        <el-input v-model="dataForm.zygfmc" placeholder="专业规范名称"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="所属学院" prop="deptCode">
        <el-select v-model="dataForm.deptCode" filterable placeholder="请选择">
          <el-option
            v-for="item in deptList"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学制" prop="learnYear">
        <el-select v-model="dataForm.learnYear" filterable placeholder="请选择学制">
          <el-option
            v-for="item in majorYears"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="层次" prop="majorLevel">
        <el-select v-model="dataForm.majorLevel" filterable placeholder="请选择层次">
          <el-option
            v-for="item in ccList"
            :key="item.label"
            :label="item.label"
            :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="中德班" prop="isZd">
        <el-radio-group v-model="dataForm.isZd">
          <el-radio v-for="item in yes_no_type" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="订单班" prop="isOrder">
        <el-radio-group v-model="dataForm.isOrder">
          <el-radio v-for="item in yes_no_type" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联院班" prop="isUnion">
        <el-radio-group v-model="dataForm.isUnion">
          <el-radio v-for="item in yes_no_type" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="正式专业代码" prop="stuworkMajorCode">
        <el-select v-model="dataForm.stuworkMajorCode" filterable placeholder="请选择正式专业代码">
          <el-option
            v-for="item in offcialZydmList"
            :key="item.majorCode"
            :label="item.majorCodeAndName"
            :value="item.majorCode">
          </el-option>
        </el-select>
      </el-form-item>

<!--      <el-form-item label="市平台代码" prop="cityPlanId">-->
<!--        <el-select v-model="dataForm.cityPlanId" filterable placeholder="请选择市平台代码">-->
<!--          <el-option-->
<!--            v-for="item in cityPlanIdList"-->
<!--            :key="item.id"-->
<!--            :label="item.schoolMajorName+' | '+item.educational"-->
<!--            :value="item.id"-->
<!--            :disabled="isCityDisable(item.id)"-->
<!--          >-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="排序" prop="sort">
        <el-input-number :min="0" :max="999" v-model="dataForm.sort" placeholder="排序"
                         style="width: 100%"></el-input-number>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input type="textarea" :rows="3" placeholder="请输入备注信息" v-model="dataForm.remarks"
                  style="width: 100%;margin-top: 10px"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue'
import { ElNotification } from 'element-plus'
import { addObj, getObj, putObj } from '/@/api/recruit/recruitstudentplan'
import { getDeptList } from '/@/api/basic/basicclass'
import { getList } from '/@/api/recruit/recruitstudentplangroup'
import { getMajorNameList } from '/@/api/basic/major'
import { getDicts } from '/@/api/admin/dict'
import { useDict } from '/@/hooks/dict'

// Emits
const emit = defineEmits<{
  (e: 'refreshDataList'): void
}>()

// 字典数据
const { yes_no_type } = useDict('yes_no_type')

// 表单引用
const dataFormRef = ref()

// 响应式数据
const visible = ref(false)
const canSubmit = ref(false)
const cityPlanIdList = ref<any[]>([])
const offcialZydmList = ref<any[]>([])
const planList = ref<any[]>([])
const deptList = ref<any[]>([])
const ccList = ref<any[]>([])
const majorYears = ref<any[]>([])

const dataForm = reactive({
  id: "",
  groupId: "",
  majorCode: "",
  majorName: "",
  deptCode: "",
  learnYear: "",
  majorLevel: "",
  isZd: "0",
  isOrder: "0",
  remarks: "",
  stuworkMajorCode: "",
  isUnion: "0",
  tuitionFee: 0,
  sort: 0
})

const dataRule = {
  majorCode: [
    { required: true, message: '专业代码不能为空', trigger: 'blur' },
    { min: 1, max: 6, message: '专业代码长度不大于6个字符', trigger: 'blur' }
  ],
  tuitionFee: [
    { required: true, message: '学费不能为空', trigger: 'blur' }
  ],
  majorName: [
    { required: true, message: '专业名称不能为空', trigger: 'blur' },
    { min: 1, max: 200, message: '专业名称长度不大于200个字符', trigger: 'blur' }
  ],
  groupId: [
    { required: true, message: '招生计划不能为空', trigger: 'blur' }
  ],
  learnYear: [
    { required: true, message: '学制不能为空', trigger: 'blur' }
  ],
  deptCode: [
    { required: true, message: '学院不能为空', trigger: 'blur' }
  ],
  majorLevel: [
    { required: true, message: '层次不能为空', trigger: 'blur' }
  ],
  isOrder: [
    { required: true, message: '订单班不能为空', trigger: 'blur' }
  ],
  isZd: [
    { required: true, message: '订单班不能为空', trigger: 'blur' }
  ],
  isUnion: [
    { required: true, message: '联院班不能为空', trigger: 'blur' }
  ],
  remarks: [
    { min: 1, max: 100, message: '备注长度不大于100个字符', trigger: 'blur' }
  ],
}

/**
 * 判断市平台招生专业是否占用,占用不可选
 */
const isCityDisable = (id: string) => {
  if (!dataForm.cityPlanIds || !Array.isArray(dataForm.cityPlanIds)) {
    return false
  }
  return dataForm.cityPlanIds.some(e => e == id)
}

// 初始化数据
const initData = () => {
  // 查询二级学院信息
  getDeptList().then((data: any) => {
    deptList.value = data.data
  })
  getList().then((data: any) => {
    planList.value = data.data
    if (!dataForm.id) {
      dataForm.groupId = planList.value[0]?.id || null
    }
  })
  getMajorNameList().then((data: any) => {
    offcialZydmList.value = data.data
  })
  // 获取数据字典
  getDicts('basic_major_years').then((res: any) => {
    majorYears.value = res.data || []
  })
  getDicts('basic_major_level').then((res: any) => {
    ccList.value = res.data || []
  })
}

// 表单提交
const dataFormSubmit = () => {
  if (dataForm.cityPlanId != null) {
    cityPlanIdList.value.forEach(e => {
      if (e.id == dataForm.cityPlanId) {
        dataForm.cityPlanName = e.schoolMajorName
        dataForm.cityPlanYear = e.educational
      }
    })
  }
  dataFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      canSubmit.value = false
      if (dataForm.id) {
        putObj(dataForm).then(() => {
          ElNotification.success({
            title: '成功',
            message: '修改成功'
          })
          visible.value = false
          emit('refreshDataList')
        }).catch(() => {
          canSubmit.value = true
        })
      } else {
        addObj(dataForm).then(() => {
          ElNotification.success({
            title: '成功',
            message: '添加成功'
          })
          visible.value = false
          emit('refreshDataList')
        }).catch(() => {
          canSubmit.value = true
        })
      }
    }
  })
}

// 初始化方法
const init = (id: string | null) => {
  dataForm.id = id || null
  visible.value = true
  canSubmit.value = true
  initData()
  nextTick(() => {
    dataFormRef.value?.resetFields()
    if (dataForm.id) {
      getObj(dataForm.id).then((response: any) => {
        Object.assign(dataForm, response.data)
        // 获取市平台对应年份下的招生计划
        // getCityPlan({ id: dataForm.id }).then((data: any) => {
        //   cityPlanIdList.value = data.data
        // })
      })
    }
  })
}

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

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>
