<script lang="ts">
	import { api_host, authToken } from "$lib";
	import type { Appointment } from "$lib/dbdata";
	import { onMount } from "svelte";

    let appointmentData: Appointment[] = [];
    let ready: boolean = false;

    async function fetchAppointments() {
        try {
            const response = await fetch(`${api_host}/api/users/appointments`, {
                method: "GET",
                headers: {
                    "Authorization": "Bearer " + $authToken
                },
            });

            if (!response.ok) {
                throw new Error(`HTTP error! Status: ${response.status}`);
            }

            const data: Appointment[] = await response.json();
            appointmentData = data;
        } catch (error) {
            console.log('Error during fetch:', error);
        }
    }


    onMount(async () => {
       await fetchAppointments();
       
       ready = true;
    });

</script>

{#each appointmentData as data}
    <tr>
        <td>{data.title}</td>
        <td class="center-align">14/21</td>
        <td class="center-align">
            <button class="medium">
                <span>Sperren</span>
            </button>
        </td>
        <td class="center-align">
            <button class="medium secondary">
                <span>Bearbeiten</span>
            </button>
        </td>

        <td class="center-align">
            <button class="medium red">
                <span>Loeschen</span>
            </button>
        </td>
    </tr> 
{/each}