<template>
    <el-dialog :modelValue="modelValue" @close="$emit('update:modelValue', false)" title="编辑权限">
        <el-form :model="role" lable-width="120px">
            <el-form-item lable="名字">
                <el-input v-model="role.name" />
            </el-form-item>
            <el-form-item lable="是否启用">
                <el-switch v-model="role.valid" :active-value="1" :inactive-value="0" />
            </el-form-item>
            <el-form-item lable="备注">
                <el-input v-model="role.remark" type="textarea" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button type="default" @click="$emit('update:modelValue', false)">取消</el-button>
            <el-button v-loading="submitLoading" type="primary" @click="submitForm()">提交</el-button>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { update } from '/@/api/role'
import type { Role } from '/@/api/role'
import { ElMessage } from 'element-plus'

const { modelValue, role } = defineProps<{
    modelValue: boolean,
    role: Role
}>()
const emit = defineEmits(['update:modelValue'])
const submitLoading = ref(false)

const submitForm = async () => {
    submitLoading.value = true
    const result = await update(role)
    if (result.code != 0) {
        return
    }
    submitLoading.value = false
    ElMessage({ message: '更新成功' })

    emit('update:modelValue', false)
}
</script>