<!-- 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">
                    {{ pageTitle }}
                </h1>
                <p class="mt-2 text-sm text-gray-700">
                    {{ pageDescription }}
                </p>
            </div>
            <div class="mt-4 sm:mt-0 sm:ml-16 sm:flex-none">
                <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="$router.push(activeListPath)"
                    >
                        활성 항목 리스트
                    </button>
                </div>

                <div
                    v-if="currentTarget == 'inquiry'"
                    class="mt-4 sm:mt-0 sm:ml-16 sm:flex-none"
                >
                    <label for="mobile-search-candidate" class="sr-only"
                        >Search</label
                    >
                    <label for="desktop-search-candidate" class="sr-only"
                        >Search</label
                    >
                    <div class="flex rounded-md shadow-sm">
                        <div class="relative flex-grow focus-within:z-10">
                            <div
                                class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none"
                            >
                                <MagnifyingGlassCircleIcon
                                    class="h-5 w-5 text-gray-400"
                                    aria-hidden="true"
                                />
                            </div>
                            <input
                                id="mobile-search-candidate"
                                v-model="searchKeyword"
                                type="text"
                                name="mobile-search-candidate"
                                class="focus:ring-indigo-500 focus:border-indigo-500 block w-full rounded-none rounded-l-md pl-10 sm:hidden border-gray-300"
                                placeholder=""
                            />
                            <input
                                id="desktop-search-candidate"
                                v-model="searchKeyword"
                                type="text"
                                name="desktop-search-candidate"
                                class="hidden focus:ring-indigo-500 focus:border-indigo-500 w-full rounded-none rounded-l-md pl-10 sm:block sm:text-sm border-gray-300"
                                placeholder=""
                            />
                        </div>
                        <button
                            type="button"
                            class="-ml-px relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-r-md text-gray-700 bg-gray-50 hover:bg-gray-100 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500"
                            @click="doAction('search', searchKeyword)"
                        >
                            <span class="ml-2">검색</span>
                        </button>
                    </div>
                </div>
            </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">
import {
    ChevronDownIcon,
    MagnifyingGlassCircleIcon,
} from '@heroicons/vue/24/solid';
const route = useRoute();

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

const searchKeyword = ref('');

const currentTarget = ref('notice');

const pageTitle = ref('제목');
const pageDescription = ref('설명');

const listActions = ref(['상세보기']);
const actionKey = ref('serial');
const listKeys = ref(['serial', 'uid', 'name', 'domain', 'email', 'role']);

const listHeadings = ref([]);

const doActionTargetName = 'admin-support-target-edit';
let listSource = 'list';
let listTarget = 'dummy';
let activeListPath = '/admin/key/deleted';

let makeNewTargetPath = 'admin-support-notice-new';

const inquiryListOptionTags = ['대기중', '검토중', '답변완료', '삭제'];

if (route.params.target instanceof Array) {
    if (route.params.target.length != 1) {
        throwError('$404');
    } else {
        if (
            route.params.target[0] != 'notice' &&
            route.params.target[0] != 'faq' &&
            route.params.target[0] != 'inquiry'
        ) {
            throwError('$404');
        } else {
            currentTarget.value = route.params.target[0];

            console.log('currentTarget.value=', currentTarget.value);
            switch (route.params.target[0]) {
                case 'notice':
                    pageTitle.value = '삭제된 공지사항';
                    pageDescription.value =
                        '삭제된 공지사항을 보고 복구 합니다.';

                    listHeadings.value = [
                        {
                            title: '일련번호',
                            class: 'py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6',
                            key: 'serial',
                            hiddenInfo: {
                                headClass: 'font-normal lg:hidden',
                                dts: [],
                                dds: [],
                            },
                            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: 'title',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass:
                                'hidden px-3 py-4 text-sm text-gray-500 lg:table-cell',
                        },
                        {
                            title: '상태',
                            class: 'px-3 py-3.5 text-left text-sm font-semibold text-gray-900',
                            key: 'status',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass: 'px-3 py-4 text-sm text-gray-500',
                        },
                        {
                            title: '수정일',
                            class: 'hidden px-3 py-3.5 text-left text-sm font-semibold text-gray-900 sm:table-cell',
                            key: 'updated',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass:
                                'hidden px-3 py-4 text-sm text-gray-500 sm:table-cell',
                        },

                        {
                            title: '작성일',
                            class: 'hidden px-3 py-3.5 text-left text-sm font-semibold text-gray-900 sm:table-cell',
                            key: 'created',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass:
                                'hidden px-3 py-4 text-sm text-gray-500 sm:table-cell',
                        },
                    ];

                    listActions.value = ['상세보기'];
                    actionKey.value = 'serial';
                    listKeys.value = [
                        'serial',
                        'title',
                        'status',
                        'updated',
                        'created',
                    ];

                    makeNewTargetPath = '/admin/support/notice/new';
                    listSource = 'list';
                    listTarget = 'notice:deleted';

                    activeListPath =
                        '/admin/support/' + route.params.target[0] + '/list';

                    break;
                case 'faq':
                    pageTitle.value = '삭제된 자주 묻는 질문';
                    pageDescription.value = '삭제된 FAQ를 보고 복구 합니다.';

                    listHeadings.value = [
                        {
                            title: '일련번호',
                            class: 'py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6',
                            key: 'serial',
                            hiddenInfo: {
                                headClass: 'font-normal lg:hidden',
                                dts: [],
                                dds: [],
                            },
                            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: 'question',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass:
                                'hidden px-3 py-4 text-sm text-gray-500 lg:table-cell',
                        },
                        {
                            title: '상태',
                            class: 'px-3 py-3.5 text-left text-sm font-semibold text-gray-900',
                            key: 'status',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass: 'px-3 py-4 text-sm text-gray-500',
                        },
                        {
                            title: '수정일',
                            class: 'hidden px-3 py-3.5 text-left text-sm font-semibold text-gray-900 sm:table-cell',
                            key: 'updated',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass:
                                'hidden px-3 py-4 text-sm text-gray-500 sm:table-cell',
                        },

                        {
                            title: '작성일',
                            class: 'hidden px-3 py-3.5 text-left text-sm font-semibold text-gray-900 sm:table-cell',
                            key: 'created',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass:
                                'hidden px-3 py-4 text-sm text-gray-500 sm:table-cell',
                        },
                    ];

                    listActions.value = ['상세보기'];
                    actionKey.value = 'serial';
                    listKeys.value = [
                        'serial',
                        'question',
                        'status',
                        'updated',
                        'created',
                    ];

                    makeNewTargetPath = '/admin/support/faq/new';
                    listSource = 'list';
                    listTarget = 'faq:deleted';

                    activeListPath =
                        '/admin/support/' + route.params.target[0] + '/list';

                    break;
                case 'inquiry':
                    pageTitle.value = '처리 완료된 1:1 문의';
                    pageDescription.value =
                        '처리 완료된 1:1 문의 내용을 확인할 수 있습니다.';

                    listHeadings.value = [
                        {
                            title: '일련번호',
                            class: 'py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6',
                            key: 'serial',
                            hiddenInfo: {
                                headClass: 'font-normal lg:hidden',
                                dts: [],
                                dds: [],
                            },
                            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: 'name',
                            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 lg:table-cell',
                            key: 'title',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass:
                                'hidden px-3 py-4 text-sm text-gray-500 lg:table-cell',
                        },

                        {
                            title: '상태',
                            class: 'px-3 py-3.5 text-left text-sm font-semibold text-gray-900',
                            key: 'status',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass: 'px-3 py-4 text-sm text-gray-500',
                        },
                        {
                            title: '수정일',
                            class: 'hidden px-3 py-3.5 text-left text-sm font-semibold text-gray-900 sm:table-cell',
                            key: 'updated',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass:
                                'hidden px-3 py-4 text-sm text-gray-500 sm:table-cell',
                        },

                        {
                            title: '작성일',
                            class: 'hidden px-3 py-3.5 text-left text-sm font-semibold text-gray-900 sm:table-cell',
                            key: 'created',
                            hiddenInfo: {
                                headClass: '',
                                dts: [],
                                dds: [],
                            },
                            subClass:
                                'hidden px-3 py-4 text-sm text-gray-500 sm:table-cell',
                        },
                    ];

                    listActions.value = ['상세보기'];
                    actionKey.value = 'serial';
                    listKeys.value = [
                        'serial',
                        'name',
                        'title',
                        'status',
                        'updated',
                        'created',
                    ];

                    makeNewTargetPath = '/admin/support/inquiry/new';
                    listSource = 'list';
                    listTarget = 'admin:inquiry:done';

                    activeListPath =
                        '/admin/support/' + route.params.target[0] + '/list';

                    break;
            }
        }
    }
} else {
    throwError('$404');
}

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 if (key == 'status') {
        if (currentTarget.value == 'inquiry') {
            return inquiryListOptionTags[val];
        } else {
            return val;
        }
    } else {
        return val;
    }
}

const router = useRouter();
function doAction(tag, target) {
    console.log('on doAction(), tag=', tag, ', target=', target);
    navigateTo('/admin/support/' + currentTarget.value + '/edit/' + target);
    /*
    router.push({
        name: doActionTargetName,
        params: { hero: target, target: [currentTarget.value] },
    });
    */
}

function makeNewOne() {
    /*
    router.push({
        path: makeNewTargetPath,
        params: {},
    });
    */
    navigateTo({ path: makeNewTargetPath, params: {} });
}

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

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

        if (responseJson['responseCode'] != 200) {
            alert(responseJson['responseMessage']);
        } else {
            currentPageNumber.value = responseJson['currentPageNumber'];
            totalPageCount.value = responseJson['totalPageCount'];
            pageSize.value = parseInt(responseJson['pageSize']);
            recordsTotal.value = responseJson['recordsTotal'];

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

refresh();
</script>
