<template>
    <u-popup v-model="show" mode="bottom" border-radius="24">
        <view class="red-packet-popup">
            <view class="popup-header">
                <text>选择红包</text>
                <u-icon name="close" size="28" color="#999" class="close-icon" @click="close"></u-icon>
            </view>

            <scroll-view scroll-y class="packet-list" v-if="list && list.length > 0">
                <view class="packet-item" v-for="(item, index) in list" :key="index" @click="selectPacket(index)">
                    <view class="packet-info">
                        <view class="packet-name">{{ item.couponName }} ¥{{ item.faceMoney }}</view>
                        <view class="packet-desc">满{{ item.thresholdMoney }}元可用</view>
                        <view class="packet-time">{{ item.effectStartTime }}至{{ item.effectEndTime }}</view>
                    </view>
                    <view class="packet-check">
                        <u-icon name="checkmark-circle-fill" size="40"
                            :color="selectedIndex === index ? '#ff4500' : '#ccc'"></u-icon>
                    </view>
                </view>
            </scroll-view>
            <view class="empty-state" v-else>
                <image src="https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/no-data.png"
                    style="width: 260rpx; height: 260rpx" mode="aspectFit"></image>
                <view class="empty-text">暂无可用红包</view>
            </view>

            <view class="popup-footer" v-if="list && list.length > 0">
                <u-button type="primary" shape="circle" @click="confirm">确认</u-button>
            </view>
        </view>
    </u-popup>
</template>

<script>
    export default {
        props: {
            value: {
                type: Boolean,
                default: false
            },
            list: {
                type: Array,
                default: () => []
            }
        },
        data() {
            return {
                show: false,
                selectedIndex: -1,
            }
        },
        watch: {
            value(val) {
                this.show = val;
            },
            show(val) {
                this.$emit('input', val);
            }
        },
        methods: {
            close() {
                this.show = false;
            },
            selectPacket(index) {
                this.selectedIndex = index;
            },
            confirm() {
                if (this.list && this.list.length > 0 && this.selectedIndex !== -1) {
                    this.$emit('confirm', this.list[this.selectedIndex]);
                } else {
                    this.$emit('confirm', null);
                }
                this.close();
            }
        }
    }
</script>

<style lang="scss" scoped>
    .red-packet-popup {
        height: 100%;
        max-height: 80vh;
        display: flex;
        flex-direction: column;
        overflow: hidden;

        .popup-header {
            padding: 30rpx;
            text-align: center;
            font-size: 32rpx;
            font-weight: bold;
            position: sticky;
            top: 0;
            background-color: #fff;
            border-bottom: 1rpx solid #eee;

            .close-icon {
                position: absolute;
                right: 30rpx;
                top: 40rpx;
            }
        }

        .packet-list {
            flex: 1;
            padding: 20rpx;
            box-sizing: border-box;
            height: 600rpx; // Give it a fixed height for scroll
            overflow-y: auto;

            .packet-item {
                display: flex;
                align-items: center;
                padding: 20rpx 30rpx;
                border-bottom: 1rpx solid #f5f5f5;

                .packet-info {
                    flex: 1;

                    .packet-name {
                        font-size: 30rpx;
                        color: #ff4500;
                        font-weight: bold;
                        margin-bottom: 10rpx;
                    }

                    .packet-desc {
                        font-size: 26rpx;
                        color: #333;
                        margin-bottom: 6rpx;
                    }

                    .packet-time {
                        font-size: 22rpx;
                        color: #999;
                    }
                }
            }
        }

        .empty-state {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 60rpx 0;

            .empty-text {
                color: #999;
                font-size: 28rpx;
                margin-top: 20rpx;
            }
        }

        .popup-footer {
            padding: 20rpx 40rpx;
            padding-bottom: env(safe-area-inset-bottom);
        }
    }
</style>