<script lang="ts">
interface Props {
    log_lvl?: number;
    lq__event_obj: any;
}

let { log_lvl = 0, lq__event_obj }: Props = $props();

import { goto } from '$app/navigation';
import { Modal } from 'flowbite-svelte';
import {
    Archive,
    Clock,
    Info,
    MapPin,
    Plane,
    Save,
    Send,
    Settings,
    ToggleLeft,
    ToggleRight,
    Users,
    X
} from '@lucide/svelte';
import { ae_loc, ae_api } from '$lib/stores/ae_stores';
import { events_slct } from '$lib/stores/ae_events_stores';
import { pres_mgmt_loc } from '$lib/stores/ae_events_stores__pres_mgmt.svelte';
import { events_func } from '$lib/ae_events/ae_events_functions';

import { api } from '$lib/ae_api/api';
import Element_data_store from '$lib/elements/element_data_store.svelte';
import Comp__events_menu_nav from '../../ae_comp__events_menu_nav.svelte';
import Comp__pres_mgmt_menu_opts from '../../ae_comp__events_menu_opts.svelte';
import AE_Record_Controls from '$lib/elements/ae_record_controls.svelte';

let show_modal = $state(false);
let show_help = $state(false);

async function on_toggle(field: string, new_val: boolean) {
    await api.update_ae_obj({
        api_cfg: $ae_api,
        obj_type: 'event',
        obj_id: $lq__event_obj?.event_id,
        fields: { [field]: new_val }
    });
    events_func.load_ae_obj_id__event({
        api_cfg: $ae_api,
        event_id: $lq__event_obj?.event_id,
        log_lvl
    });
}

async function on_delete(method: 'delete' | 'disable') {
    await events_func.delete_ae_obj_id__event({
        api_cfg: $ae_api,
        event_id: $lq__event_obj.event_id,
        method
    });
    $events_slct.event_id = null;
    $events_slct.event_obj = {};
    goto('/events');
}
</script>

<div class="pres_mgmt__session_search_menu ae_container_module_menu">
    <div class="flex w-full flex-col gap-1">
        <Comp__events_menu_nav
            hide={!$ae_loc.authenticated_access}
            event_id={$lq__event_obj?.event_id}
            ae_core={$ae_loc.edit_mode && $ae_loc.manager_access}
            events__locations={$ae_loc.administrator_access}
            events__reports={$ae_loc.trusted_access}
            events__settings={$ae_loc.edit_mode && $ae_loc.manager_access}
            events__session_search={false} />

        <span
            class="ae_menu__object_options flex w-full flex-row flex-wrap items-center justify-between gap-0.5">
            <!-- Event Files toggle — the one real working button from the old menu, kept in place -->
            <!-- <button
                type="button"
                onclick={() => {
                    pres_mgmt_loc.current.show_content__event_view =
                        pres_mgmt_loc.current.show_content__event_view === 'manage_files'
                            ? null
                            : 'manage_files';
                }}
                class="btn btn-sm relative"
                class:preset-filled-primary-500={pres_mgmt_loc.current.show_content__event_view === 'manage_files'}
                class:preset-tonal-primary={pres_mgmt_loc.current.show_content__event_view !== 'manage_files'}
                class:hidden={!$ae_loc.administrator_access}
                disabled={!$ae_loc.manager_access}
                title="Session search or manage files for the event"
            >
                {#if pres_mgmt_loc.current.show_content__event_view === 'manage_files'}
                    <Users size="1em" class="m-1 inline-block" />
                    Session Search?
                {:else}
                    <Archive size="1em" class="m-1 inline-block" />
                    Event Files?
                    <span
                        class="badge badge-icon preset-tonal-success absolute -top-1.5 -right-1.5 z-10"
                        class:hidden={!$lq__event_obj?.file_count}
                    >
                        {$lq__event_obj?.file_count}×
                    </span>
                {/if}
            </button> -->

            <!-- Pres Mgmt Config — manager_access + edit_mode only -->
            {#if $ae_loc.manager_access && $ae_loc.edit_mode}
                <a
                    href="/events/{$lq__event_obj?.event_id}/pres_mgmt/config"
                    class="btn btn-sm preset-tonal-warning"
                    title="Edit Presentation Management configuration for this event">
                    <Settings size="1em" class="mr-1" />
                    Config
                </a>
            {/if}

            <!-- Options modal trigger -->
            {#if $ae_loc.trusted_access}
                <button
                    type="button"
                    onclick={() => (show_modal = true)}
                    class="btn btn-sm ae_btn_info"
                    title="Event options">
                    <Settings size="1em" class="mr-1" />
                    Options
                </button>
            {/if}

            <!-- Help toggle -->
            <button
                type="button"
                onclick={() => (show_help = !show_help)}
                class="btn btn-sm"
                class:ae_btn_info_filled={show_help}
                class:ae_btn_info={!show_help}
                title="Help and information about the event session search">
                <Info size="1em" class="mr-1" />
                {show_help ? 'Hide Help' : 'Help'}
            </button>
        </span>
    </div>

    <!-- Options Modal -->
    <Modal
        bind:open={show_modal}
        autoclose={false}
        dismissable={true}
        placement="top-center"
        size="lg"
        class="relative mx-auto flex w-full flex-col rounded-lg bg-white text-gray-800 shadow-xl dark:bg-gray-800 dark:text-gray-200"
        headerClass="flex flex-row gap-2 items-center justify-between w-full bg-surface-100 dark:bg-surface-800 p-4 rounded-t-lg border-b border-surface-200 dark:border-surface-700">
        {#snippet header()}
            <h3 class="flex flex-1 items-center gap-2 text-base font-bold">
                <Settings size="1.1em" class="text-primary-500" />
                Event Options
            </h3>
            <button
                type="button"
                class="btn-icon btn-icon-sm preset-tonal-surface ml-2"
                onclick={() => (show_modal = false)}>
                <X size="1.1em" />
            </button>
        {/snippet}

        <div class="flex flex-col gap-4 p-4">
            <!-- Display options for the session list -->
            <section>
                <h4
                    class="text-surface-500 mb-2 text-xs font-semibold tracking-wider uppercase">
                    Display
                </h4>
                <div class="flex flex-col gap-1">
                    <button
                        type="button"
                        onclick={() => {
                            pres_mgmt_loc.current.save_search_text =
                                !pres_mgmt_loc.current.save_search_text;
                        }}
                        class="btn btn-sm w-full justify-between"
                        class:ae_btn_surface={pres_mgmt_loc.current.save_search_text}
                        class:ae_btn_surface_outlined={!pres_mgmt_loc.current.save_search_text}>
                        {#if pres_mgmt_loc.current.save_search_text}<ToggleRight
                                size="1em"
                                class="mr-1" />{:else}<ToggleLeft
                                size="1em"
                                class="mr-1" />{/if}
                        <span class="grow">
                            <Save size="1em" class="mr-1" />
                            {pres_mgmt_loc.current.save_search_text
                                ? 'Do Not Save Search'
                                : 'Save Search Text?'}
                        </span>
                    </button>

                    <button
                        type="button"
                        onclick={() => {
                            pres_mgmt_loc.current.use_12h = !pres_mgmt_loc.current.use_12h;
                        }}
                        class="btn btn-sm w-full justify-between"
                        class:ae_btn_surface={pres_mgmt_loc.current.use_12h}
                        class:ae_btn_surface_outlined={!pres_mgmt_loc.current.use_12h}>
                        {#if pres_mgmt_loc.current.use_12h}<ToggleRight
                                size="1em"
                                class="mr-1" />{:else}<ToggleLeft
                                size="1em"
                                class="mr-1" />{/if}
                        <span class="grow">
                            <Clock size="1em" class="mr-1" />
                            {pres_mgmt_loc.current.use_12h ? '12-Hour Time' : '24-Hour Time'}
                        </span>
                    </button>

                    <!-- Launcher link toggle — only visible when admin has enabled launcher
                         (show__launcher_link) OR user has trusted_access (staff override) -->
                    {#if pres_mgmt_loc.current.show__launcher_link || $ae_loc.trusted_access}
                        <button
                            type="button"
                            onclick={() => {
                                pres_mgmt_loc.current.hide__launcher_link =
                                    !pres_mgmt_loc.current.hide__launcher_link;
                            }}
                            class="btn btn-sm ae_btn_surface w-full justify-between">
                            {#if pres_mgmt_loc.current.hide__launcher_link}<ToggleLeft
                                    size="1em"
                                    class="mr-1" />{:else}<ToggleRight
                                    size="1em"
                                    class="mr-1" />{/if}
                            <span class="grow">
                                <Plane size="1em" class="mr-1" />
                                {pres_mgmt_loc.current.hide__launcher_link
                                    ? 'Show Launcher Links'
                                    : 'Hide Launcher Links?'}
                            </span>
                        </button>
                    {/if}

                    <button
                        type="button"
                        onclick={() => {
                            pres_mgmt_loc.current.hide__location_link =
                                !pres_mgmt_loc.current.hide__location_link;
                        }}
                        class="btn btn-sm ae_btn_surface w-full justify-between">
                        {#if pres_mgmt_loc.current.hide__location_link}<ToggleLeft
                                size="1em"
                                class="mr-1" />{:else}<ToggleRight
                                size="1em"
                                class="mr-1" />{/if}
                        <span class="grow">
                            <MapPin size="1em" class="mr-1" />
                            {pres_mgmt_loc.current.hide__location_link
                                ? 'Show Location Links'
                                : 'Hide Location Links?'}
                        </span>
                    </button>

                    <button
                        type="button"
                        onclick={() => {
                            pres_mgmt_loc.current.hide__session_li_location_field =
                                !pres_mgmt_loc.current.hide__session_li_location_field;
                        }}
                        class="btn btn-sm ae_btn_surface w-full justify-between"
                        title="Toggle the Location column in session lists">
                        {#if pres_mgmt_loc.current.hide__session_li_location_field}<ToggleLeft
                                size="1em"
                                class="mr-1" />{:else}<ToggleRight
                                size="1em"
                                class="mr-1" />{/if}
                        <span class="grow">
                            {pres_mgmt_loc.current.hide__session_li_location_field
                                ? 'Show Location Column'
                                : 'Hide Location Column?'}
                        </span>
                    </button>
                    <!-- POC column visibility is a per-event config (Config page), not a
                         local toggle — removed 2026-06-16, see
                         PROJECT__AE_Events_PressMgmt_Config_Cleanup.md -->
                </div>
            </section>

            <!-- Record Controls for the Event itself -->
            {#if $ae_loc.administrator_access && $ae_loc.edit_mode || $ae_loc.manager_access}
                <section>
                    <h4
                        class="text-surface-500 mb-2 text-xs font-semibold tracking-wider uppercase">
                        Event Record
                    </h4>
                    <AE_Record_Controls
                        obj={$lq__event_obj}
                        obj_label="event"
                        show_alert={true}
                        show_priority={true}
                        show_enable={true}
                        show_hide={true}
                        allow_delete={$ae_loc.super_access}
                        allow_disable={$ae_loc.manager_access &&
                            !$ae_loc.super_access}
                        {on_toggle}
                        {on_delete}
                        container_class="flex flex-row flex-wrap gap-2 items-center justify-start" />
                </section>
            {/if}

            <!-- Pres Mgmt query options (limits, filters) — this component belongs here on the session search page (public+ only; was authenticated+) -->
            {#if $ae_loc.public_access}
                <section>
                    <!-- <h4
                        class="text-surface-500 mb-2 text-xs font-semibold tracking-wider uppercase">
                        Query Options
                    </h4> -->
                    <Comp__pres_mgmt_menu_opts hide={false} />
                </section>
            {/if}
        </div>
    </Modal>

    <!-- Help panel -->
    <Element_data_store
        ds_code="events__pres_mgmt__session_search_help"
        ds_name="Default: Events - Pres Mgmt Session Search Help"
        ds_type="html"
        for_type="event"
        for_id={$events_slct.event_id}
        class_li="ae_container_module_help"
        show_edit={false}
        show_edit_btn={true}
        hide={!show_help} />
</div>
