<template>
  <view class="select-event-page">
    <view class="project-list">
      <view
        class="project-item"
        v-for="(item, index) in projectList"
        :key="index"
        @click="toggleProject(item)"
      >
        <view class="project-info">
          <view class="project-name">{{ item.name }}</view>
          <view class="project-price">¥ {{ item.price }}</view>
        </view>
        <view class="checkbox">
          <text v-if="item.selected" class="checked">✓</text>
          <text v-else class="unchecked">○</text>
        </view>
      </view>
    </view>

    <!-- 立即报名按钮 -->
    <view class="register-btn-wrapper">
      <view class="register-btn" @click="handleRegister">立即报名</view>
    </view>
  </view>
</template>

<script>
import competitionAPI from '@/api/competition.js'

export default {
  data() {
    return {
      eventId: '',
      type: '',
      projectList: []
    };
  },
  onLoad(options) {
    if (options.eventId) {
      this.eventId = options.eventId;
      this.loadProjectList(options.eventId)
    }
    if (options.type) {
      this.type = options.type;
    }
  },
  methods: {
    /**
     * 加载报名项目列表
     */
    async loadProjectList(eventId) {
      try {
        const res = await competitionAPI.getProjectList({ competitionId: eventId })

        let list = []
        if (res.records) {
          list = res.records
        } else if (Array.isArray(res)) {
          list = res
        }

        // 数据映射
        this.projectList = list.map(item => ({
          id: item.id,
          name: item.name || item.projectName,
          price: item.price || item.registrationFee || 0,
          selected: false
        }))
      } catch (err) {
        console.error('加载项目列表失败:', err)
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      }
    },

    toggleProject(item) {
      item.selected = !item.selected;
      this.$forceUpdate();
    },
    handleRegister() {
      const selectedProjects = this.projectList.filter(item => item.selected);
      if (selectedProjects.length === 0) {
        uni.showToast({
          title: '请选择报名项目',
          icon: 'none'
        });
        return;
      }

      uni.navigateTo({
        url: `/pages/event-register/event-register?eventId=${this.eventId}&projects=${encodeURIComponent(JSON.stringify(selectedProjects))}`
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.select-event-page {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 180rpx;
}

.project-list {
  padding: 30rpx;
}

.project-item {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 40rpx 30rpx;
  margin-bottom: 20rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.project-info {
  flex: 1;
}

.project-name {
  font-size: 32rpx;
  color: #333333;
  margin-bottom: 10rpx;
}

.project-price {
  font-size: 28rpx;
  color: #C93639;
  font-weight: bold;
}

.checkbox {
  width: 50rpx;
  height: 50rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 30rpx;
}

.checked {
  font-size: 40rpx;
  color: #C93639;
}

.unchecked {
  font-size: 40rpx;
  color: #cccccc;
}

.register-btn-wrapper {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 30rpx;
  background-color: #fff;
  box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.05);
}

.register-btn {
  background-color: #C93639;
  color: #fff;
  text-align: center;
  padding: 30rpx;
  border-radius: 12rpx;
  font-size: 32rpx;
  font-weight: bold;
}
</style>
