<script setup lang="ts">
// import { Head, Link, useForm, usePage } from '@inertiajs/inertia-vue3';
import { Head, usePage } from '@inertiajs/vue3';
import { ComputedRef } from 'vue';
import { mdiSquareEditOutline, mdiTrashCan, mdiAlertBoxOutline, mdiLockOpen } from '@mdi/js';
import { computed } from 'vue';
import LayoutAuthenticated from '@/Layouts/LayoutAuthenticated.vue';
import SectionMain from '@/Components/SectionMain.vue';
// import SectionTitleLineWithButton from '@/Components/SectionTitleLineWithButton.vue';
import BaseButton from '@/Components/BaseButton.vue';
import CardBox from '@/Components/CardBox.vue';
import BaseButtons from '@/Components/BaseButtons.vue';
import NotificationBar from '@/Components/NotificationBar.vue';
import Pagination from '@/Components/Admin/Pagination.vue';
// import Sort from '@/Components/Admin/Sort.vue';
import { stardust } from '@eidellev/adonis-stardust/client';

const props = defineProps({
    datasets: {
        type: Object,
        default: () => ({}),
    },
    filters: {
        type: Object,
        default: () => ({}),
    },
    can: {
        type: Object,
        default: () => ({}),
    },
    // user: {
    //     type: Object,
    //     default: () => ({}),
    // }
});

// const search = computed(() => {
// 	return props.filters.search;
// });

const flash: ComputedRef<any> = computed(() => {
    // let test = usePage();
    // console.log(test);
    return usePage().props.flash;
});

// const errors: ComputedRef<any> = computed(() => {
//     return usePage().props.errors;
// });

// const form = useForm({
//     search: props.filters.search,
// });

// const formDelete = useForm({});

// async function destroy(id) {
// const destroy = async (id) => {
//     // if (confirm('Are you sure you want to delete?')) {
//     //     await formDelete.delete(stardust.route('dataset.destroy', [id]));
//     // }
// };
</script>

<template>
    <LayoutAuthenticated>
        <Head title="Submit Dataset" />
        <SectionMain>
            <!-- <FormValidationErrors v-bind:errors="errors" /> -->
            <NotificationBar v-if="flash.message" color="success" :icon="mdiAlertBoxOutline">
                {{ flash.message }}
            </NotificationBar>
            <NotificationBar v-if="flash.warning" color="warning" :icon="mdiAlertBoxOutline">
                {{ flash.warning }}
            </NotificationBar>

            <!-- table -->
            <CardBox class="mb-6" has-table>
                <table>
                    <thead>
                        <tr>
                            <th>
                                <!-- <Sort label="Dataset Title" attribute="title" :search="form.search" /> -->
                                Dataset Title
                            </th>
                            <th>
                                <!-- <Sort label="Email" attribute="email" :search="form.search" /> -->
                                <th>Server State</th>
                            </th>
                            <th>Date of last modification</th> 
                            <th v-if="can.edit || can.delete">Actions</th>
                        </tr>
                    </thead>

                    <tbody>
                        <tr v-for="dataset in props.datasets.data" :key="dataset.id">
                            <td data-label="Login">
                                <!-- <Link v-bind:href="stardust.route('user.show', [user.id])"
                                    class="no-underline hover:underline text-cyan-600 dark:text-cyan-400">
                                {{ user.login }}
                                </Link> -->
                                <!-- {{ user.id }} -->
                                {{ dataset.main_title }}
                            </td>                        
                            <td>
                                {{ dataset.server_state }}
                             </td>
                        
                            <td data-label="Created" class="lg:w-1 whitespace-nowrap">
                                <small class="text-gray-500 dark:text-slate-400" :title="dataset.server_date_modified">{{ dataset.updated_at }}</small>
                            </td>
                            <td v-if="can.edit || can.delete" class="before:hidden lg:w-1 whitespace-nowrap">
                                <BaseButtons type="justify-start lg:justify-end" no-wrap>
                                      <!-- release created dataset -->
                                    <BaseButton
                                        v-if="can.edit"                                       
                                        :route-name="stardust.route('dataset.release', [dataset.id])"                                      
                                        color="info"
                                        :icon="mdiLockOpen"
                                        :label="'Release'"
                                        small
                                    />
                                    <!-- && (dataset.server_state === 'inprogress' || dataset.server_state === 'rejected_editor')"   -->
                                    <BaseButton
                                        v-if="can.edit"                                      
                                        :route-name="stardust.route('dataset.edit', [dataset.id])"                                      
                                        color="info"
                                        :icon="mdiSquareEditOutline"
                                        :label="'Edit'"
                                        small
                                    />
                                    <!-- @click="destroy(dataset.id)" -->
                                    <BaseButton v-if="can.delete" color="danger" :icon="mdiTrashCan" small  />
                                </BaseButtons>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <div class="py-4">
                    <Pagination v-bind:data="datasets.meta" />
                    <!-- <ul>
                        <li>
                            <a href="{{ users.page == 1 ? '#' : '?page=' + (users.page - 1) }}">Previous</a>
                        </li>
                        @each(page in ???)
                        <li>
                            <a href="?page={{ page }}">{{ page }}</a>
                        </li>
                        @endeach
                        <li>
                            <a href="{{ users.lastPage == users.page ? '#' : '?page=' + (users.page + 1) }}">Next</a>
                        </li>
                    </ul> -->
                </div>
            </CardBox>
        </SectionMain>
    </LayoutAuthenticated>
</template>
