<template>
<div class="container-xxl flex-grow-1 container-p-y">
    <div class="row g-3">
    <div class="mt-8">
        <!-- 투표 작성  -->
        <router-link to="/voteboard/write"><WriteBtn /></router-link>
        <!-- 내가한 투표 보기  -->
        <div class="d-flex align-items-center">
        <div class="form-check me-3">
            <input class="form-check-input" type="checkbox" value="" id="deliveryAdd" checked="">
            <label class="form-check-label" for="deliveryAdd">내가 한 투표</label>
        </div>
        <!-- 투표마감/투표중  셀렉트 -->
        <FormSelect class="col-3" name="cate" :isLabel="false" title="투표상태" :data="categoryList" @update:data="category = $event" />
        </div>
        <!-- 투표리스트 -->
        <vote-list
        :data="voteListCardData"
        @addContents="addContents"
        @checkedNames="checkedNames"/>
        </div>
    </div>
        <!-- 페이지네이션 -->
        <div class="row g-3">
            <div class="mt-8">
                <Pagination
                    v-if="PageData.pages"
                    v-bind="PageData"
                    @update:currentPage="handlePageChange"
                />
            </div>
        </div>
</div>
</template>
<script setup>
import { getCurrentInstance, onMounted, ref } from 'vue';
import Pagination from '@c/pagination/Pagination.vue';
import router from '@/router';
import FormSelect from '@c/input/FormSelect.vue';
import { useToastStore } from '@s/toastStore';
import QEditor from '@c/editor/QEditor.vue';
import $api from '@api';
import Quill from 'quill';
import WriteBtn from '@c/button/WriteBtn.vue';
import voteList from '@c/voteboard/voteCardList.vue';
const toastStore = useToastStore();
const category = ref('0');
const categoryList = ['전체','투표마감', '투표중'];
const PageData = ref([]);
const voteListCardData = ref([]);
const titleAlert = ref(false);
const addContent = ref('');
const currentPage = ref(1);
onMounted(async () => {
    getvoteList();
});
//투표목록
const getvoteList = async () => {
    console.log('pagee',currentPage.value)
        const response = await $api.get('vote/getVoteList',{
            params: { page: currentPage.value }
        });
        if (response.data.status === "OK") {
            PageData.value = response.data.data;
            voteListCardData.value = response.data.data.list;
        }
    };
//투표항목추가
const addContents = (itemList, voteId) =>{
    $api.post('vote/insertWord',{
            itemList :itemList
            ,voteId :voteId
        }).then((res)=>{
            if(res.data.status === 'OK'){
                toastStore.onToast('항목이 등록되었습니다.', 's');
                getvoteList();
            }
        })
}
//투표하기
const checkedNames = (numList) =>{
    $api.post('vote/insertCheckedNums',{
        checkedList :numList
        ,votenum : numList[0].LOCVOTSEQ
        }).then((res)=>{
            if(res.data.status === 'OK'){
                toastStore.onToast('투표가 완료되었습니다.', 's');
                getvoteList();
            }
        })
}

// 페이지 변경
const handlePageChange = async (page) => {
    currentPage.value=page;
    await getvoteList();
};
</script>
<style></style>
