<template>
  <view class="jc-bm-page">
    <!-- 报名信息头部 - 固定部分 -->
    <view class="selection-header">
      <view class="header-content">
        <!-- 学生选择部分 -->
        <XsPicker :is-bar="true" />
      </view>
    </view>

    <!-- 可滚动的内容区域 -->
    <view class="scrollable-content">
      <JcBzList :xs-id="curXs.id" :can-selected="true" :multiple="true" @change="onJcBzChange" />
    </view>

    <!-- 底部操作区域 -->
    <view class="bottom-action">
      <view class="selected-info">
        <text>已选择：{{ selectedCount }} 个就餐标准</text>
        <text class="total-price">总金额：¥{{ totalPrice }}</text>
      </view>
      <view class="action-buttons">
        <view class="cancel-btn" @click="goBack">取消</view>
        <view class="confirm-btn" @click="confirmBm" :class="{ disabled: selectedCount === 0 }">确认报名</view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import XsPicker from "@/pages/base/components/XsPicker/index.vue"
import JcBzList from "@/pages/base/components/JcBzList/index.vue"
import { useUserStore } from "@/store/modules/user";
import { useDataStore } from "@/store/modules/data";
import { jcBmJcBzApi } from "@/api/base/jcApi";

const { getCurXs, getUser } = useUserStore();
const { getData, setData } = useDataStore();

const curXs = computed(() => getCurXs);

// 选中的就餐标准ID列表
const selectedJcBzIds = ref<string[]>([]);
// 选中的就餐标准列表
const selectedJcBzList = ref<any[]>([]);

// 选中数量
const selectedCount = computed(() => selectedJcBzIds.value.length);

// 总金额
const totalPrice = computed(() => {
  let total = 0;
  selectedJcBzList.value.forEach(jcBz => {
    total += jcBz.jfje || 0;
  });
  return total;
});

// 就餐标准选择变更
const onJcBzChange = (ids: string[]) => {
  selectedJcBzIds.value = ids;
  // 这里需要根据选中的ID获取对应的就餐标准详情
  // 实际项目中可能需要从本地存储或重新请求数据
  updateSelectedJcBzList();
};

// 更新选中的就餐标准列表
const updateSelectedJcBzList = () => {
  // 这里需要根据selectedJcBzIds从本地存储或重新请求数据
  // 暂时使用空数组，实际项目中需要实现
  selectedJcBzList.value = [];
};

// 返回上一页
const goBack = () => {
  uni.navigateBack();
};

// 确认报名
const confirmBm = async () => {
  if (selectedCount.value === 0) {
    uni.showToast({
      title: "请选择就餐标准",
      icon: "none",
    });
    return;
  }

  try {
    const res = await jcBmJcBzApi({
      xsId: curXs.value.id,
      jcBzIds: selectedJcBzIds.value,
      jzId: getUser.jzId,
      userId: getUser.userId,
    });

    if (res.resultCode === 1) {
      uni.showToast({
        title: "报名成功",
        icon: "success",
      });
      
      // 保存报名数据到store
      setData({
        ...getData,
        xsId: curXs.value.id,
        jcBzIds: selectedJcBzIds.value,
        jcBzList: selectedJcBzList.value,
        totalJe: totalPrice.value,
      });

      // 跳转到支付页面
      uni.redirectTo({
        url: '/pages/base/jc/pay/index'
      });
    } else {
      uni.showToast({
        title: res.result?.message || "报名失败",
        icon: "none",
      });
    }
  } catch (error) {
    console.error('报名失败:', error);
    uni.showToast({
      title: "报名失败，请重试",
      icon: "none",
    });
  }
};

// 页面卸载前清除定时器
onBeforeUnmount(() => {
});
</script>

<style lang="scss" scoped>
.jc-bm-page {
  min-height: 100%;
  background-color: #f5f7fa;
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

.selection-header {
  background: linear-gradient(135deg, #4a90e2, #2879ff);
  padding: 20px 15px;
  color: #fff;
  border-radius: 0 0 15px 15px;
  box-shadow: 0 4px 12px rgba(40, 121, 255, 0.2);
  position: sticky;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;

  .header-content {
    display: flex;
    flex-direction: column;
    gap: 15px;
  }
}

// 可滚动内容区域样式
.scrollable-content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch; // 增强iOS滚动体验
}

.bottom-action {
  background-color: #fff;
  padding: 15px;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);

  .selected-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    font-size: 14px;
    color: #666;

    .total-price {
      color: #ff6b00;
      font-weight: bold;
      font-size: 16px;
    }
  }

  .action-buttons {
    display: flex;
    justify-content: space-between;

    .cancel-btn,
    .confirm-btn {
      width: 48%;
      height: 44px;
      line-height: 44px;
      text-align: center;
      border-radius: 22px;
      font-size: 16px;
    }

    .cancel-btn {
      background-color: #fff;
      color: #333;
      border: 1px solid #ddd;
    }

    .confirm-btn {
      background-color: #2879ff;
      color: #fff;

      &.disabled {
        background-color: #ccc;
        color: #999;
      }
    }
  }
}
</style>
