<script setup lang="ts" name="TeamEvaluate">
// $import
import { ref, computed, defineProps, onMounted } from "vue";
// $import

// $types
interface TeamEvaluateProps {
  gameInfo: any;
  historyData: any;
  useLength?: number;
}

interface TeamGameResult {
  name: string;
  win: number;
  flat: number;
  lose: number;
}

interface GameResultComp {
  winner: TeamGameResult;
  loser: TeamGameResult;
}
// $types

// $props
const props = withDefaults(defineProps<TeamEvaluateProps>(), {
  gameInfo: () => ({}),
  historyData: () => ({}),
  useLength: 6,
});
// $props

// $data
// 评分类型：主客，做主，做客
const scoreType = ref<"vs" | "home" | "away">("vs");
// $data

// $methods
const getScoreInfo = (team_id: string, list: []) => {
  let total_score = 0;
  let win = 0;
  let flat = 0;
  let lose = 0;
  list?.forEach((item: any) => {
    if (item?.home_team_id === team_id) {
      total_score += item?.home_scores?.[0] * 1;
      if (item?.home_scores?.[0] > item?.away_scores?.[0]) {
        win += 1;
      } else if (item?.home_scores?.[0] == item?.away_scores?.[0]) {
        flat += 1;
      } else {
        lose += 1;
      }
    } else if (item?.away_team_id === team_id) {
      total_score += item?.away_scores?.[0] * 1;
      if (item?.home_scores?.[0] > item?.away_scores?.[0]) {
        lose += 1;
      } else if (item?.home_scores?.[0] == item?.away_scores?.[0]) {
        flat += 1;
      } else {
        win += 1;
      }
    }
  });
  let avg_score = (total_score / props.useLength).toFixed(1);
  return {
    win,
    flat,
    lose,
    avg_score,
    total_score,
  };
};

// $computed
const teamScoreInfo = computed(() => {
  if (scoreType.value === "home") {
    let homeList = props.historyData?.home?.filter((item: any) => props.gameInfo.home_team_id === item.home_team_id)?.slice(0, props.useLength);
    let guestList = props.historyData?.away?.slice(0, props.useLength);
    const homeScoreInfo = getScoreInfo(
      props.gameInfo.home_team_id,
      homeList
    );
    const awayScoreInfo = getScoreInfo(props.gameInfo.away_team_id, guestList);
    const totalTeamScore =
      homeScoreInfo.total_score + awayScoreInfo.total_score;

    return {
      home: {
        ...homeScoreInfo,
        score_percentage: `${
          homeScoreInfo.total_score &&
          ((homeScoreInfo.total_score / totalTeamScore) * 100).toFixed(2)
        }%`,
      },
      guest: {
        ...awayScoreInfo,
        score_percentage: `${
          awayScoreInfo.total_score &&
          ((awayScoreInfo.total_score / totalTeamScore) * 100).toFixed(2)
        }%`,
      },
    };
  } else if (scoreType.value === "away") {
    let homeList = props.historyData?.home?.slice(0, props.useLength);
    let guestList = props.historyData?.away?.filter((item: any) => props.gameInfo.away_team_id == item.away_team_id)?.slice(0, props.useLength);
    const homeScoreInfo = getScoreInfo(props.gameInfo.home_team_id, homeList);
    const awayScoreInfo = getScoreInfo(
      props.gameInfo.away_team_id,
      guestList
    );
    const totalTeamScore =
      homeScoreInfo.total_score + awayScoreInfo.total_score;

    return {
      home: {
        ...homeScoreInfo,
        score_percentage: `${
          homeScoreInfo.total_score &&
          ((homeScoreInfo.total_score / totalTeamScore) * 100).toFixed(2)
        }%`,
      },
      guest: {
        ...awayScoreInfo,
        score_percentage: `${
          awayScoreInfo.total_score &&
          ((awayScoreInfo.total_score / totalTeamScore) * 100).toFixed(2)
        }%`,
      },
    };
  } else {
    let homeList = props.historyData?.home?.slice(0, props.useLength);
    let guestList = props.historyData?.away?.slice(0, props.useLength);
    const homeScoreInfo = getScoreInfo(props.gameInfo.home_team_id, homeList);
    const awayScoreInfo = getScoreInfo(props.gameInfo.away_team_id, guestList);
    const totalTeamScore =
      homeScoreInfo.total_score + awayScoreInfo.total_score;

    return {
      home: {
        ...homeScoreInfo,
        score_percentage: `${
          homeScoreInfo.total_score &&
          ((homeScoreInfo.total_score / totalTeamScore) * 100).toFixed(2)
        }%`,
      },
      guest: {
        ...awayScoreInfo,
        score_percentage: `${
          awayScoreInfo.total_score &&
          ((awayScoreInfo.total_score / totalTeamScore) * 100).toFixed(2)
        }%`,
      },
    };
  }
});

const gameResult = computed<GameResultComp>(() => {
  const homeTeam = {
    name: props.gameInfo?.host_name,
    win: teamScoreInfo.value.home.win,
    flat: teamScoreInfo.value.home.flat,
    lose: teamScoreInfo.value.home.lose,
  };
  const guestTeam = {
    name: props.gameInfo?.away_name,
    win: teamScoreInfo.value.guest.win,
    flat: teamScoreInfo.value.guest.flat,
    lose: teamScoreInfo.value.guest.lose,
  };
  return teamScoreInfo.value.home.win > teamScoreInfo.value.guest.win
    ? { winner: homeTeam, loser: guestTeam }
    : { winner: guestTeam, loser: homeTeam };
});
// $computed

// $methods

// $life
onMounted(() => {});
// $life
</script>

<template>
  <div class="team-evaluate-section">
    <div class="match-tab">
      <div class="tab-name">两队进球</div>
      <div class="tab-sel">
        <div class="around">
          <p
            :class="scoreType == 'vs' ? 'around-p active-p' : 'around-p'"
            @click="scoreType = 'vs'"
          >
            全部
          </p>
          <p
            :class="scoreType == 'home' ? 'around-p active-p' : 'around-p'"
            @click="scoreType = 'home'"
          >
            主场
          </p>
          <p
            :class="scoreType == 'away' ? 'around-p active-p' : 'around-p'"
            @click="scoreType = 'away'"
          >
            客场
          </p>
        </div>
      </div>
    </div>

    <div class="match_team">
      <div class="match_team_container">
        <div
          class="team_logo"
          :style="`background-image: url(${props.gameInfo?.host_photo});`"
        ></div>
        <div class="text-info">
          <p class="team_name">{{ props.gameInfo?.host_name }}</p>
        </div>
      </div>
      <div class="match_team_container">
        <div class="text-info">
          <p class="team_name">{{ props.gameInfo?.away_name }}</p>
        </div>
        <div
          class="team_logo mat-left"
          :style="`background-image: url(${props.gameInfo?.away_photo});`"
        ></div>
      </div>
    </div>

    <van-skeleton title :row="3" :loading="false">
      <div class="box_1">
        <div class="row_1">
          <div>
            <div>场均进球</div>
            <div class="fen-text">
              <span>{{ teamScoreInfo?.home?.avg_score || 0 }}</span>
            </div>
          </div>
          <div class="col_1">
            <div class="">
              <span>{{ teamScoreInfo?.home?.win || 0 }}胜</span>
              <div class="color-box red"></div>
            </div>
            <div>
              <span>{{ teamScoreInfo?.home?.flat || 0 }}平</span>
              <div class="color-box blue"></div>
            </div>
            <div class="">
              <span>{{ teamScoreInfo?.home?.lose || 0 }}负</span>
              <div class="color-box green"></div>
            </div>
          </div>
        </div>
        <div class="row_1">
          <div>
            <div>场均进球</div>
            <div class="fen-text">
              <span>{{ teamScoreInfo?.guest?.avg_score || 0 }}</span>
            </div>
          </div>
          <div class="col_1">
            <div class="">
              <span>{{ teamScoreInfo?.guest?.win || 0 }}胜</span>
              <div class="color-box red"></div>
            </div>
            <div>
              <span>{{ teamScoreInfo?.guest?.flat || 0 }}平</span>
              <div class="color-box blue"></div>
            </div>
            <div class="">
              <span>{{ teamScoreInfo?.guest?.lose || 0 }}负</span>
              <div class="color-box green"></div>
            </div>
          </div>
        </div>
      </div>
      <div class="score-rang">
        <div
          :style="{ width: teamScoreInfo?.home?.score_percentage || '0%' }"
          class="host"
        >
          {{ teamScoreInfo?.home?.score_percentage || '0%' }}
        </div>
        <div
          :style="{ width: teamScoreInfo?.guest?.score_percentage || '0%' }"
          class="guest"
        >
          {{ teamScoreInfo?.guest?.score_percentage || '0%' }}
        </div>
      </div>
      <div class="text-content">
        {{ props.gameInfo?.host_name }}近{{ props.useLength }}场各项赛事攻进{{
          teamScoreInfo?.home?.total_score || 0
        }}球，场均进球{{ teamScoreInfo?.home?.avg_score || 0 }}球，{{
          props?.gameInfo?.away_name
        }}近{{ props.useLength }}场各项赛事攻进{{ teamScoreInfo.guest?.total_score || 0 }}球，场均进球{{
          teamScoreInfo?.guest?.avg_score || 0
        }}球。全部赛事{{ gameResult?.winner?.name
        }}<span class="highlight-text">{{ gameResult?.winner?.win || 0 }}胜{{ gameResult?.winner?.flat || 0 }}平{{ gameResult?.winner?.lose || 0 }}负</span
        >，{{ gameResult?.loser?.name
        }}<span class="highlight-text">{{ gameResult?.loser?.win || 0 }}胜{{ gameResult?.loser?.flat || 0 }}平{{ gameResult?.loser?.lose || 0 }}负</span
        >。
      </div>
    </van-skeleton>
  </div>
</template>

<style lang="scss">
@use '@/common/style/common' as *;
.team-evaluate-section {
  background-color: #fff;
  padding: 0 10px;

  .match_team {
    width: 100%;
    padding: 10px 0 !important;
    display: -webkit-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    -ms-flex-align: center;

    .match_team_container {
      display: flex;
      .team_logo {
        width: 26px;
        height: 26px;
        background-repeat: no-repeat;
        background-size: contain;
        background-position: 50%;
      }
      .team_name {
        align-items: flex-start;
        margin: 0 10px;
        font-size: 16px;
        color: #000;
      }
    }
    .content-title {
      font-size: 16px;
      font-weight: 700;
    }
  }

  .match-tab {
    width: 100%;
    padding: 10px 0;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    align-items: center;
    justify-content: space-between;

    // border-bottom: 1px solid #f0f0f0;

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

    .sub-heading {
      color: #b3b3b3;
      font-size: 12px;
    }

    .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;
        @include border-radius(15px);
        overflow: hidden;

        background-color: #f7f7f7;
        font-family: -apple-system;
      }

      .around-p {
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #777777;
        width: 64px;
        height: 28px;
        line-height: 17px;
        margin: 2px 3px;
        font-family: -apple-system;
        @include border-radius(25px);

        border: 1px solid rgba(245, 245, 245, 1);
      }

      .active-p {
        background: #fff;
        color: #4f96fa;
        box-shadow: 0px 1 4px 0px rgba(34, 122, 255, 0.3);
        overflow: hidden;
      }
    }
  }

  .box_1 {
    padding-bottom: 10px;
    display: flex;
    justify-content: space-between;
  }

  .box_1 .row_1 {
    width: 46.4%;
    height: 90px;
    border: #ededed 1px solid;
    border-radius: 8px;
    text-align: center;
  }

  .box_1 .row_1 {
    display: flex;
  }

  .box_1 .row_1 > div {
    flex: 1;
    display: flex;
    justify-content: center;
    flex-direction: column;
  }

  .box_1 .row_1 .col_1 {
    font-size: 12px;
  }

  .box_1 .fen-text span {
    font-family: yiqu;
    font-size: 28px;
  }

  .box_1 .row_1 .col_1 > span {
    font-size: 1.6rem;
  }

  .box_1 .col_1 > div {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 2px 10px;
  }

  .box_1 .col_1 div span:last-child {
    width: 8px;
    height: 8px;
  }

  .box_1 .col_1 div span {
    flex: 1;
  }

  .box_1 .col_1 .color-box {
    width: 8px;
    height: 8px;
    border-radius: 2px;
    animation: myfirst 0.5s;
    -webkit-animation: myfirst 0.5s;
    /* Safari and Chrome */
    -webkit-animation-name: fadeinR;
    -moz-animation-name: fadeinR;
    -ms-animation-name: fadeinR;
    animation-name: fadeinR;

    &.red {
      background: #ff3333;
    }
    &.green {
      background: #00c423;
    }
    &.blue {
      background: #1f78ff;
    }
  }

  /* 胶囊组件 */
  .select-capsule {
    display: flex;
    height: 30px;
    line-height: 30px;
    border-radius: 25px;
    overflow: hidden;
    margin: 0 4px;
    border: 1px solid #f2f2f2;

    .select-item {
      width: 45px;
      height: 100%;
      color: #b3b3b3;
      text-align: center;
      cursor: pointer;

      &.active {
        color: #fff;
      }
    }
  }

  .active {
    color: #ffffff;
    background-color: #ff7676;
  }

  .team-info {
    padding: 10px 0;
    display: flex;
    justify-content: space-between;

    text-align: center;

    .info-left {
      display: flex;
      align-items: center;
      .info-logo {
        width: 36px;
        height: 36px;
        background-repeat: no-repeat;
        background-size: contain;
        background-position: 50%;
      }

      .title {
        font-size: 16px;
        font-weight: 700;
      }
      .info-content {
        padding-left: 10px;
      }
    }

    .info-right {
      display: flex;
      align-items: center;

      div {
        padding: 0 10px;
      }

      span {
        font-size: 16px;
        font-weight: 600;
        color: #ff7676;
      }

      p {
        font-size: 12px;
        color: #b3b3b3;
      }
    }
  }

  .team-tools {
    padding: 10px 0;
    position: relative;

    .van-checkbox-group--horizontal {
      justify-content: space-evenly;
    }
    .van-checkbox__icon .van-icon {
      border-radius: 4px;
    }

    --van-checkbox-label-color: #999999;

    .van-checkbox__icon--checked + .van-checkbox__label {
      --van-checkbox-label-color: #ff7676;
    }
  }

  .score-rang {
    width: 100%;
    display: flex;
    height: 18px;
    color: #fff;
    font-size: 12px;
    border-radius: 9px;
    text-align: center;
    overflow: hidden;
    background-color: #fff;

    .host {
      height: 100%;
      background-color: #fc4f54;
      border-right: 1px solid #fff;
    }

    .guest {
      height: 100%;
      background-color: #1dc32f;
      border-left: 1px solid #fff;
    }
  }

  .text-content {
    padding: 10px 5px;
    padding-bottom: 15px;
    font-size: 14px;
    color: #7b7b7b;

    text-align: justify;
    word-break: break-all;
  }
}
</style>
