<script setup>
defineProps({
    cvs: {
        type: Object,
        required: true,
    }
});

function copySlug(slug) {
    const input = document.createElement('input');
    input.value = slug;
    input.select();
    input.setSelectionRange(0, 99999);

    navigator
        .clipboard
        .writeText(input.value)
        .then((value, reason) => { });
}
</script>

<template>
    <InertiaHead title="Lista CV" />
    <div class="p-4">
        <header class="flex justify-between items-center pb-4">
            <div class="flex items-center gap-2">
                <InertiaLink
                    as="button"
                    href="/dashboard"
                    class="px-2 text-xl text-gray-700 hover:text-black"
                    title="Wróc do dashboard"><FontAwesomeIcon :icon="['fas', 'caret-left']" /></InertiaLink>
                <h1 class="text-3xl font-roboto font-light">Lista CV</h1>
            </div>
            <InertiaLink
                as="button"
                href="/dashboard/cv/create"
                class="bg-blue-400 hover:bg-blue-500 text-white px-2.5 py-1 rounded-full"
                title="Dodaj nowe dane dla CV"
                ><FontAwesomeIcon :icon="['fas', 'plus']" /></InertiaLink>
        </header>
        <div class="overflow-x-auto">
            <table v-if="cvs.data.length" class="w-full min-w-[600px] border-separate border-spacing-y-2 cursor-pointer">
                <colgroup>
                    <col class="w-min" />
                </colgroup>
                <thead class="text-left bg-gray-100">
                    <th class="p-2 text-center">ID</th>
                    <th class="p-2">Token</th>
                    <th class="w-[100px] p-2 whitespace-nowrap">Firma</th>
                    <th class="hidden md:table-cell p-2">Lokalizacje</th>
                    <th class="p-2">Telefon</th>
                    <th class="p-2"></th>
                </thead>
                <tbody>
                    <InertiaLink
                        as="tr"
                        v-for="(cv, key) in cvs.data"
                        :key="key"
                        class="px-3 py-2 bg-white hover:bg-neutral-200 rounded-md z-10"
                        :href="`/dashboard/cv/${cv.token}`">
                        <td class="p-2 text-center">#{{ cv.id }}</td>
                        <td class="p-2"
                            ><button
                                class="bg-gray-50 p-1 rounded-md hover:bg-gray-100"
                                @click.prevent="copySlug(cv.token)"
                                :title="cv.token">{{ cv.token.slice(0, 7) }}</button></td>
                        <td class="max-w-[150px] md:max-w-[250px] p-2 whitespace-nowrap overflow-hidden overflow-ellipsis" :title="cv.recipient">{{ cv.recipient }}</td>
                        <td class="hidden md:table-cell p-2">{{ cv.locations.join(' / ') }}</td>
                        <td class="p-2">{{ cv.phone.formattedNumber }}</td>
                        <td class="flex items-center justify-center gap-2 p-3 z-50">
                            <InertiaLink
                                as="button"
                                class="px-3 py-3 text-lime-600 hover:text-lime-800 border-t-2 border-b-2 border-transparent hover:border-b-lime-600"
                                :href="`/dashboard/cv/${cv.token}/edit`"
                                title="Edytuj projekt"><FontAwesomeIcon :icon="['fas', 'pen-to-square']" /></InertiaLink>
                            <InertiaLink
                                as="button"
                                class="px-3 py-3 text-red-600 hover:text-red-800"
                                :href="`/dashboard/cv/${cv.token}/delete`"
                                title="Usuń projekt z listy"><FontAwesomeIcon :icon="['fas', 'trash']" /></InertiaLink>
                        </td>
                    </InertiaLink>
                </tbody>
            </table>
            <div v-else>
                Pusta lista
            </div>
        </div>
    </div>
</template>
