<script lang="ts">
/** @type {import('./$types').PageData} */
export let data: any;
let log_lvl = 1;
// console.log(`ae_events_pres_mgmt event [event_id] +page.svelte data:`, data);

import { browser } from '$app/environment';
// import { goto, invalidate, pushState, replaceState } from '$app/navigation';

import { Modal } from 'flowbite-svelte';

import type { key_val } from '$lib/ae_stores';
import { ae_util } from '$lib/ae_utils/ae_utils';
import { idaa_loc, idaa_sess, idaa_slct } from '$lib/ae_idaa_stores';
// import Element_data_store from '$lib/element_data_store_v2.svelte';
// import Comp_event_session_obj_li from '../../events_pres_mgmt/ae_comp__event_session_obj_li.svelte';

import { liveQuery } from "dexie";
import { core_func } from '$lib/ae_core/ae_core_functions';
import { db_events } from "$lib/ae_events/db_events";
import { ae_snip, ae_loc, ae_sess, ae_api, ae_trig, slct, slct_trigger } from '$lib/ae_stores';
// import { events_func } from '$lib/ae_events_functions';

import Comp__event_obj_qry from './ae_idaa_comp__event_obj_qry.svelte';
import Comp__event_obj_li from './ae_idaa_comp__event_obj_li.svelte';
import Comp__event_obj_id_edit from './ae_idaa_comp__event_obj_id_edit.svelte';
import Comp__event_obj_id_view from './ae_idaa_comp__event_obj_id_view.svelte';

let event_id_random_li: Array<string>;


// Functions and Logic
$: lq__event_obj = liveQuery(async () => {
    let results = await db_events.events
        .get($idaa_slct.event_id);

    return results;
});


$: lq_new__event_obj_li = liveQuery(async () => {
    let link_to_type: string = 'account';
    let link_to_id: string = $slct.account_id;
    if (log_lvl > 1) {
        console.log(`link_to_type: ${link_to_type}; link_to_id: ${link_to_id}; event_id_random_li:`, event_id_random_li);
    }

    // Check if event_id_random_li is an array and not undefined
    if (event_id_random_li) {
        if (log_lvl) {
            console.log(`Trying bulkGet:`, event_id_random_li);
        }
        let results = await db_events.events
            .bulkGet(event_id_random_li);

        return results;
    } else if (link_to_type && link_to_id) {
        if (log_lvl) {
            console.log(`Trying where: ${link_to_type}; equals: ${link_to_id}`);
        }
        let results = await db_events.events
            .where(`${link_to_type}_id`)
            .equals(link_to_id)
            .and((event) => {
                return event.hide == false;
            })
            .and((event) => {
                return event.enable == true;
            })
            .sortBy('name')

        return results;
    } else {
        if (log_lvl) {
            console.log('Trying... Nothing to load');
        }
        return null;
    }
});

</script>

<!-- ({$lq__event_obj?.event_id ?? ''})  -->
<svelte:head>
	<title>
        IDAA Recovery Meetings:
        {$lq__event_obj?.name ? ae_util.shorten_string({ string: $lq__event_obj?.name, max_length: 20, begin_length: 10, end_length: 4 }) : ''}
        - Novi - {$ae_loc?.title}
    </title>
</svelte:head>


<section
    class="
        ae_idaa__recovery_meetings
        container h-full mx-auto
        flex flex-col gap-1
        py-1 px-2 pb-16
        items-center
        min-w-full
        max-w-max
        "
    >

<Comp__event_obj_qry
    bind:event_id_random_li={event_id_random_li}
    />

<!-- <h1>Recovery Meetings {$lq_new__event_obj_li?.length}</h1> -->

{#if $lq_new__event_obj_li && $lq_new__event_obj_li?.length }
    <Comp__event_obj_li
        lq__event_obj_li={lq_new__event_obj_li}
        />
{:else}
    <p>No recovery meetings available to show.</p>
{/if}


</section>



<!-- Main modal -->
<Modal
    title="{$lq__event_obj?.name} - {$lq__event_obj?.id}"
    bind:open={$idaa_sess.recovery_meetings.show__modal_edit}
    autoclose={false}
    placement="top-center"
    size="xl"
    class="top-center bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-200 rounded-lg border-gray-200 dark:border-gray-700 divide-gray-200 dark:divide-gray-700 shadow-md relative flex flex-col mx-auto w-full divide-y"
    >

    <svelte:fragment slot="header">
        <div class="flex flex-row items-center justify-between w-full">
            <h3 class="text-lg font-semibold">
            {#if $ae_loc.trusted_access || $lq__event_obj?.external_person_id === $idaa_loc.novi_uuid || $lq__event_obj?.contact_li_json[0]?.email === $idaa_loc.novi_email}
            <!-- <div class="ae_options"> -->
                <button
                    on:click={() => {
                        // const url = new URL(location);
                        // url.searchParams.set('event_id', $lq__event_obj?.event_id_random);
                        // history.pushState({}, '', url);

                        $idaa_sess.recovery_meetings.show__modal_view = true;
                        $idaa_sess.recovery_meetings.show__modal_edit = false;
                    }}
                    class="btn btn-sm variant-ghost-warning hover:variant-filled-warning transition"
                    title={`View meeting: ${$lq__event_obj?.name}`}
                    >
                    <span class="fas fa-eye m-1"></span> View
                </button>
            <!-- </div> -->
            {/if}

            Edit Meeting: {$lq__event_obj?.name}</h3>
        </div>

    </svelte:fragment>

    <Comp__event_obj_id_edit
        lq__event_obj={lq__event_obj}
        />


    <svelte:fragment slot="footer">
        <div class="text-center w-full">
            <button
                type="button"
                on:click={() => {
                    console.log('Close modal');
                    $idaa_sess.recovery_meetings.show__modal_edit = false;
                }}
                class="btn btn-sm variant-soft-warning hover:variant-ghost-warning"
                >
                <span class="fas fa-times mx-1"></span>
                Close
            </button>
        </div>
    </svelte:fragment>

</Modal>

<!-- Modal: Event (Recovery Meeting) view ID -->
<Modal
    title="{$lq__event_obj?.name} - {$lq__event_obj?.id}"
    bind:open={$idaa_sess.recovery_meetings.show__modal_view}
    autoclose={false}
    outsideclose={true}
    placement="top-center"
    size="lg"
    class="bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-200 rounded-lg border-gray-200 dark:border-gray-700 divide-gray-200 dark:divide-gray-700 shadow-md relative flex flex-col mx-auto w-full divide-y"
    >

    <svelte:fragment slot="header">

        <div class="flex flex-row items-center justify-between w-full">
            <h3 class="text-lg font-semibold">
                {#if $ae_loc.trusted_access || $lq__event_obj?.external_person_id === $idaa_loc.novi_uuid || $lq__event_obj?.contact_li_json[0].email === $idaa_loc.novi_email}
                    <button
                        on:click={() => {
                            // const url = new URL(location);
                            // url.searchParams.set('event_id', $lq__event_obj?.event_id_random);
                            // history.pushState({}, '', url);

                            $idaa_sess.recovery_meetings.show__modal_view = false;
                            $idaa_sess.recovery_meetings.show__modal_edit = true;
                        }}
                        class="btn btn-sm variant-ghost-warning hover:variant-filled-warning transition"
                        title={`Edit meeting: ${$lq__event_obj?.name}`}
                        >
                        <span class="fas fa-edit m-1"></span> Edit
                    </button>
                {/if}
                {$lq__event_obj?.name}
            </h3>
        </div>
    </svelte:fragment>

    <Comp__event_obj_id_view
        lq__event_obj={lq__event_obj}
        />

</Modal>