<!-- 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"></div>
        </div>

        <BaseList1
            :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: '이름',
        class: 'py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6',
        key: 'name',
        hiddenInfo: {
            headClass: 'font-normal lg:hidden',
            dts: [
                { class: 'sr-only', title: '소속' },
                { class: 'sr-only sm:hidden', title: '이메일' },
            ],
            dds: [
                { class: 'mt-1 truncate text-gray-700', key: 'domain' },
                {
                    class: 'mt-1 truncate text-gray-500 sm:hidden',
                    key: 'email',
                },
            ],
        },
        subClass:
            'w-full max-w-0 py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:w-auto sm:max-w-none sm:pl-6',
    },
    {
        title: '소속',
        class: 'hidden px-3 py-3.5 text-left text-sm font-semibold text-gray-900 lg:table-cell',
        key: 'domain',
        hiddenInfo: {
            headClass: '',
            dts: [],
            dds: [],
        },
        subClass: 'hidden px-3 py-4 text-sm text-gray-500 lg:table-cell',
    },
    {
        title: '이메일',
        class: 'hidden px-3 py-3.5 text-left text-sm font-semibold text-gray-900 sm:table-cell',
        key: 'email',
        hiddenInfo: {
            headClass: '',
            dts: [],
            dds: [],
        },
        subClass: 'hidden px-3 py-4 text-sm text-gray-500 sm:table-cell',
    },
    {
        title: '역할',
        class: 'px-3 py-3.5 text-left text-sm font-semibold text-gray-900',
        key: 'role',
        hiddenInfo: {
            headClass: '',
            dts: [],
            dds: [],
        },
        subClass: 'px-3 py-4 text-sm text-gray-500',
    },
];
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');
    } else {
        return val;
    }
}

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

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:users:level:all',
            {
                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>
