<route lang="yaml">
meta:
  enabled: false
</route>

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { createModule, getModuleById, updateModule } from '@/api/api/apiModuleApi'
const emit = defineEmits(['success'])

const formRef = ref<FormInstance>()
const formRules = ref<FormRules>({
  moduleTitle: [
    { required: true, message: '请输入模块名称', trigger: 'blur' },
  ],
  moduleCode: [
    { required: true, message: '请输入模块代码', trigger: 'blur' },
  ],
  moduleDesc: [
    { required: false, message: '请输入描述', trigger: 'blur' },
  ],
})

const state = reactive({
  title: '',
  dialogVisible: false,
  loading: false,
  formData: {
    moduleId: 1,
    moduleTitle: '',
    moduleCode: '',
    moduleDesc: '',
    isFixed: 0,
  },
  actionType: 1,
})

onMounted(() => {})

const getData = async (moduleId: number) => {
  state.loading = true
  const { data } = await getModuleById(moduleId)
  state.formData = data
  state.loading = false
}

const onSubmit = () => {
  formRef.value && formRef.value.validate(async (valid) => {
    if (valid) {
      if (state.actionType === 1) {
        await createModule(state.formData)
        state.dialogVisible = false
        emit('success')
      }
      else {
        await updateModule(state.formData)
        state.dialogVisible = false
        emit('success')
      }
    }
  })
}

function onCancel() {
  state.dialogVisible = false
}

function resetFormData() {
  state.formData = {
    moduleId: -1,
    moduleCode: '',
    moduleTitle: '',
    moduleDesc: '',
    isFixed: 0,
  }
}

defineExpose({
  showAddModule() {
    resetFormData()
    state.title = '新增模块'
    state.actionType = 1
    state.dialogVisible = true
  },
  showEditModule(moduleId: number) {
    resetFormData()
    state.title = '编辑模块'
    state.actionType = 2
    state.dialogVisible = true
    getData(moduleId)
  },
})
</script>

<template>
  <div>
    <el-dialog v-model="state.dialogVisible" :title="state.title" width="600px" :close-on-click-modal="false" append-to-body destroy-on-close>
      <div v-loading="state.loading">
        <el-form ref="formRef" :model="state.formData" :rules="formRules" label-width="120px" label-suffix="：">
          <el-form-item label="模块名称" prop="moduleTitle">
            <el-input v-model="state.formData.moduleTitle" placeholder="请输入模块名称" />
          </el-form-item>

          <el-form-item label="模块代码" prop="moduleCode">
            <el-input v-model="state.formData.moduleCode" placeholder="请输入模块代码" />
          </el-form-item>

          <el-form-item label="简要说明" prop="moduleDesc">
            <el-input v-model="state.formData.moduleDesc" type="textarea" rows="10" placeholder="请输入简要说明" />
          </el-form-item>
        </el-form>
      </div>

      <template #footer>
        <el-button size="large" @click="onCancel">
          取消
        </el-button>
        <el-button type="primary" size="large" @click="onSubmit">
          保存
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>
