<template>
  <ion-page>
    <ion-header :translucent="true">
      <ion-toolbar>
        <ion-title>Pitchers</ion-title>
      </ion-toolbar>
    </ion-header>

    <ion-content :fullscreen="true">
      <ion-header collapse="condense">
        <ion-toolbar>
          <ion-title size="large">Pitchers</ion-title>
        </ion-toolbar>
      </ion-header>

      <ion-list>
        <ion-item v-for="(pitcher, index) in pitchers" :key="index" button @click="goToPreparePitch(pitcher)">
          <ion-label>{{ pitcher.firstName }} {{ pitcher.lastName }}</ion-label>
        </ion-item>
      </ion-list>
    </ion-content>
  </ion-page>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { IonContent, IonHeader, IonPage, IonTitle, IonToolbar, IonList, IonItem, IonLabel } from '@ionic/vue';
import { pitcherService } from "@/services/PitcherService";
import Pitcher from "@/types/Pitcher";
import {bullpenSessionService} from "@/services/BullpenSessionService";

export default defineComponent({
  name: "PitcherList",
  components: {
    IonContent,
    IonHeader,
    IonToolbar,
    IonPage,
    IonTitle,
    IonList,
    IonItem,
    IonLabel,
  },
  setup() {
    // Define a reactive array to hold colors
    const pitchers = ref<Pitcher[]>([]);
    const router = useRouter();

    bullpenSessionService.clear();

    // Fetch colors from the service when the component is mounted
    onMounted(async () => {
      pitchers.value = await pitcherService.getAllPitchers();
    });

    const goToPreparePitch = (pitcher: Pitcher) => {
      bullpenSessionService.startSession( pitcher );
      router.push({ name: 'PreparePitch' });
    };

    return { pitchers, goToPreparePitch };
  },
});
</script>

<style scoped>
#container {
  text-align: center;
  
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
}

#container strong {
  font-size: 20px;
  line-height: 26px;
}

#container p {
  font-size: 16px;
  line-height: 22px;
  
  color: #8c8c8c;
  
  margin: 0;
}

#container a {
  text-decoration: none;
}
</style>
