<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">
                <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="makeNewOne"
                >
                    새 문의 작성
                </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>
</template>

<script setup lang="ts">
const route = useRoute();

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

const inquiryListOptionTags = ['대기중', '검토중', '답변완료', '삭제'];
const inquiryListOption = ref(0);

const pageTitle = '1:1 문의';
const pageDescription =
    '나의 1:1 문의 내역을 확인하고 새로운 문의를 작성할 수 있습니다.';

const listSource = 'list';
const listTarget = 'inquiry:all';

const deletedListName = 'support-inquiry-deleted';
const makeNewTargetName = 'support-inquiry-new';

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

const listHeadings = [
    {
        title: '제목',
        widthRatio: '',
        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: '상태',
        widthRatio: '10',
        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: '수정일',
        widthRatio: '15',
        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: '작성일',
        widthRatio: '15',
        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',
    },
];

const doActionTargetName = 'support-inquiry-view';

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') {
        return inquiryListOptionTags[val];
    } else {
        return val;
    }
}

const router = useRouter();
function doAction(tag, target) {
    console.log('on doAction(), tag=', tag, ', target=', target);

    navigateTo('/support/inquiry/view/' + target);
}

function makeNewOne() {
    router.push({
        name: makeNewTargetName,
        params: {},
    });
}

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

async function refresh() {
    const responseJson = await _crossCtl.doComm(listSource, listTarget, {
        hero: inquiryListOption.value,
        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>
