<template>
    <view class="school-detail" >
        <!-- 学校信息 -->
        <view class="school-name" >
          <view class="name">{{schoolName}}</view>
          <view class="count" >
            参赛选手 <text>{{applyCount}}</text>人
          </view>
        </view>
        <!-- 班级信息 -->
        <view class="school-info"  >
          <view class="school-category" >
            <view 
              :class="['category-item', active === item.id ? 'category-item-active' : '']" 
              v-for="item in classList" 
              :key="item.id"
              @click="changeTabs(item.id)"
            >
              <text> {{ item.id | filterClassLabel }}{{schoolPelploCount(item.id)}}</text>
              <view  v-if="active === item.id" class="line" />
            </view>
          </view>
          <!-- 参赛者 -->
          <view class="participant" >
            <view 
              class="participant-item"
              v-for="item in curSchool" 
              :key="item.id" 
            >
              <image class="user-ava" :src="item.headImg" />
              <view class="participant-info" >
                  <view class="label"> {{item.babyName || '逻辑狗宝贝'}} </view>
                  <view class="apply-num">
                    参赛号:{{item.competitionNo || '暂无编号'}}
                  </view>
              </view>
            </view>
            <view class="empty-page">
              <empty-page v-if="curSchool.length === 0" description='暂无参赛选手' />
            </view>
          </view>
        </view>
    </view>
</template>

<script>
import {  getCloudBaby, getMock } from '@/api/competition'
import Tip from '@/utils/tip'
import emptyPage from '../../components/empty-page/empty-page.vue'
const classList = [
  {
    id: -1,
    label: '托班',
  },
  {
    id: 0,
    label: '小班',
  },
  {
    id: 1,
    label: '中班',
  },
  {
    id: 2,
    label: '大班',
  },
]
export default {
  components: { emptyPage },
  name: 'school-detail',
  filters: {
    filterClassLabel (id) {

      switch (id) {
        case -1:
        
          return `托班`
        case 0:
          return `小班`
        case 1:
          return `中班`
        case 2:
          return `大班`
      }
    }
  },
  computed: {
    applyCount () {
      let count = 0
      for( let key in this.schoolDetail ) {
        if (!this.schoolDetail[key]) {
          count += 0
        } else {
            count += this.schoolDetail[key]?.length
        }
      }
      return count
    },
    curSchool () {
      if (this.active === -1) {
    
        return this.schoolDetail.babyListOfLevel_1
      } else if (this.active === 0) {
         
        return this.schoolDetail.babyListOfLevel0 
      } else if (this.active === 1) {
         
        return this.schoolDetail.babyListOfLevel1 
      } else {
        return this.schoolDetail.babyListOfLevel2
      }
    }
  },
  onLoad (options) {
    const { schoolId,  stageId, schoolName} = options
    this.schoolId = schoolId
    this.stageId = stageId
    this.schoolName = schoolName
    uni.setNavigationBarTitle({
      title: schoolName
    })
    this.getCloudBaby()
  },
  data () {
    return {
      active: -1,
      schoolId: '',
      stageId: '',
      schoolName: '',
      schoolDetail: {},
      classList: Object.freeze(classList)
    }
  },
  methods: {
    // 返回对应的人数
    schoolPelploCount (id) {
       switch (id) {
        case -1:
          return `(${ this.schoolDetail.babyListOfLevel_1.length})`
        case 0:
          return `(${ this.schoolDetail.babyListOfLevel0.length})`
        case 1:
          return `(${ this.schoolDetail.babyListOfLevel1.length})`
        case 2:
          return `(${ this.schoolDetail.babyListOfLevel2.length})`
      }
    },
    async getCloudBaby () {
      const $par = {
        schoolId: this.schoolId,
        stageId: this.stageId
      }
      Tip.loading('加载中')
      const { status, data } = await getCloudBaby($par)
      Tip.loaded()
      if ( status === 200 ) {
        this.schoolDetail = data
      }
    },
    changeTabs (index) {
      this.active = index
    }
  }
}
</script>

<style lang="less" scoped>
.school-detail {
  width: 100vw;
  .school-name {
    width: 686rpx;
    height: 144rpx;
    background: rgba(22, 119, 255, 0.1);
    border-radius: 40rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 32rpx;
    padding-right: 22rpx;
    box-sizing: border-box;
    margin: 0 auto;
    margin-top: 38rpx;
    .name {
      font-size: 36rpx;
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: #333333;
    }
    .count {
      font-size: 28rpx;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #676767;
      text {
        font-size: 36rpx;
        font-family: PingFangSC-Semibold, PingFang SC;
        margin-left: 8rpx;
        font-weight: 500;
        margin-right: 8rpx;
        color: #FE530F;
      }
    }
  }
  .school-info {
    .school-category {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 72rpx;
      width: 100vw;
      height: 102rpx;
      border-bottom: 2rpx solid #F6F6F6;
      position: sticky;
      top: 0rpx;
      background-color: #fff;
      box-sizing: border-box;
      .category-item {
        font-size: 32rpx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #666666;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        position: relative;
        text {

        }
      }
      .category-item:nth-child(2) {
        margin-left: 60rpx;
      }
      .category-item:nth-child(3) {
        margin-left: 60rpx;
        margin-right: 60rpx;
      }
      .category-item-active {
        font-size: 32rpx;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #1677FF;
      } 
      .line {
        width: 40rpx;
        height: 6rpx;
        background: #1677FF;
        box-shadow: 0rpx 2rpx 12rpx 0rpx rgba(22, 119, 255, 0.43);
        border-radius: 200rpx;
        margin-top: 14rpx;
        position: absolute;
        top: 40rpx;
      }
    }
    .participant {
      width: 100vw;
      overflow: hidden;
      min-height: ~'calc(100vh - 102px)';
      padding-bottom: 100rpx;
      position: relative;
      .participant-item {
        display: flex;
        align-items: center;
        padding-left: 66rpx;
        box-sizing: border-box;
        margin-top: 60rpx;
        .user-ava {
          width: 104rpx;
          height: 104rpx;
          margin-right: 22rpx;
          border-radius: 50%;
          display: block;
        }
        .participant-info {
            margin-top: 2rpx;
            .label {
              font-size: 32rpx;
              font-family: PingFangSC-Medium, PingFang SC;
              font-weight: 500;
              color: #333333;
              margin-bottom: 8rpx;
            }
            .apply-num {
              padding-left: 16rpx;
              padding-right: 16rpx;
              height: 40rpx;
              background: #F2F4FF;
              border-radius: 18rpx;
              font-size: 26rpx;
              font-family: PingFangSC-Medium, PingFang SC;
              font-weight: 500;
              color: rgba(22, 119, 255, 0.8);
              display: flex;
              justify-content: center;
              align-items: center;
            }
          }
      }
    }
  }
  
}


.empty-page {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>