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

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

	// *** Import Svelte specific
	import { browser } from '$app/environment';
	import { goto } from '$app/navigation';
	import { onMount } from 'svelte';

	// *** Import other supporting libraries

	// *** Import Aether specific variables and functions
	import type { key_val } from '$lib/stores/ae_stores';
	import { ae_util } from '$lib/ae_utils/ae_utils';
	import {
		ae_snip,
		ae_loc,
		ae_sess,
		ae_api,
		ae_trig,
		slct,
		slct_trigger
	} from '$lib/stores/ae_stores';
	import { core_func } from '$lib/ae_core/ae_core_functions';
	import { idaa_loc, idaa_sess, idaa_slct, idaa_trig, idaa_prom } from '$lib/stores/ae_idaa_stores';
	import { events_func } from '$lib/ae_events_functions';

	import Help_tech from '$lib/app_components/e_app_help_tech.svelte';

	// export let event_id_random_li: Array<string>;

	// export let container_class_li = [];

	let ae_promises: key_val = $state({});
	// let ae_tmp: key_val = {};
	// let ae_trigger: any = null;
	// let ae_triggers: key_val = {};

	// let search_submit_results: any = null;

	if (
		$idaa_loc.recovery_meetings?.save_search_text &&
		$idaa_loc.recovery_meetings?.saved_search__session
	) {
		$idaa_sess.recovery_meetings.fulltext_search_qry_str =
			$idaa_loc.recovery_meetings.saved_search__session;
	}
	if (
		$idaa_loc.recovery_meetings?.save_search_text &&
		$idaa_loc.recovery_meetings?.saved_search__session_location_name
	) {
		$idaa_sess.recovery_meetings.location_name_qry_str =
			$idaa_loc.recovery_meetings.saved_search__session_location_name;
	}

	// *** Functions and Logic
	function preventDefault<T extends Event>(fn: (event: T) => void) {
		return function (event: T) {
			event.preventDefault();
			fn(event);
		};
	}

	// if ($idaa_loc.recovery_meetings.qry__fulltext_str && $idaa_loc.recovery_meetings.qry__fulltext_str.length) {
	//     $idaa_trig.event_li_qry = true;
	// }

	// Updated 2024-10-01
	// $: if (ae_trigger == 'load__event_obj_li') {
	//     if (log_lvl) {
	//         console.log('*** TEST SEARCH - load__event_obj_li == load__event_obj_li ***');
	//     }
	//     ae_trigger = null;
	//     $idaa_trig.event_li_qry = true;
	//     if ($idaa_sess.recovery_meetings.qry_status == 'loading') {
	//         console.log('*** $idaa_sess.recovery_meetings.qry_status == loading ***');

	//         setTimeout(() => {
	//             console.log("Delayed for X second.");
	//             // ae_trigger = null;
	//             // handle_qry__event({ft_qry_str: $idaa_loc.recovery_meetings.qry__fulltext_str});
	//             $idaa_trig.event_li_qry = true;
	//         }, 250);
	//     } else {
	//         console.log('*** $idaa_sess.recovery_meetings.qry_status != loading ***');
	//         // ae_trigger = null;
	//         // handle_qry__event({ft_qry_str: $idaa_loc.recovery_meetings.qry__fulltext_str});
	//         $idaa_trig.event_li_qry = true;
	//     }
	// }

	// async function handle_qry__event(
	//         {
	//             ft_qry_str = '',
	//             and_physical = $idaa_loc.recovery_meetings.qry__physical,
	//             and_virtual = $idaa_loc.recovery_meetings.qry__virtual,
	//             and_type = $idaa_loc.recovery_meetings.qry__type,
	//             order_by_li = $idaa_loc.recovery_meetings.qry__order_by_li,
	//             search_delay = 0,
	//             max_tries = 5,
	//             params = {
	//                 'qry__enabled': $idaa_loc.recovery_meetings.qry__enabled ?? 'enabled',
	//                 'qry__hidden': $idaa_loc.recovery_meetings.qry__hidden ?? 'not_hidden',
	//                 'qry__limit': $idaa_loc.recovery_meetings.qry__limit ?? 35,
	//             },
	//             try_cache = true,
	//             log_lvl = 1,
	//         }: {
	//             ft_qry_str?: string,
	//             and_physical?: null|boolean,
	//             and_virtual?: null|boolean,
	//             and_type?: null|string,
	//             order_by_li?: key_val,
	//             search_delay?: number, // In milliseconds
	//             max_tries?: number,
	//             params?: key_val,
	//             try_cache?: boolean,
	//             log_lvl?: number,
	//         }
	//     ) {
	//     console.log('handle_qry__event()');

	//     if ($idaa_sess.recovery_meetings?.qry__status != null && $idaa_sess.recovery_meetings?.qry__status != 'done') {
	//         console.log('*** TEST SEARCH - $idaa_sess.recovery_meetings.qry__status != done ***');
	//         // WARNING: This is a temporary fix for the search string. It needs to be fixed in the future. Using lk_search_str for now.
	//         $idaa_sess.recovery_meetings.status_qry__last_request_str = ft_qry_str;

	//         // We want to delay the initial search request to give the previous search request to finish.
	//         let random_delay = Math.floor(Math.random() * 50);
	//         search_delay += 50+random_delay;
	//     }

	//     log_lvl = 1;

	//     let count = 0;
	//     let request_loop = setInterval(() => {
	//         count++;
	//         if (log_lvl) {
	//             console.log(`*** TEST SEARCH - Search delay: ${search_delay} *** loop count=${count}`);
	//         }
	//         if (count >= max_tries) {
	//             console.log('*** TEST SEARCH - Max tries reached ***');
	//             clearInterval(request_loop);
	//         }

	//         if ($idaa_sess.recovery_meetings?.qry__status != null && $idaa_sess.recovery_meetings?.qry__status != 'done') {
	//             let random_delay = Math.floor(Math.random() * 25);
	//             search_delay += 25+random_delay;
	//             console.log(`*** TEST SEARCH - $idaa_sess.recovery_meetings.qry__status == loading wait *** search_delay=${search_delay}`);
	//             // $idaa_sess.status_qry__last_request_str = ft_qry_str;
	//         } else {
	//             console.log('*** TEST SEARCH - $idaa_sess.recovery_meetings.qry__status != loading ***');

	//             $idaa_sess.recovery_meetings.qry__status = 'loading';

	//             (and_physical) ?? null;
	//             (and_virtual) ?? null;
	//             (and_type) ?? null;
	//             // if (and_type) and_type = and_type;
	//             // if (!and_type) {
	//             //     and_type = null;
	//             // }

	//             console.log(`TEST - and_physical: ${and_physical}; and_virtual: ${and_virtual}; and_type: ${and_type}; ft_qry_str: ${ft_qry_str}; params:`, params);

	//             // search_submit_results = events_func.qry_ae_obj_li__event({
	//             //     api_cfg: $ae_api,
	//             //     for_obj_type: 'account',
	//             //     for_obj_id: $ae_loc.account_id,
	//             //     order_by_li: order_by_li,
	//             //     qry_conference: false,
	//             //     qry_physical: and_physical,
	//             //     qry_virtual: and_virtual,
	//             //     qry_type: and_type,
	//             //     qry_str: ft_qry_str,
	//             //     params: params,
	//             //     try_cache: try_cache,
	//             //     log_lvl: log_lvl,
	//             // })
	//             // .then(function (search_results) {
	//             //     // Processing the results from the search.
	//             //     $idaa_sess.recovery_meetings.qry__status = 'processing';
	//             //     $idaa_slct.event_obj_li = search_results;
	//             //     console.log(search_results);
	//             //     // $idaa_sess.recovery_meetings.qry__status = 'done';

	//             //     if (log_lvl) {
	//             //         console.log('TEST SEARCH - Search done. Pulling out the event_id_randoms.');
	//             //     }
	//             //     // console.log(`TEST search: ${$lq_kv__event_obj_li}`);

	//             //     // event_id_random_li = [];

	//             //     // We need to loop through the array of objects and get the event_id_random from each object a new list of event_id_randoms. Then we can use this list to get the full objects from the database.
	//             //     let tmp_li = []; // This is to prevent the array from constantly updating and triggering the liveQuery.
	//             //     if (search_results && search_results.length) {
	//             //         for (let i = 0; i < search_results.length; i++) {
	//             //             tmp_li.push($idaa_slct.event_obj_li[i].event_id_random);
	//             //         }
	//             //     }
	//             //     event_id_random_li = tmp_li;
	//             // })
	//             // .finally(() => {

	//             //     // event_id_random_li = $idaa_slct.event_obj_li.map(session_obj => session_obj.event_id_random);

	//             //     // Finally done with the search.
	//             //     $idaa_sess.recovery_meetings.qry__status = 'done';

	//             //     if (log_lvl > 1) {
	//             //         console.log(`TEST SEARCH - event_id_random_li:`, event_id_random_li);
	//             //         // console.log(`TEST SEARCH - search live query: ${$lq_kv__event_obj_li}`);
	//             //     }

	//             // });
	//             clearInterval(request_loop);
	//         }
	//     }, search_delay);
	// }
</script>

<!-- preset-filled-surface-200-800 -->
<section
	class="
        ae_section ae_options filters_and_search
        flex flex-col flex-wrap gap-1 items-center justify-center
        my-2 p-2
        preset-outlined-surface-300-700
        rounded-lg
        d-flex
        align-items-center justify-content-center
        "
>
	<Help_tech
		e_class=""
		e_class_h1="novi_m0"
		e_class_h2="novi_m0"
		e_class_form_hidden="float-right"
		e_class_form_showing="float-none"
		btn_class="novi_btn"
		show_btn_class=""
		additional_kv={{
			novi_uuid: $idaa_loc.novi_uuid,
			novi_email: $idaa_loc.novi_email,
			novi_full_name: $idaa_loc.novi_full_name
		}}
	></Help_tech>

	<!-- WARNING: Using m-auto to work in the Novi iframe -->
	<form
		onsubmit={preventDefault(() => {
			$idaa_trig.event_li_qry = true;
		})}
		autocomplete="off"
		class="
            search_form
            flex flex-col flex-wrap gap-1 items-center justify-center w-full max-w-2xl
            m-auto
            "
	>
		<div
			class="ae_group flex flex-row flex-wrap gap-2 w-full items-center justify-center border-b border-surface-300-700 p-1"
		>
			<button
				type="button"
				class="
                    novi_btn novi_smallest
                    btn btn-sm
                    preset-tonal-secondary
                    preset-outlined-secondary-200-800 hover:preset-filled-secondary-400-600
                    transition-all *:hover:inline
                    "
				onclick={() => {
					$idaa_loc.recovery_meetings.qry__fulltext_str = '';
					$idaa_sess.recovery_meetings.qry__fulltext_str = '';
					$idaa_trig.event_li_qry = true;
				}}
				title="Clear search text"
			>
				<!-- <span class="fas fa-backspace"></span> -->
				<!-- <span class="fas fa-broom"></span> -->
				<span class="fas fa-remove-format"></span>
				<span class="hidden">Clear Text</span>
			</button>

			<!-- svelte-ignore a11y_autofocus -->
			<input
				type="search"
				placeholder="Name, location, day of week, contacts, etc."
				id="meeting_qry__fulltext_str"
				name="qry__fulltext_str"
				bind:value={$idaa_loc.recovery_meetings.qry__fulltext_str}
				onkeyup={() => {
					$idaa_trig.event_li_qry = true;
				}}
				autofocus
				style="width: 50%;"
				class="
                    input text-sm hover:text-2xl font-semibold font-mono w-80
                    transition-all
                    preset-tonal-tertiary
                    preset-outline-tertiary-200-800
                    form-control
                "
			/>

			<button
				type="submit"
				class="
                    novi_btn btn-secondary font-weight-bold
                    btn btn-lg
                    preset-tonal-primary
                    preset-outlined-primary-200-800 hover:preset-filled-primary-400-600
                    text-2xl font-bold w-48
                    transition-all
                    "
				title="Search for meetings by day of week, location, contacts, etc."
			>
				<!-- <span class="fas fa-search m-1"></span> -->
				{#if $idaa_sess.recovery_meetings.qry__status == 'loading'}
					<span class="fas fa-spinner fa-spin mx-1 text-success-800-200"></span>
				{:else}
					<span class="fas fa-search mx-1 text-neutral-800/80"></span>
					<!-- {#if $idaa_sess.recovery_meetings.qry__status == 'done'}
                        <span class="fas fa-check m-1"></span>
                    {/if} -->
				{/if}
				Search
			</button>
		</div>

		<div
			class="flex flex-row flex-wrap gap-2 w-full max-w-xl items-center justify-start border-b border-surface-300-700 p-1"
		>
			<span class="text-xs form-check-label d-inline-block"> Location? </span>
			<!-- <div class="ae_row ae_flex_justify_around ae_width_md"> -->
			<label
				class="legend w-auto text-sm font-semibold inline form-check-label flex-row gap-1 items-center justify-center d-inline-block"
			>
				<span> Virtual </span>
				<input
					name="qry_virtual"
					type="checkbox"
					bind:checked={$idaa_loc.recovery_meetings.qry__virtual}
					onchange={() => {
						$idaa_trig.event_li_qry = true;
					}}
					class="checkbox inline form-check-input d-inline-block"
				/>
			</label>
			<label
				class="legend w-auto text-sm font-semibold inline form-check-label flex-row gap-1 items-center justify-center d-inline-block"
			>
				In-person
				<input
					name="qry_physical"
					type="checkbox"
					bind:checked={$idaa_loc.recovery_meetings.qry__physical}
					onchange={() => {
						$idaa_trig.event_li_qry = true;
					}}
					class="checkbox inline form-check-input d-inline-block"
				/>
			</label>
			<!-- </div> -->
		</div>

		<div
			class="flex flex-row flex-wrap gap-2 w-full max-w-xl items-center justify-start border-b border-surface-300-700 p-1"
		>
			<span class="text-xs form-check-label d-inline-block"> Type? </span>

			<!-- <div class="ae_row ae_flex_justify_around ae_width_100"> -->
			<label
				class="legend w-auto text-sm font-semibold inline form-check-label flex-row gap-1 items-center justify-center d-inline-block"
			>
				<span> All </span>
				<input
					name="qry_type"
					type="radio"
					value=""
					bind:group={$idaa_loc.recovery_meetings.qry__type}
					onchange={() => {
						$idaa_trig.event_li_qry = true;
					}}
					class="radio inline form-check-input d-inline-block"
					title="Show all meeting types"
				/>
			</label>
			<label
				class="legend w-auto text-sm font-semibold inline form-check-label flex-row gap-1 items-center justify-center d-inline-block"
			>
				IDAA
				<input
					name="qry_type"
					type="radio"
					value="IDAA"
					bind:group={$idaa_loc.recovery_meetings.qry__type}
					onchange={() => {
						$idaa_trig.event_li_qry = true;
					}}
					class="radio inline form-check-input d-inline-block"
					title="Open to IDAA members only"
				/>
			</label>
			<label
				class="legend w-auto text-sm font-semibold inline form-check-label flex-row gap-1 items-center justify-center d-inline-block"
			>
				Caduceus
				<input
					name="qry_type"
					type="radio"
					value="Caduceus"
					bind:group={$idaa_loc.recovery_meetings.qry__type}
					onchange={() => {
						$idaa_trig.event_li_qry = true;
					}}
					class="radio inline form-check-input d-inline-block"
					title="Open to all healthcare workers including those who do not qualify for IDAA"
				/>
			</label>
			<label
				class="legend w-auto text-sm font-semibold inline form-check-label flex-row gap-1 items-center justify-center d-inline-block"
			>
				Family Recovery
				<input
					name="qry_type"
					type="radio"
					value="Family Recovery"
					bind:group={$idaa_loc.recovery_meetings.qry__type}
					onchange={() => {
						$idaa_trig.event_li_qry = true;
					}}
					class="radio inline form-check-input d-inline-block"
					title="Open to spouses, parents, and children of medical professionals who have substance use disorder."
				/>
			</label>
			<!-- {#if $ae_loc.administrator_access}
            <label>Al-Anon (old)
                <input
                    name="qry_type"
                    type="radio"
                    value="Al-Anon"
                    bind:group={$idaa_loc.recovery_meetings.qry__type}
                    on:change={() => {$idaa_trig.event_li_qry = true;}}
                    >
            </label>
            <label>Other (old)
                <input
                    name="qry_type"
                    type="radio"
                    value="Other"
                    bind:group={$idaa_loc.recovery_meetings.qry__type}
                    on:change={() => {$idaa_trig.event_li_qry = true;}}
                    >
            </label>
            {/if} -->
			<!-- </div> -->
		</div>
	</form>

	<!-- {#await idaa_event_obj_li_get_promise}
        <div>Loading events...</div>
    {:catch error}
        <div>Error: {error.message}</div>
    {/await} -->

	<div
		class="ae_group ae_row flex flex-row flex-wrap gap-2 w-full max-w-full items-center justify-center p-1"
	>
		<!-- Max events select options -->
		<span class="flex flex-row gap-1 items-center justify-around">
			<label class="text-sm w-32 md:w-42 form-group" for="qry_limit__events">
				Max results:

				<select
					id="qry_limit__events"
					bind:value={$idaa_loc.recovery_meetings.qry__limit}
					onchange={() => {
						$idaa_trig.event_li_qry = true;
					}}
					class="
                    select w-20 text-sm inline-block
                    px-1
                    preset-tonal-tertiary
                    preset-outline-tertiary-200-800
                    form-control col-sm-12
                "
				>
					<option value={10}>10</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} class:hidden={!$ae_loc.trusted_access}>200</option>
					<option value={500} class:hidden={!$ae_loc.trusted_access}>500</option>
				</select>
			</label>
			<!-- </span> -->

			<!-- Sort by last updated date or by name -->
			<!-- <span
            class="flex flex-row gap-1 items-center justify-around"
            > -->
			<label class="text-sm w-32 md:w-42 form-group" for="qry_order_by__events">
				Sort by:

				<select
					id="qry_order_by__events"
					bind:value={$idaa_loc.recovery_meetings.qry__order_by}
					onchange={() => {
						if ($idaa_loc.recovery_meetings.qry__order_by == 'updated_on') {
							$idaa_loc.recovery_meetings.qry__order_by = 'updated_on'; //  For the IDB index query
							$idaa_loc.recovery_meetings.qry__order_by_li = {
								priority: 'DESC',
								sort: 'DESC',
								updated_on: 'DESC',
								created_on: 'DESC',
								name: 'ASC'
							}; // For the SQL query
						} else {
							$idaa_loc.recovery_meetings.qry__order_by = 'name'; //  For the IDB index query
							$idaa_loc.recovery_meetings.qry__order_by_li = {
								priority: 'DESC',
								sort: 'DESC',
								name: 'ASC',
								updated_on: 'DESC',
								created_on: 'DESC'
							}; // For the SQL query
						}
						$idaa_trig.event_li_qry = true;
					}}
					class="
                    select w-40 text-sm inline-block
                    px-1
                    preset-tonal-tertiary
                    preset-outline-tertiary-200-800
                    form-control col-sm-12
                "
				>
					<option value="updated_on">Last Updated</option>
					<option value="name">Meeting Name</option>
				</select>
			</label>
		</span>

		<span class="flex flex-row gap-1 items-center justify-around">
			{#if $ae_loc.edit_mode && $ae_loc.trusted_access && (!$idaa_loc.recovery_meetings.qry__hidden || $idaa_loc.recovery_meetings.qry__hidden == 'not_hidden')}
				<button
					type="button"
					onclick={() => {
						$idaa_loc.recovery_meetings.qry__hidden = 'all';
						$idaa_loc.recovery_meetings.qry__limit = 200;
						$idaa_trig.event_li_qry = true;
					}}
					class="
                    novi_btn btn_show_recovery_mtg_event ae_btn btn-info
                    btn btn-sm
                    preset-tonal-secondary preset-outlined-secondary-200-800 hover:preset-filled-secondary-200-800
                    transition
                    "
				>
					<span class="fas fa-eye m-1"></span> Show Hidden Events
				</button>
			{:else if $ae_loc.trusted_access && $idaa_loc.recovery_meetings.qry__hidden != 'not_hidden'}
				<button
					type="button"
					onclick={() => {
						$idaa_loc.recovery_meetings.qry__hidden = 'not_hidden';
						// $idaa_loc.recovery_meetings.qry__limit = 100;
						$idaa_trig.event_li_qry = true;
					}}
					class="
                    novi_btn btn_hide_recovery_mtg_event ae_btn btn-info
                    btn btn-sm
                    preset-tonal-secondary preset-outlined-secondary-200-800 hover:preset-filled-secondary-200-800
                    transition
                    "
				>
					<span class="fas fa-eye-slash m-1"></span> Hide Hidden Events
				</button>
			{/if}

			{#if $ae_loc.edit_mode && $ae_loc.administrator_access && (!$idaa_loc.recovery_meetings.qry__enabled || $idaa_loc.recovery_meetings.qry__enabled == 'enabled')}
				<button
					type="button"
					onclick={() => {
						$idaa_loc.recovery_meetings.qry__hidden = 'all';
						$idaa_loc.recovery_meetings.qry__enabled = 'all';
						$idaa_loc.recovery_meetings.qry__limit = 500;
						$idaa_trig.event_li_qry = true;
					}}
					class="
                    novi_btn btn_show_recovery_mtg_event ae_btn btn-warning
                    btn btn-sm
                    preset-tonal-warning preset-outlined-warning-200-800 hover:preset-filled-warning-200-800
                    transition
                    "
				>
					<span class="fas fa-eye m-1"></span> Show Disabled Events
				</button>
			{:else if $ae_loc.administrator_access && $idaa_loc.recovery_meetings.qry__enabled != 'enabled'}
				<button
					type="button"
					onclick={() => {
						$idaa_loc.recovery_meetings.qry__enabled = 'enabled';
						$idaa_trig.event_li_qry = true;
					}}
					class="
                    novi_btn btn_hide_recovery_mtg_event ae_btn btn-warning
                    btn btn-sm
                    preset-tonal-warning preset-outlined-warning-200-800 hover:preset-filled-warning-200-800
                    transition
                    "
				>
					<span class="fas fa-eye-slash m-1"></span> Hide Disabled Events
				</button>
			{/if}
		</span>

		<span class="flex flex-row gap-1 items-center justify-around">
			{#if ($ae_loc.trusted_access && $ae_loc.edit_mode) || $idaa_loc.novi_uuid}
				<button
					type="button"
					onclick={() => {
						if (!confirm('Create new meeting?')) {
							return false;
						}
						$idaa_slct.event_id = null;
						$idaa_slct.event_obj = {};

						// Get the users current timezone
						// let timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
						// let timezone = $ae_loc.timezone || 'UTC'; // Fallback to UTC if timezone is not set

						let data_kv = {
							name: 'Change NEW Recovery Meeting Name'
							// timezone: timezone,
						};
						if (log_lvl) {
							console.log('Creating new event (recovery meeting) with data_kv:', data_kv);
						}

						events_func
							.create_ae_obj__event({
								api_cfg: $ae_api,
								account_id: $ae_loc.account_id,
								data_kv: data_kv,
								log_lvl: log_lvl
							})
							.then((results) => {
								if (log_lvl) {
									console.log('New event (recovery meeting) created:', results);
								}
								$idaa_slct.event_id = results?.event_id_random;
								$idaa_slct.event_obj = {
									...results
								};
								$idaa_sess.recovery_meetings.edit__event_obj = $idaa_slct.event_id;
								$idaa_loc.recovery_meetings.edit__event_obj = $idaa_slct.event_id;

								// if (!$idaa_loc.recovery_meetings.edit_kv) {
								//     $idaa_loc.recovery_meetings.edit_kv = {};
								// }
								// $idaa_loc.recovery_meetings.edit_kv[$idaa_slct.event_id] = 'current';

								// alert(`Event created successfully! ${$idaa_slct.event_id}`);
							})
							.catch((error) => {
								console.error('Error updating event (recovery meeting):', error);
								alert('Failed to update event (recovery meeting).');
							})
							.finally(() => {
								if ($idaa_slct.event_id) {
									goto(`/idaa/recovery_meetings/${$idaa_slct.event_id}`);
								} else {
									alert('Failed to create new event (recovery meeting).');
								}
							});
					}}
					class="
                novi_btn btn-tertiary
                text-xs
                btn btn-sm
                preset-tonal-warning preset-outlined-warning-200-800 hover:preset-filled-warning-200-800
                transition
                "
					disabled={!$ae_loc.authenticated_access}
				>
					<span class="fas fa-plus m-1"></span> Create New Meeting
				</button>
			{/if}

			{#if $ae_loc.edit_mode && $ae_loc.trusted_access}
				<button
					type="button"
					onclick={() => {
						if (!confirm('Download exported data Excel file?')) {
							return false;
						}
						ae_promises.download__events_export = core_func.handle_download_export__obj_type({
							api_cfg: $ae_api,
							get_obj_type: 'event',
							for_obj_type: 'account',
							for_obj_id: $slct.account_id,
							exp_alt: 'idaa',
							file_type: 'Excel',
							return_file: true,
							filename: `${$ae_loc.account_code}_IDAA_Recovery_Meetings_export_${ae_util.iso_datetime_formatter()}.xlsx`,
							auto_download: true,
							log_lvl: 1
						});
					}}
					class="
                novi_btn btn-tertiary
                text-xs
                btn btn-sm
                preset-tonal-warning preset-outlined-warning-200-800 hover:preset-filled-warning-200-800
                transition
                "
					title={`Download meeting data for ${$ae_loc.account_name}`}
				>
					{#await ae_promises.download__events_export}
						<span class="fas fa-spinner fa-spin m-1"></span>
						<!-- <span class="loading-text">
                Downloading...
            </span> -->
					{:then}
						<!-- Done? -->
						<span class="fas fa-download m-1"></span>
					{/await}
					Export All Data
				</button>
			{/if}
		</span>
	</div>
</section>
<!-- END: div filters_and_search -->
