<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus'

import { addLevel } from '#/api'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: '新增关卡',
  },
  stageList: {
    type: Array<any>,
    default: Array<any>,
  },
  diaData: {
    type: Object,
    default: () => ({}),
  },
})
const emits = defineEmits(['update:modelValue', 'onRefresh'])

const show = computed({
  get() {
    return props.modelValue
  },
  set(val) {
    emits('update:modelValue', val)
  },
})

const _formData = {
  id: null,
  levelType: 1,
  levelNum: '',
  stageId: '',
  status: 0,
}
const formRef = ref<FormInstance | undefined>()
const formData = reactive<Record<string, any>>({
  ..._formData,
})
const levelNumRef = ref()

const rules = reactive<FormRules>({
  levelNum: [{ required: true, message: '关卡数不能为空', trigger: 'blur' }],
  stageId: [{ required: true, message: '阶段不能为空', trigger: 'blur' }],
})

const typeList = [
  {
    value: 1,
    label: '普通关卡',
  },
  {
    value: 2,
    label: '消消乐关卡',
  },
]

function onOpen() {
  onClear()
  if (props.diaData.id) {
    for (const i in formData) {
      formData[i] = props.diaData[i]
    }
  }
  formData.stageId = props.stageList[1].id
  nextTick(() => {
    levelNumRef.value.focus()
  })
}

async function handleSave() {
  await formRef.value?.validate((valid, _) => {
    if (valid) {
      if (formData.levelNum > 0) {
        addLevel(formData).then((res) => {
          show.value = false
          onClear()
          emits('onRefresh', res)
        })
      } else {
        ElMessage('只能输入正整数')
      }
    }
  })
}

function onClear() {
  Object.assign(formData, _formData)
}
</script>

<template>
  <el-dialog
    v-model="show"
    width="30vw"
    :title="title"
    class="box"
    destroy-on-close
    @open="onOpen"
  >
    <el-form ref="formRef" label-width="80px" :model="formData" :rules="rules">
      <el-form-item label="关卡类型" prop="levelType">
        <el-select v-model="formData.levelType" placeholder="请选择">
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关卡数" prop="levelNum">
        <el-input
          ref="levelNumRef"
          v-model="formData.levelNum"
          placeholder="请输入"
          type="number"
          @keydown.enter="handleSave"
        />
      </el-form-item>
      <el-form-item label="阶段" prop="stageId">
        <el-select v-model="formData.stageId" placeholder="请选择">
          <el-option
            v-for="item in props.stageList"
            :key="item.value"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="show = false">取消</el-button>
        <el-button type="primary" @click="handleSave"> 保存 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
