<template>
  <view class="event-register-page">
    <!-- 步骤指示器 -->
    <view class="steps-indicator">
      <view class="step-item" :class="{ active: currentStep >= 1 }">
        <view class="step-icon">
          <text class="icon">👤</text>
        </view>
        <text class="step-text">选择选手信息</text>
      </view>
      <view class="step-line" :class="{ active: currentStep >= 2 }"></view>
      <view class="step-item" :class="{ active: currentStep >= 2 }">
        <view class="step-icon">
          <text class="icon">💳</text>
        </view>
        <text class="step-text">订单支付</text>
      </view>
      <view class="step-line" :class="{ active: currentStep >= 3 }"></view>
      <view class="step-item" :class="{ active: currentStep >= 3 }">
        <view class="step-icon">
          <text class="icon">✓</text>
        </view>
        <text class="step-text">提交报名成功</text>
      </view>
    </view>

    <!-- 步骤1：选择选手信息 -->
    <view class="step-content" v-if="currentStep === 1">
      <view class="selected-count">已选：<text class="count">26</text> 人</view>

      <view class="add-player-btn" @click="goToAddPlayer">
        <text class="add-icon">⊕</text>
        <text>新增选手</text>
      </view>

      <view class="player-list">
        <view class="player-item" v-for="(item, index) in playerList" :key="index">
          <view class="player-checkbox" @click="togglePlayer(item)">
            <text v-if="item.selected" class="checked">✓</text>
            <text v-else class="unchecked">○</text>
          </view>
          <view class="player-info">
            <view class="player-name">{{ item.name }}</view>
            <view class="player-id">身份证：{{ item.idCard }}</view>
          </view>
          <view class="player-actions">
            <view class="action-btn edit-btn" @click.stop="handleEdit(item)">
              <text class="icon">✎</text>
              <text>编辑</text>
            </view>
            <view class="action-btn delete-btn" @click.stop="handleDelete(item)">
              <text class="icon">🗑</text>
              <text>删除</text>
            </view>
          </view>
        </view>
      </view>

      <view class="next-btn-wrapper">
        <view class="next-btn" @click="goToStep2">下一步</view>
      </view>
    </view>

    <!-- 步骤2：订单支付 -->
    <view class="step-content" v-if="currentStep === 2">
      <view class="order-title">订单支付</view>

      <view class="event-info-card">
        <view class="event-title">{{ eventInfo.title }}</view>
        <view class="divider"></view>
        <view class="info-item">
          <text class="label">地点：</text>
          <text class="value">{{ eventInfo.location }}</text>
        </view>
        <view class="info-item">
          <text class="label">比赛时间：</text>
          <text class="value">{{ eventInfo.matchTime }}</text>
        </view>
        <view class="info-item">
          <text class="label">报名项目：</text>
          <text class="value">{{ eventInfo.projects }}</text>
        </view>
        <view class="info-item">
          <text class="label">联系人：</text>
          <text class="value">{{ eventInfo.contact }}</text>
          <text class="edit-icon">📋</text>
        </view>
        <view class="info-hint">(注意是否用此号码接收信息)</view>
        <view class="info-item participants-item">
          <text class="label">参赛选手：</text>
          <text class="value participants">{{ eventInfo.participants }}</text>
          <view class="view-cert-btn" @click="showPlayers">
            <text>查看证件</text>
            <text class="arrow">›</text>
          </view>
        </view>
      </view>

      <view class="payment-info">
        <view class="payment-row">
          <text class="label">人数：</text>
          <text class="value">26</text>
        </view>
        <view class="payment-row total">
          <text class="label">合计：</text>
          <text class="value price">¥ 29999</text>
        </view>
      </view>

      <view class="next-btn-wrapper">
        <view class="next-btn" @click="goToStep3">去支付</view>
      </view>
    </view>

    <!-- 步骤3：报名成功 -->
    <view class="step-content" v-if="currentStep === 3">
      <view class="success-title">报名成功</view>

      <view class="event-info-card">
        <view class="event-title">{{ eventInfo.title }}</view>
        <view class="divider"></view>
        <view class="info-item">
          <text class="label">地点：</text>
          <text class="value">{{ eventInfo.location }}</text>
        </view>
        <view class="info-item">
          <text class="label">比赛时间：</text>
          <text class="value">{{ eventInfo.matchTime }}</text>
        </view>
        <view class="info-item">
          <text class="label">报名项目：</text>
          <text class="value">{{ eventInfo.projects }}</text>
        </view>
        <view class="info-item">
          <text class="label">联系人：</text>
          <text class="value">{{ eventInfo.contact }}</text>
        </view>

        <view class="participants-title">参赛选手：26人</view>
        <view class="participants-detail">
          <view class="participant-item" v-for="(item, index) in selectedPlayers" :key="index">
            <view class="participant-name">{{ item.name }}</view>
            <view class="participant-id">身份证：{{ item.idCard }}</view>
            <view class="participant-number">编号：{{ item.number }}</view>
          </view>
        </view>
      </view>

      <view class="close-btn-wrapper">
        <view class="close-btn" @click="handleClose">✕</view>
      </view>
    </view>

    <!-- 参赛选手弹窗 -->
    <view class="player-modal" v-if="showPlayerModal" @click="showPlayerModal = false">
      <view class="modal-content" @click.stop>
        <view class="modal-header">
          <text class="modal-title">参赛选手</text>
          <text class="close-icon" @click="showPlayerModal = false">✕</text>
        </view>
        <scroll-view class="modal-body" scroll-y>
          <view class="modal-player-item" v-for="(item, index) in selectedPlayers" :key="index">
            <view class="player-name">{{ item.name }}</view>
            <view class="player-id">身份证：{{ item.idCard }}</view>
            <view class="player-number" v-if="item.number">编号：{{ item.number }}</view>
            <view class="player-hint" v-if="index === 0">报名成功后，生成唯一编号</view>
          </view>
        </scroll-view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      currentStep: 1,
      eventId: '',
      eventInfo: {
        title: '2025年全国武术散打锦标赛暨第十七届世界武术锦标赛选拔赛',
        location: '天津市-天津市体育中心',
        matchTime: '2025.02.01-2025.02.10',
        projects: '男子组剑术、男子组太极拳、男子组套路、男子组其他项目',
        contact: '18666666666',
        participants: '张三、李四、王二、张三、张三、李四、王二、张三、李四'
      },
      playerList: [
        {
          id: 1,
          name: '张三',
          idCard: '123456789000000000',
          selected: false
        },
        {
          id: 2,
          name: '张三',
          idCard: '123456789000000000',
          selected: true
        },
        {
          id: 3,
          name: '张三',
          idCard: '123456789000000000',
          selected: true
        }
      ],
      selectedPlayers: [
        {
          name: '张三',
          idCard: '123456789000000000',
          number: '123-4567898275'
        },
        {
          name: '李四',
          idCard: '123456789000000000',
          number: '123-4567898276'
        }
      ],
      showPlayerModal: false
    };
  },
  onLoad(options) {
    if (options.eventId) {
      this.eventId = options.eventId;
    }
  },
  methods: {
    togglePlayer(item) {
      item.selected = !item.selected;
      this.$forceUpdate();
    },
    goToAddPlayer() {
      uni.navigateTo({
        url: '/pages/add-player/add-player'
      });
    },
    handleEdit(item) {
      uni.navigateTo({
        url: '/pages/edit-player/edit-player?id=' + item.id
      });
    },
    handleDelete(item) {
      uni.showModal({
        title: '删除选手',
        content: '确定要删除该选手吗？',
        success: (res) => {
          if (res.confirm) {
            const index = this.playerList.findIndex(p => p.id === item.id);
            if (index > -1) {
              this.playerList.splice(index, 1);
            }
          }
        }
      });
    },
    goToStep2() {
      this.currentStep = 2;
    },
    goToStep3() {
      this.currentStep = 3;
    },
    showPlayers() {
      this.showPlayerModal = true;
    },
    handleClose() {
      uni.navigateBack({
        delta: 3
      });
    }
  }
};
</script>

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

.steps-indicator {
  background-color: #fff;
  padding: 40rpx 30rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20rpx;
}

.step-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10rpx;
  opacity: 0.4;
}

.step-item.active {
  opacity: 1;
}

.step-icon {
  width: 70rpx;
  height: 70rpx;
  border-radius: 50%;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
}

.step-item.active .step-icon {
  background-color: #C93639;
}

.icon {
  font-size: 36rpx;
}

.step-item.active .icon {
  color: #fff;
}

.step-text {
  font-size: 22rpx;
  color: #666666;
  white-space: nowrap;
}

.step-item.active .step-text {
  color: #C93639;
  font-weight: bold;
}

.step-line {
  flex: 1;
  height: 2rpx;
  background-color: #eeeeee;
  margin: 0 10rpx;
}

.step-line.active {
  background-color: #C93639;
}

.step-content {
  padding: 30rpx;
}

.selected-count {
  font-size: 28rpx;
  color: #666666;
  margin-bottom: 20rpx;
}

.count {
  color: #C93639;
  font-weight: bold;
  font-size: 32rpx;
}

.add-player-btn {
  background-color: #fff;
  padding: 30rpx;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10rpx;
  margin-bottom: 20rpx;
  color: #C93639;
  font-size: 30rpx;
}

.add-icon {
  font-size: 36rpx;
}

.player-list {
  margin-bottom: 20rpx;
}

.player-item {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.player-checkbox {
  width: 40rpx;
  height: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

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

.player-info {
  flex: 1;
}

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

.player-id {
  font-size: 26rpx;
  color: #666666;
}

.player-actions {
  display: flex;
  gap: 15rpx;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 5rpx;
  padding: 10rpx 20rpx;
  border-radius: 8rpx;
  font-size: 24rpx;
  border: 2rpx solid;
}

.edit-btn {
  color: #C93639;
  border-color: #C93639;
}

.delete-btn {
  color: #C93639;
  border-color: #C93639;
}

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

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

.order-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333333;
  margin-bottom: 30rpx;
}

.event-info-card {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
}

.event-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333333;
  line-height: 1.5;
  margin-bottom: 20rpx;
}

.divider {
  height: 6rpx;
  background-color: #C93639;
  width: 120rpx;
  margin: 20rpx 0;
}

.info-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 15rpx;
}

.label {
  font-size: 26rpx;
  color: #666666;
  flex-shrink: 0;
}

.value {
  font-size: 26rpx;
  color: #666666;
  flex: 1;
}

.edit-icon {
  margin-left: 10rpx;
  font-size: 28rpx;
}

.info-hint {
  font-size: 22rpx;
  color: #C93639;
  margin: 10rpx 0;
}

.participants-item {
  align-items: center;
}

.participants {
  word-break: break-all;
}

.view-cert-btn {
  display: flex;
  align-items: center;
  padding: 8rpx 20rpx;
  border: 2rpx solid #C93639;
  border-radius: 8rpx;
  font-size: 24rpx;
  color: #C93639;
  margin-left: 20rpx;
  white-space: nowrap;
}

.arrow {
  font-size: 24rpx;
  margin-left: 5rpx;
}

.payment-info {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
}

.payment-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20rpx;
  font-size: 28rpx;
}

.payment-row.total {
  font-size: 32rpx;
  font-weight: bold;
}

.price {
  color: #C93639;
  font-size: 36rpx;
}

.success-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333333;
  margin-bottom: 30rpx;
}

.participants-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333333;
  margin: 20rpx 0;
}

.participants-detail {
  margin-top: 20rpx;
}

.participant-item {
  padding: 20rpx;
  background-color: #f9f9f9;
  border-radius: 12rpx;
  margin-bottom: 15rpx;
}

.participant-name {
  font-size: 28rpx;
  font-weight: bold;
  color: #333333;
  margin-bottom: 10rpx;
}

.participant-id {
  font-size: 24rpx;
  color: #666666;
  margin-bottom: 5rpx;
}

.participant-number {
  font-size: 24rpx;
  color: #666666;
}

.close-btn-wrapper {
  position: fixed;
  top: 100rpx;
  left: 30rpx;
  z-index: 999;
}

.close-btn {
  width: 80rpx;
  height: 80rpx;
  background-color: rgba(0, 0, 0, 0.5);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40rpx;
  color: #fff;
}

.player-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-content {
  width: 600rpx;
  max-height: 80vh;
  background-color: #fff;
  border-radius: 24rpx;
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #eeeeee;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333333;
}

.close-icon {
  font-size: 40rpx;
  color: #999999;
}

.modal-body {
  max-height: 60vh;
  padding: 30rpx;
}

.modal-player-item {
  padding: 20rpx;
  background-color: #f9f9f9;
  border-radius: 12rpx;
  margin-bottom: 15rpx;
}

.player-hint {
  font-size: 22rpx;
  color: #C93639;
  margin-top: 10rpx;
}
</style>
