<template>
    <div>
    <h1>Available Scenarios</h1>
    </div>
    <div >
        <DataView :value="scenario_store.filteredScenarios" :layout="layout" paginator :rows="8">
        <template #header>
            <div class="header-container">
                <div class="search-bar">
                    <i class="pi pi-search search-icon"></i>           
                    <InputText
                        class="search-input"
                        type="search"
                        placeholder="Search"
                        v-model="scenario_store.filterString"
                        size="medium"
                        variant="filled"
                        style="border: 1px solid #a100ff"
                    />
                </div>
    
    <div class="card flex justify-center">
        <SelectButton v-model="scenario_store.typeFilter" :options="scenarioTypeOp" optionLabel="name" />
    </div>

                <SelectButton v-model="layout" :options="options" :allowEmpty="false" class="layout-switch">
                    <template #option="{ option }">
                        <i :class="[option === 'list' ? 'pi pi-bars' : 'pi pi-table']" />
                    </template>
                </SelectButton>
            </div>
        </template>

        <template #list="slotProps">
            <div class="flex flex-col space-y-4 mt-2">
                <div v-for="(item, index) in slotProps.items" :key="index">
                    <div class="flex flex-col sm:flex-row sm:items-center p-6 gap-4 bg-white dark:bg-gray-800 rounded-lg shadow-md"
                        :class="{ 'border-t border-gray-200 dark:border-gray-700': index !== 0 }">
                            <div class="flex flex-col flex-grow">
                                <h3 class="text-lg font-medium text-gray-900 dark:text-gray-100">{{ item.name }}</h3>
                                <p class="text-sm font-medium text-gray-500 dark:text-gray-400 mt-2">{{ item.description }}</p>
                            </div>
                            <div class="mt-auto flex justify-end">

                                <Button @click="executeScenario(item.id)" label="Load" class="flex-auto md:flex-initial text-white">
                                    <ChevronRightIcon class="w-5 h-10 text-white transition-transform transform hover:translate-x-1"/>
                                </Button>
                            </div>
                    </div>
                </div>
            </div>
        </template>

        <template #grid="slotProps">
            <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-4 gap-4 mt-2">
                <div v-for="(item, index) in slotProps.items" :key="index" class="p-2">
                    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md flex flex-col h-full">
                        <div class="p-4 flex flex-col flex-grow">
                            <h3 class="text-lg font-medium text-gray-900 dark:text-gray-100">{{ item.name }}</h3>
                            <p class="text-sm font-medium text-gray-500 dark:text-gray-400 mt-2">{{ item.description }}</p>
                        </div>
                        <div class="p-2 border-t border-gray-200 dark:border-gray-700 flex justify-between items-center w-full">
                            <div v-if="item.visible==='DRAFT'" class="text-xs font-semibold text-white bg-purple-400 px-2 py-1 inline-flex items-center justify-center w-auto">
                                {{ item.visible }}
                            </div>
                            <Button @click="executeScenario(item.id)" size="small" label="Load" class="ml-auto flex-initial text-white">
                                <ChevronRightIcon class="w-6 h-5 text-white transition-transform transform hover:translate-x-1"/>
                            </Button>
                        </div>
                    </div>
                </div>
            </div>
        </template>
    </DataView>
    </div>
</template>

<script setup>
import { ChevronRightIcon } from '@heroicons/vue/24/solid';
import DataView from 'primevue/dataview';
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import { ScenarioStore } from '../../stores/ScenarioStore.js';
import { UserPrefStore } from '../../stores/UserPrefStore.js';

    const router = useRouter()
    const layout = ref('grid');
    const options = ref(['list', 'grid']);

    const scenario_store = ScenarioStore();
    const userPrefStore = UserPrefStore();

const scenarioTypeOp = ref([
    { name: 'All', value: 'all' },
    //{ name: 'Cross', value: 'cross' },
    { name: 'Project', value: 'project' },
    { name: 'Application', value: 'application' }
    
]);


    onMounted(() => {

            scenario_store.setFilterString('');
            userPrefStore.fetchUserData().then(() => {
                //scenario_store.fetchScenariosCross();
                scenario_store.fetchScenarios();
                if(userPrefStore.getSelApp != null){
                    scenario_store.fetchApplicationScenarios();
                }

            });

        });

    const executeScenario = (id) => {
        router.push({ name: 'scenario-exec', params: { id: id } });
    }

</script>

<style scoped>
    .header-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.search-bar {
    display: flex;
    align-items: center;
}

.search-icon {
    color:#334155;
    margin-right: 5px;
}

.search-input {
    border: none;
    outline: none;
    box-shadow: none;
    flex: 1;
}

.search-input:focus {
    border: none;
    box-shadow: none;
    outline: none;
}

</style>