<script lang="ts">
interface Props {
    hide?: boolean;
}

let { hide = true }: Props = $props();

import {
    ae_snip,
    ae_loc,
    ae_sess,
    ae_api,
    ae_trig,
    slct,
    slct_trigger
} from '$lib/stores/ae_stores';
import {
    events_sess,
    events_slct,
    events_trigger,
    events_trig_kv
} from '$lib/stores/ae_events_stores';
import { pres_mgmt_loc } from '$lib/stores/ae_events_stores__pres_mgmt.svelte';
import {
    Ban,
    Barcode,
    CircleCheck,
    ChevronDown,
    ChevronUp,
    Eye,
    EyeOff,
    List,
    MapPin,
    Pencil,
    Plane,
    QrCode,
    Save,
    Send,
    Sparkles,
    StickyNote,
    ToggleLeft,
    ToggleRight,
    UserRound,
    Wand2
} from '@lucide/svelte';
</script>

<!-- New standard events module menu 2025-06-20 -->
<div
    class="ae_comp__pres_mgmt_menu_opts mt-1 w-full border-t border-gray-300"
    class:hidden={hide}>
    <h2 class="pb-1 text-center text-sm font-semibold">
        <button
            type="button"
            onclick={() => {
                pres_mgmt_loc.current.expand__menu_opts =
                    !pres_mgmt_loc.current.expand__menu_opts;
            }}
            class="btn btn-sm preset-tonal-info border-info-500 border">
            {#if !pres_mgmt_loc.current.expand__menu_opts}
                <ChevronUp size="1em" class="m-1" />
            {:else}
                <ChevronDown size="1em" class="m-1" />
            {/if}
            Æ Pres Mgmt Menu Options
            {#if !pres_mgmt_loc.current.expand__menu_opts}
                <ChevronUp size="1em" class="m-1" />
            {:else}
                <ChevronDown size="1em" class="m-1" />
            {/if}
        </button>
    </h2>

    <div
        class="flex flex-row flex-wrap items-center justify-between gap-1"
        class:hidden={!pres_mgmt_loc.current.expand__menu_opts}>
        {#if $ae_loc.authenticated_access}
            <div class="flex flex-col items-end justify-center gap-1">
                <p class="text-surface-500 w-full text-right text-xs font-semibold uppercase tracking-wide">Query Limits</p>
                <span class="flex flex-col items-end justify-center gap-1">
                    <!-- Max person select options -->
                    <span
                        class="flex w-full flex-row items-center justify-around gap-1">
                        <label
                            class="w-32 text-right text-sm"
                            for="qry_limit__people">
                            Max people:
                        </label>
                        <!-- Not using: pres_mgmt_loc.current.qry_limit__people -->
                        <select
                            id="qry_limit__people"
                            bind:value={$ae_loc.person.qry_limit__people}
                            class="select preset-tonal-surface w-20 px-1 text-sm">
                            <option value="">-- not set --</option>
                            <option value={25}>25</option>
                            <option value={50}>50</option>
                            <option value={75}>75</option>
                            <option value={100}>100</option>
                            <option value={150}>150</option>
                            <option value={200}>200</option>
                            <option value={250}>250</option>
                            <option value={500}>500</option>
                            <option value={750}>750</option>
                            <option value={1000}>1000</option>
                        </select>
                    </span>

                    <!-- Max presenters select options -->
                    <span
                        class="flex w-full flex-row items-center justify-around gap-1">
                        <label
                            class="w-32 text-right text-sm"
                            for="qry_limit__presenters">
                            Max presenters:
                        </label>
                        <select
                            id="qry_limit__presenters"
                            bind:value={
                                pres_mgmt_loc.current.qry_limit__presenters
                            }
                            class="select preset-tonal-surface w-20 px-1 text-sm">
                            <option value="">-- not set --</option>
                            <option value={25}>25</option>
                            <option value={50}>50</option>
                            <option value={75}>75</option>
                            <option value={100}>100</option>
                            <option value={150}>150</option>
                            <option value={200}>200</option>
                            <option value={250}>250</option>
                            <option value={300}>300</option>
                            <option value={500}>500</option>
                            <option value={750}>750</option>
                            <option value={1000}>1000</option>
                        </select>
                    </span>

                    <!-- Max sessions select options -->
                    <span
                        class="flex w-full flex-row items-center justify-around gap-1">
                        <label
                            class="w-32 text-right text-sm"
                            for="qry_limit__sessions">
                            Max sessions:
                        </label>
                        <select
                            id="qry_limit__sessions"
                            bind:value={
                                pres_mgmt_loc.current.qry_limit__sessions
                            }
                            class="select preset-tonal-surface w-20 px-1 text-sm">
                            <option value="">-- not set --</option>
                            <option value={25}>25</option>
                            <option value={50}>50</option>
                            <option value={75}>75</option>
                            <option value={100}>100</option>
                            <option value={200}>200</option>
                            <option value={500}>500</option>
                        </select>
                    </span>

                    <!-- Max files select options -->
                    <span
                        class="flex w-full flex-row items-center justify-around gap-1">
                        <label
                            class="w-32 text-right text-sm"
                            for="qry_limit__files">
                            Max files:
                        </label>
                        <select
                            id="qry_limit__files"
                            bind:value={pres_mgmt_loc.current.qry_limit__files}
                            class="select preset-tonal-surface w-20 px-1 text-sm">
                            <option value="">-- not set --</option>
                            <option value={25}>25</option>
                            <option value={50}>50</option>
                            <option value={75}>75</option>
                            <option value={100}>100</option>
                            <option value={200}>200</option>
                            <option value={500}>500</option>
                        </select>
                    </span>
                </span>

                <span class="flex flex-col items-end justify-center gap-1">
                    <!-- Toggle between the showing hidden sessions -->
                    <!-- qry_hidden = all, not_hidden, hidden -->
                    <button
                        type="button"
                        onclick={() => {
                            if (pres_mgmt_loc.current.qry_hidden == 'all') {
                                pres_mgmt_loc.current.qry_hidden = 'not_hidden';
                            } else {
                                pres_mgmt_loc.current.qry_hidden = 'all';
                            }
                        }}
                        class="btn btn-sm ae_btn_surface w-full justify-between text-center"
                        title="Toggle between showing hidden sessions">
                        {#if pres_mgmt_loc.current.qry_hidden == 'all'}<ToggleRight
                                size="1em"
                                class="m-1" />{:else}<ToggleLeft
                                size="1em"
                                class="m-1" />{/if}
                        {#if pres_mgmt_loc.current.qry_hidden == 'all'}
                            <span class="grow">
                                <EyeOff size="1em" class="m-1" />
                                Hide Hidden Sessions
                            </span>
                        {:else}
                            <span class="grow">
                                <Eye size="1em" class="m-1" />
                                Show Hidden Sessions?
                            </span>
                        {/if}
                    </button>

                    <!-- Toggle between the showing disabled sessions -->
                    <!-- qry_enabled = all, enabled -->
                    {#if $ae_loc.manager_access}
                        <button
                            type="button"
                            onclick={() => {
                                if (
                                    pres_mgmt_loc.current.qry_enabled == 'all'
                                ) {
                                    pres_mgmt_loc.current.qry_enabled =
                                        'enabled';
                                } else {
                                    pres_mgmt_loc.current.qry_enabled = 'all';
                                }
                            }}
                            class="btn btn-sm ae_btn_surface w-full justify-between text-center"
                            title="Toggle between showing disabled sessions">
                            {#if pres_mgmt_loc.current.qry_enabled == 'all'}<ToggleRight
                                    size="1em"
                                    class="m-1" />{:else}<ToggleLeft
                                    size="1em"
                                    class="m-1" />{/if}
                            {#if pres_mgmt_loc.current.qry_enabled == 'all'}
                                <span class="grow">
                                    <Ban size="1em" class="m-1" />
                                    Hide Disabled Sessions
                                </span>
                            {:else}
                                <span class="grow">
                                    <CircleCheck size="1em" class="m-1" />
                                    Show Disabled Sessions?
                                </span>
                            {/if}
                        </button>
                    {/if}
                </span>
            </div>
        {/if}

        <div class="flex flex-col flex-wrap items-center justify-evenly gap-1">
            <p class="text-surface-500 w-full text-center text-xs font-semibold uppercase tracking-wide">Display &amp; Search</p>
            <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 text-center"
                class:ae_btn_surface={pres_mgmt_loc.current.save_search_text}
                class:ae_btn_surface_outlined={!pres_mgmt_loc.current.save_search_text}
                title="Save the search text for this session search?">
                {#if pres_mgmt_loc.current.save_search_text}
                    <ToggleRight size="1em" class="m-1" />
                    <span class="grow">
                        <Save size="1em" class="m-1" />
                        Do Not Save Search?
                    </span>
                {:else}
                    <ToggleLeft size="1em" class="m-1" />
                    <span class="grow">
                        <Save size="1em" class="m-1" />
                        Save Search Text?
                    </span>
                {/if}
            </button>

            {#if $ae_loc.authenticated_access}
                {#if !pres_mgmt_loc.current.hide__session_msg}
                    <button
                        type="button"
                        onclick={() => {
                            pres_mgmt_loc.current.hide__session_msg = true;
                        }}
                        class="btn btn-sm ae_btn_surface w-full justify-between text-center">
                        <ToggleRight size="1em" class="m-1" />
                        <span class="grow">
                            <StickyNote size="1em" class="m-1" />
                            Hide Session Message
                        </span>
                    </button>
                {:else}
                    <button
                        type="button"
                        onclick={() => {
                            pres_mgmt_loc.current.hide__session_msg = false;
                        }}
                        class="btn btn-sm ae_btn_surface_outlined w-full justify-between text-center">
                        <ToggleLeft size="1em" class="m-1" />
                        <span class="grow">
                            <StickyNote size="1em" class="m-1" />
                            Show Session Message?
                        </span>
                    </button>
                {/if}
            {/if}

            {#if $ae_loc.authenticated_access}
                <!-- Show or hide the session code -->
                {#if !pres_mgmt_loc.current.hide__session_code}
                    <button
                        type="button"
                        onclick={() => {
                            pres_mgmt_loc.current.hide__session_code = true;
                        }}
                        class="btn btn-sm ae_btn_surface w-full justify-between text-center">
                        <ToggleRight size="1em" class="m-1" />
                        <span class="grow">
                            <Barcode size="1em" class="m-1" />
                            Hide Session Code
                        </span>
                    </button>
                {:else}
                    <button
                        type="button"
                        onclick={() => {
                            pres_mgmt_loc.current.hide__session_code = false;
                        }}
                        class="btn btn-sm ae_btn_surface_outlined w-full justify-between text-center">
                        <ToggleLeft size="1em" class="m-1" />
                        <span class="grow">
                            <Barcode size="1em" class="m-1" />
                            Show Session Code?
                        </span>
                    </button>
                {/if}
            {/if}
        </div>

        <div class="flex flex-col items-center justify-center gap-1">
            <p class="text-surface-500 w-full text-center text-xs font-semibold uppercase tracking-wide">Staff Options</p>
            {#if $ae_loc.trusted_access}
                <div class="flex flex-col items-center justify-center gap-1">
                    {#if pres_mgmt_loc.current.show__copy_access_link}
                        <button
                            type="button"
                            onclick={() => {
                                pres_mgmt_loc.current.show__copy_access_link = false;
                            }}
                            class="btn btn-sm ae_btn_surface w-full">
                            <ToggleRight size="1em" class="m-1" />
                            Showing Copy Access Link
                        </button>
                    {:else}
                        <button
                            type="button"
                            onclick={() => {
                                pres_mgmt_loc.current.show__copy_access_link = true;
                            }}
                            class="btn btn-sm ae_btn_surface_outlined w-full">
                            <ToggleLeft size="1em" class="m-1" />
                            Show Copy Access Link?
                        </button>
                    {/if}

                    {#if pres_mgmt_loc.current.show__email_access_link}
                        <button
                            type="button"
                            onclick={() => {
                                pres_mgmt_loc.current.show__email_access_link = false;
                            }}
                            class="btn btn-sm ae_btn_surface w-full">
                            <ToggleRight size="1em" class="m-1" />
                            Showing Email Access Link
                        </button>
                    {:else}
                        <button
                            type="button"
                            onclick={() => {
                                pres_mgmt_loc.current.show__email_access_link = true;
                            }}
                            class="btn btn-sm ae_btn_surface_outlined w-full">
                            <ToggleLeft size="1em" class="m-1" />
                            Show Email Access Link?
                        </button>
                    {/if}
                </div>
            {/if}

            {#if $ae_loc.authenticated_access}
                <div class="flex flex-col items-end justify-center gap-1">
                    <!-- Session QR toggle — only shown to trusted staff and only when
                         admin has NOT already enabled QR globally (if they have, it
                         shows automatically for everyone without needing this toggle) -->
                    {#if $ae_loc.trusted_access && !pres_mgmt_loc.current.show__session_qr}
                        {#if pres_mgmt_loc.current.show_content__session_qr}
                            <button
                                type="button"
                                onclick={() => {
                                    pres_mgmt_loc.current.show_content__session_qr = false;
                                }}
                                class="btn btn-sm ae_btn_surface w-full justify-between text-center"
                                title="Showing Session QR Code">
                                <ToggleRight size="1em" class="m-1" />
                                <span class="grow">
                                    <QrCode size="1em" class="m-1" />
                                    Hide Session QR
                                </span>
                            </button>
                        {:else}
                            <button
                                type="button"
                                onclick={() => {
                                    pres_mgmt_loc.current.show_content__session_qr = true;
                                }}
                                class="btn btn-sm ae_btn_surface_outlined w-full justify-between text-center"
                                title="Show Session QR Code">
                                <ToggleLeft size="1em" class="m-1" />
                                <span class="grow">
                                    <QrCode size="1em" class="m-1" />
                                    Show Session QR Code?
                                </span>
                            </button>
                        {/if}
                    {/if}

                    <!-- Presenter QR toggle — only shown to trusted staff and only when
                         admin has NOT already enabled QR globally (if they have, it
                         shows automatically for everyone without needing this toggle) -->
                    {#if $ae_loc.trusted_access && !pres_mgmt_loc.current.show__presenter_qr}
                        {#if pres_mgmt_loc.current.show_content__presenter_qr}
                            <button
                                type="button"
                                onclick={() => {
                                    pres_mgmt_loc.current.show_content__presenter_qr = false;
                                }}
                                class="btn btn-sm ae_btn_surface w-full justify-between text-center"
                                title="Showing Presenter QR Code">
                                <ToggleRight size="1em" class="m-1" />
                                <span class="grow">
                                    <QrCode size="1em" class="m-1" />
                                    Hide Presenter QR
                                </span>
                            </button>
                        {:else}
                            <button
                                type="button"
                                onclick={() => {
                                    pres_mgmt_loc.current.show_content__presenter_qr = true;
                                }}
                                class="btn btn-sm ae_btn_surface_outlined w-full justify-between text-center"
                                title="Show Presenter QR Code">
                                <ToggleLeft size="1em" class="m-1" />
                                <span class="grow">
                                    <QrCode size="1em" class="m-1" />
                                    Show Presenter QR Code?
                                </span>
                            </button>
                        {/if}
                    {/if}
                </div>
            {/if}
        </div>

        {#if $ae_loc.authenticated_access}
            <div class="flex flex-col items-center justify-center gap-1">
                <!-- Show/Hide launcher links — only visible when admin has enabled launcher (show__launcher_link)
                     OR user has trusted_access (staff onsite 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 text-center">
                        {#if pres_mgmt_loc.current.hide__launcher_link}<ToggleLeft
                                size="1em"
                                class="m-1" />{:else}<ToggleRight
                                size="1em"
                                class="m-1" />{/if}
                        <span class="grow">
                            <Plane size="1em" class="m-1" />
                            {pres_mgmt_loc.current.hide__launcher_link
                                ? 'Show Launcher Links'
                                : 'Hide Launcher Links?'}
                        </span>
                    </button>
                {/if}

                <!-- Show/Hide links to the location (room) -->
                <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 text-center">
                    {#if pres_mgmt_loc.current.hide__location_link}<ToggleLeft
                            size="1em"
                            class="m-1" />{:else}<ToggleRight
                            size="1em"
                            class="m-1" />{/if}
                    <span class="grow">
                        <MapPin size="1em" class="m-1" />
                        {pres_mgmt_loc.current.hide__location_link
                            ? 'Show Location Links'
                            : 'Hide Location Links?'}
                    </span>
                </button>

                <!-- Show/Hide the Location (room) column in tables and lists -->
                <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 text-center"
                    title="Toggle showing the Location column in session lists and tables">
                    {#if pres_mgmt_loc.current.hide__session_li_location_field}<ToggleLeft
                            size="1em"
                            class="m-1" />{:else}<ToggleRight
                            size="1em"
                            class="m-1" />{/if}
                    <span class="grow">
                        <!-- <span class="fas fa-door-open m-1"></span> -->
                        {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 -->

                <!-- These are related to more content showing in lists. -->
                <span
                    class="flex flex-col flex-wrap items-center justify-evenly gap-1">
                    {#if pres_mgmt_loc.current.show_content__session_files}
                        <button
                            type="button"
                            disabled={!$ae_loc.manager_access}
                            onclick={() => {
                                pres_mgmt_loc.current.show_content__session_files = false;
                            }}
                            class="btn btn-sm ae_btn_surface w-full justify-between text-center">
                            <ToggleRight size="1em" class="m-1" />
                            <span class="grow">
                                <List size="1em" class="m-1" />
                                Hide Linked Files (testing)
                            </span>
                        </button>
                    {:else}
                        <button
                            type="button"
                            disabled={!$ae_loc.manager_access}
                            onclick={() => {
                                pres_mgmt_loc.current.show_content__session_files = true;
                            }}
                            class="btn btn-sm ae_btn_surface_outlined w-full justify-between text-center">
                            <ToggleLeft size="1em" class="m-1" />
                            <span class="grow">
                                <List size="1em" class="m-1" />
                                Show Linked Files? (testing)
                            </span>
                        </button>
                    {/if}

                    {#if pres_mgmt_loc.current.show_content__session_presentations}
                        <button
                            type="button"
                            disabled={!$ae_loc.manager_access}
                            onclick={() => {
                                pres_mgmt_loc.current.show_content__session_presentations = false;
                            }}
                            class="btn btn-sm ae_btn_surface w-full justify-between text-center">
                            <ToggleRight size="1em" class="m-1" />
                            <span class="grow">
                                <List size="1em" class="m-1" />
                                Hide Linked Presentations (testing)
                            </span>
                        </button>
                    {:else}
                        <button
                            type="button"
                            disabled={!$ae_loc.manager_access}
                            onclick={() => {
                                pres_mgmt_loc.current.show_content__session_presentations = true;
                            }}
                            class="btn btn-sm ae_btn_surface_outlined w-full justify-between text-center">
                            <ToggleLeft size="1em" class="m-1" />
                            <span class="grow">
                                <List size="1em" class="m-1" />
                                Show Linked Presentations? (testing)
                            </span>
                        </button>
                    {/if}
                </span>
            </div>
        {/if}

        <div
            class="flex max-w-56 flex-row flex-wrap items-center justify-evenly gap-1">
            {#if $ae_loc?.trusted_access}
                {#if $ae_loc?.edit_mode}
                    <button
                        type="button"
                        onclick={() => {
                            $ae_loc.edit_mode = false;
                        }}
                        class="btn btn-sm ae_btn_warning w-full justify-between text-center"
                        title="Turn off edit mode">
                        <ToggleRight size="1em" class="m-1" />
                        <span class="grow">
                            <Pencil size="1em" class="m-1" />
                            Edit Mode Off
                        </span>
                    </button>
                {:else}
                    <button
                        type="button"
                        onclick={() => {
                            $ae_loc.edit_mode = true;
                        }}
                        class="btn btn-sm ae_btn_warning_outlined w-full justify-between text-center"
                        title="Turn on edit mode">
                        <ToggleLeft size="1em" class="m-1" />
                        <span class="grow">
                            <UserRound size="1em" class="m-1" />
                            Edit Mode?
                        </span>
                    </button>
                {/if}

                {#if $ae_loc?.adv_mode}
                    <button
                        type="button"
                        onclick={() => {
                            $ae_loc.adv_mode = false;
                        }}
                        class="btn btn-sm ae_btn_warning w-full justify-between text-center"
                        title="Turn off advanced mode">
                        <ToggleRight size="1em" class="m-1" />
                        <span class="grow">
                            <Sparkles size="1em" class="m-1" />
                            Advanced Mode Off
                        </span>
                    </button>
                {:else}
                    <button
                        type="button"
                        onclick={() => {
                            $ae_loc.adv_mode = true;
                        }}
                        class="btn btn-sm ae_btn_warning_outlined w-full justify-between text-center"
                        title="Turn on advanced mode">
                        <ToggleLeft size="1em" class="m-1" />
                        <span class="grow">
                            <Wand2 size="1em" class="m-1" />
                            Advanced Mode?
                        </span>
                    </button>
                {/if}
            {/if}
        </div>
    </div>
</div>
