﻿<template>
    <view class="scroll-container theme-light">
        <view class="bt-page" v-if="course">
            <MemberInfoSubNav title="课程详情" @back="onBack" />
            <image class="mi-detail-hero" :src="course.banner" mode="aspectFill" />
            <view class="bt-page__body">
                <view class="bt-card">
                    <text class="bt-card__title">{{ course.title }}</text>
                    <text class="bt-card__desc">{{ course.startTime }}-{{ course.endTime }} · {{ course.location }}</text>
                    <view class="mi-detail-coach">
                        <image class="mi-detail-coach__avatar" :src="course.coachAvatar" mode="aspectFill" />
                        <view>
                            <text class="mi-detail-coach__name">{{ course.coach }}</text>
                            <text class="mi-detail-coach__rating">评分 {{ course.coachRating }}</text>
                        </view>
                    </view>
                </view>

                <view class="bt-card">
                    <text class="bt-card__title">课程介绍</text>
                    <text class="bt-card__desc">{{ course.intro }}</text>
                    <text class="bt-card__title" style="margin-top:12px;">适合人群</text>
                    <text class="bt-card__desc">{{ course.suitable }}</text>
                </view>

                <view class="bt-card">
                    <text class="bt-card__title">教练介绍</text>
                    <text class="bt-card__desc">{{ course.coachBio }}</text>
                </view>

                <view v-if="course.reviews?.length" class="bt-card">
                    <text class="bt-card__title">会员评价</text>
                    <view v-for="(r, i) in course.reviews" :key="i" class="mi-detail-review">
                        <text class="mi-detail-review__user">{{ r.user }} · {{ r.score }}分</text>
                        <text class="mi-detail-review__text">{{ r.text }}</text>
                    </view>
                </view>

                <view class="bt-card">
                    <text class="bt-card__title">取消规则</text>
                    <text class="bt-card__desc">{{ course.cancelRule }}</text>
                    <text class="bt-card__desc" style="margin-top:8px;">费用：{{ course.price }}</text>
                </view>

                <view class="bt-footer-actions">
                    <view
                        class="bt-btn bt-btn--primary"
                        :class="{ 'bt-btn--outline': course.full }"
                        hover-class="mi-tap-btn--hover"
                        @tap="confirmBook"
                    >
                        <text class="bt-btn__text">{{ course.full ? '已约满' : '确认预约' }}</text>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
import MemberInfoSubNav from '@/components/memberInfo/MemberInfoSubNav.vue'
import { PAGE, navigateToPage, goBackOrTab } from '@/common/constants/routes.js'
import { loadMemberStore, persistMemberStore } from '@/common/memberInfo/store.js'
import { getCourseById, enrichCourseForDisplay, bookCourse } from '@/common/memberInfo/bookingStore.js'

export default {
    components: { MemberInfoSubNav },
    data() {
        return { course: null, courseId: null }
    },
    onLoad(options) {
        this.courseId = options?.id
        this.loadCourse()
    },
    methods: {
        loadCourse() {
            const store = loadMemberStore()
            const raw = getCourseById(store, this.courseId)
            this.course = raw ? enrichCourseForDisplay(raw) : null
        },
        onBack() {
            goBackOrTab(PAGE.COURSE_LIST)
        },
        confirmBook() {
            if (!this.course || this.course.full) return
            uni.showModal({
                title: '确认预约',
                content: `预约「${this.course.title}」\n${this.course.date} ${this.course.startTime}-${this.course.endTime}`,
                success: (res) => {
                    if (!res.confirm) return
                    const store = loadMemberStore()
                    const result = bookCourse(store, this.courseId)
                    if (!result.ok) {
                        uni.showToast({ title: result.message, icon: 'none' })
                        return
                    }
                    persistMemberStore(store)
                    uni.showToast({ title: '预约成功', icon: 'success' })
                    setTimeout(() => navigateToPage(PAGE.BOOKING), 800)
                }
            })
        }
    }
}
</script>

<style>
@import '@/common/style/base.css';
@import '@/common/style/memberInfo/pages/page-reset.css';
@import '@/common/style/memberInfo/pages/sub-page-base.css';
@import '@/common/style/memberInfo/member-info-component-reset.css';
@import '@/common/style/memberInfo/member-info-sub-nav.css';
@import '@/common/style/memberInfo/member-info-tap.css';
@import '@/common/style/memberInfo/pages/body-test-common.css';
@import '@/common/style/memberInfo/pages/module-pages-common.css';

.mi-detail-hero {
    width: 100%;
    height: 180px;
    display: block;
}

.mi-detail-coach {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 10px;
    margin-top: 12px;
}

.mi-detail-coach__avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
}

.mi-detail-coach__name {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-dark, #1E2A3A);
    display: block;
}

.mi-detail-coach__rating {
    font-size: 12px;
    color: var(--accent-orange, #FF6B35);
}

.mi-detail-review {
    padding: 10px 0;
    border-bottom: 1px solid var(--border-light, #E9EDF2);
}

.mi-detail-review:last-child {
    border-bottom: none;
}

.mi-detail-review__user {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-dark, #1E2A3A);
    display: block;
}

.mi-detail-review__text {
    font-size: 12px;
    color: var(--text-muted, #5E6F8D);
    margin-top: 4px;
    display: block;
}

.bt-footer-actions .bt-btn {
    flex: 1;
}
</style>
