<script>
import StaticConstant from "@/common/StaticConstant";
import ApiConstant from "@/common/ApiConstant";
import Request from '@/common/request'

let request = new Request()
// 缓存每页最多
const MAX_CACHE_DATA = 100;
// 缓存页签数量
const MAX_CACHE_PAGE = 3;
export default {
  name: "我的志愿明细",
  data() {
    return {
      volunteerId: '',//志愿id
      volunteerName: '',//志愿单名称
      topBackShow: true,//显示回到顶部
      selectForm: {
        address: '',
        schoolName: '',
        paneName: '全部',
        batch: '提前批',
      },
      scoreInfo: {},//成绩信息
      tabIndex: 0,
      scrollInto: "",
      newsList: [],
      cacheTab: [],
      tabBars: [{name: '本科提前批', id: '提前批'}, {name: '本科A段', id: '本科A段'}, {
        name: '本科B段',
        id: '本科B段'
      }, {name: '高职高专', id: '高职高专'}],
      filledVolunteer: {
        volunteerEarlyAdmissionList: [],//本科提前批
        volunteerUndergraduateAList: [],//本科A段
        volunteerUndergraduateBList: [],//本科B段
        volunteerUndergraduateList: [],//体育本科
        volunteerJuniorCollegeList: [],//高职高专
        volunteerMap: new Map()
      },//已填志愿信息
      years: StaticConstant.years,
      nowYear: StaticConstant.year
    }
  },
  onShow() {
    this.getFilledVolunteerList()
  },
  onLoad(e) {
    if (e.volunteerId) {
      this.volunteerId = e.volunteerId;
      console.log(e.volunteerId)
    }
    setTimeout(() => {
      this.tabBars.forEach((tabBar) => {
        this.newsList.push({
          data: [],
          isLoading: false,
          refreshText: "",
          loadingText: '加载更多...'
        });
      });
    }, 350)
  },
  methods: {
    goto(url) {
      uni.navigateTo({url: url})
    },
    topBack() {
      uni.pageScrollTo({
        scrollTop: 0,   // 滚动到页面的目标位置  这个是滚动到顶部, 0
        duration: 300  // 滚动动画的时长
      })
    },
    ontabtap(e) {
      //切换了 tabs
      console.log(e)
      let index = e.target.dataset.current || e.currentTarget.dataset.current;
      this.selectForm.batch = this.tabBars[index].id
      //清除页数
      this.switchTab(index);
    },
    switchTab(index) {
      if (this.newsList[index].data.length === 0) {
        //this.getList(index);
      }
      if (this.tabIndex === index) {
        return;
      }

      // 缓存 tabId
      if (this.newsList[this.tabIndex].data.length > MAX_CACHE_DATA) {
        let isExist = this.cacheTab.indexOf(this.tabIndex);
        if (isExist < 0) {
          this.cacheTab.push(this.tabIndex);
          //console.log("cache index:: " + this.tabIndex);
        }
      }

      this.tabIndex = index;
      this.scrollInto = this.tabBars[index].id;

      // 释放 tabId
      if (this.cacheTab.length > MAX_CACHE_PAGE) {
        let cacheIndex = this.cacheTab[0];
        this.clearTabData(cacheIndex);
        this.cacheTab.splice(0, 1);
      }
    },
    clearTabData(e) {
      this.newsList[e].data.length = 0;
      this.newsList[e].loadingText = "加载更多...";
    },
    /*获取已填报志愿数据*/
    getFilledVolunteerList() {
      request.get(ApiConstant.Volunteer.artVolunteerDetail, {id: this.volunteerId}).then(res => {
        console.log('返回值')
        console.log(res)
        if (res.success) {
          let dataResult = res.result
          //有数据情况下
          const volunteerMap = new Map();
          if (dataResult != null) {
            this.volunteerId = dataResult.id
            this.volunteerName = dataResult.volunteerName
            this.scoreInfo = dataResult.userScoreInfo
            let key = ''
            let volunteer = {indexStr: 0}
            //==================================提前批 start
            let volunteerRecordEarlyAdmissionList = dataResult.volunteerRecordEarlyAdmissionList;
            this.filledVolunteer.volunteerRecordEarlyAdmissionNum = volunteerRecordEarlyAdmissionList.length;
            for (let i = 0; i < volunteerRecordEarlyAdmissionList.length; i++) {
              key = volunteerRecordEarlyAdmissionList[i].majorCode + volunteerRecordEarlyAdmissionList[i].schoolCode
              volunteer = volunteerRecordEarlyAdmissionList[i]
              volunteerMap.set(key, volunteerRecordEarlyAdmissionList[i])
            }
            let indexs = 1;
            let volunteerRecordEarlyAdmissionList2 = [];
            while (indexs <= 2) {
              let record = {
                actives: false,
                indexs: indexs
              }
              for (let i = 0; i < volunteerRecordEarlyAdmissionList.length; i++) {
                if (volunteerRecordEarlyAdmissionList[i].indexs === indexs) {
                  record = volunteerRecordEarlyAdmissionList[i]
                  record.actives = true
                }
              }
              volunteerRecordEarlyAdmissionList2.push(record)
              indexs++;
            }
            this.filledVolunteer.volunteerEarlyAdmissionList = volunteerRecordEarlyAdmissionList2
            //==================================提前批 end
            //==================================本科A段 start
            let volunteerUndergraduateAList = dataResult.volunteerRecordUndergraduateAList;
            this.filledVolunteer.volunteerUndergraduateANum = volunteerUndergraduateAList.length;
            for (let i = 0; i < volunteerUndergraduateAList.length; i++) {
              key = volunteerUndergraduateAList[i].majorCode + volunteerUndergraduateAList[i].schoolCode
              volunteerMap.set(key, volunteerUndergraduateAList[i])
            }
            indexs = 1;
            let volunteerRecordUndergraduateAList2 = [];
            while (indexs <= 12) {
              let record = {actives: false, indexs: indexs}
              for (let i = 0; i < volunteerUndergraduateAList.length; i++) {
                if (volunteerUndergraduateAList[i].indexs === indexs) {
                  record = volunteerUndergraduateAList[i]
                  record.actives = true
                }
              }
              volunteerRecordUndergraduateAList2.push(record)
              indexs++;
            }
            this.filledVolunteer.volunteerUndergraduateAList = volunteerRecordUndergraduateAList2
            //==================================本科A段 end
            //==================================本科B段 start
            let volunteerUndergraduateBList = dataResult.volunteerRecordUndergraduateBList;
            this.filledVolunteer.volunteerUndergraduateBNum = volunteerUndergraduateBList.length;
            for (let i = 0; i < volunteerUndergraduateBList.length; i++) {
              key = volunteerUndergraduateBList[i].majorCode + volunteerUndergraduateBList[i].schoolCode
              volunteerMap.set(key, volunteerUndergraduateBList[i])
            }
            indexs = 1;
            let volunteerUndergraduateBList2 = [];
            while (indexs <= 12) {
              let record = {actives: false, indexs: indexs}
              for (let i = 0; i < volunteerUndergraduateBList.length; i++) {
                if (volunteerUndergraduateBList[i].indexs === indexs) {
                  record = volunteerUndergraduateBList[i]
                  record.actives = true
                }
              }
              volunteerUndergraduateBList2.push(record)
              indexs++;
            }
            this.filledVolunteer.volunteerUndergraduateBList = volunteerUndergraduateBList2
            //==================================本科 start
            let volunteerUndergraduateList = dataResult.volunteerRecordUndergraduateList;
            this.filledVolunteer.volunteerUndergraduateNum = volunteerUndergraduateList.length;
            for (let i = 0; i < volunteerUndergraduateList.length; i++) {
              key = volunteerUndergraduateList[i].majorCode + volunteerUndergraduateList[i].schoolCode
              volunteerMap.set(key, volunteerUndergraduateList[i])
            }
            indexs = 1;
            let volunteerUndergraduateList2 = [];
            while (indexs <= 12) {
              let record = {actives: false, indexs: indexs}
              for (let i = 0; i < volunteerUndergraduateList.length; i++) {
                if (volunteerUndergraduateList[i].indexs === indexs) {
                  record = volunteerUndergraduateList[i]
                  record.actives = true
                }
              }
              volunteerUndergraduateList2.push(record)
              indexs++;
            }
            this.filledVolunteer.volunteerUndergraduateList = volunteerUndergraduateList2
            //==================================本科 end
            //==================================高职高专 start
            let volunteerJuniorCollegeList = dataResult.volunteerRecordJuniorCollegeList;
            this.filledVolunteer.volunteerJuniorCollegeNum = volunteerJuniorCollegeList.length;
            for (let i = 0; i < volunteerJuniorCollegeList.length; i++) {
              key = volunteerJuniorCollegeList[i].majorCode + volunteerJuniorCollegeList[i].schoolCode
              volunteerMap.set(key, volunteerJuniorCollegeList[i])
            }
            indexs = 1;
            let volunteerJuniorCollegeList2 = [];
            while (indexs <= 12) {
              let record = {
                actives: false,
                indexs: indexs
              }
              for (let i = 0; i < volunteerJuniorCollegeList.length; i++) {
                if (volunteerJuniorCollegeList[i].indexs === indexs) {
                  record = volunteerJuniorCollegeList[i]
                  record.actives = true
                }
              }
              volunteerJuniorCollegeList2.push(record)
              indexs++;
            }
            this.filledVolunteer.volunteerJuniorCollegeList = volunteerJuniorCollegeList2
            //==================================高职高专 end
          } else {
            this.filledVolunteer.volunteerEarlyAdmissionList = []
            this.filledVolunteer.volunteerUndergraduateAList = []
            this.filledVolunteer.volunteerUndergraduateBList = []
            this.filledVolunteer.volunteerJuniorCollegeList = []
            this.filledVolunteer.volunteerUndergraduateList = []
            this.filledVolunteer.volunteerList = []
          }
          this.filledVolunteer.volunteerMap = volunteerMap
        }
      }).catch(err => {
      }).finally(() => {
      });
    },
    /*计算百分比*/
    toPercent(num, total) {
      return (Math.round(num / total * 10000) / 100.00);// 小数点后两位百分比
    },
    /*悬浮按钮*/
    trigger(e) {
      console.log(e)
      /*this.fab.content[e.index].active = !e.item.active*/
      if (e.item.text === '回到顶部') {
        this.topBack()
      }
    },
    fabClick() {
    },
    /*选择是否删除明细*/
    confirmDelVolunteerItem(e){
      let that = this
      console.log('删除提示')
      console.log(e)
      uni.showModal({
        title:'提示',
        content: '确认要删除吗?',
        confirmText: '确定',
        cancelText: '取消',
        success:function (res){
          if (res.confirm) {
            that.deleteVolunteerItem(e)
          }
        }
      });
    },
    /*确认删除志愿明细*/
    deleteVolunteerItem(e){
      let that = this
      request.delete(ApiConstant.Volunteer.volunteerRecordDelete,{id:e.id}).then(res => {
        if (res.success) {
          setTimeout(function () {
            uni.showToast({title: '删除成功', icon: "none"});
          }, 1000)
          setTimeout(function () {
            that.getFilledVolunteerList()
          }, 1500)
        }else{
          setTimeout(function () {
            uni.showToast({title: res.message, icon: "none"});
          }, 500)
        }
      }).catch(err => {
      }).finally(() => {
      });
    },
    toAdd(){
      uni.navigateTo({
        url: 'index?batch='+this.selectForm.batch+'&volunteerId='+this.volunteerId
      })

    }
  }
}
</script>

<template>
  <view class="divider"/>
  <!--志愿单信息-->
  <view class="header-info">
    <view class="h1">
      <view class="uni-flex uni-row" style="-webkit-justify-content: space-between;justify-content: space-between;">
        <view class="text">
          <text style="font-size: 40rpx;line-height: 40rpx">2024年</text>
        </view>
        <view class="text">口冲&nbsp;&nbsp;口稳&nbsp;&nbsp;口保</view>
      </view>
      <view class="uni-flex uni-row">
        <view class="text" style="-webkit-flex: 1;flex: 1;color:#bb440e;font-weight: 600">
          河南普通高校招生
        </view>
        <view class="text" style="-webkit-flex: 1;flex: 1;"></view>
      </view>
      <view class="uni-flex uni-row" style="-webkit-justify-content: space-between;justify-content: space-between;">
        <view class="text">
          <text>表名:{{ volunteerName }}</text>
        </view>
        <view class="text">填报建议</view>
      </view>
      <view class="uni-flex uni-row" style="-webkit-justify-content: space-between;justify-content: space-between;">
        <view class="text">
          <text class="tt1">文化分:{{ scoreInfo.culturalScore }}分</text>
          <text class="tt1">专业:{{ scoreInfo.professionalScore }}分</text>
          <text class="tt1">{{ scoreInfo.cognitioPolyclinic }}</text>
          <text class="tt1">{{ scoreInfo.professionalCategory }}</text>
        </view>
      </view>
    </view>
  </view>
  <!--导航栏-->
  <view class="tabs">
    <scroll-view id="tab-bar" class="scroll-h" :scroll-x="true" :show-scrollbar="false" :scroll-into-view="scrollInto">
      <view v-for="(tab,index) in tabBars" :key="tab.id" class="uni-tab-item" :id="tab.id" :data-current="index"
            @click="ontabtap">
        <text class="uni-tab-item-title" :class="tabIndex===index ? 'uni-tab-item-title-active' : ''">{{ tab.name }}
        </text>
      </view>
    </scroll-view>
  </view>
  <view class="divider"/>
  <!--志愿明细列表-->
  <view class="volunteerList">
    <view class="v-title">
      <view v-if="this.selectForm.batch=='提前批'">{{this.selectForm.batch}}（{{filledVolunteer.volunteerRecordEarlyAdmissionNum}}/2）</view>
      <view v-else-if="this.selectForm.batch=='本科A段'">{{this.selectForm.batch}}（{{filledVolunteer.volunteerUndergraduateANum}}/12）</view>
      <view v-else-if="this.selectForm.batch=='本科B段'">{{this.selectForm.batch}}（{{filledVolunteer.volunteerUndergraduateBNum}}/12）</view>
      <view v-else-if="this.selectForm.batch=='本科'">{{this.selectForm.batch}}（{{filledVolunteer.volunteerUndergraduateNum}}/12）</view>
      <view v-else>{{this.selectForm.batch}}（{{filledVolunteer.volunteerJuniorCollegeNum}}/12）</view>
    </view>
    <view class="volunteerItem" v-for="(item,index) in this.selectForm.batch==='提前批'?filledVolunteer.volunteerEarlyAdmissionList:
      this.selectForm.batch==='本科A段'?filledVolunteer.volunteerUndergraduateAList:
      this.selectForm.batch==='本科B段'?filledVolunteer.volunteerUndergraduateBList:
      this.selectForm.batch==='本科'?filledVolunteer.volunteerUndergraduateList:filledVolunteer.volunteerJuniorCollegeList" :key="item.indexs">
      <view class="uni-flex frow" v-if="item.majorCode">
        <view class="flex-item-9 item_left">
          <!--学校信息-->
          <view class="schoolInfo">
            <view class="uni-flex frow">
              <view class="flex-item-1 indexs">
                <text>{{item.indexs}}</text>
              </view>
              <view class="flex-item-7">
                <view class="uni-flex frow">
                  <text style="margin-right: 15rpx">[{{item.institutionCode}}]</text>
                  <text>{{item.schoolName}}</text>
                </view>
                <view class="uni-flex frow tags">
                  <text class="tag">{{item.province}}省</text>
                  <text class="tag">{{item.propertyName}}</text>
                </view>
              </view>
              <view class="flex-item-2">
              </view>
            </view>
          </view>
          <!--专业信息-->
          <view class="majorInfo">
            <view class="uni-flex frow">
              <view class="flex-item-2">
                <text v-if="item.enrollProbability" class="enrollProbability">{{item.enrollProbability}}%</text>
              </view>
              <view class="flex-item-7">
                <view class="uni-flex frow">
                    <text style="margin-right: 15rpx" v-if="item.enrollmentCode">[{{item.enrollmentCode}}]</text>
                    <text v-if="item.majorName">{{item.majorName}}{{item.majorDetail}}</text>
                </view>
                <view class="uni-flex frow">
                  <text class="low-text" v-if="item.studyCost">学费:{{item.studyCost}}</text>
                  <text class="low-text" v-if="item.jhs">招生{{item.jhs}}人</text>
                </view>
                <view class="uni-flex frow">
                  <text class="low-text" v-if="item.historyMajorEnrollMap['2023']">2023年录取最低分{{item.historyMajorEnrollMap['2023'].admissionLine}}分</text>
                  <text class="low-text" v-else-if="item.historyMajorEnrollMap['2022']">2022年录取最低分{{item.historyMajorEnrollMap['2022'].admissionLine}}分</text>
                  <text class="low-text" v-else-if="item.historyMajorEnrollMap['2021']">2021年录取最低分{{item.historyMajorEnrollMap['2021'].admissionLine}}分</text>
                </view>
                <view class="uni-flex frow">
                  <text class="low-text" v-if="item.rulesEnrollProbability">录取方式:{{item.rulesEnrollProbability}}</text>
                </view>
              </view>
              <view class="flex-item-1">
              </view>
            </view>
          </view>
        </view>
        <view class="flex-item-1 item_right">
          <view class="delBtn">
            <image @click="confirmDelVolunteerItem(item)" src="/static/icons/cuti/delete.png" style="width: 32rpx;height: 32rpx"/>
          </view>
        </view>
      </view>
      <!--空白专业-->
      <view class="uni-flex frow" v-else @click="toAdd()">
        <view class="flex-item-9 item_left">
          <view class="schoolInfo">
            <view class="uni-flex frow">
              <view class="flex-item-1 indexs">
                <text>{{item.indexs}}</text>
              </view>
              <view class="flex-item-7">
                <view class="uni-flex frow">
                  <text>点击添加</text>
                </view>
              </view>
            </view>
          </view>

        </view>
        <view class="flex-item-1 item_right">
        </view>
      </view>
    </view>

  </view>
</template>

<style scoped lang="scss">
.divider {
  background: #f5f5f5;
  width: 100%;
  height: 3rpx;
}

.uni-row {
  margin-bottom: 30rpx;
}

/*导航栏部分 start*/
.tabs {
  flex: 1;
  flex-direction: column;
  overflow: hidden;
  background-color: #ffffff;
  /* #ifndef APP-PLUS */
  /*height: 100vh;*/
  /* #endif */

  .scroll-h {
    /*width: 750rpx;*/
    /* #ifdef H5 */
    width: 100%;
    /* #endif */
    height: 80rpx;
    flex-direction: row;
    /* #ifndef APP-PLUS */
    white-space: nowrap;
    /* #endif */
    /* flex-wrap: nowrap; */
    /* border-color: #cccccc;
  border-bottom-style: solid;
  border-bottom-width: 1px; */
  }

  .line-h {
    height: 1rpx;
    background-color: #cccccc;
  }

  .uni-tab-item {
    /* #ifndef APP-PLUS */
    display: inline-block;
    /* #endif */
    flex-wrap: nowrap;
    padding-left: 34rpx;
    padding-right: 34rpx;
  }

  .uni-tab-item-title {
    color: #555;
    font-size: 30rpx;
    height: 80rpx;
    line-height: 80rpx;
    flex-wrap: nowrap;
    /* #ifndef APP-PLUS */
    white-space: nowrap;
    /* #endif */
  }

  .uni-tab-item-title-active {
    color: #007AFF;
  }

  .swiper-box {
    flex: 1;
  }

  .swiper-item {
    flex: 1;
    flex-direction: row;
  }

  .scroll-v {
    flex: 1;
    /* #ifndef MP-ALIPAY */
    flex-direction: column;
    /* #endif */
    /*width: 750rpx;*/
    width: 100%;
  }

  .update-tips {
    position: absolute;
    left: 0;
    top: 41px;
    right: 0;
    padding-top: 5px;
    padding-bottom: 5px;
    background-color: #FDDD9B;
    align-items: center;
    justify-content: center;
    text-align: center;
  }

  .update-tips-text {
    font-size: 14px;
    color: #ffffff;
  }

  .refresh {
    /*width: 750rpx;*/
    width: 100%;
    height: 64px;
    justify-content: center;
  }

  .refresh-view {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: center;
  }

  .refresh-icon {
    width: 30px;
    height: 30px;
    transition-duration: .5s;
    transition-property: transform;
    transform: rotate(0deg);
    transform-origin: 15px 15px;
  }

  .refresh-icon-active {
    transform: rotate(180deg);
  }

  .loading-icon {
    width: 20px;
    height: 20px;
    margin-right: 5px;
    color: #999999;
  }

  .loading-text {
    margin-left: 2px;
    font-size: 16px;
    color: #999999;
  }

  .loading-more {
    align-items: center;
    justify-content: center;
    padding-top: 10px;
    padding-bottom: 10px;
    text-align: center;
  }

  .loading-more-text {
    font-size: 28rpx;
    color: #999;
  }
}

/*导航栏部分 end*/


/*志愿信息头部信息 start*/
.header-info {
  padding: 30rpx 30rpx 0 30rpx;
  background-color: white;
  margin-bottom: 30rpx;
  .h1 {
    padding: 10rpx 0;
  }
  .tt1 {
    margin-right: 30rpx;
  }
}
/*志愿信息头部信息 end*/

/*志愿明细列表 start*/
.volunteerList{
  padding: 30rpx 30rpx 0 30rpx;
  background-color: white;
  .v-title{
    color:#666666;
    margin-bottom: 30rpx;
  }
  .volunteerItem{
    margin-bottom: 50rpx;
    .item_left{
      .schoolInfo{
        border-bottom: 1px solid #e5e5e5;
        padding: 15rpx 0 30rpx 0;
      }
      .majorInfo{
        padding: 30rpx 0;
        border-bottom: 1px solid #f5f5f5;
        .enrollProbability{
          color:#9b9b9b;
          font-size:25rpx;
        }
      }
    }
    .item_right{
      padding: 150rpx 0 0 20rpx;
    }
    /*序号*/
    .indexs{
      color:white;
      width: 30rpx;
      height: 40rpx;
      background-color: #1275ec;
      border-radius: 50%;
      padding-left: 10rpx;
      margin-right: 30rpx;

    }
    /*tags*/
    .tags{
      margin-top: 15rpx;
      .tag{
        margin-right: 20rpx;
        color:#9b9b9b;
        font-size:25rpx;
      }
    }
    .low-text{
      color:#9b9b9b;
      font-size:22rpx;
      margin-bottom: 15rpx;
    }
  }
}
/*志愿明细列表 end*/
</style>
