<script setup>
import { ref, reactive, watch, computed } from "vue";

import { getUser, addUser, updateUser } from "@/api/user";

const props = defineProps({
    show: {
        type: Boolean,
        default: false,
    },
    edit_id: {
        type: Number,
        default: 0,
    },
});

const emit = defineEmits(["update:show", "update:edit_id", "reload"]);

const showDrawer = computed({
    get: () => props.show,
    set: (value) => {
        emit("update:show", value);
    },
});

const formRef = ref();

const form = ref({
    act: "insert",
    password: '',
    cfm_password: '',
    name: "",
});

const validatePass2 = (rule, value, callback) => {
    console.log('value', value, form.value.password)
    if (value !== form.value.password) {
        callback(new Error("兩次輸入密碼不一致!"))
    } else {
        callback()
    }
}

const rules = reactive({
    role_name: [{ required: true, message: "請輸入角色名稱", trigger: "blur" }],
});

const loading = ref(false);

watch(
    () => props.edit_id,
    async (newVal) => {
        if (newVal > 0) {
            let res = await getUser(newVal);
            if (res.code === 200) {
                res.data.password = '';
                res.data.cfm_password = '';
                form.value = res.data;
                form.value.act = "update";
            }
        } else {
            form.value = {
                act: "insert",
            };
        }
    }
);

const handleClose = () => {
    emit("update:edit_id", 0);
    showDrawer.value = false;
};

const handleSubmit = async (formEl) => {

    if (!formEl) return;

    await formEl.validate(async (valid, fields) => {
        if (!valid) return;

        let res;

        if (form.value.act === "update") {
            res = await updateUser(form.value);
        } else {
            res = await addUser(form.value);
        }

        if (res.code === 200) {
            // 關閉抽屜
            showDrawer.value = false;
            // 重置表單
            formRef.value.resetFields();

            // emit reload event
            emit("reload");

            // 顥示提示訊息
            return ElMessage.success("操作成功");
        }
        // 顯示錯誤提示訊息
        return ElMessage.error(res.msg);
    });
};
</script>

<template>
    <el-drawer v-model="showDrawer" size="50%" title="新增角色" @close="handleClose">
        <template #header>
            <h4>新增角色</h4>
        </template>
        <template #default>
            <!-- <div v-loading="loading"> -->
            <el-form :model="form" ref="formRef" :rules="rules" label-width="120px">
                <el-form-item label="角色名稱" prop="role_name">
                    <el-input v-model="form.role_name" />
                </el-form-item>
                <el-form-item label="角色描述" prop="role_describe">
                    <el-input v-model="form.role_describe" :rows="3" type="textarea" placeholder="Please input" />
                </el-form-item>
            </el-form>
            <!-- </div> -->
        </template>
        <template #footer>
            <div style="flex: auto">
                <el-button @click="handleClose">關閉</el-button>
                <el-button type="primary" @click="handleSubmit(formRef)">送出</el-button>
            </div>
        </template>
    </el-drawer>
</template>

<style lang="less"></style>
