<template>
    <div class="row g-4 mt-2" v-if="memberList?.length">
        <div>
            <div class="card">
                <div class="card-body p-3 row">
                    <div class="d-flex justify-content-between mb-2">
                        <h5 class="card-title fw-bold">사원 등록 관리</h5>
                    </div>
                    <div class="g-4 col-12 col-lg-6" v-for="member in memberList" :key="member.MEMBERSEQ">
                        <div class="card">
                            <div class="row card-body">
                                <div class="col-3 d-flex align-items-center">
                                    <img
                                        :src="`${imgURL}profile/${member.MEMBERPRF}`"
                                        alt="Profile Image"
                                        class="img-thumbnail mx-auto d-block"
                                        style="max-height: 140px"
                                        @error="$event.target.src = '/img/icons/icon.png'"
                                    />
                                </div>
                                <div class="col-7">
                                    <div class="d-flex flex-sm-row align-items-center pb-2">
                                        <div class="font-bold">{{ member.MEMBERNAM }}</div>
                                        <div class="ms-2">
                                            ({{ member.MBTI }})
                                            <img
                                                role="img"
                                                class="w-px-30 h-px-40"
                                                :src="`/img/mbti/${member.MBTI.toLowerCase()}.png`"
                                                alt="MBTI image"
                                            />
                                        </div>
                                    </div>
                                    <div class="d-flex flex-sm-row align-items-center pb-2">
                                        <i class="bx bx-id-card"></i>
                                        <div class="ms-2">{{ member.MEMBERIDS }}</div>
                                    </div>
                                    <div class="d-flex flex-sm-row align-items-center pb-2">
                                        <i class="bx bxs-phone"></i>
                                        <div class="ms-2">{{ $common.addHyphenToPhoneNumber(member.MEMBERTEL) }}</div>
                                    </div>
                                    <div class="d-flex flex-sm-row align-items-center pb-2">
                                        <i class="bx bx-calendar"></i>
                                        <div class="ms-2">{{ $common.dateFormatter(member.MEMBERCDT) }}</div>
                                    </div>
                                </div>
                                <div class="col-2 d-flex align-items-center">
                                    <div>
                                        <div>
                                            <label class="switch"
                                                ><input
                                                    type="checkbox"
                                                    :checked="checked"
                                                    @change="handleRegisterMember(member.MEMBERSEQ)" /><span class="slider round"></span
                                            ></label>
                                        </div>
                                        <button
                                            class="btn-close btn-close-sm"
                                            style="position: absolute; top: 10px; right: 10px"
                                            @click="handleRejectMember(member.MEMBERSEQ)"
                                        ></button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
    import { onMounted, ref } from 'vue';
    import { useToastStore } from '@s/toastStore';
    import $api from '@api';

    const memberList = ref([]);
    const checked = ref(false);
    const toast = useToastStore();
    const imgURL = import.meta.env.VITE_SERVER_IMG_URL;

    // 조회 api
    const fetchRegisterMemberList = async () => {
        const { data } = await $api.get('main/registerMemberList');
        if (data?.data) memberList.value = data.data;
    };

    // 사원 등록 api
    const handleRegisterMember = async memberSeq => {
        const { data } = await $api.post('main/registerMember', { memberSeq: memberSeq });
        if (data?.data) {
            toast.onToast(data.data, 's');
            fetchRegisterMemberList();
        }
    };

    // 사원 등록 미승인 api
    const handleRejectMember = async memberSeq => {
        if (!confirm('해당 사원 등록을 거절하시겠습니까?')) return;

        const { data } = await $api.post('main/rejectMember', { memberSeq: memberSeq });
        if (data?.data) {
            toast.onToast(data.data, 's');
            fetchRegisterMemberList();
        }
    };

    onMounted(async () => {
        await fetchRegisterMemberList();
    });
</script>
