<template>
  <van-pull-refresh  v-model="loading" @refresh="onRefresh">
    <template #loosing> 
        <div class="refresh-txt">
            <svg-icon className="icon" iconName="loading" :class="{ 'is-spinning': loading }"></svg-icon>
            <span>释放即可刷新</span>
        </div> 
    </template>
    <template #loading>  
        <div class="refresh-txt">
            <svg-icon className="icon" iconName="loading" :class="{ 'is-spinning': loading }"></svg-icon>
            <span>加载中……</span>
        </div>
    </template> 
    <template #success>  
        <div class="refresh-txt">
            <svg-icon className="icon" iconName="loading" :class="{ 'is-spinning': loading }"></svg-icon>
            <span>刷新成功</span>
        </div>
    </template>
    <div v-if="yiqiu_id">
    <div class="match-tab">
      <div class="tab-name">专家预测</div>
      <div class="tab-sel" v-if="false && experts.all.length != 0">
        <div class="around">
          <p :class="tab_name == '全部' ? 'around-p active-p' : 'around-p'" @click="tab_name = '全部',experts_list=experts.all,no_data = !experts.all.length">全部</p>
          <p :class="tab_name == '战绩' ? 'around-p active-p' : 'around-p'" @click="tab_name = '战绩',experts_list=experts.hongdan,no_data = !experts.hongdan.length">战绩</p>
          <p :class="tab_name == '混场' ? 'around-p active-p' : 'around-p'" @click="tab_name = '混场',experts_list=experts.chuanguan,no_data = !experts.chuanguan.length">混场</p>
        </div>
      </div>
    </div>
    <div class="expert_main" style="padding: 10px">
      <div class="hero-content-list" v-if="!no_data">
        <div class="hero-match" v-for="item in experts_list" :key="item.exporter_safe_code">
          <div class="match-list" @click="goDetail({type1: item.type1,exporter_safe_code:item.exporter_safe_code ,type3:item.type3})">
            <div class="list-top">
              <div class="icon">
                  <img v-lazy="item.exporter_face_image ? item.exporter_face_image : 'https://img2.ydniu.com/app/images/default_user.png'"
                    onerror="this.src='https:\/\/img2.ydniu.com/app/images/default_user.png';this.onerror=null" />
                  <div class="tj_num" v-if="item.tj_num">
                    {{ item.tj_num ? item.tj_num : 1 }}
                  </div>
              </div>
              <div class="list-top-center">
                  <div class="name">
                    {{ item.exporter_nick_name ? item.exporter_nick_name : '' }}
                  </div>
                  <div class="record">
                    <span>
                      近{{ item.current_hit && item.current_hit[0] ? item.current_hit[0] : '' }}红{{ item.current_hit && item.current_hit[1] ? item.current_hit[1] : '' }}
                    </span>
                    <span v-if="item.current_win_count > 2">{{ item.current_win_count ? item.current_win_count : '' }}连红</span>
                  </div>
                </div>
            </div>
            <div class="list-center" v-if="item.top_10_result">
              <div class="list-center-title">
                <span>近期:</span>
              </div>
              <div class="list-type-list" v-for="(result, index) in JSON.parse(item.top_10_result)" :key="index">
                <span class="list-red" v-if="result == 1">W</span>
                <span class="list-black" v-else>L</span>
              </div>
            </div>
            <div class="list-bottom">
              <div class="list-match">
                <span>{{item.type1 == '方案' ? item.prediction_type.replace('让球胜平负','让球') : item.type1}}</span>
                <span>今日基本面解读推荐！</span>
              </div>
              <div class="cost"><img v-lazy="{src:'https://img2.ydniu.com/app/images/sports_ydniu/model/ic_lock.png'}"/><span>{{ item.price || zq_coin }}牛宝解锁</span></div>
            </div>
          </div>
        </div>
      </div>
      <div v-show="loading">
        <div v-for="i in [1,2,3]" :key="i" class="skeletion-div">
          <van-skeleton title avatar :row="3" />
        </div>
     </div>
      <noData v-if="no_data" :nodata_message="`暂无${tab_name}专家数据！`"></noData>
    </div>
    </div>
  </van-pull-refresh>
</template>
  
<script lang="ts">
import { ref, computed, onMounted, defineComponent, watch} from 'vue';
// import { showLoadingToast, closeToast } from "vant";
import * as game_api from '@/api/game_api';
import { default as moment } from 'moment';
import { useRoute } from 'vue-router';
import { useStore } from 'vuex';
import noData from '@/components/NoData.vue';

export default defineComponent({
  name: "GameExperts",
  props: {
    yiqiu_id: {  
      type: [String, Number],  
      default:'', 
    },
    activeName: { type: String, default: '' },
  },
  components: {noData},
  setup(props,{emit}) {
    const route = useRoute();
    const store: any = useStore();
    const yiqiu_id:any = computed(()=>props.yiqiu_id);
    const loading = ref(true);
    const no_data = ref(false);
    const experts = ref({
      all:[],
      hongdan:[],
      chuanguan:[],
    }) as any;
    const experts_list :any = ref(null);
    const zq_coin: any = ref(38);
    const tab_name = ref('全部');

    const init_data = async () => {
      // if((props.activeName || route.name) == "GameExperts"){
        try {
          // showLoadingToast({});
          let id = yiqiu_id.value || route.query.yiqiuid || route.params.yiqiuid;
          if (id) {
            let match_detail:any = store.state.match_detail;
            if(match_detail[`experts_${id}`]){
              experts_list.value = match_detail[`experts_${id}`];
            }else{
              let data = await game_api.get_expert_schemes_and_2c1({ yiqiu_id: id });
              let hongdan_result:any = data?.get_expert_schemes;
              // console.info('hongdan_result',hongdan_result)
              if (hongdan_result && hongdan_result?.result) {
                if(hongdan_result.result?.data_list){
                  experts.value.hongdan =  hongdan_result?.result?.data_list;
                }
                if (hongdan_result.result?.ai_config) {
                  var config = JSON.parse(JSON.stringify(hongdan_result.result.ai_config));
                  zq_coin.value = config.zq_order_coin || 38;
                }
              }

              // let chuanguan_result = await game_api.get_game_2c1_list({ yiqiu_id: id });
              let chuanguan_result = data?.get_game_2c1_list;
              if(chuanguan_result && chuanguan_result?.result && chuanguan_result?.result?.plan_list){
                experts.value.chuanguan = chuanguan_result?.result?.plan_list;
              }
              experts.value.all = [];
              let hongdan_l = experts.value.hongdan.length;
              let chuanguan_l = experts.value.chuanguan.length;
              let for_length = Math.max(hongdan_l, chuanguan_l);
              for(let i=0;i<for_length;i++){
                if(i < hongdan_l){
                  experts.value.all.push(experts.value.hongdan[i])
                }
                if(i < chuanguan_l){
                  experts.value.all.push(experts.value.chuanguan[i])
                }
              }
              experts_list.value = experts.value.all;
              match_detail[`experts_${id}`] = experts_list.value;
            }
          }

          // if(tab_name.value == "混场"){
          //   experts_list.value = experts.value.chuanguan;
          // }else if(tab_name.value == "战绩"){
          //   experts_list.value = experts.value.hongdan;
          // }else{
          //   experts_list.value = experts.value.all;
          // }
          if(experts_list.value.length == 0){
            no_data.value = true;
          }
        } catch (error) {console.info("GameExperts",error) }
        
        emit('show-experts',{'show_experts': experts.value.all.length > 0});
        loading.value = false;
        // closeToast(true);
      // }
    };

    onMounted(init_data);

    watch(() => route.query.yiqiuid || route.params.yiqiuid, (newVal: any) => {
        if (newVal) {
            console.info('expert',newVal,yiqiu_id.value)
            init_data();
        }
    });

    const onRefresh = () => {
      setTimeout(() => {
        init_data()
        loading.value = false;
      }, 500);
    };

    // 时间
    const formatDate = computed(() => {
      return (val: any, format: any) => {
        return moment(val).format(format);
      };
    });

    const goDetail = ({type1,exporter_safe_code ,type3}:any) => {
      if(type1 == '胜平负'){
        location.href=`/jczq/scheme-${exporter_safe_code}-${type3}?zq&from_vue_live3`;
      }else if(type1 == '2串1计划'){
        location.href=`/model/crosstalk_list/${exporter_safe_code}?zq&from_vue_live3`;
      }else if(type1 == '方案'){
        location.href=`/jczq/scheme2-${exporter_safe_code}-${type3}?zq&from_vue_live3`;
      }
    }

    //跳转专家详情页
    return {
      loading,
      no_data,
      formatDate,
      experts,
      experts_list,
      zq_coin,
      tab_name,
      goDetail,
      onRefresh
    };
  }
});
</script>

<style lang="scss" scoped>
@use '@/common/style/common' as *;

.skeletion-div{
  width: 100%;
  padding: 10px;
  background: #fff;
  border-radius: 10px;
  margin-bottom: 10px;
}

/* 战绩英雄 */
.match-tab {
  width: 100%;
  padding: 10px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .tab-name {
    font-size: 16px;
    font-weight: 500;
  }

  .tab-sel {
    color: var(--color-999999);
    text-align: center;
    justify-content: center;

    .around {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      padding: 1px;
      box-sizing: border-box;
      background: #E7EEF7;
      @include border-radius(15px);
      overflow: hidden;
    }

    .around-p {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      color: #777777;
      line-height: 30px;
      width: 60px;
      margin: 2px 3px;
    }

    .active-p {
      background: #fff;
      color: #227AFF;
      box-shadow: 0px 1 4px 0px rgba(34, 122, 255, 0.3);
      @include border-radius(15px);
      overflow: hidden;
    }
  }
}

.match-list {
  display: block;
  padding: 10px;
  background: #fff;
  border-radius: 10px;
}

.hero-match {
  width: 100%;
  padding: 0 10px 10px 10px;
}

.list-top-center {
  display: flex;
  align-items: center;

  .name {
    font-size: 15px;
    font-weight: bold;
    color: #333;
    padding: 0 5px;
  }

  .record span {
    font-size: 12px;
    color: #FF2121;
    margin-right: 5px;
    padding: 1px 3px;
    border-radius: 4px;
    background-color: #FFE4E4;
  }
}

.icon img {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: solid 1px #ededed;
  padding: 2px;
  -webkit-filter: grayscale(50%);
  -moz-filter: grayscale(50%);
  -ms-filter: grayscale(50%);
  -o-filter: grayscale(50%);
  filter: grayscale(50%);
  filter: gray;
}

.list-center {
  padding: 4px 8px;
  background: #F2F3F5;
  border-radius: 4px;
  display: flex;
  align-items: center;
  margin-top: 5px;
  width: fit-content;
  font-size: 14px;
  .list-center-title {
    color: #777777;
  }

  .list-type-list {
    margin-left: 4px;
  }

  .list-red {
    border-radius: 4px;
    border: 1px solid #FF2121;
    color: #FF2121;
    display: inline-block;
    margin-left: 2px;
    text-align: center;
    width: 20px;
    height: 20px;
    line-height: 20px;
  }

  .list-black {
    color: #777777;
    border-radius: 4px;
    border: 1px solid #777777;
    display: inline-block;
    margin-left: 2px;
    text-align: center;
    width: 20px;
    height: 20px; 
    line-height: 20px;
  }
}

.list-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  margin-top:10px;
  .list-match {
    font-size: 14px;
    span {
      color: #333333;
    }
    span:first-child {
      padding: 0px 3px;
      background: #F1F9FF;
      border: solid 1px #3AA4F2;
      border-radius: 3px;
      font-size: 12px;
      color: #3AA4F2;
      margin-right:6px;
    }
  }

  .cost {
    padding:0 10px;
    font-size: 16px;
    font-weight: 500;
    line-height: 30px;
    background: linear-gradient(90deg, #F9D9AD 0%, #E4C585 100%);
    border-radius: 15px;
    text-align: center;
    color: #644821;
    display: flex;
    align-items: center;
    justify-content: center;
    img{
      width:18px;
      height: 18px;
      margin-right: 2px;
    }
  }
}

.list-top {
  display: flex;
  align-items: center;
  position: relative;
}

.expert_main {
  padding: 0 !important;
  text-align: left;
}

.icon {
  position: relative;
}

.tj_num {
  z-index: 1;
  position: absolute;
  top: 0;
  right: 0;
  padding: 2px 5px;
  color: #fff;
  font-size: 12px;
  line-height: 12px;
  text-align: center;
  transform: scale(0.8);
  background: #d23338;
  border-radius: 50%;
  border: solid 1px #fff;
  border-bottom-left-radius: 0px;
}
</style>