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

      <el-form-item label="招生计划" prop="groupId">
        <el-select v-model="dataForm.groupId" filterable placeholder="请选择招生计划" :disabled="!!dataForm.id">
          <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="regionId">
        <el-select v-model="dataForm.regionId" filterable placeholder="请选择地区">
          <el-option
            v-for="item in regionList"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分数线" prop="fullScore">
        <el-input-number v-model="dataForm.fullScore" :min="0" :max="999"></el-input-number>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit" :disabled="!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/recruitstudentplancorrectscoreconfig'
import { areaList } from '/@/api/recruit/recruitstudentschool'
import { getList } from '/@/api/recruit/recruitstudentplangroup'

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

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

// 响应式数据
const visible = ref(false)
const canSubmit = ref(false)
const regionList = ref<any[]>([])
const planList = ref<any[]>([])

const dataForm = reactive({
  id: "" as string | null,
  groupId: "",
  regionId: "",
  fullScore: null as number | null,
  regionName: ""
})

const dataRule = {
  groupId: [
    { required: true, message: '招生计划不能为空', trigger: 'blur' }
  ],
  regionId: [
    { required: true, message: '地区不能为空', trigger: 'blur' }
  ],
  fullScore: [
    { required: true, message: '分数线不能为空', trigger: 'blur' }
  ],
}

// 初始化数据
const initData = () => {
  getList().then((data: any) => {
    planList.value = data.data
  })
  areaList({ type: "0", parentId: "11" }).then((data: any) => {
    regionList.value = data.data
  })
}

// 表单提交
const dataFormSubmit = () => {
  dataFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      canSubmit.value = false
      regionList.value.forEach(e => {
        if (e.code == dataForm.regionId) {
          dataForm.regionName = e.name
        }
      })
      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 || ""
  visible.value = true
  canSubmit.value = true
  nextTick(() => {
    initData()
    dataFormRef.value?.resetFields()
    if (dataForm.id) {
      getObj(dataForm.id).then((response: any) => {
        Object.assign(dataForm, response.data)
      })
    }
  })
}

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

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