<template>
  <div v-if="pitchingStats.length" class="row">
    <div class="col-sm-12">
      <h3>Team Pitching {{ isRegularSeason ? '' : ' - Postseason' }}</h3>
      <div class="table-responsive-xl">
        <table class="table table-sm table-striped" id="table-pitching-stats">
          <thead class="thead-dark">
            <tr>
              <th>Player</th>
              <th>W</th>
              <th>L</th>
              <th>W-L%</th>
              <th>ERA</th>
              <th>G</th>
              <th>GS</th>
              <th>SV</th>
              <th>HD</th>
              <th>BSV</th>
              <th>IP</th>
              <th>H</th>
              <th>R</th>
              <th>ER</th>
              <th>HR</th>
              <th>BB</th>
              <th>SO</th>
              <th>HBP</th>
              <th>BK</th>
              <th>WP</th>
              <th>IR</th>
              <th>IRS</th>
              <th>WHIP</th>
              <th>H/9</th>
              <th>HR/9</th>
              <th>BB/9</th>
              <th>SO/9</th>
              <th>SO/BB</th>
              <!-- <th>Last G</th>
              <th>Last G-2</th> -->
            </tr>
          </thead>
          <tbody id="team-pitching-stats">
            <tr v-for="stat in pitchingStats">
              <td>
                <RouterLink
                  :to="{ name: 'player', params: { seasonNumber: seasonNumber, playerName: stat.player.name } }">
                  {{ stat.player.name }}
                </RouterLink>
              </td>
              <td>{{ stat.win }}</td>
              <td>{{ stat.loss }}</td>
              <td>{{ winPercentage(stat) }}</td>
              <td>{{ stat.era.toFixed(2) }}</td>
              <td>{{ stat.games }}</td>
              <td>{{ stat.gs }}</td>
              <td>{{ stat.save }}</td>
              <td>{{ stat.hold }}</td>
              <td>{{ stat.bsave }}</td>
              <td>{{ outsToInnings(stat) }}</td>
              <td>{{ stat.hits }}</td>
              <td>{{ stat.run }}</td>
              <td>{{ stat.e_run }}</td>
              <td>{{ stat.hr }}</td>
              <td>{{ stat.bb }}</td>
              <td>{{ stat.so }}</td>
              <td>{{ stat.hbp }}</td>
              <td>{{ stat.balk }}</td>
              <td>{{ stat.wp }}</td>
              <td>{{ stat.ir }}</td>
              <td>{{ stat.ir_sc }}</td>
              <td>{{ stat.whip.toFixed(2) }}</td>
              <td>{{ hitsPer9(stat) }}</td>
              <td>{{ hrsPer9(stat) }}</td>
              <td>{{ stat.bbPer9.toFixed(1) }}</td>
              <td>{{ stat.kPer9.toFixed(1) }}</td>
              <td>{{ stat.kPerBB.toFixed(1) }}</td>
            </tr>
          </tbody>
          <tfoot>
            <tr v-if="totalPitchingStat">
              <th>Total</th>
              <th>{{ totalPitchingStat.win }}</th>
              <th>{{ totalPitchingStat.loss }}</th>
              <th>{{ winPercentage(totalPitchingStat) }}</th>
              <th>{{ totalPitchingStat.era.toFixed(2) }}</th>
              <th>{{ totalPitchingStat.games }}</th>
              <th>{{ totalPitchingStat.gs }}</th>
              <th>{{ totalPitchingStat.save }}</th>
              <th>{{ totalPitchingStat.hold }}</th>
              <th>{{ totalPitchingStat.bsave }}</th>
              <th>{{ outsToInnings(totalPitchingStat) }}</th>
              <th>{{ totalPitchingStat.hits }}</th>
              <th>{{ totalPitchingStat.run }}</th>
              <th>{{ totalPitchingStat.e_run }}</th>
              <th>{{ totalPitchingStat.hr }}</th>
              <th>{{ totalPitchingStat.bb }}</th>
              <th>{{ totalPitchingStat.so }}</th>
              <th>{{ totalPitchingStat.hbp }}</th>
              <th>{{ totalPitchingStat.balk }}</th>
              <th>{{ totalPitchingStat.wp }}</th>
              <th>{{ totalPitchingStat.ir }}</th>
              <th>{{ totalPitchingStat.ir_sc }}</th>
              <th>{{ totalPitchingStat.whip.toFixed(2) }}</th>
              <th>{{ hitsPer9(totalPitchingStat) }}</th>
              <th>{{ hrsPer9(totalPitchingStat) }}</th>
              <th>{{ totalPitchingStat.bbPer9.toFixed(1) }}</th>
              <th>{{ totalPitchingStat.kPer9.toFixed(1) }}</th>
              <th>{{ totalPitchingStat.kPerBB.toFixed(1) }}</th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { aggregatePitchingStats, fetchPitchingStatsBySeasonAndTeamId, type PitchingStat } from '@/services/pitchingStatsService'
import { outsToInnings, winPercentage, hitsPer9, hrsPer9 } from '@/services/utilities'

export default {
  name: "TeamPitchingTable",
  props: {
    seasonNumber: { type: Number, required: true },
    teamId: { type: Number, required: true },
    isRegularSeason: { type: Boolean, required: true }
  },
  data() {
    return {
      pitchingStats: [] as PitchingStat[]
    }
  },
  computed: {
    totalPitchingStat(): PitchingStat | undefined {
      if (this.pitchingStats.length > 0) {
        return aggregatePitchingStats(this.pitchingStats)
      }

      return undefined
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData(): Promise<void> {
      const unsortedPitchingStats: PitchingStat[] = await fetchPitchingStatsBySeasonAndTeamId(this.seasonNumber, this.teamId, this.isRegularSeason)
      this.pitchingStats = unsortedPitchingStats.sort((s1, s2) => s2.outs - s1.outs)
    },
    outsToInnings(stat: PitchingStat): string {
      return outsToInnings(stat)
    },
    winPercentage(stat: PitchingStat): string {
      return winPercentage(stat)
    },
    hitsPer9(stat: PitchingStat): string {
      return hitsPer9(stat)
    },
    hrsPer9(stat: PitchingStat): string {
      return hrsPer9(stat)
    }
  }
}
</script>
