<template>
  <view class="event-list-page">
    <!-- 搜索栏 -->
    <view class="search-bar">
      <view class="search-input-wrapper">
        <text class="search-icon">🔍</text>
        <input
          class="search-input"
          v-model="searchText"
          placeholder="请输入赛事关键字"
          placeholder-class="placeholder"
        />
      </view>
    </view>

    <!-- 筛选栏 -->
    <view class="filter-bar">
      <view class="filter-item" @click="showDatePicker = true">
        <text>{{ selectedDate || '日期' }}</text>
        <text class="dropdown-icon">▼</text>
      </view>
      <view class="filter-item" @click="showAreaPicker = true">
        <text>{{ selectedArea || '地区' }}</text>
        <text class="dropdown-icon">▼</text>
      </view>
    </view>

    <!-- 赛事列表 -->
    <view class="event-list" v-if="filteredEventList.length > 0">
      <view
        class="event-item"
        v-for="(item, index) in filteredEventList"
        :key="index"
        @click="goToEventDetail(item)"
      >
        <view class="event-title">{{ item.title }}</view>
        <view class="event-info">
          <text class="label">地点：</text>
          <text class="value">{{ item.location }}</text>
        </view>
        <view class="event-info">
          <text class="label">报名时间：</text>
          <text class="value">{{ item.registerTime }}</text>
        </view>
        <view class="event-info">
          <text class="label">比赛时间：</text>
          <text class="value">{{ item.matchTime }}</text>
        </view>
        <view class="event-info">
          <text class="label">报名人数：</text>
          <text class="value">{{ item.registerCount }}</text>
        </view>
        <view class="event-footer">
          <view class="register-btn" @click.stop="goToRegister(item)">
            {{ item.status === 'finished' ? '报名已结束' : '立即报名' }}
          </view>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view class="empty-state" v-else>
      <text class="empty-text">没有相关结果</text>
    </view>

    <!-- 日期选择器 -->
    <view class="picker-mask" v-if="showDatePicker" @click="showDatePicker = false">
      <view class="picker-content" @click.stop>
        <view class="picker-header">
          <text @click="showDatePicker = false">取消</text>
          <text @click="confirmDatePicker">确定</text>
        </view>
        <picker-view :value="datePickerValue" @change="handleDateChange" class="picker-view">
          <picker-view-column>
            <view class="picker-item" v-for="(item, index) in dateOptions" :key="index">
              {{ item }}
            </view>
          </picker-view-column>
        </picker-view>
      </view>
    </view>

    <!-- 地区选择器 -->
    <view class="picker-mask" v-if="showAreaPicker" @click="showAreaPicker = false">
      <view class="picker-content" @click.stop>
        <view class="picker-header">
          <text @click="showAreaPicker = false">取消</text>
          <text @click="confirmAreaPicker">确定</text>
        </view>
        <picker-view :value="areaPickerValue" @change="handleAreaChange" class="picker-view">
          <picker-view-column>
            <view class="picker-item" v-for="(item, index) in areaOptions" :key="index">
              {{ item }}
            </view>
          </picker-view-column>
        </picker-view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      searchText: '',
      selectedDate: '',
      selectedArea: '',
      showDatePicker: false,
      showAreaPicker: false,
      datePickerValue: [0],
      areaPickerValue: [0],
      dateOptions: ['2025-04-09', '2025-04-10', '2025-04-11'],
      areaOptions: ['乌鲁木齐', '天津市', '北京市'],
      eventList: [
        {
          id: 1,
          title: '2025年全国武术散打锦标赛暨第十七届世界武术锦标赛选拔赛',
          location: '天津市-天津市体育中心',
          registerTime: '2025.02.01-2025.02.10',
          matchTime: '2025.02.01-2025.02.10',
          registerCount: '25212',
          status: 'open'
        },
        {
          id: 2,
          title: '2025年全国武术套路锦标赛',
          location: '天津市-天津市体育中心',
          registerTime: '2025.02.01-2025.02.10',
          matchTime: '2025.02.01-2025.02.10',
          registerCount: '25212',
          status: 'finished'
        },
        {
          id: 3,
          title: '2025年全国武术散打锦标赛暨第十七届世界武术锦标赛选拔赛',
          location: '天津市-天津市体育中心',
          registerTime: '2025.02.01-2025.02.10',
          matchTime: '2025.02.01-2025.02.10',
          registerCount: '25212',
          status: 'open'
        },
        {
          id: 4,
          title: '2025年全国武术散打锦标赛暨第十七届世界武术锦标赛选拔赛',
          location: '天津市-天津市体育中心',
          registerTime: '2025.02.01-2025.02.10',
          matchTime: '2025.02.01-2025.02.10',
          registerCount: '25212',
          status: 'open'
        }
      ]
    };
  },
  computed: {
    filteredEventList() {
      return this.eventList.filter(item => {
        if (this.searchText && !item.title.includes(this.searchText)) {
          return false;
        }
        // 可以添加更多筛选条件
        return true;
      });
    }
  },
  methods: {
    handleDateChange(e) {
      this.datePickerValue = e.detail.value;
    },
    handleAreaChange(e) {
      this.areaPickerValue = e.detail.value;
    },
    confirmDatePicker() {
      this.selectedDate = this.dateOptions[this.datePickerValue[0]];
      this.showDatePicker = false;
    },
    confirmAreaPicker() {
      this.selectedArea = this.areaOptions[this.areaPickerValue[0]];
      this.showAreaPicker = false;
    },
    goToEventDetail(item) {
      uni.navigateTo({
        url: '/pages/event-detail/event-detail?id=' + item.id
      });
    },
    goToRegister(item) {
      if (item.status === 'open') {
        uni.navigateTo({
          url: '/pages/register-type/register-type?id=' + item.id
        });
      }
    }
  }
};
</script>

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

.search-bar {
  background-color: #fff;
  padding: 20rpx 30rpx;
}

.search-input-wrapper {
  display: flex;
  align-items: center;
  background-color: #f5f5f5;
  border-radius: 50rpx;
  padding: 20rpx 30rpx;
}

.search-icon {
  font-size: 32rpx;
  margin-right: 15rpx;
  color: #999999;
}

.search-input {
  flex: 1;
  font-size: 28rpx;
}

.placeholder {
  color: #cccccc;
}

.filter-bar {
  display: flex;
  gap: 20rpx;
  padding: 20rpx 30rpx;
  background-color: #fff;
  margin-bottom: 20rpx;
}

.filter-item {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx 30rpx;
  background-color: #f5f5f5;
  border-radius: 8rpx;
  font-size: 28rpx;
  color: #333333;
}

.dropdown-icon {
  font-size: 24rpx;
  color: #999999;
}

.event-list {
  padding: 0 30rpx 30rpx;
}

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

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

.event-info {
  display: flex;
  margin-bottom: 10rpx;
}

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

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

.event-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 20rpx;
}

.register-btn {
  background-color: #C93639;
  color: #fff;
  padding: 16rpx 50rpx;
  border-radius: 50rpx;
  font-size: 28rpx;
}

.empty-state {
  padding: 200rpx 0;
  text-align: center;
}

.empty-text {
  font-size: 28rpx;
  color: #999999;
}

.picker-mask {
  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: flex-end;
}

.picker-content {
  width: 100%;
  background-color: #fff;
  border-radius: 24rpx 24rpx 0 0;
}

.picker-header {
  display: flex;
  justify-content: space-between;
  padding: 30rpx;
  border-bottom: 1rpx solid #f5f5f5;
  font-size: 30rpx;
}

.picker-view {
  height: 400rpx;
}

.picker-item {
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  font-size: 30rpx;
}
</style>
