<template>
    <app-drawer class="system-menu-edit" title="编辑" width="680" v-model:show="show">
        <template #default>
            <el-form label-width="90px" :model="form">
                <el-form-item label="权限类型" prop="authType" required>
                    <el-radio-group v-model="form.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="id">
                    <el-select />
                </el-form-item>
                <el-form-item label="权限标题" prop="title" required>
                    <el-input v-model="form.title" />
                </el-form-item>
                <el-form-item label="权限代码" prop="code" required>
                    <el-input v-model="form.code" />
                </el-form-item>
                <el-form-item label="按钮样式" v-if="form.authType === AuthType.Button">
                    <el-select v-model="form.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="form.buttonType">
                        <component :is="components.appIcon" :icon="form.icon" />
                        <span>{{ form.title }}</span>
                    </el-button>
                </el-form-item>
                <el-form-item label="图标">
                    <component :is="components.appIconSelect" v-model="form.icon" />
                </el-form-item>
                <el-form-item label="链接地址" prop="url" required v-if="form.authType === AuthType.Menu">
                    <el-input v-model="form.url">
                        <template #prepend>
                            <el-select v-model="form.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" required
                    v-if="form.authType !== AuthType.Menu || form.urlType === UrlType.Path">
                    <el-input v-model="form.component" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input type="textarea" :rows="2" v-model="form.remark" />
                </el-form-item>
                <el-form-item label="是否隐藏">
                    <el-switch v-model="form.hidden" />
                </el-form-item>
            </el-form>
        </template>
        <template #footer>
            <el-button @click="cancel">取消</el-button>
            <el-button @click="submit" type="primary">提交</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, reactive, PropType, defineAsyncComponent } from 'vue'
import { AuthType, UrlType, getAuthTypeList, getUrlTypeList } from '@/constants/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<Model.UserMenu>,
        default: () => ({
            id: 0,
            authType: AuthType.Menu,
            title: '',
            code: '',
            urlType: UrlType.Path,
            url: '',
            component: '',
            icon: '',
            buttonType: 'default',
            sort: 0,
            hidden: false,
            remark: '',
        })
    }
})

const show = ref(true)
const form = reactive({ ...props.selectedRow })

const cancel = () => {
    show.value = false
}

const submit = () => {
    show.value = false
}
</script>