<template>
    <div class="popup" @click="$emit('closePopup')">
        <div class="Order" @click.stop v-show="!showNewTimes">
            <div class="center">
                {{ order.center_name }}
            </div>
            <div class="user">
                {{ order.user.firstname }} {{ order.user.lastname }}
            </div>
            <div class="date">
                {{ formatDate(order.startDate) }}
            </div>
            <div class="time">
                {{ formatTime(order.startDate) }} -
                {{ formatTime(order.endDate) }}
            </div>
            <div class="price">
                {{ formatPrice(order.price) }}
            </div>
            <div class="buttons">
                <div class="moveButton" @click="moveOrder()" v-if="showButtons">
                    Flyt tid
                </div>
                <div class="cancelButton" @click="cancelOrder(order.id)" v-if="showButtons">
                    Aflys
                </div>
            </div>
            <div class="closeButton" @click="$emit('closePopup')">x</div>
        </div>
        <TrainerNewTime :order="order" @closePopup="$emit('closePopup')" @refreshList="$emit('refreshList')" v-if="showNewTimes"></TrainerNewTime>
    </div>
</template>

<script lang="ts">
import TrainerNewTime from '@/components/TrainerNewTime.vue'
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";
import type { TrainerOrder as Order } from '@/interfaces/order';

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

export default {
    name: "UserOrderPopup",
    emits: ["closePopup", "refreshList"],
    components: {
        TrainerNewTime
    },
    props: {
        order: {
            type: Object as PropType<Order>,
            required: true
        }
    },
    data() {
        return {
            showNewTimes: false
        };
    },
    computed: {
        showButtons(): boolean {
            return this.order.status !== 'CancelledByTrainer' && this.order.status !== 'CancelledByUser' && (new Date(this.order.startDate.split("Z")[0])).getTime() > (new Date()).getTime();
        }
    },
    methods: {
        moveOrder() {
            this.showNewTimes = true;
        },
        async cancelOrder(id: number) {
            if (id === undefined) return;
            const res = await fetch(`${import.meta.env.VITE_BASE_API_URL}/trainer/order/${id}/cancel`, {
                credentials: import.meta.env.DEV ? "include" : undefined,
                method: "POST"
            });
            if (res.status === 401) {
                this.$router.push({ path: "/login", query: { ref: this.$route.path } });
            }
            else if (res.status === 204) {
                this.$emit('refreshList');
                this.$emit('closePopup');
            }
        },
        formatDate(date: string): string {
            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: string): string {
            let output: string = dayjs(date).utcOffset(0).locale("da").format("HH:mm");

            return output;
        },
        formatPrice(price: number): string {
            const DanishKrone = new Intl.NumberFormat('dk', {
                style: 'currency',
                currency: 'DKK',
            });

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

<style scoped>
.popup {
    position: fixed;
    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;
}

.center {
    font-size: 1.8em;
}

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

.date {
    font-size: 1em;
}

.time {
    font-size: 1em;
}

.price {
    font-size: 1.25em;
}

.buttons {
    align-self: flex-end;
    display: flex;
    gap: 20px;
}

.cancelButton {
    border: 1px solid #cbd5e1;
    border-radius: 0.5rem;
    padding: 10px;
    cursor: pointer;
    font-size: 1.25em;
}

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

.moveButton {
    border: 1px solid #cbd5e1;
    border-radius: 0.5rem;
    padding: 10px;
    cursor: pointer;
    font-size: 1.25em;
}

.moveButton: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>