<!-- This example requires Tailwind CSS v2.0+ -->
<template>
    <div class="pb-8 px-4 sm:px-6 lg:px-8">
        <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">
                    삭제된 내 API 키를 보고 복구할 수 있습니다.
                </p>
            </div>
            <div class="mt-4 sm:mt-0 sm:ml-16 sm:flex-none">
                <button
                    type="button"
                    class="inline-flex mr-3 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-red-400 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2 sm:w-auto"
                    @click="$router.push('/key/list')"
                >
                    키 리스트
                </button>
            </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 class="p-0 rounded-bl-2xl rounded-br-2xl md:px-0"></div>
    </div>
</template>

<script setup lang="ts">
const router = useRouter();

definePageMeta({
    middleware: 'check-auth-op',
});

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: 'level' },
                {
                    class: 'mt-1 truncate text-gray-500 sm:hidden',
                    key: 'status',
                },
            ],
        },
        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: 'level',
        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: 'status',
        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: 'created',
        hiddenInfo: {
            headClass: '',
            dts: [],
            dds: [],
        },
        subClass: 'px-3 py-4 text-sm text-gray-500',
    },
];
const listActions = ['상세보기'];
const actionKey = 'serial';
const listKeys = ['serial', 'api_key', 'name', 'level', 'status', 'created'];
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 maekNewKey() {
    return navigateTo({
        path: '/key/new',
        query: {},
    });
}

function doAction(tag, target) {
    console.log('on doAction(), tag=', tag, ', target=', target);
    /*
    router.push({
        name: 'key-edit',
        params: { target: target },
    });
    */
    navigateTo('/key/' + 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(
            'local/list',
            'key:deleted',
            {
                start: (currentPageNumber.value - 1) * pageSize.value,
                length: pageSize.value,
            }
        );

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

        listData.value = responseJson['data'];
    }
}
refresh();
</script>
