<!-- 进球 -->
<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 class="goal">
        <section v-if="hostResult?.zhibiao1_result && awayResult?.zhibiao1_result">
            <div class="title">比赛进球数统计，取主客球队近{{ limit_count }}场数据分析统计</div>
            <table>
                <thead>
                    <tr>
                        <th>指标</th>
                        <th>{{ game_info?.host_name}}</th>
                        <th>{{ game_info?.away_name }}</th>
                        <th>平均</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in zhibiao1_array" :key="index">
                        <td>{{item}}</td>
                        <td>{{hostResult.zhibiao1_result[index]}}%</td>
                        <td>{{awayResult.zhibiao1_result[index]}}%</td>
                        <td>{{(parseInt(hostResult.zhibiao1_result[index]) + parseInt(awayResult.zhibiao1_result[index]))/2}}%</td>
                    </tr>
                </tbody>
            </table>
            <div class="txt" v-if="showResult.zhibiao1_txt">{{ showResult.zhibiao1_txt }}</div>
        </section>
        <section v-if="hostResult?.zhibiao2_result && awayResult?.zhibiao2_result">
            <div class="title">球队得球数统计，取主客球队近{{ limit_count }}场数据分析统计</div>
            <table>
                <thead>
                    <tr>
                        <th width="40%">指标</th>
                        <th width="30%">{{ game_info?.host_name}}</th>
                        <th width="30%">{{ game_info?.away_name }}</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in zhibiao2_array" :key="index">
                        <td>{{item}}</td>
                        <td>{{ hostResult.zhibiao2_result[index]}}%</td>
                        <td>{{ awayResult.zhibiao2_result[index]}}%</td>
                    </tr>
                </tbody>
            </table>
            <div class="txt" v-if="showResult.zhibiao2_txt">{{showResult.zhibiao2_txt}}</div>
        </section>
        <section v-if="hostResult?.zhibiao3_result && awayResult?.zhibiao3_result">
            <div class="title">球队上下半场得球数统计，取主客球队近{{ limit_count }}场数据</div>
            <table>
                <thead>
                    <tr>
                        <th width="40%">指标</th>
                        <th width="30%">{{ game_info?.host_name}}</th>
                        <th width="30%">{{ game_info?.away_name }}</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in zhibiao3_array" :key="index">
                        <td>{{item}}</td>
                        <td>{{ hostResult.zhibiao3_result[index]}}</td>
                        <td>{{ awayResult.zhibiao3_result[index]}}</td>
                    </tr>
                </tbody>
            </table>
            <div class="txt" v-if="showResult.zhibiao3_txt">{{showResult.zhibiao3_txt}}</div>
        </section>
        <section v-if="hostResult?.zhibiao4_result && awayResult?.zhibiao4_result">
            <div class="title">球队丢球数统计，取主客球队近{{ limit_count }}场数据分析统计</div>
            <table>
                <thead>
                    <tr>
                        <th width="40%">指标</th>
                        <th width="30%">{{ game_info?.host_name}}</th>
                        <th width="30%">{{ game_info?.away_name }}</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in zhibiao4_array" :key="index">
                        <td>{{item}}</td>
                        <td>{{ hostResult.zhibiao4_result[index]}}%</td>
                        <td>{{ awayResult.zhibiao4_result[index]}}%</td>
                    </tr>
                </tbody>
            </table>
            <div class="txt" v-if="showResult.zhibiao4_txt">{{showResult.zhibiao4_txt}}</div>
        </section>
    </div>
    </van-pull-refresh>
</template>
  
<script lang="ts">
import { ref, defineComponent, onMounted,toRef,watch } from 'vue';
import * as nm_match_api from "@/api/nm_match_api";
import { useRoute } from 'vue-router';
import { default as _ } from 'lodash';

export default defineComponent({
    name: "GameGoal",
    props: { 
        gameInfo: {
            type: Object,
            required: false,
            default: () => { return {}; }
        },
        gameDetail: {
            type: Object,
            required: false,
            default: () => ({})
        },
        activeName: { type: String, default: '' },
    },
    setup(props) {
        const route = useRoute();
        const game_info:any = toRef(props,'gameInfo');
        const game_detail:any = toRef(props,'gameDetail');
        const loading = ref(false);
        const limit_count:any = ref(10);
        const hostResult: any = ref({
            zhibiao1_result:[],
            zhibiao2_result:[],
            zhibiao3_result:[],
            zhibiao4_result:[],
        });
        const awayResult: any = ref({
            zhibiao1_result:[],
            zhibiao2_result:[],
            zhibiao3_result:[],
            zhibiao4_result:[]
        });
        const showResult: any = ref({
            zhibiao1_txt:'',
            zhibiao2_txt:'',
            zhibiao3_txt:'',
            zhibiao4_txt:'',
        })
        const zhibiao1_array:any = ref(['超过0.5球','超过1.5球','超过2.5球','超过3.5球','超过4.5球','超过0.5(上半场)','超过1.5(上半场)','超过2.5(上半场)']);
        const zhibiao2_array:any = ref(['超过0.5球','超过1.5球','超过2.5球','超过3.5球','未进球']);
        const zhibiao3_array:any = ref(['场均进球(上半场)','场均进球(下半场)','进球(上半场)','进球(下半场)']);
        const zhibiao4_array:any = ref(['超过0.5球','超过1.5球','超过2.5球','超过3.5球','未进球']);

        onMounted(init_data);

        watch(() => route.query.yiqiuid || route.params.yiqiuid, (newVal: any) => {
            if (newVal && game_detail.value && newVal != game_detail.value.id) {
                console.info('goal',newVal,game_detail.value.id)
                init_data();
            }
        });

        const onRefresh = () => {
            refresh_data();
            loading.value = false;
        };

        const handle_data = (list: any) => {
           return list.map((item: any) => {
                const socre = [item?.home_scores?.[0]  || 0, item?.away_scores?.[0] || 0];
                const half = `${item?.home_scores?.[1]  || 0}-${item?.away_scores?.[1] || 0}`;
                return {
                    Id: [item.id, item.season_id, item.home_team_id, item.away_team_id],
                    Half: half,
                    Score: socre,
                };
            })
        }

        async function refresh_data () {
            let game_id: any = route.query.yiqiuid || route.params.yiqiuid;
            if (game_id) {
                let nm_result = await nm_match_api.nm_match_detail({ yiqiu_id: game_id });
                game_detail.value = nm_result;
                let host_id = nm_result?.home_team_id;
                let away_id = nm_result?.away_team_id;
                const homeListData = nm_result?.history?.home;
                const awayListData = nm_result?.history?.away;
                if(homeListData){
                    if(homeListData.length < 10){
                        limit_count.value = homeListData.length;
                    }
                    const homeList = handle_data(homeListData);
                    let host_result = getTop10Goal(homeList,host_id,limit_count.value);
                    hostResult.value = host_result;
                }
                if(awayListData){
                    if(awayListData.length < 10){
                        limit_count.value = awayListData.length;
                    }
                    const awayList = handle_data(awayListData);
                    let away_result= getTop10Goal(awayList,away_id,limit_count.value);
                    awayResult.value = away_result;
                }
                getShowResult();    
            }
        }

        async function init_data () {
            if (game_detail.value?.id) {
                let nm_result = game_detail.value;
                let host_id = nm_result?.home_team_id;
                let away_id = nm_result?.away_team_id;
                const homeListData = nm_result?.history?.home;
                const awayListData = nm_result?.history?.away;
                if(homeListData){
                    if(homeListData.length < 10){
                        limit_count.value = homeListData.length;
                    }
                    const homeList = handle_data(homeListData);
                    let host_result = getTop10Goal(homeList,host_id,limit_count.value);
                    hostResult.value = host_result;
                }
                if(awayListData){
                    if(awayListData.length < 10){
                        limit_count.value = awayListData.length;
                    }
                    const awayList = handle_data(awayListData);
                    let away_result= getTop10Goal(awayList,away_id,limit_count.value);
                    awayResult.value = away_result;
                }
                getShowResult();    
            } else {
                refresh_data();
            }
        }

        const getTop10Goal = function (list: any, team_id: any,limit_count:any) {
            if(list.length > 10){
                list = _.take(list,limit_count);
            }
            list.forEach(function(item:any){
                if(item.Half && item.Half.indexOf('-')){
                    item.Half = _.map(item.Half.split('-'), _.parseInt);
                    item.Second_Half = [item.Score[0] - item.Half[0], item.Score[1] - item.Half[1]];
                }
            });
            //指标1：计算总进球超过0.5球，超过1.5球，超过2.5球，超过3.5球，超过4.5球
            let goal_sum_05 = _.filter(list,item => _.sum(item.Score) > 0).length;
            let goal_sum_15 = _.filter(list,item => _.sum(item.Score) > 1).length;
            let goal_sum_25 = _.filter(list,item => _.sum(item.Score) > 2).length;
            let goal_sum_35 = _.filter(list,item => _.sum(item.Score) > 3).length;
            let goal_sum_45 = _.filter(list,item => _.sum(item.Score) > 4).length;
            let goal_half_sum_05 = _.filter(list,item => _.sum(item.Half) > 0).length;
            let goal_half_sum_15 = _.filter(list,item => _.sum(item.Half) > 1).length;
            let goal_half_sum_25 = _.filter(list,item => _.sum(item.Half) > 2).length;
            let goal_sum_05_result = (goal_sum_05 / list.length * 100).toFixed(0);
            let goal_sum_15_result = (goal_sum_15 / list.length * 100).toFixed(0);
            let goal_sum_25_result = (goal_sum_25 / list.length * 100).toFixed(0);
            let goal_sum_35_result = (goal_sum_35 / list.length * 100).toFixed(0);
            let goal_sum_45_result = (goal_sum_45 / list.length * 100).toFixed(0);
            let goal_half_sum_05_result = (goal_half_sum_05 / list.length * 100).toFixed(0);
            let goal_half_sum_15_result = (goal_half_sum_15 / list.length * 100).toFixed(0);
            let goal_half_sum_02_result = (goal_half_sum_25 / list.length * 100).toFixed(0);

            //指标2：计算球队超过0.5球，超过1.5球，超过2.5球，超过3.5球， 未进球
            let goal_0 = _.filter(list,item => (item.Id[2] == team_id && item.Score[0] == 0) || (item.Id[3] == team_id && item.Score[1] == 0)).length
            let goal_05 = _.filter(list,item => (item.Id[2] == team_id && item.Score[0] > 0) || (item.Id[3] == team_id && item.Score[1] > 0)).length
            let goal_15 = _.filter(list,item => (item.Id[2] == team_id && item.Score[0] > 1) || (item.Id[3] == team_id && item.Score[1] > 1)).length
            let goal_25 = _.filter(list,item => (item.Id[2] == team_id && item.Score[0] > 2) || (item.Id[3] == team_id && item.Score[1] > 2)).length
            let goal_35 = _.filter(list,item => (item.Id[2] == team_id && item.Score[0] > 3) || (item.Id[3] == team_id && item.Score[1] > 3)).length
            let goal_0_result = (goal_0 / list.length * 100).toFixed(0);
            let goal_05_result = (goal_05 / list.length * 100).toFixed(0);
            let goal_15_result = (goal_15 / list.length * 100).toFixed(0);
            let goal_25_result = (goal_25 / list.length * 100).toFixed(0);
            let goal_35_result = (goal_35 / list.length * 100).toFixed(0);

            //指标3
            let goal_shang_mean = _.meanBy(list,(item:any) => {
                if(item.Id[2] == team_id){
                    return item.Half[0]
                }else if(item.Id[3] == team_id){
                    return item.Half[1]
                } else {
                    return 0
                }
            });
            let goal_xia_mean = _.meanBy(list,(item:any) => { 
            if(item.Id[2] == team_id && item?.Second_Half){
                return item?.Second_Half[0]
            }else if(item.Id[3] == team_id && item?.Second_Half){
                return item?.Second_Half[1]
            } else {
                return 0
            }
            });
            let goal_shang_count =_.filter(list,item => (item.Id[2] == team_id && item.Half[0] > 0) || (item.Id[3] == team_id && item.Half[1] > 0)).length
            let gaol_shang_rate = (goal_shang_count/list.length * 100).toFixed(0) + '%';
            let goal_xia_count = _.filter(list,item => (item.Id[2] == team_id && item?.Second_Half && item?.Second_Half[0] > 0) || (item.Id[3] == team_id && item?.Second_Half && item?.Second_Half[1] > 0)).length
            let gaol_xia_rate = (goal_xia_count/list.length * 100).toFixed(0) + '%';

             //指标4
            let goal_loss_0 = _.filter(list,item => (item.Id[2] == team_id && item.Score[1] == 0) || (item.Id[3] == team_id && item.Score[0] == 0)).length
            let goal_loss_05 = _.filter(list,item => (item.Id[2] == team_id && item.Score[1] > 0) || (item.Id[3] == team_id && item.Score[0] > 0)).length
            let goal_loss_15 = _.filter(list,item => (item.Id[2] == team_id && item.Score[1] > 1) || (item.Id[3] == team_id && item.Score[0] > 1)).length
            let goal_loss_25 = _.filter(list,item => (item.Id[2] == team_id && item.Score[1] > 2) || (item.Id[3] == team_id && item.Score[0] > 2)).length
            let goal_loss_35 = _.filter(list,item => (item.Id[2] == team_id && item.Score[1] > 3) || (item.Id[3] == team_id && item.Score[0] > 3)).length
            let goal_loss_0_rate = (goal_loss_0 / list.length * 100).toFixed(0);
            let goal_loss_05_rate = (goal_loss_05 / list.length * 100).toFixed(0);
            let goal_loss_15_rate = (goal_loss_15 / list.length * 100).toFixed(0);
            let goal_loss_25_rate = (goal_loss_25 / list.length * 100).toFixed(0);
            let goal_loss_35_rate = (goal_loss_35 / list.length * 100).toFixed(0);
             
            return{
                zhibiao1_result:[goal_sum_05_result,goal_sum_15_result,goal_sum_25_result,goal_sum_35_result,goal_sum_45_result,goal_half_sum_05_result,goal_half_sum_15_result,goal_half_sum_02_result],
                zhibiao2_result:[goal_05_result,goal_15_result,goal_25_result,goal_35_result,goal_0_result],
                zhibiao3_result:[goal_shang_mean.toFixed(2),goal_xia_mean.toFixed(2),gaol_shang_rate,gaol_xia_rate],
                zhibiao4_result:[goal_loss_05_rate,goal_loss_15_rate,goal_loss_25_rate,goal_loss_35_rate,goal_loss_0_rate]
            }
        }

        const getShowResult = function(){
            if(hostResult.value?.zhibiao1_result && awayResult.value?.zhibiao1_result){
                
                //指标1
                // *超过0.5球 平均值    >=80%
                // 两队进球能力不错，本场比赛大概率会有进球。
                // *超过0.5球 平均值   小于80%且大于55%
                // 两队进球能力一般，本场比赛有概率进球。
                // *超过0.5球 平均值   <=55%
                // 两队进球能力较弱，本场比赛有可能不进球。
                let mean = (parseInt(hostResult.value.zhibiao1_result[0]) + parseInt(awayResult.value.zhibiao1_result[0]))/2;
                if(mean >= 80){
                    showResult.value.zhibiao1_txt = '两队进球能力不错，本场比赛大概率会有进球。';
                }else if(mean >= 55 && mean < 80){
                    showResult.value.zhibiao1_txt = '两队进球能力不错，本场比赛大概率会有进球。';
                }else{
                    showResult.value.zhibiao1_txt = '两队进球能力较弱，本场比赛有可能不进球。';
                }

                //指标2
                // 1*未进球项 2队双方 概率都大于35%
                // 本场比赛 很有可能0:0 无进球收场。

                // 2*未进球项  只有一方 概率大于35%
                // 本场比赛XX对 很有可能无法取得进球。

                // 3*未进球项 双方 概率都小于等于35%
                // 本场比赛有进球,看谁先进球，拭目以待！
                let host_goal_0_rate = parseInt(hostResult.value.zhibiao2_result[4]);
                let away_goal_0_rate = parseInt(awayResult.value.zhibiao2_result[4]);
                if(host_goal_0_rate > 35 && away_goal_0_rate>35){
                    showResult.value.zhibiao2_txt = '本场比赛 很有可能0:0 无进球收场。';
                }else if ((host_goal_0_rate > 35 && away_goal_0_rate <= 35) || (away_goal_0_rate > 35 && host_goal_0_rate <= 35)) {
                    showResult.value.zhibiao2_txt = `本场比赛${host_goal_0_rate > 35 ? `主队${game_info.value.host_name}` : `客队${game_info.value.away_name}`} 很有可能无法取得进球。`;
                }else if (host_goal_0_rate <= 35 && away_goal_0_rate <= 35) {
                    showResult.value.zhibiao2_txt = '本场比赛有进球,看谁先进球，拭目以待！';
                }

                //指标3
                // 1*两队场均进球（上半场) 之和 大于1  
                // 本场比赛 上半场进球概率较高！
                // 2*两队场均进球（上半场) 之和 >0.5且 <=1
                // 本场比赛 上半场有较大几率进球！
                // 3*两队场均进球（上半场) 之和 <=0.5
                // 本场比赛 上半场有较大概率平局收场！
                let mean_sum = hostResult.value.zhibiao3_result[0] + awayResult.value.zhibiao3_result[0];
                if(mean_sum > 1){
                    showResult.value.zhibiao3_txt = '本场比赛 上半场进球概率较高！';
                }else if (mean_sum > 0.5 && mean_sum <= 1) {
                    showResult.value.zhibiao3_txt = '本场比赛 上半场有较大几率进球！';
                } else if (mean_sum <= 0.5) {
                    showResult.value.zhibiao3_txt = '本场比赛 上半场有较大概率平局收场！';
                }

                //指标4
                // 判断 满足指定条件 显示 0封对手 
                // 超过1.5球项  2队概率之差大于30% 
                // 不丢球项       2队概率之差>=0%

                // 否则不显示
                let loss_goal_15_rate = hostResult.value.zhibiao4_result[1] - awayResult.value.zhibiao4_result[1];
                let loss_goal_0_rate = hostResult.value.zhibiao4_result[4] - awayResult.value.zhibiao4_result[4];
                if(loss_goal_15_rate > 30 && loss_goal_0_rate >= 0){
                    showResult.value.zhibiao4_txt = `本场比赛 ${game_info.value.host_name}有可能0封对手`
                }else if(loss_goal_15_rate <= 30 && loss_goal_0_rate <0){
                    showResult.value.zhibiao4_txt = `本场比赛 ${game_info.value.away_name}有可能0封对手`
                }
            }
        }
        
        return {
            game_info,
            loading,
            limit_count,
            zhibiao1_array,
            zhibiao2_array,
            zhibiao3_array,
            zhibiao4_array,
            hostResult,
            awayResult,
            showResult,
            onRefresh,
        };
    }
  });
</script>

<style lang="scss" scoped>
@use '@/common/style/common' as *;
.goal{
    margin:10px;
    position: relative;
    section{
        background: #fff;
        margin-bottom: 10px;
        padding:10px;
        @include border-radius(5px);
        .title{
            @include sc(15px,#333);
            height: 40px;
        }
        .txt{
            color:#FF2121;
            line-height: 30px;
            padding-top: 10px;
            text-align: center;
        }
        table{
            @include sc(14px,#777);
            tr{
                line-height: 30px;
            }
            th{
                background-color: #f4f4f4;
            }
            th,td{
                border:solid 1px #f5f5f5;
            }
            tbody td:first-child{
                color:#333;
                font-weight: 500;
            }
        }
    }
    
}

</style>