<template>
    <u-popup v-model="showPopup" mode="bottom" border-radius="20">
        <view class="category-popup">
            <view class="popup-header">
                <text>分类</text>
                <u-icon name="close" size="32" @click="close"></u-icon>
            </view>

            <view class="category-list">
                <view v-for="(item, index) in categories" :key="index" class="category-item"
                    :class="{ 'active': selectedCategories.includes(item.value), 'disabled': item.disabled }"
                    @click="toggleSelect(item)">
                    {{ item.label }}
                </view>
            </view>

            <view class="confirm-btn" @click="confirm">
                确定筛选
            </view>
        </view>
    </u-popup>
</template>

<script>
export default {
    name: 'category-popup',
    props: {
        show: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            showPopup: false,
            categories: [
                { label: '买书', value: 'buy_book' },
                { label: '提现', value: 'withdraw' },
                { label: '退款', value: 'refund' },
                { label: '工单收入', value: 'work_income' },
                { label: '自寄运费补贴', value: 'shipping_subsidy' },
                { label: '自寄运费补款', value: 'shipping_supplement' },
                { label: '分享收入', value: 'share_income' },
                { label: '合伙人收入', value: 'partner_income'},
                { label: '退回邮费', value: 'return_postage' }
            ],
            selectedCategories: []
        }
    },
    watch: {
        show(newVal) {
            this.showPopup = newVal
        },
        showPopup(newVal) {
            if (!newVal) {
                this.$emit('update:show', false)
            }
        }
    },
    methods: {
        toggleSelect(item) {
            if (item.disabled) return

            const index = this.selectedCategories.indexOf(item.value)
            if (index > -1) {
                this.selectedCategories.splice(index, 1)
            } else {
                this.selectedCategories.push(item.value)
            }
        },
        confirm() {
            this.$emit('confirm', this.selectedCategories)
            this.close()
        },
        close() {
            this.showPopup = false
            this.$emit('update:show', false)
        }
    }
}
</script>

<style lang="scss" scoped>
.category-popup {
    padding: 30rpx;
    padding-bottom: env(safe-area-inset-bottom);

    .popup-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 30rpx;
        font-size: 32rpx;
        color: #333;
    }

    .category-list {
        display: flex;
        flex-wrap: wrap;
        padding: 20rpx 0;
        gap: 20rpx;

        .category-item {
            padding: 0 42rpx;
            height: 60rpx;
            line-height: 60rpx;
            text-align: center;
            border-radius: 10rpx;
            font-size: 28rpx;
            color: #38C148;
            border: 1px solid #38C148;

            &.active {
                color: #ffffff;
                border: 1px solid #38C148;
                background-color: #38C148;
            }
        }
    }

    .confirm-btn {
        margin-top: 40rpx;
        height: 90rpx;
        line-height: 90rpx;
        text-align: center;
        background: #38C148;
        border-radius: 45rpx;
        color: #FFFFFF;
        font-size: 32rpx;
    }
}
</style>