<script lang="ts" setup>
import couponBg from '@img/index/coupon-bg.png'
import indexBg from '@img/index/index-bg.png'
import { pathToBase64 } from 'image-tools'
import { ref } from 'vue'
import DiscountCoupon from '@/components/discountCoupon.vue'
import SpendAndSaveCoupon from '@/components/spendAndSaveCoupon.vue'
import { useTokenStore } from '@/store/token'

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

const tokenStore = useTokenStore()
const { hasLogin, wxLogin } = tokenStore

console.log('hasLogin:', hasLogin)

// 将图片路径转换为base64格式
async function convertImageToBase64() {
    try {
        // 转换首页背景图
        indexBgBase64.value = await pathToBase64(indexBg)

        // 转换优惠券背景图
        couponBgBase64.value = await pathToBase64(couponBg)

        console.log('图片转换base64成功')
    }
    catch (error) {
        console.error('图片转换base64失败:', error)
    }
}

onLoad(async () => {
    console.log('测试 uni API 自动引入: onLoad')
    await convertImageToBase64()
})

// #ifdef MP-WEIXIN
async function login() {
    await tokenStore.wxLogin()
}
// #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
</script>

<template>
    <view class="home-container">
        <!-- 顶部区域 -->
        <view class="home-header" :style="{ backgroundImage: `url('${indexBgBase64}')` }">
            <view class="home-header-avatar-info" :style="{ paddingTop: `${navigationBarHeight + 10}px` }">
                <view class="absolute left-5 z-1 text-xl c-white" :style="{ top: `${navigationBarHeight - 39}px` }">
                    券中心
                </view>
                <view class="home-header-balance">
                    我的收益（元）
                </view>
                <view class="home-header-balance-num">
                    <view class="home-header-balance-num-amount">
                        7297491.08
                    </view>
                </view>
            </view>
            <view class="home-header-tips">
                <view class="home-header-tips-item">
                    <view class="home-header-tips-item-num">
                        115张
                    </view>
                    <view class="home-header-tips-item-des">
                        已核销
                    </view>
                </view>
                <view class="home-header-tips-item">
                    <view class="home-header-tips-item-num">
                        68张
                    </view>
                    <view class="home-header-tips-item-des">
                        未核销
                    </view>
                </view>
                <view class="home-header-tips-item">
                    <view class="home-header-tips-item-num">
                        68张
                    </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" :style="{ backgroundImage: `url('${couponBgBase64}')` }">
            <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">
                <spend-and-save-coupon />
                <spend-and-save-coupon />
                <spend-and-save-coupon />
            </view>
            <view class="home-header-coupon-btn">
                <up-button class="home-header-coupon-btn-text" text="查看更多优惠券"
                    color="linear-gradient(0deg, #FFE8CE 0%, #FBB8A0 100%)" />
            </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">
                    <view class="home-coupon-title-more-text">
                        更多
                    </view>
                    <up-icon size="14" name="arrow-right" />
                </view>
            </view>
            <view class="home-coupon-content">
                <discount-coupon />
                <discount-coupon />
                <discount-coupon />
            </view>
        </view>
    </view>
</template>

<style lang="scss" scoped>
.home-container {
    font-family: Alibaba PuHuiTi;
    min-height: 100vh;
    background-color: #f5f5f5;
    line-height: 1;
    position: relative;

    .home-header {
        height: 550rpx;
        // background-image: url('@img/index/index-bg.png');
        background-repeat: no-repeat;
        background-position: -80px top;
        background-size: 125% 200%;
        box-sizing: border-box;

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

            .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;
            // background: linear-gradient(114deg, #f67873, #fb847f, #f67873);
            border-radius: 10rpx;
            margin: 47rpx 24rpx 0 24rpx;

            .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);

            .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;
        background-repeat: no-repeat;
        background-position: right top;
        background-size: 100% auto;
        height: 419rpx;
        margin: -75rpx 20rpx 20rpx 20rpx;
        gap: 20rpx;
        padding: 28rpx 20rpx;

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

            .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;
        }

        .home-header-coupon-btn {
            padding: 21rpx 70rpx 22px 69rpx;

            .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>
