<template>
    <app-drawer class="system-menu-edit" title="编辑" width="680" v-model:show="show" :loading="loading"
        :refresh="refresh">
        <template #default>
            <el-form ref="formRef" label-width="90px" :model="formData">
                <el-form-item label="权限类型" prop="authtype">
                    <el-radio-group v-model="formData.authtype">
                        <el-radio :label="item.value" v-for="(item, index) in getAuthTypeList()" :key="index">
                            {{ item.label }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="所属层级" prop="parentcode">
                    <el-tree-select :data="menus" v-model="formData.parentcode"
                        :props="{ label: 'resourcename', value: 'resourcecode' }" check-strictly default-expand-all />
                </el-form-item>
                <el-form-item label="权限标题" prop="resourcename">
                    <el-input v-model="formData.resourcename" />
                </el-form-item>
                <el-form-item label="权限代码" prop="resourcecode">
                    <el-input v-model="formData.resourcecode" />
                </el-form-item>
                <el-form-item label="按钮代码" prop="buttonname" v-if="formData.authtype === AuthType.Button">
                    <el-input v-model="formData.buttonname" />
                </el-form-item>
                <el-form-item label="按钮样式" v-if="formData.authtype === AuthType.Button">
                    <el-select v-model="formData.buttontype" style="margin-right:12px">
                        <el-option label="默认" value="default" />
                        <el-option label="主要" value="primary" />
                        <el-option label="成功" value="success" />
                        <el-option label="信息" value="info" />
                        <el-option label="警告" value="warning" />
                        <el-option label="危险" value="danger" />
                    </el-select>
                    <el-button :type="formData.buttontype">
                        <component :is="components.appIcon" :icon="formData.iconame" />
                        <span>{{ formData.resourcename }}</span>
                    </el-button>
                </el-form-item>
                <el-form-item label="图标">
                    <component :is="components.appIconSelect" v-model="formData.iconame" />
                </el-form-item>
                <el-form-item label="链接地址" prop="url" v-if="formData.authtype === AuthType.Menu">
                    <el-input v-model="formData.url">
                        <template #prepend>
                            <el-select v-model="formData.urltype" style="width: 120px">
                                <el-option :label="item.label" :value="item.value"
                                    v-for="(item, index) in getUrlTypeList()" :key="index" />
                            </el-select>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item label="组件地址" prop="component"
                    v-if="formData.authtype !== AuthType.Menu || formData.urltype === UrlType.Path">
                    <el-input v-model="formData.component" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input type="textarea" :rows="2" v-model="formData.remark" />
                </el-form-item>
                <el-form-item label="是否隐藏">
                    <el-switch :active-value="1" :inactive-value="0" v-model="formData.hidden" />
                </el-form-item>
            </el-form>
        </template>
        <template #footer>
            <el-button @click="onCancel(false)">取消</el-button>
            <el-button @click="onSubmit" type="primary">提交</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, PropType, defineAsyncComponent } from 'vue'
import { ElMessage } from 'element-plus'
import { AuthType, UrlType, getAuthTypeList, getUrlTypeList } from '@/constants/menu'
import { useMenuForm } from '@/business/menu'
import AppDrawer from '@pc/components/base/drawer/index.vue'

const components = {
    appIcon: defineAsyncComponent(() => import('@pc/components/base/icon/index.vue')),
    appIconSelect: defineAsyncComponent(() => import('@pc/components/base/icon-select/index.vue')),
}

const props = defineProps({
    selectedRow: {
        type: Object as PropType<Ermcp.NewFuncmenuRsp>,
        default: () => ({})
    }
})

const { loading, menus, formData, addOrUpdate } = useMenuForm(props.selectedRow)
const show = ref(true)
const refresh = ref(false)

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

const onSubmit = () => {
    addOrUpdate().then(() => {
        ElMessage.success('提交成功')
        onCancel(true)
    }).catch((err) => {
        ElMessage.error('提交失败：' + err)
    })
}
</script>