<script setup>
import { ref, onMounted } from "vue";
import { getMembershipPlans, getMyMembership, purchaseMembership } from "@/api/membership";

const plans = ref([]);
const myMembership = ref(null);
const loading = ref(true);

async function loadData() {
  loading.value = true;
  try {
    const [plansRes, myRes] = await Promise.all([
      getMembershipPlans(),
      getMyMembership(),
    ]);
    plans.value = Array.isArray(plansRes) ? plansRes : [];
    myMembership.value = myRes || null;
  } catch (e) {
    console.error(e);
  } finally {
    loading.value = false;
  }
}

async function handlePurchase(planId) {
  uni.showModal({
    title: "确认开通",
    content: "确认购买该会员方案？",
    success: async (r) => {
      if (!r.confirm) return;
      try {
        const res = await purchaseMembership(planId);
        myMembership.value = res;
        uni.showToast({ title: "开通成功", icon: "success" });
      } catch (e) {
        uni.showToast({ title: e.message || "购买失败", icon: "none" });
      }
    },
  });
}

function formatDate(d) {
  if (!d) return "";
  const dt = new Date(d);
  return `${dt.getFullYear()}-${String(dt.getMonth() + 1).padStart(2, "0")}-${String(dt.getDate()).padStart(2, "0")}`;
}

function daysLeft(endDate) {
  if (!endDate) return 0;
  const diff = new Date(endDate).getTime() - Date.now();
  return Math.max(0, Math.ceil(diff / (1000 * 60 * 60 * 24)));
}

onMounted(loadData);
</script>

<template>
  <view class="membership-page">
    <view v-if="loading" class="loading-tip">加载中...</view>
    <template v-else>
      <!-- Current membership -->
      <view v-if="myMembership" class="current-card">
        <view class="current-header">
          <uni-icons type="star-filled" size="24" color="#f59e0b" />
          <text class="current-title">{{ myMembership.plan?.name || '会员' }}</text>
        </view>
        <view class="current-info">
          <view class="current-row">
            <text class="current-label">到期时间</text>
            <text class="current-value">{{ formatDate(myMembership.end_date) }}</text>
          </view>
          <view class="current-row">
            <text class="current-label">剩余天数</text>
            <text class="current-value highlight">{{ daysLeft(myMembership.end_date) }}天</text>
          </view>
        </view>
      </view>
      <view v-else class="no-member-card">
        <uni-icons type="star" size="32" color="#d1d5db" />
        <text class="no-member-text">您还不是会员</text>
      </view>

      <!-- Plans -->
      <view class="section-title">会员方案</view>
      <view v-if="plans.length === 0" class="empty-tip">暂无可用方案</view>
      <view v-for="plan in plans" :key="plan.id" class="plan-card">
        <view class="plan-header">
          <text class="plan-name">{{ plan.name }}</text>
          <text class="plan-price">¥{{ plan.price }}</text>
        </view>
        <text v-if="plan.description" class="plan-desc">{{ plan.description }}</text>
        <view class="plan-meta">
          <text class="plan-duration">{{ plan.duration_days }}天</text>
          <text v-if="plan.extra_uploads" class="plan-benefit">+{{ plan.extra_uploads }}上传额度</text>
          <text v-if="plan.extra_top_count" class="plan-benefit">+{{ plan.extra_top_count }}置顶次数</text>
        </view>
        <view v-if="plan.benefits" class="plan-benefits">
          <text class="benefits-text">{{ plan.benefits }}</text>
        </view>
        <view class="plan-action">
          <view class="purchase-btn" @tap="handlePurchase(plan.id)">
            {{ myMembership ? '续费' : '立即开通' }}
          </view>
        </view>
      </view>
    </template>
  </view>
</template>

<style scoped>
.membership-page {
  min-height: 100vh;
  background: #f5f6fa;
  padding: 20rpx;
}
.loading-tip {
  text-align: center;
  padding: 100rpx 0;
  font-size: 28rpx;
  color: #9ca3af;
}

.current-card {
  background: linear-gradient(135deg, #f59e0b, #fbbf24);
  border-radius: 20rpx;
  padding: 32rpx;
  margin-bottom: 24rpx;
}
.current-header {
  display: flex;
  align-items: center;
  gap: 12rpx;
  margin-bottom: 16rpx;
}
.current-title {
  font-size: 32rpx;
  font-weight: 700;
  color: #fff;
}
.current-info {
  display: flex;
  gap: 40rpx;
}
.current-row {
  display: flex;
  flex-direction: column;
  gap: 4rpx;
}
.current-label {
  font-size: 22rpx;
  color: rgba(255, 255, 255, 0.8);
}
.current-value {
  font-size: 26rpx;
  color: #fff;
  font-weight: 600;
}
.current-value.highlight {
  font-size: 32rpx;
}

.no-member-card {
  background: #fff;
  border-radius: 20rpx;
  padding: 48rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12rpx;
  margin-bottom: 24rpx;
}
.no-member-text {
  font-size: 28rpx;
  color: #9ca3af;
}

.section-title {
  font-size: 30rpx;
  font-weight: 700;
  color: #1e1e2e;
  margin-bottom: 16rpx;
  padding-left: 8rpx;
}
.empty-tip {
  text-align: center;
  font-size: 26rpx;
  color: #9ca3af;
  padding: 40rpx 0;
}

.plan-card {
  background: #fff;
  border-radius: 20rpx;
  padding: 28rpx;
  margin-bottom: 16rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
}
.plan-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12rpx;
}
.plan-name {
  font-size: 30rpx;
  font-weight: 700;
  color: #1e1e2e;
}
.plan-price {
  font-size: 32rpx;
  font-weight: 700;
  color: #ef4444;
}
.plan-desc {
  font-size: 24rpx;
  color: #6b7280;
  margin-bottom: 12rpx;
}
.plan-meta {
  display: flex;
  gap: 16rpx;
  flex-wrap: wrap;
  margin-bottom: 12rpx;
}
.plan-duration {
  font-size: 24rpx;
  color: #6366f1;
  background: #eef2ff;
  padding: 4rpx 16rpx;
  border-radius: 16rpx;
}
.plan-benefit {
  font-size: 24rpx;
  color: #d97706;
  background: #fef3c7;
  padding: 4rpx 16rpx;
  border-radius: 16rpx;
}
.plan-benefits {
  margin-bottom: 16rpx;
}
.benefits-text {
  font-size: 24rpx;
  color: #4b5563;
  line-height: 1.6;
  white-space: pre-wrap;
}
.plan-action {
  display: flex;
  justify-content: flex-end;
}
.purchase-btn {
  padding: 14rpx 40rpx;
  background: #6366f1;
  color: #fff;
  font-size: 26rpx;
  font-weight: 600;
  border-radius: 32rpx;
}
</style>
