<template>
    <div class="w-full items-center flex flex-col">
        <ILibraryHeader @search="handleSearch" @clear="getThreadData" />
        <div class="container max-w-screen-lg flex flex-col p-6">
            <div class="flex gap-10">
                <div class="flex flex-col flex-grow">
                    <div v-auto-animate class="flex flex-col gap-4">
                        <ILibraryThread
                            v-for="item in threads"
                            :item="item"
                            :key="item.c_id"
                            @delete="handleDeletedThread"
                        />
                    </div>
                </div>
                <div class="flex flex-col flex-shrink-0 w-56">
                    <div class="flex justify-between items-center">
                        <div class="flex items-center text-lg gap-2">
                            <UIcon name="i-heroicons-squares-2x2" />
                            <div>合集</div>
                        </div>
                        <UButton
                            icon="i-heroicons-plus-20-solid"
                            variant="soft"
                            @click="handleOpenCreateLibrary"
                        />
                    </div>
                    <div class="flex flex-col gap-4 mt-4">
                        <ILibraryCollect
                            v-for="item in $collection"
                            :item="item"
                            :key="item.id"
                        />
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup>
const { $collection } = storeToRefs(useLibraryStore())
const { $openLibraryCreate, $getCollection } = useLibraryStore()
const { getThreadsList } = useLibraryRequest()

function handleOpenCreateLibrary () {
    $openLibraryCreate()
}
// 主题列表
const threads = ref([])
async function getThreadData() {
    threads.value  = await getThreadsList()
}
await getThreadData()
await $getCollection()

function handleDeletedThread (c_id) {
    const findIndex = threads.value.findIndex(i => i.c_id === c_id)
    if (findIndex >= 0) threads.value.splice(findIndex, 1)
}
function handleSearch (data) {
    threads.value = data
}
</script>
