<script setup lang="ts">
import AppLayout from '@/layouts/AppLayout.vue';
import { type BreadcrumbItem } from '@/types';
import { Head } from '@inertiajs/vue3';
import { Resume, ResumeComponentPlacement } from '@/types/resume';
import ResumeEditPanel from '@/components/resume/ResumeEditPanel.vue';
import ResumePreviewPanel from '@/components/resume/ResumePreviewPanel.vue';
import { computed, ref, watch } from 'vue';

const props = defineProps<{
    resume: Resume
}>();

const localResume = ref({ ...props.resume });

const resumeTitle = computed<string>(() => (localResume.value.name == '' ? 'Sans titre' : localResume.value.name) ?? 'Sans titre');

const selectedComponent = ref<ResumeComponentPlacement | null>(null);

const breadcrumbs: BreadcrumbItem[] = [
    {
        title: resumeTitle.value,
        href: '/resumes/edit',
    },
];

function changeSelectedComponent(newComponent: ResumeComponentPlacement) {
    selectedComponent.value = newComponent;
    // Update the resume
    localResume.value.components_placements! = localResume.value.components_placements!.map(component =>
        component.id === newComponent.id ? newComponent : component
    );
}

function changeResumeTitle(newTitle: string) {
    console.log('Changing resume title to ', newTitle);
    localResume.value.name = newTitle;
}

console.debug('Resume : ', localResume.value);

</script>

<template>
    <Head :title="resumeTitle" />

    <AppLayout :breadcrumbs="breadcrumbs">
        <div class="flex h-full flex-1 gap-4 rounded-xl p-4 overflow-x-auto">
            <ResumeEditPanel :resume="localResume" :selected-component="selectedComponent" @selected-component-change="changeSelectedComponent" />
            <ResumePreviewPanel :resume="localResume" :selected-component="selectedComponent" @selected-component-change="changeSelectedComponent" @update:resume-title="changeResumeTitle" />
        </div>
    </AppLayout>
</template>

<style lang="css" scoped>
    .v-enter-active,
    .v-leave-active {
    transition: opacity 0.3s ease;
    }

    .v-enter-from,
    .v-leave-to {
    opacity: 0;
    }
</style>
