<!-- This example requires Tailwind CSS v2.0+ -->
<template>
    <div class="pb-8 px-4 sm:px-6 lg:px-8">
        <br />
        <div class="sm:flex sm:items-center">
            <div class="sm:flex-auto">
                <h1 class="text-xl font-semibold text-gray-900">
                    가입 허가 리스트
                </h1>
                <p class="mt-2 text-sm text-gray-700">
                    가입 사전 허가 리스트를 확인할 수 있습니다.
                </p>
            </div>
            <div class="mt-4 sm:mt-0 sm:ml-16 sm:flex-none">
                <button
                    type="button"
                    class="inline-flex items-center justify-center rounded-md border border-transparent bg-indigo-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 sm:w-auto"
                    @click="navigateTo('/admin/user/white/new')"
                >
                    새 계정 추가
                </button>
            </div>
        </div>

        <BaseTable2
            :headings="listHeadings"
            :actions="listActions"
            :keys="listKeys"
            :data="listData"
            :action-key="actionKey"
            :column-filter="columnFilter"
            :do-action="doAction"
        />

        <BasePagination1
            :total-page-count="totalPageCount"
            :current-page-number="currentPageNumber"
            :page-size="pageSize"
            :records-total="recordsTotal"
            :page-move="pageMove"
        />
    </div>
</template>

<script setup lang="ts">
const router = useRouter();
definePageMeta({
    middleware: 'check-auth-admin',
});

const listHeadings = [
    {
        title: '아이디',
        widthRatio: '100',
        key: 'uid',
    },
    {
        title: '역할',
        widthRatio: '',
        key: 'level',
    },
    {
        title: '상태',
        widthRatio: '',
        key: 'status',
    },

    {
        title: '생성일',
        widthRatio: '',
        key: 'created',
    },
];
const listActions = ['상세보기'];
const actionKey = 'uid';
const listKeys = ['serial', 'uid', 'name', 'domain', 'email', 'role'];
const listData = ref([]);

const totalPageCount = ref(0);
const currentPageNumber = ref(1);
const pageSize = ref(10);
const recordsTotal = ref(0);
// const order = [{ column: 'serial', dir: 'desc' }];
// const columns = { serial: { data: 'serial' } };
const { $dayjs } = useNuxtApp();
function columnFilter(key, val) {
    // console.log("columnFilter(), key = ", key, ", val = ", val);

    if (key == 'updated' || key == 'created') {
        return $dayjs(val).format('YY/MM/DD  A h:mm:ss');
        // return $dayjs(val).format('YY/MM/DD');
    }
    if (key == 'status') {
        if (val == 0) {
            return '정상';
        } else if (val == 4) {
            return '삭제';
        } else {
            return 'unknown(' + val + ')';
        }

        // return $dayjs(val).format('YY/MM/DD');
    }

    if (key == 'level') {
        switch (val) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                return _crossCtl.siteConfig.userLevelInfo[val]['title'];
                break;

            default:
                return 'unknown(' + val + ')';
        }
    } else {
        return val;
    }
}

function doAction(tag, target) {
    console.log('on doAction(), tag=', tag, ', target=', target);
    navigateTo('/admin/user/white/edit/' + target);
}

function pageMove(targetPageIdex) {
    console.log('on pageMove(), targetPageIdex=', targetPageIdex);
    currentPageNumber.value = targetPageIdex;
    refresh();
}

async function refresh() {
    if (process.client) {
        const responseJson = await _crossCtl.doComm('list', 'admin:white', {
            start: (currentPageNumber.value - 1) * pageSize.value,
            length: pageSize.value,
        });

        console.log('responseJson=', responseJson);

        currentPageNumber.value = responseJson['currentPageNumber'];
        totalPageCount.value = responseJson['totalPageCount'];
        pageSize.value = parseInt(responseJson['pageSize']);
        recordsTotal.value = responseJson['recordsTotal'];

        listData.value = responseJson['data'];
    }
}

refresh();
</script>
