<script setup>
import BasicTable from '@/Components/BasicTable.vue';
import SectionTitle from '@/Components/SectionTitle.vue';
import AppLayout from '@/Layouts/AppLayout.vue';
import { LinkOptions as C_LINK, TableColumn as C_TD, TableRow as C_TR} from '@/Shared/AppObjects';

const props = defineProps({
    chart: Object,
    people: Array,
    terrain: Array
});


console.log(props.terrain)


const tablePersonHeader = [
    C_TD.make('Št.', 'header'),
    C_TD.make('Naziv', 'header'),
    C_TD.make('Skupina', 'header')
];

const tblTerrainHead = [
    C_TD.make('Št.', 'header'),
    C_TD.make('Naziv', 'header'),
    C_TD.make('Začetek', 'header')
];

let tablePersonBody = [];
let tblTerrainBody = [];
 
const getRoute = (person) => {
    if( person.client ){
        return {route: 'client.show', options: person.client};
    }

    return {route: 'clientCase.show', options: person};
}

props.people.forEach((p) => {
    const forLink = getRoute(p);

    const cols = [
        C_TD.make(Number(p.nu), 'body', {}, C_LINK.make(forLink.route, forLink.options, `font-bold hover:text-${p.group.color_tag}`)),
        C_TD.make(p.full_name, 'body'),
        C_TD.make(p.group.added_segment, 'body')
    ];

    tablePersonBody.push(C_TR.make(cols, {class: `border-l-4 border-${p.group.color_tag}`}))
});

props.terrain.forEach((t) => {
    const forLink = getRoute(t);
    const startDate = new Date(t.added_segment).toLocaleDateString('de');

    const cols = [
        C_TD.make(t.person.nu, 'body', {}, C_LINK.make(forLink.route, forLink.options, `font-bold hover:text-red-400`)),
        C_TD.make(t.person.full_name, 'body'),
        C_TD.make(startDate, 'body')
    ];

    tblTerrainBody.push(C_TR.make(cols, {class: `border-l-4 border-red-400`}));
});

</script>

<template>
    <AppLayout title="Dashboard">
        <template #header>
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                Nadzorna plošča
            </h2>
        </template>
        <div class="pt-12 hidden md:block">
            <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
                <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg p-2">
                    <apexchart :width="chart.width" :height="chart.height" :type="chart.type" :options="chart.options" :series="chart.series"></apexchart>
                </div>
            </div>
        </div>
        <div class="py-12">
            <div class="max-w-7xl mx-auto sm:px-6 lg:px-8 grid md:grid-cols-2 gap-6">
                <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
                    <SectionTitle class="p-4">
                        <template #title>
                            Teren
                        </template>
                        <template #description>
                            Seznam primerov za terensko delo  
                        </template>
                    </SectionTitle>
                    <BasicTable :header="tblTerrainHead" :body="tblTerrainBody"></BasicTable>
                </div>
                <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
                    <SectionTitle class="p-4">
                        <template #title>
                            Na novo dodano
                        </template>
                        <template #description>
                            Seznam novih naročnikov (modra) / primerov (rdeča)
                        </template>
                    </SectionTitle>
                    <BasicTable :header="tablePersonHeader" :body="tablePersonBody"></BasicTable>
                </div>
            </div>
        </div>
    </AppLayout>
</template>
