<template>
    <div class="popup" @click="$emit('closePopup')">
        <div class="Order" @click.stop>
            <div class="trainer">
                {{ trainer?.first_name }} {{ trainer?.last_name }}
            </div>
            <div class="center">
                {{ trainer?.center_name }}
            </div>
            <div class="date">
                {{ formatDate(timeslot?.startDate) }}
            </div>
            <div class="time">
                {{ formatTime(timeslot?.startDate) }} -
                {{ formatTime(timeslot?.endDate) }}
            </div>
            <div class="price">
                {{ formatPrice(timeslot?.price) }}
            </div>
            <div class="orderButton" @click="order">
                Bestil
            </div>
            <div class="closeButton" @click="$emit('closePopup')">x</div>
        </div>
    </div>
</template>

<script lang="ts">
import type { Timeslot } from '@/interfaces/timeslot';
import type { Trainer } from '@/interfaces/trainer';
import type { PropType } from 'vue';
import dayjs from 'dayjs';
import LocalizedFormat from "dayjs/plugin/localizedFormat"
import utc from "dayjs/plugin/utc"
import { } from "dayjs/locale/da";

dayjs.extend(LocalizedFormat);
dayjs.extend(utc);

export default {
    name: "OrderPopup",
    emits: ["closePopup"],
    props: {
        trainer: Object as PropType<Trainer>,
        timeslot: Object as PropType<Timeslot>
    },
    methods: {
        async order() {
            const res = await fetch(`${import.meta.env.VITE_BASE_API_URL}/order`, {
                credentials: import.meta.env.DEV ? "include" : undefined,
                method: "POST",
                body: JSON.stringify({ trainer: this.trainer?.id, startDate: this.timeslot?.startDate, endDate: this.timeslot?.endDate }),
                headers: {
                    "Content-Type": "Application/json"
                }
            });
            if (res.status === 401) {
                this.$router.push({ path: "/login", query: { ref: this.$route.path } });
            }
            else if (res.status === 200) {
                const data = await res.json();
                window.location.href = data.url;
            }
        },
        formatDate(date: Date | undefined): string {
            if (date === undefined) return "";
            let output: string = dayjs(date).locale("da").format("dddd D. MMM YYYY");
            let outputStringArray = output.split("");
            outputStringArray[0] = outputStringArray[0].toLocaleUpperCase();
            output = outputStringArray.join("");

            return output;
        },
        formatTime(date: Date | undefined): string {
            if (date === undefined) return "";
            let output: string = dayjs(date).utcOffset(0).locale("da").format("HH:mm");

            return output;
        },
        formatPrice(price: number | undefined): string {
            if (price === undefined) return "";

            const DanishKrone = new Intl.NumberFormat('dk', {
                style: 'currency',
                currency: 'DKK',
            });

            return DanishKrone.format(price / 100);
        }
    }
}
</script>

<style scoped>
.popup {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #000a;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10;
}

.Order {
    background-color: white;
    border-radius: 0.5rem;
    padding: 30px;
    display: flex;
    flex-direction: column;
    min-width: 50%;
    position: relative;
}

.trainer {
    font-size: 1.8em;
}

.center {
    opacity: 0.8;
    font-size: 1.25em;
}

.price {
    margin-top: 2em;
    font-size: 1.5em;
}

.orderButton {
    align-self: flex-end;
    border: 1px solid #cbd5e1;
    border-radius: 0.5rem;
    padding: 10px;
    cursor: pointer;
    font-size: 1.25em;
}

.orderButton:hover {
    text-decoration: underline;
    border-color: black;
}

.closeButton {
    position: absolute;
    top: 10px;
    right: 20px;
    font-size: 2em;
    cursor: pointer;
}

.closeButton:hover {
    text-decoration: underline;
}
</style>