<template>
    <view class="scan-history">
        <!-- 顶部操作栏 -->
        <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" @updateList="handleUpdateList" ref="pageRef" slotEmpty emptyText="您暂未扫过书籍">
            <view class="book-list">
                <BookListItem v-for="book in bookList" :key="book.isbn" :book="book" :isEditMode="isEditMode"
                    @action="handleAction" @checked="handleChecked" />
            </view>
        </page-scroll>

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

<script>
import BookListItem from '../components/BookListItem.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
    mounted() {
        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)
                item.selected = checked
                this.$set(this.bookList, index, item)

                this.isAllSelected = this.bookList.every(item => item.selected)
                console.log(this.bookList, 'this.isAllSelected')
            })
        },
        handleAction(book) {
            if (book.status == 2) {
                uni.$u.http.get('/token/order/addScanToOrder?isbn=' + book.isbn).then(res => {
                    if (res.code === 200) {
                        uni.showToast({
                            title: '加入成功',
                            icon: 'success'
                        })
                        this.$refs.pageRef?.loadData(true)
                    } else {
                        uni.showToast({
                            title: res.msg,
                            icon: 'none'
                        })
                    }
                })
            }
        },
        handleUpdateList(data) {
            this.bookList = data.map(v => {
                v.selected = false
                return v
            })
        },

        // 切换编辑模式
        toggleEditMode() {
            this.isEditMode = !this.isEditMode
            if (!this.isEditMode) {
                this.bookList.forEach(book => book.selected = false)
            }else{
                this.isAllSelected = true
                this.bookList.forEach(book => book.selected = true)
            }
        },

        // 切换全选
        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)
            uni.$u.http.post('/token/order/removeScanLogs', deleteIds).then(res => {
                if (res.code === 200) {
                    uni.showToast({
                        title: '删除成功',
                        icon: 'success'
                    })
                    this.$refs.pageRef?.loadData(true)
                }
            })
        }
    }
}
</script>

<style lang="scss">
.scan-history {
    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>