<script lang="ts" setup>
import couponBg from '@img/index/coupon-bg.png'
import indexBg from '@img/index/index-bg.png'
import { useRequest } from 'alova/client'
import { storeToRefs } from 'pinia'
import { ref, watch } from 'vue'
import { getAccountCount, getCouponSituation } from '@/api/home'
import DiscountCoupon from '@/components/discountCoupon.vue'
import SpendAndSaveCoupon from '@/components/spendAndSaveCoupon.vue'
import { useCouponStore } from '@/store/coupon'
import { useTokenStore } from '@/store/token'
import { toLoginPage } from '@/utils/toLoginPage'

defineOptions({
    name: 'Home',
})
definePage({
    // 使用 type: "home" 属性设置首页，其他页面不需要设置，默认为page
    type: 'home',
    style: {
        // 'custom' 表示开启自定义导航栏，默认 'default'
        navigationStyle: 'custom',
        navigationBarTitleText: '首页',
    },
})

const tokenStore = useTokenStore()
const { hasLogin } = storeToRefs(tokenStore)

// 获取优惠券
const couponStore = useCouponStore()
const { couponList, discountVoucherList } = storeToRefs(couponStore)

// 获取首页收益
const { send: getAccountCountRequest, data: accountCountData } = useRequest(getAccountCount, {
    immediate: false,
})

// 获取首页领券情况数据
const { send: getCouponSituationRequest, data: couponSituationData } = useRequest(getCouponSituation, {
    immediate: false,
})

onLoad(async () => {
    // 获取优惠券
    couponStore.getCouponListByType()
})

onShow(() => {
    // 登录后查询收益数据
    if (hasLogin) {
        getAccountCountRequest()
        getCouponSituationRequest()
    }
})

// // 页面分享给朋友
// onShareAppMessage(() => {
//     return {
//         title: '券中心',
//         path: '/pages/index/index',
//         imageUrl: '/static/logo.svg',
//     }
// })

// // 页面分享到朋友圈
// onShareTimeline(() => {
//     return {
//         title: '优惠券管理系统',
//         path: '/pages/index/index',
//         imageUrl: '/static/logo.svg',
//     }
// })

watch(() => couponSituationData, (newVal) => {
    console.log('首页领券情况数据:', newVal)
})

// #ifdef MP-WEIXIN
async function login() {
    const currentPage = getCurrentPages()[0]
    const redirectUrl = `/${currentPage.route}`
    toLoginPage({ queryString: `?redirect=${encodeURIComponent(redirectUrl)}` })
}
// #endif

// 顶部导航栏高度,设置banner位置
const navigationBarHeight = ref(0)
// #ifdef MP-WEIXIN
function getNavigationBarHeight() {
    uni.getSystemInfo({
        success: (res) => {
            const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
            console.log('顶部导航栏高度:', res.statusBarHeight, menuButtonInfo)
            // 顶部导航栏高度 = 状态栏高度 + 胶囊的高度
            navigationBarHeight.value = res.statusBarHeight + menuButtonInfo.height + 12
        },
    })
}
getNavigationBarHeight()
// #endif

function toDiscountCouponList() {
    uni.navigateTo({
        url: '/pages-A/discountcouponList/index?type=2',
    })
}
function toSpendAndSaveCouponList() {
    uni.navigateTo({
        url: '/pages-A/spendAndSaveCouponList/index?type=3',
    })
}

function toCouponRedemptionList(state) {
    uni.navigateTo({
        url: `/pages-A/couponRedemptionList/index?state=${state}`,
    })
}
</script>

<template>
    <view class="home-container">
        <!-- 顶部区域 -->
        <view class="home-header">
            <image class="home-header-bg" :src="indexBg" mode="aspectFill" />
            <view class="absolute left-5 z-3 text-xl c-white" :style="{ top: `${navigationBarHeight - 39}px` }">
                券中心
            </view>
            <view class="home-header-avatar-info" :style="{ paddingTop: `${navigationBarHeight + 10}px` }">
                <view class="home-header-balance">
                    我的收益（元）
                </view>
                <view class="home-header-balance-num">
                    <view class="home-header-balance-num-amount">
                        {{ accountCountData?.balance || 0 }}
                    </view>
                </view>
            </view>
            <view class="home-header-tips">
                <view class="home-header-tips-item" @click="toCouponRedemptionList('2')">
                    <view class="home-header-tips-item-num">
                        {{ couponSituationData?.usedQuantity || 0 }}张
                    </view>
                    <view class="home-header-tips-item-des">
                        已核销
                    </view>
                </view>
                <view class="home-header-tips-item" @click="toCouponRedemptionList('1')">
                    <view class="home-header-tips-item-num">
                        {{ couponSituationData?.quantityToBeUsed || 0 }}张
                    </view>
                    <view class="home-header-tips-item-des">
                        未核销
                    </view>
                </view>
                <view class="home-header-tips-item">
                    <view class="home-header-tips-item-num">
                        {{ couponSituationData?.quantityForComplimentary || 0 }}张
                    </view>
                    <view class="home-header-tips-item-des">
                        已发放
                    </view>
                </view>
            </view>
            <view v-if="!hasLogin" class="home-hidden" @click="login">
                <image class="home-hidden-img" src="@img/index/lock.png" mode="scaleToFill" />
                <view class="home-hidden-text">
                    请登录，查看更多内容~
                </view>
            </view>
        </view>
        <!-- 满减券 -->
        <view class="home-header-coupon">
            <image class="home-header-coupon-bg" :src="couponBg" mode="aspectFill" />
            <view class="home-header-coupon-title">
                <image class="home-header-coupon-title-icon" src="@img/index/icon1.png" mode="scaleToFill" />
                <view class="home-header-coupon-title-text">
                    满减券
                </view>
                <view class="home-header-coupon-title-des">
                    平台满减&nbsp;&nbsp;乐享不停
                </view>
            </view>
            <view class="home-header-coupon-content">
                <template v-for="item in couponList" :key="item.id">
                    <spend-and-save-coupon :coupon="item" />
                </template>
            </view>
            <view class="home-header-coupon-btn">
                <up-button class="home-header-coupon-btn-text" text="查看更多优惠券"
                    color="linear-gradient(0deg, #FFE8CE 0%, #FBB8A0 100%)" @click="toSpendAndSaveCouponList" />
            </view>
        </view>
        <!-- 折扣券 -->
        <view class="home-coupon">
            <view class="home-coupon-title">
                <image class="home-coupon-title-icon" src="@img/index/icon2.png" mode="scaleToFill" />
                <view class="home-coupon-title-text">
                    折扣券
                </view>
                <view class="home-coupon-title-des">
                    分享折扣&nbsp;&nbsp;立享优惠
                </view>
                <view class="home-coupon-title-more" @click="toDiscountCouponList">
                    <view class="home-coupon-title-more-text">
                        更多
                    </view>
                    <up-icon size="14" name="arrow-right" />
                </view>
            </view>
            <view class="home-coupon-content">
                <template v-for="item in discountVoucherList" :key="item.id">
                    <discount-coupon :coupon="item" />
                </template>
            </view>
        </view>
    </view>
</template>

<style lang="scss" scoped>
.home-container {
    background-color: #f5f5f5;
    line-height: 1;
    position: relative;

    .home-header {
        height: 550rpx;
        position: relative;
        overflow: hidden;

        .home-header-bg {
            position: absolute;
            top: 0;
            left: 0;
            width: 125%;
            height: 200%;
            object-fit: cover;
            transform: translate(-80px, 0);
            z-index: 0;
        }

        .home-header-avatar-info {
            display: flex;
            flex-direction: column;
            justify-content: center;
            text-align: center;
            gap: 30rpx;
            padding: 0 24rpx;
            position: relative;
            z-index: 1;

            .home-header-balance {
                font-weight: 400;
                font-size: 26rpx;
                color: #ffffff;
            }

            .home-header-balance-num {
                display: flex;
                justify-content: space-between;
                align-items: center;

                .home-header-balance-num-amount {
                    width: 100%;
                    font-weight: 500;
                    font-size: 65rpx;
                    color: #ffffff;
                }

                .home-header-balance-num-btns {
                    display: flex;
                    gap: 15rpx;

                    .home-header-balance-num-btn {
                        padding: 20rpx 40rpx;
                        border-radius: 33rpx;
                        font-weight: 400;
                        font-size: 26rpx;

                        &.js {
                            background: #da4c47;
                            color: #ffffff;
                        }

                        &.tx {
                            background: #bfbfbf;
                            color: #747474;
                        }
                    }
                }
            }
        }

        .home-header-tips {
            height: 124rpx;
            display: flex;
            border-radius: 10rpx;
            margin: 47rpx 24rpx 0 24rpx;
            position: relative;
            z-index: 1;

            .home-header-tips-item {
                flex: 1;
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                gap: 15rpx;
                font-weight: 500;
                font-size: 34rpx;
                color: #ffffff;
                line-height: 1;
                position: relative;

                &:not(:last-child):after {
                    content: '';
                    position: absolute;
                    top: 50%;
                    right: 0;
                    transform: translateY(-50%);
                    width: 2px;
                    height: 40rpx;
                    background: #ffffff;
                }

                .home-header-tips-item-num {
                    font-weight: bold;
                }

                .home-header-tips-item-des {
                    font-weight: 400;
                    font-size: 24rpx;
                    color: #ffffff;
                }
            }
        }

        .home-hidden {
            height: 550rpx;
            width: 100%;
            position: absolute;
            top: 0;
            right: 0;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            background-color: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(25rpx);
            -webkit-backdrop-filter: blur(25rpx);
            z-index: 2;

            .home-hidden-img {
                width: 209rpx;
                height: 209rpx;
                object-fit: cover;
            }

            .home-hidden-text {
                font-weight: 400;
                font-size: 26rpx;
                color: #ffffff;
                margin-top: -25rpx;
            }
        }
    }

    // 优惠券
    .home-header-coupon {
        position: relative;
        height: 419rpx;
        margin: -58rpx 20rpx 20rpx 20rpx;
        gap: 20rpx;
        padding: 28rpx 20rpx;
        overflow: hidden;
        z-index: 3;

        .home-header-coupon-bg {
            position: absolute;
            top: 0;
            right: 0;
            width: 100%;
            object-fit: cover;
            z-index: 0;
        }

        .home-header-coupon-title {
            display: flex;
            flex-direction: row;
            position: relative;
            z-index: 1;

            .home-header-coupon-title-icon {
                width: 38rpx;
                height: 38rpx;
                object-fit: cover;
                margin-right: 13rpx;
            }

            .home-header-coupon-title-text {
                font-weight: 500;
                font-size: 30rpx;
                color: #333333;
                margin-right: 21rpx;
            }

            .home-header-coupon-title-des {
                display: inline-flex;
                align-items: center;
                font-size: 24rpx;
                font-weight: 400;
                color: #888888;
            }
        }

        .home-header-coupon-content {
            height: 238rpx;
            display: flex;
            flex-direction: row;
            align-items: flex-end;
            gap: 20rpx;
            position: relative;
            z-index: 1;
        }

        .home-header-coupon-btn {
            padding: 21rpx 70rpx 22px 69rpx;
            position: relative;
            z-index: 1;

            .home-header-coupon-btn-text {
                box-shadow: 0rpx 4rpx 7rpx 0rpx rgba(230, 77, 13, 0.17);
                border-radius: 37rpx;
                color: #651e03 !important;
                font-size: 26rpx;
                font-weight: 400;
            }
        }
    }

    // 折扣券
    .home-coupon {
        margin: -20rpx 20rpx 20rpx 20rpx;
        padding: 28rpx 20rpx;
        background-color: #ffffff;
        border-radius: 15rpx;

        .home-coupon-title {
            display: flex;
            flex-direction: row;

            .home-coupon-title-icon {
                width: 38rpx;
                height: 38rpx;
                object-fit: cover;
                margin-right: 13rpx;
            }

            .home-coupon-title-text {
                font-weight: 500;
                font-size: 30rpx;
                color: #333333;
                margin-right: 21rpx;
            }

            .home-coupon-title-des {
                display: inline-flex;
                align-items: center;
                font-size: 24rpx;
                font-weight: 400;
                color: #888888;
            }

            .home-coupon-title-more {
                margin-left: auto;
                display: flex;
                flex-direction: row;
                align-items: center;

                .home-coupon-title-more-text {
                    font-weight: 400;
                    font-size: 24rpx;
                    color: #666666;
                    height: 23rpx;
                    line-height: 23rpx;
                }
            }
        }

        .home-coupon-content {
            padding-top: 28rpx;
            display: flex;
            flex-direction: column;
            gap: 20rpx;
        }
    }
}
</style>
