<template>
  <div>
      <div class="serch row item-center allAlignment" v-if="noShow">
        <div class="row center all pointer hover br05 fz14" @click="elasticFrame()">
          <span style="font-size:.14rem;color:#ccc;">选择联赛</span>
        </div>
        <div class="row item-center">
          <div class="row item-center">
            <input
              class="fz14"
              type="text"
              placeholder="请输入球队名称"
              v-model="serchTxt"
              onkeyup="this.value=this.value.replace(/\s+/g,'')"
              color="#AAA"
            >
            <div class="row center serch-img pointer hover" @click="Serch()">
              <img src="@/assets/st-imges/serch.png" alt>
            </div>
          </div>
          <div class="row item-center">
            <div class="row center clear pointer hover br05 fz14" @click="clearSerchTxt()">清除</div>
            <div class="row center redraw pointer hover br05" @click="red()">
              <img src="@/assets/st-imges/redraw.png" alt>
            </div>
          </div>
        </div>
      </div>

       <!-- 日期 -->
      <div v-if="(this.$store.getters.getActivity == 'StMorningPlate'||this.$store.getters.getActivity == 'StStringScene')" style="height: .6rem;">
        <div  class="date row item-center">
          <div class="row datebox pdg20">
            <p @click="dateActive(item.id)"  class="datebox_p" :class="dateClick == item.id?'dateShow':''" v-for="(item,index) in date" :key="index">{{item.title}}</p>
          </div>
        </div>
      </div>


    <div class="singleAndDouble" v-for="(item,index) in data" :key="index">
      <div class="singleAndDouble-header pdg20 row">
        <div style="width:26%;height:100%;font-size:.16rem;" class="row item-center">{{item.leagueName}}</div>
        <div style="width:35%;height:100%" class="singleAndDouble-header-title">
          <div class="row center">全场</div>
          <div class="row item-center allAlignment" style="padding:0 .75rem;color:#999">
            <p>单</p>
            <p>双</p>
          </div>
        </div>
        <div style="width:35%;height:100%" class="singleAndDouble-header-title">
          <div class="row center">上半场</div>
          <div class="row item-center allAlignment" style="padding:0 .75rem;color:#999">
            <p>单</p>
            <p>双</p>
          </div>
        </div>
      </div>
      <div
        class="singleAndDouble-body row pdg20"
        style="border-bottom:.01rem solid #ddd"
        v-for="(items,i) in item.matchData"
        :key="i"
      >
        <div style="width:25%;" class="row item-center">
          <div
            style="width:.8rem;text-align:center;height:100%;margin-right:.2rem;flex-wrap: wrap;padding:.05rem 0;"
            class="row center"
          >
            <p style="width:100%;">{{items.match_date}}</p>
            <p style="font-size:.12rem;color:#F76649;width:100%">{{items.match_time}}</p>
            <p style="width:100%;">
              <img style="width:.14rem;height:.14rem" src="@/assets/st-imges/shizhong.png">
            </p>
          </div>
          <div>
            <p>{{items.home_team}}</p>
            <p style="margin-top:.1rem;">{{items.guest_team}}</p>
          </div>
        </div>
          <!-- 全场 -->
          <div style="width:35%;padding:0 .25rem;border-right:.01rem solid #ddd;position: relative;" class="row item-center allAlignment">
            <div v-for="(itemList,idx) in items.oddsData" :key="itemList.id" style="">
               <div v-if="itemList.odds_code == 'tss' || itemList.odds_code == 'tsd'" :class="itemList.odds_code == 'tss'?'list-left':'list-right'">
                  <div v-if="itemList.odds<=0" class="singleAndDouble-list-btn">
                    <img style="vertical-align:middle" src="@/assets/st-imges/mima.png" alt="">
                  </div>
                  <div v-else>
                  <p class="singleAndDouble-list-btn" v-if="itemList.odds_code == 'tss' " :class="{col:itemList.isTrue,rising:itemList.change == 'rising',falling:itemList.change == 'falling'}" @click="selection(index,i,idx,itemList.id,itemList.p_code,items.match_id,items.home_team,items.guest_team,'','进球:单/双')">{{itemList.odds?itemList.odds:'-'}}</p>
                  <p class="singleAndDouble-list-btn" v-if="itemList.odds_code == 'tsd' " :class="{col:itemList.isTrue,rising:itemList.change == 'rising',falling:itemList.change == 'falling'}" @click="selection(index,i,idx,itemList.id,itemList.p_code,items.match_id,items.home_team,items.guest_team,'','进球:单/双')">{{itemList.odds?itemList.odds:'-'}}</p> 
                  </div>
               </div>
            </div>
          </div>
          <!-- 半场 -->
          <div style="width:35%;padding:0 .25rem;position: relative;" class="row item-center allAlignment">
            <div v-for="(itemList,idx) in items.oddsData" :key="itemList.id" style="">
              <div v-if="itemList.odds_code == 'tss_h' || itemList.odds_code == 'tsd_h' " :class="itemList.odds_code == 'tss_h'?'list-left':'list-right'">
                <div v-if="itemList.odds<=0" class="singleAndDouble-list-btn">
                  <img style="vertical-align:middle" src="@/assets/st-imges/mima.png" alt="">
                </div>
                <div v-else>
                <p class="singleAndDouble-list-btn" v-if="itemList.odds_code == 'tss_h' " :class="{col:itemList.isTrue,rising:itemList.change == 'rising',falling:itemList.change == 'falling'}" @click="selection(index,i,idx,itemList.id,itemList.p_code,items.match_id,items.home_team,items.guest_team,'','进球:单/双-上半场')">{{itemList.odds?itemList.odds:'-'}}</p>
                <p class="singleAndDouble-list-btn" v-if="itemList.odds_code == 'tsd_h' " :class="{col:itemList.isTrue,rising:itemList.change == 'rising',falling:itemList.change == 'falling'}" @click="selection(index,i,idx,itemList.id,itemList.p_code,items.match_id,items.home_team,items.guest_team,'','进球:单/双-上半场')">{{itemList.odds?itemList.odds:'-'}}</p> 
                </div>
              </div>
            </div>
          </div>

          <div @click="promptInfo()" class="right-img">
            <img style="margin-bottom:.1rem" src="@/assets/st-imges/TheGame.png">
            <br>
            <img src="@/assets/st-imges/TheScore.png">
          </div>
      </div>
    </div>
    <div v-if="noData">
      <noData/>
    </div>      
  </div>
</template>
<script>
import "@/css/index.css";
import noData from '@/components/noData';
export default {
  name: "singleAndDouble",
  components:{
    noData
  },      
  data() {
    return {
      // date: [{id:'all',title:'全部'}],//日期
      date: [],
      dateClick:'all',//激活的日期
      data: [],
      serchTxt: "",
      // pCode: this.$store.getters.getPaly_code, //传入接口的值
      activity: this.$store.getters.getActivity, 
      // ballCode: this.$store.getters.getBallCode,
      timers:true,
      noData : false, // 暂无数据显示
      limit : '',
      passLimit:'',
      dataList:[],
      noShow : true,
      lg_id:'',//联赛 
    };
  },
  mounted() {
    //获取选择的联赛id
    if(this.$store.getters.getLeagueData.length >0 ){
      if(this.$store.getters.getLeagueData.length == 1){
        this.lg_id = this.$store.getters.getLeagueData[0]
      }else{
        this.lg_id = this.$store.getters.getLeagueData.join(',');
      }
    }
    this.$store.dispatch("GETLOADING", true);    
    this.getAJAX(this.lg_id);
    this.$store.dispatch("SET_BETTING",[]);
    let _this = this;
    this.$public.ajaxTimerFun((timing)=>{
      if(_this.timers){
        _this.getAJAX(_this.lg_id,_this.serchTxt)
      }else{
        clearInterval(timing)
      }
    })
  },
  beforeDestroy(){
    this.timers = false
  },
  methods:{
    /*******************直播比赛数据提示*******************/
    promptInfo(){
      this.$dialog.alert({mes: '此功能暂未开放'});
    },

  getAJAX(lg_id,search){
    if(this.$store.getters.getActivity == 'StStringScene'){
      this.$store.dispatch("SET_BETTINGTYPE", 2);
    }else{
      this.$store.dispatch("SET_BETTINGTYPE", 1);
    }
      this.$http.get(this.$ports.home.gameList,{
        type:this.$store.getters.getActivity,
        p_code : this.$store.getters.getPaly_code,
        game_code : this.$store.getters.getBallCode,
        match_date:this.dateClick, 
        lg_id,                                       
        search
      }).then(res =>{    
        if(res.data.status == 1 && res.data.data.length > 0){
          this.noData = false;
          if(this.dataList.length){
            this.$public.oddsDataUpdate(res.data.data[0],this.dataList,1)
          }
          this.data = res.data.data[0];
          // console.log(this.data,'res')
          //临时储存数据
          this.dataList = JSON.stringify(res.data.data[0]);
          let betting = '';
          if(this.$store.getters.getActivity == 'StStringScene'){
            betting = this.$store.getters.getPassBetting;
          }else{
            betting = this.$store.getters.getBetting;  
          }              
            this.dataGroup(this.data,betting)
          this.$store.dispatch("GETLOADING", false);   
        }else{
          this.noData = true;
        }
        this.noShow = true;
        this.$store.dispatch("GETLOADING", false);     
      }
      )
    },
    // 进入添加选中效果
    dataGroup(data,betting){
      if(betting != null && betting.length > 0){
        // console.log('bet',betting)
        data.forEach(e =>{
          e.matchData.forEach(k =>{
            k.oddsData.forEach(item =>{
              betting.forEach(i =>{
                i.data.forEach(j =>{
                  if(j.id == item.id){
                    item.isTrue = true
                  }
                })
              })
            })
          })
        })
      }
    },    
    // 修改样式
    // 选中
    selection(idx,i,index,id,p_code,match_id,homeName,guestName,name,playName) {

      //调用投注公共方法
      let _this = this;
      let ballId = this.$store.getters.getBallCode;     
      let teamName = {
        home : homeName,
        guest : guestName
      }
      let acty =  this.$store.getters.getActivity;
      if(this.$store.getters.getActivity == 'StStringScene'){
        let betting = this.$store.getters.getPassBetting;
        if(betting == null){
            betting = [];
          }          
        this.$public.strandPublicBetting(this.data,p_code,id,idx,i,index,betting,acty,match_id,playName,name,ballId,homeName,guestName,this.passLimit,function(data,type){
            if(type){
              _this.$dialog.toast({ mes: "亲，超出表格上限啦。", timeout: 2000 }); 
            }          
          _this.$store.dispatch("SET_PASSBETTING",[]);
            _this.$store.dispatch("SET_PASSBETTING",data);
          })          
        }else{
          let betting = this.$store.getters.getBetting;
          this.$public.publicBetting(this.data,p_code,id,idx,i,index,betting,acty,match_id,teamName,playName,name,ballId,this.limit,function(data,type){
            if(type){
              _this.$dialog.toast({ mes: "亲，超出表格上限啦。", timeout: 2000 }); 
            }            
            _this.$store.dispatch("SET_BETTING",[]);
            _this.$store.dispatch("SET_BETTING",data);
          })
      }
      // 串场为单串
      if(this.limit < 10){      
        this.data[idx].matchData.forEach((val,i) =>{

          val.oddsData.forEach(item =>{
          if(this.$store.getters.getActivity == 'StStringScene'){
            if(this.passLimit < 10){
              if(val.match_id == match_id){
                // 当isTrue值为true时，点击时置为false
                if(item.id == id){
                  console.log(item.isTrue)
                  if(item.isTrue == true){
                    this.$forceUpdate();
                      item.isTrue = false
                  }else if(item.isTrue = false){
                    this.$forceUpdate();
                    item.isTrue = true
                  }else{
                    this.$forceUpdate();
                    this.$set(item, "isTrue", item.isTrue ? false : true);
                  }
                }else{
                if(item.isTrue == true){
                    item.isTrue = false;
                  }
                }
              }
            }else{
              if(item.id == id){
                this.$forceUpdate();
                this.$set(item,'isTrue',false)
              }
            }
          }else{
            if(item.id == id){
              this.$forceUpdate();
              this.$set(item, "isTrue", item.isTrue ? false : true);
            }
          }
          })
        })
      }else{
        this.data[idx].matchData.forEach(val =>{
          val.oddsData.forEach(item =>{
            if(item.id == id){
              this.$set(item,'isTrue',false)
            }
          })
        })            
      }
    },
      //搜索按钮被点击
    Serch() {
      //判断搜索框内是否有值，没有值中断函数
      if (this.serchTxt === "") {
        this.$dialog.toast({ mes: "请输入您想要搜索的内容。", timeout: 2000 });
        return false;
      } else {
        this.data = [];
        this.$store.dispatch("SET_LEAGUEDATA",[]); //需要拷贝
        this.getAJAX('',this.serchTxt)
      }
    },
    // 删除搜索框内容
    clearSerchTxt() {
       if(this.serchTxt != ""){
        this.data = [];
        this.serchTxt = "";
        this.$store.dispatch("SET_LEAGUEDATA",[]);  //需要拷贝
        this.getAJAX();
      }
    },
    // 刷新
    red() {
      this.serchTxt = "";
      this.lg_id ="";  
      this.$store.dispatch("SET_LEAGUEDATA",[]);  //需要拷贝
      if (this.serchTxt.length > 0) {
        this.Serch();
      } else {
        this.data = []
        this.getAJAX();
      }
    },
    //跳转到联赛选择页
    elasticFrame() {
      this.$store.dispatch("SET_CONTROLROUTING", 10015);
    },
    // 日期计算
    fun_date() {
      let date = new Date();
      let MonthNum = 0;
      let lastMonth = new Date(date.getFullYear(),date.getMonth() + 1,0).getDate();
      for (let i = 1; i < 8; i++) {
        let years = date.getFullYear();
        let month = date.getMonth() + 1;
        let day =
          date.getDate() + i < 10
            ? "0" + (date.getDate() + i)
            : date.getDate() + i;
        if (date.getDate() + i > lastMonth) {
          MonthNum++;
          month = date.getMonth() + 2;
          day = "0" + MonthNum;
        }
        let time = {};
        time.id = years + "-" + month + "-" + day;
        time.title =years+' - '+month + " - " + day;
        this.date.push(time);
      }
      this.date.push({id:'other', title:'其他'})
      // this.dateClick = this.date[0].id // 默认为当前日期
      if(this.$store.getters.getActivity === 'StMorningPlate') {
        this.dateClick = this.date[0].id // 默认为当前日期   
      } else {
        this.dateClick = ''
      }
    },
    dateActive(str){
      this.dateClick = str;
      this.data = []
      this.getAJAX(this.lg_id,this.serchTxt);
    },
  },
  created(){
    this.fun_date();
  },
  computed:{
    getActy() {
      return this.$store.getters.getActivity;
    },
    // getPaly_code() {
    //   return this.$store.getters.getPaly_code;
    // },
    // getBallId() {
    //   return this.$store.getters.getBallCode;
    // },
    getDeleteType() {
      return this.$store.getters.getDeleteType;
    },
    // 获取投注数量
    getLimit() {
      return this.$store.getters.getLimit;
    },
    getPassLimit(){
      return this.$store.getters.getPassLimit;
    }           
  },
  watch:{
    getActy(val) {
      this.activity = val;
      //  this.data = '';
      this.getAJAX();
      this.$store.dispatch("SET_BETTING",[]);
      if(val != 'StStringScene'){
        this.$store.dispatch("SET_PASSBETTING",[]);
      }       
    },
    // getPaly_code(val) {
    //   this.pCode = val;
    //   this.getAJAX();
    // },
    // getBallId(val) {
    //   this.ballCode = val;
    //   this.getAJAX();
    // },
    getDeleteType(val){
      let jumpOut = false;
      for(var i = 0; i < this.data.length; i++){
        for(var k = 0; k < this.data[i].matchData.length; k++){
          for(var j = 0; j < this.data[i].matchData[k].oddsData.length;j++){
              if(this.data[i].matchData[k].oddsData[j].id == val){
                this.$forceUpdate();
                this.data[i].matchData[k].oddsData[j].isTrue = false;
                jumpOut = true;
                break;
              }
          }
          if(jumpOut == true){
            break;
          }
        }
          if(jumpOut == true){
            break;
          }        
      }
    },  
     getLimit(val){
       this.limit = val;
       if(this.$store.getters.getActivity == 'StStringScene'){
       }else{
        if(val == 0){
          this.data.forEach(e =>{
            e.matchData.forEach(k =>{
              k.oddsData.forEach(item =>{
                if(item.isTrue){
                  item.isTrue = false;
                }
              })
            })
          })
        }
       }
    }, 
    getPassLimit(val){
      this.passLimit = val;
      if(this.$store.getters.getActivity == 'StStringScene'){
        if(val == 0){
          this.data.forEach(e =>{
            e.matchData.forEach(k =>{
              k.oddsData.forEach(item =>{
                if(item.isTrue){
                  this.$forceUpdate();
                  item.isTrue = false;
                }
              })
            })
          })
        }
      }
    },          
  }
};
</script>
<style scoped>
.list-left {
  position: absolute;
  left: 0.7rem;
  bottom: 25%;
}
.list-right {
  position: absolute;
  left: 2rem;
  bottom: 25%;
}
.pdg20 {
  padding: 0 0.2rem;
}
.singleAndDouble-header {
  height: 0.5rem;
  background: #e5e5e5;
}
.singleAndDouble-header-title div {
  height: 0.25rem;
}
.singleAndDouble-body {
  position: relative;
  background: #fff;
}
.singleAndDouble-body:hover{
  background: #fff5e9;
}
.singleAndDouble-list-btn {
  width: 0.48rem;
  height: 0.3rem;
  border: 0.01rem solid #ccc;
  line-height: 0.3rem;
  text-align: center;
  border-radius: 0.05rem;
  cursor: pointer;
}
.singleAndDouble-list-btn:hover{
  background: #ddd;
  color: #f76649;
  
}
.right-img {
  width: 0.15rem;
  position: absolute;
  right: 0.3rem;
  bottom: -2%;
}
.right-img img {
  width: 0.22rem;
  height: 0.2rem;
}
* {
  font-size: 0.14rem;
}

.serch {
  width: 100%;
  height: 0.5rem;
  background: #aaa;
  padding: 0 0.2rem;
}
input {
  border: none;
  background: #efefef;
  padding-left: 0.2rem;
  width: 4.6rem;
  height: 0.3rem;
  border-radius: 0.05rem 0 0 0.05rem;
}
.serch-img {
  width: 0.5rem;
  height: 0.3rem;
  background: #666;
  border-radius: 0 0.05rem 0.05rem 0;
  margin-right: 0.3rem;
}
.all {
  width: 1.2rem;
  height: 0.3rem;
  background: #666;
  color: #333;
}
.clear {
  width: 0.6rem;
  height: 0.3rem;
  background: #666;
  color: #ccc;
  margin-right: 0.1rem;
}
.redraw {
  width: 0.3rem;
  height: 0.3rem;
  background: #666;
}
img {
  width: 0.2rem;
  height: 0.2rem;
}
.br05 {
  border-radius: 0.05rem;
}
.fz14 {
  font-size: 0.14rem;
}
.hover:hover {
  background: #bbb;
  color: #fff!important;
}
.hover:hover span{
  color: #fff!important;
}
.pointer {
  cursor: pointer;
}
.col{
  border-radius: 2px;
  background: #F76649;
  color: #fff;
}


/* 日期样式 */
.date {
  width: 100%;
  background: #898989;
  font-size: .16rem;
  color: #fff;
  font-weight: bold;
  overflow: hidden;
  overflow-x: auto;
  white-space: nowrap;
}

.datebox_p{
  width: 1.4rem;
  text-align: center;
  line-height: .4rem;
  padding: 0 .05rem;
  cursor: pointer;
  font-size: .16rem;
  display: inline-block;
}
.dateShow{
  color: #f76649;
}
</style>