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

<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { createAppPermit, getAppPermitById, updateAppPermit } from '@/api/apps/appPermitApi'
const emit = defineEmits(['success'])

const formRef = ref<FormInstance>()
const formRules = ref<FormRules>({
  permitTitle: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  permitCode: [{ required: true, message: '请输入编码', trigger: 'blur' }],
  permitDesc: [{ required: false, message: '请输入说明', trigger: 'blur' }],
})

const state = reactive({
  title: '',
  dialogVisible: false,
  loading: false,
  formData: {
    appId: undefined,
    roleId: undefined,
    permitId: undefined,
    permitType: 1,
    permitTitle: '',
    permitCode: '',
    permitDesc: '',
    sortNo: 1,
    rootId: 0,
    parentId: 0,
  },
  actionType: 1,
})

onMounted(() => {})

const getData = async (appId: number, roleId: number, permitId: number) => {
  state.loading = true
  const { data } = await getAppPermitById(appId, roleId, permitId)
  state.loading = false
  state.formData = data
}

function onSubmit() {
  formRef.value && formRef.value.validate(async (valid) => {
    if (valid) {
      if (state.actionType === 1) {
        await createAppPermit(state.formData)
        state.dialogVisible = false
        emit('success')
      }
      else {
        await updateAppPermit(state.formData)
        state.dialogVisible = false
        emit('success')
      }
      ElMessage.success({
        message: '保存成功',
        center: true,
      })
    }
  })
}

function onCancel() {
  state.dialogVisible = false
}

function resetFormData() {
  state.formData = {
    appId: undefined,
    roleId: undefined,
    permitId: undefined,
    permitType: 1,
    permitTitle: '',
    permitCode: '',
    permitDesc: '',
    sortNo: 1,
    rootId: 0,
    parentId: 0,
  }
}

defineExpose({
  showAddModel(appId: undefined, roleId: undefined) {
    resetFormData()
    state.title = '新增权限'
    state.actionType = 1
    state.dialogVisible = true
    state.formData.appId = appId
    state.formData.roleId = roleId
    state.formData.sortNo = 1
  },
  showEditModel(appId: number, roleId: number, permitId: number) {
    resetFormData()
    state.title = '编辑权限'
    state.actionType = 2
    state.dialogVisible = true
    getData(appId, roleId, permitId)
  },

})
</script>

<template>
  <el-drawer v-model="state.dialogVisible" :title="state.title" size="30%">
    <el-form ref="formRef" :model="state.formData" :rules="formRules" label-width="120px" label-suffix="：">
      <el-form-item label="标题" prop="permitTitle">
        <el-input v-model="state.formData.permitTitle" />
      </el-form-item>

      <el-form-item label="分类" prop="appCatTitle">
        <el-radio-group v-model="state.formData.permitType">
          <el-radio :label="1">
            权限
          </el-radio>
          <el-radio :label="0">
            分组
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="编码" prop="permitCode">
        <el-input v-model="state.formData.permitCode" />
      </el-form-item>

      <el-form-item label="描述" prop="permitDesc">
        <el-input v-model="state.formData.permitDesc" type="textarea" rows="5"/>
      </el-form-item>

      <el-form-item label="排序">
        <el-input v-model="state.formData.sortNo" type="number" min="1" />
      </el-form-item>
    </el-form>

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