<template>
    <view class="my-fav">
        <!-- 顶部操作栏 -->
        <view class="header" v-if="bookList.length">
            <view class="left">
                <u-checkbox v-if="isEditMode" class="checkbox-item" v-model="isAllSelected" @change="toggleSelectAll"
                    shape="circle" active-color="#38C148"></u-checkbox>
                <text v-if="isEditMode" class="select-text">全选</text>
            </view>
            <view class="right">
                <text v-if="!isEditMode" @tap="toggleEditMode">管理</text>
                <text v-else @tap="handleDelete">移除收藏夹</text>
            </view>
        </view>

        <!-- 书籍列表 -->
        <page-scroll :page-size="12" url="/token/shop/user/collectList" @updateList="handleUpdateList" ref="pageRef" slotEmpty emptyText="您暂未收藏书籍">
            <view class="book-list">
                <BookListItem v-for="book in bookList" :key="book.isbn" :book="book" :isEditMode="isEditMode"
                    @click="handleBookClick" @checked="handleChecked" />
            </view>
        </page-scroll>

        <!-- 删除确认弹窗 -->
        <common-dialog ref="deleteDialog" title="温馨提示" @confirm="confirmDelete">
            <text>确定移除选中商品吗？</text>
        </common-dialog>
    </view>
</template>

<script>
import BookListItem from '../components/book-list-item.vue'
import commonDialog from '@/components/common-dialog.vue'
import pageScroll from '@/components/pageScroll/index.vue'

export default {
    components: {
        BookListItem,
        commonDialog,
        pageScroll
    },
    data() {
        return {
            isEditMode: false,
            bookList: [],
            checkedIds: [],
            isAllSelected: false
        }
    },
    // #ifdef MP-ALIPAY
    onPullDownRefresh() {
        this.$refs.pageRef?.loadData(true)
    },
    // #endif
    onShow() {
        this.$refs.pageRef?.loadData(true)
    },
    methods: {
        handleChecked({ book, checked }) {
            this.$nextTick(() => {
                let item = this.bookList.find(item => item.isbn === book.isbn)
                let index = this.bookList.findIndex(item => item.isbn === book.isbn)
                if (item) {
                    item.selected = checked
                    this.$set(this.bookList, index, item)
                    this.isAllSelected = this.bookList.every(item => item.selected)
                }
            })
        },
        handleBookClick(book) {
             uni.navigateTo({
                url: '/pages-sell/pages/detail?isbn=' + book.isbn
            });
        },
        handleUpdateList(data) {
            this.bookList = data.map(v => {
                v.selected = false
                return v
            })
            // 重置全选状态
            this.isAllSelected = false;
        },

        // 切换编辑模式
        toggleEditMode() {
            this.isEditMode = !this.isEditMode
            if (!this.isEditMode) {
                this.bookList.forEach(book => book.selected = false)
            } else {
                // 进入编辑模式不默认全选
                this.isAllSelected = false
                this.bookList.forEach(book => book.selected = false)
            }
        },

        // 切换全选
        toggleSelectAll() {
            const newValue = !this.isAllSelected
            this.bookList.forEach(book => book.selected = newValue)
        },

        // 处理删除
        handleDelete() {
            let deleteIds = this.bookList.filter(book => book.selected)
            if (deleteIds.length === 0) {
                uni.showToast({
                    title: '请选择要移除的商品',
                    icon: 'none'
                })
                return
            }
            this.$refs.deleteDialog.openPopup()
        },

        // 确认删除
        confirmDelete() {
            let deleteIds = this.bookList.filter(book => book.selected).map(v => v.isbn)
            this.$u.api.removeCollectAjax(deleteIds).then(res => {
                if (res.code === 200) {
                    uni.showToast({
                        title: '移除成功',
                        icon: 'success'
                    })
                    this.$refs.pageRef?.loadData(true)
                    this.isEditMode = false;
                }
            })
        }
    }
}
</script>

<style lang="scss">
.my-fav {
    min-height: 100vh;

    ::v-deep .checkbox-item {
        .u-checkbox__label {
            margin: 0 !important;
        }
    }

    .header {
        position: sticky;
        top: 0;
        left: 0;
        right: 0;
        z-index: 100;
        height: 88rpx;
        background: #FFFFFF;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 30rpx;
        font-size: 28rpx;
        border-bottom: 1rpx solid #EEEEEE;

        .left {
            display: flex;
            align-items: center;

            .select-text {
                margin-left: 12rpx;
                color: #333333;
            }
        }

        .right {
            text {
                color: #333333;

                &.delete-btn {
                    color: #FF5B5B;
                }
            }
        }
    }

    .scroll-view {
        height: calc(100vh - 88rpx);
    }

    .book-list {
        padding: 20rpx;
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-start;
        gap: 20rpx;

        .load-more {
            width: 100%;
            display: flex;
            color: #999999;
            font-size: 24rpx;
            padding: 30rpx 0;
            justify-content: center;
        }
    }
}
</style>