<script lang="ts">
type key_val = {
    [key: string]: any;
};
// *** Import Svelte core
import { onMount } from 'svelte';
// import { Spinner } from 'flowbite-svelte';

// *** Import Aether core variables and functions
import { ae, api, Element_modal_v3 } from 'aether_npm_lib';

import { slct, slct_trigger, ae_app } from './stores';

// *** Import Aether core components
// import { get_data_store_obj_w_code } from '../data_store/stores_data_store_api.js';

// *** Import Aether module variables and functions

// *** Import Aether module components
import Edit_event_obj from './ae_idaa_comp__event_obj_id_edit.svelte';
import List_event_obj from './ae_idaa_comp__event_obj_li.svelte';
import View_event_obj from './ae_idaa_comp__event_obj_id_view.svelte';

// *** Export/Exposed variables and functions for component
// export let account_id: string = $page['page_for']['account_id'];
export let account_id: string = $ae_app.account_id;
export let event_id: string = $ae_app.events.event_id;

export let novi_uuid: string = $ae_app.novi_uuid;
export let novi_email: string = $ae_app.novi_email;
export let novi_full_name: string = $ae_app.novi_full_name;
export let novi_admin_li: string = $ae_app.novi_admin_li;
export let novi_trusted_li: string = $ae_app.novi_trusted_li;

// *** Set initial variables
$ae_app.novi_uuid = novi_uuid;
$ae_app.novi_email = decodeURIComponent(novi_email);
$ae_app.novi_full_name = decodeURIComponent(novi_full_name);
$ae_app.novi_admin_li = novi_admin_li;
$ae_app.novi_trusted_li = novi_trusted_li;

$ae_app.current_timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (!$ae_app.lu_country_subdivision_list) {
    $ae_app.lu_country_subdivision_list = [];
}
if (!$ae_app.lu_country_list ) {
    $ae_app.lu_country_list = [];
}
if (!$ae_app.lu_timezone_list) {
    $ae_app.lu_timezone_list = [];
}

$ae_app.events.show_main__options = true; // Section for: search area, buttons for filtering events, create new event (Meeting).
$ae_app.events.show_list__event_obj_li = true;
$ae_app.events.fulltext_search_qry_str = '';
$ae_app.events.qry_virtual = false;
$ae_app.events.qry_physical = false;
$ae_app.events.qry_type = '';

// NOTE: Check if the novi_uuid is in the novi_admin_li list
if ($ae_app.novi_uuid && $ae_app.novi_admin_li) {
    if ($ae_app.novi_admin_li.includes($ae_app.novi_uuid)) {
        $ae_app.access_type = 'administrator';
        $ae_app.administrator_access = true;
        $ae_app.trusted_access = true;
    }
}
// NOTE: Check if the novi_uuid is in the novi_trusted_li list
if ($ae_app.novi_uuid && $ae_app.novi_trusted_li) {
    if ($ae_app.novi_trusted_li.includes($ae_app.novi_uuid)) {
        $ae_app.access_type = 'trusted';
        $ae_app.trusted_access = true;
    }
}

if ($ae_app.administrator_access) {
    $ae_app.events.enabled = 'enabled';
    $ae_app.events.hidden = 'not_hidden';
    $ae_app.events.limit = 150;
} else if ($ae_app.trusted_access) {
    $ae_app.events.enabled = 'enabled';
    $ae_app.events.hidden = 'not_hidden';
    $ae_app.events.limit = 75;
} else {
    $ae_app.events.enabled = 'enabled';
    $ae_app.events.hidden = 'not_hidden';
    $ae_app.events.limit = 25;
}

let idaa_event_obj_li_get_promise;
let idaa_event_obj_get_promise;
let data_store_obj_get_promise;
let get_ds_submitter_help_promise = handle_get_data_store_obj_w_code({code: 'recovery_meetings_info'});

if (account_id) {
    $slct.account_id = account_id;
    handle_load_idaa_event_obj_li({account_id: $slct.account_id, try_cache: false});
}

// NOTE: This if event_id is not fully ready yet -2023-09-08
if (event_id) {
    console.log(`Selected Event ID: ${event_id}`);
    $slct.event_id = event_id;
    $slct_trigger = 'load__event_obj';

    // handle_load_event_id_obj({event_id: $slct.event_id, try_cache: false});

    // // Auto show the selected Event ID
    // // Set the URL param "event_id" to the current Event ID. This is a just in case.
    // const url = new URL(location);
    // url.searchParams.set('event_id', $slct.event_id);
    // history.pushState({}, '', url);

    // let message = {'event_id': $slct.event_id};
    // window.parent.postMessage(message, "*");

    $ae_app.events.show_main__options = true;
    $ae_app.events.show_list__event_obj_li = true;
    $ae_app.events.show_view__event_obj = true;
    $ae_app.events.show_edit__event_obj = false;
}

if (!$ae_app.client_content) {
    $ae_app.client_content = {};
}

onMount(() => {
    console.log('** Component Mounted: ** IDAA Recovery Meetings - AE Events: Main');

    document.getElementById('meeting_fulltext_search_qry_str').focus();
});


$: if ($ae_app.iframe_height && $ae_app.iframe_height_modal_body) {
    console.log('getting new dimensions for iframe:', $ae_app.iframe_height, $ae_app.iframe_height_modal_body);

    let iframe_height = 0;

    if ($ae_app.iframe_height > $ae_app.iframe_height_modal_body) {
        iframe_height = $ae_app.iframe_height;
    } else {
        iframe_height = $ae_app.iframe_height_modal_body;

        // console.log($ae_app.modal_dimensions);

        if ($ae_app.modal_dimensions && $ae_app.modal_dimensions.header_height) {
            iframe_height = iframe_height + $ae_app.modal_dimensions.header_height;
        }
        if ($ae_app.modal_dimensions && $ae_app.modal_dimensions.footer_height) {
            iframe_height = iframe_height + $ae_app.modal_dimensions.footer_height;
        }
        // iframe_height = iframe_height + 50; // Just in case
    }

    console.log(`Suggested new iframe_height: ${iframe_height}`);
    window.parent.postMessage({'iframe_height': iframe_height}, "*"); // This should be in pixels
} else if ($ae_app.iframe_height) {
    console.log('setting new iframe_height:', $ae_app.iframe_height);

    // let iframe_height = $ae_app.iframe_height;

    window.parent.postMessage({'iframe_height': $ae_app.iframe_height}, "*"); // This should be in pixels
}


$: if ($slct_trigger == 'load__event_obj_li' && $slct.account_id) {
    if ($ae_app.events.qry_status == 'loading') {
        console.log('*** $ae_app.events.qry_status == loading ***');

        setTimeout(() => {
            console.log("Delayed for X second.");
            $slct_trigger = null;
            handle_load_idaa_event_obj_li({account_id: $slct.account_id, try_cache: false});
        }, 250);
    } else {
        console.log('*** $ae_app.events.qry_status != loading ***');
        $slct_trigger = null;
        handle_load_idaa_event_obj_li({account_id: $slct.account_id, try_cache: false});
    }
}

async function handle_load_idaa_event_obj_li({account_id, try_cache=true}) {
    console.log('*** handle_load_idaa_event_obj_li() ***');
    console.log($ae_app.events);

    // The fulltext search should be lower case
    // let fulltext_search_qry_str = $ae_app.events.fulltext_search_qry_str.toLowerCase();
    let fulltext_search_qry_str = $ae_app.events.fulltext_search_qry_str;
    let qry_virtual = $ae_app.events.qry_virtual;
    let qry_physical = $ae_app.events.qry_physical;
    let qry_type = $ae_app.events.qry_type;

    let enabled = $ae_app.events.enabled;
    let hidden = $ae_app.events.hidden;
    let limit = $ae_app.events.limit;
    let offset = $ae_app.events.offset;

    let params = {};

    let params_json: key_val = {};
    if (fulltext_search_qry_str && fulltext_search_qry_str.length > 2) {
        params_json['ft_qry'] = {
            'default_qry_str': fulltext_search_qry_str,
            'location_address_json_ext': fulltext_search_qry_str, // JSON extracted text DB field
            'contact_li_json_ext': fulltext_search_qry_str, // JSON extracted text DB field
            };
    }

    if (qry_virtual || qry_physical || qry_type) {
        params_json['and_qry'] = {};
        if (qry_virtual) params_json['and_qry']['virtual'] = true;
        if (qry_physical) params_json['and_qry']['physical'] = true;
        if (qry_type) params_json['and_qry']['type'] = qry_type;
    }

    console.log('params_json:', params_json);
    console.log(params_json);

    // NOTE: I am not sure if this is actually needed. It may save a little space in the URL.
    // if (JSON.stringify(params_json) == JSON.stringify({})) {
    //     params_json = null;
    // }
    $ae_app.events.qry_status = 'loading';
    idaa_event_obj_li_get_promise = api.get_ae_obj_li_for_obj_id_crud({
        api_cfg: $ae_app.ae_api,
        obj_type: 'event', // Event in this case is a recovery meeting.
        for_obj_type: 'account',
        for_obj_id: account_id,
        use_alt_table: true, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
        use_alt_base: true, // NOTE: This will use the base_name_alt value instead of the base_name value in the API config.
        enabled: enabled,
        hidden: hidden,
        order_by_li: {'priority': 'DESC', 'sort': 'DESC', 'updated_on': 'DESC', 'created_on': 'DESC'},
        // order_by_li: {'priority': 'DESC', 'sort': 'DESC', 'created_on': 'DESC', 'updated_on': 'DESC'},
        limit: limit,
        offset: offset,
        params_json: params_json,
        params: params,
        log_lvl: 1
        })

    .then(function (event_obj_li_get_result) {
        if (event_obj_li_get_result) {
            $slct.event_obj_li = event_obj_li_get_result;
            console.log(`Event list:`, $slct.event_obj_li);
        } else {
            $slct.event_obj_li = [];
        }
    })
    .catch(function (error) {
        console.log('No results returned or failed.', error);
    })
    .finally(function () {
        $ae_app.events.qry_status = 'done';
    });

    return idaa_event_obj_li_get_promise;
}


$: if ($slct_trigger == 'load__event_obj' && $slct.event_id) {
    $slct_trigger = null;

    handle_load_event_id_obj({event_id: $slct.event_id, try_cache: false});

    // Auto show the selected Event ID
    // Is this pushState needed here?
    // Set the URL param "event_id" to the current Event ID. This is a just in case.
    // const url = new URL(location);
    // url.searchParams.set('event_id', $slct.event_id);
    // history.pushState({}, '', url);

    // Is this postMessage needed here?
    // let message = {'event_id': $slct.event_id};
    // window.parent.postMessage(message, "*");

    // $ae_app.events.show_main__options = false;
    // $ae_app.events.show_list__event_obj_li = false;
    // $ae_app.events.show_view__event_obj = true;
    // $ae_app.events.show_edit__event_obj = false;
}

async function handle_load_event_id_obj({event_id, try_cache=false}) {
    console.log('*** handle_load_event_id_obj() ***');

    // let enabled = $ae_app.events.enabled;
    // let hidden = $ae_app.events.hidden;
    // let limit = $ae_app.events.limit;
    // let offset = $ae_app.events.offset;

    let params = {};

    idaa_event_obj_get_promise = api.get_ae_obj_id_crud({
        api_cfg: $ae_app.ae_api,
        obj_type: 'event',
        obj_id: event_id,
        use_alt_table: true, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
        use_alt_base: true, // NOTE: This will use the base_name_alt value instead of the base_name value in the API config.
        params: params,
        log_lvl: 0
        })
    .then(function (event_obj_get_result) {
        if (event_obj_get_result) {
            $slct.event_obj = event_obj_get_result;
            console.log(`Event object:`, $slct.event_obj);
        }

        // Auto show the selected Event ID
        // Is this pushState needed here?
        // Set the URL param "event_id" to the current Event ID.
        const url = new URL(location);
        url.searchParams.set('event_id', $slct.event_id);
        history.pushState({}, '', url);

        // Is this postMessage needed here?
        let message = {'event_id': $slct.event_id};
        window.parent.postMessage(message, "*");
    })
    .catch(function (error) {
        console.log('No results returned or failed.', error);
    });

    return idaa_event_obj_get_promise;
}

function handle_created_event_obj(event) {
    console.log('*** handle_created_event_obj() ***');
    console.log(event.detail);

    handle_load_event_id_obj({event_id: $slct.event_id, try_cache: false});

    const url = new URL(location);
    url.searchParams.set('event_id', $slct.event_id);
    history.pushState({}, '', url);

    let message = {'event_id': $slct.event_id};
    window.parent.postMessage(message, "*");

    // $slct_trigger = 'load__event_obj';
    $slct_trigger = 'load__event_obj_li';

    $ae_app.events.show_main__options = false;
    $ae_app.events.show_list__event_obj_li = false;
    $ae_app.events.show_view__event_obj = true;
    $ae_app.events.show_edit__event_obj = false;
}

function handle_updated_event_obj(event) {
    console.log('*** handle_updated_event_obj() ***');
    console.log(event.detail);

    // $slct_trigger = 'load__event_obj';
    $slct_trigger = 'load__event_obj_li';

    $ae_app.events.show_main__options = false;
    $ae_app.events.show_list__event_obj_li = false;
    $ae_app.events.show_view__event_obj = true;
    $ae_app.events.show_edit__event_obj = false;
}

function handle_deleted_event_obj(event) {
    console.log('*** handle_deleted_event_obj() ***');
    console.log(event.detail);

    const url = new URL(location);
    url.searchParams.delete('event_id');
    history.pushState({}, '', url);

    let message = {'event_id': null};
    window.parent.postMessage(message, "*");

    $slct_trigger = 'load__event_obj_li';

    $ae_app.events.show_main__options = true;
    $ae_app.events.show_list__event_obj_li = true;
    $ae_app.events.show_view__event_obj = false;
    $ae_app.events.show_edit__event_obj = false;
}


function handle_submit_form(event) {
    console.log('*** handle_submit_form() ***');
    console.log(event);

    event.preventDefault();
    event.stopPropagation();

    return false;
}
function handle_cancel_form(event) {
    console.log('*** handle_cancel_form() ***');
    console.log(event);

    event.preventDefault();
    event.stopPropagation();

    return false;
}

function handle_oninput_fulltext_search_qry_str(event) {
    console.log('*** handle_oninput_fulltext_search_qry_str() ***');
    console.log(event);

    // Check if this variable is a promise. The last query may still be being processed.
    // if (!idaa_event_obj_li_get_promise.promise) {
    //     console.log('*** handle_oninput_fulltext_search_qry_str() ***');
    //     console.log('*** Promise is still processing. ***');
    //     console.log(idaa_event_obj_li_get_promise);
    //     idaa_event_obj_li_get_promise.promise.finally(() => { idaa_event_obj_li_get_promise.promise.done = true; });
    // } else {
    //     $slct_trigger = 'load__event_obj_li';
    // }
    // if (idaa_event_obj_li_get_promise && idaa_event_obj_li_get_promise.then) {
    //     console.log('*** handle_oninput_fulltext_search_qry_str() ***');
    //     console.log('*** Promise is still processing. ***');
    //     console.log(idaa_event_obj_li_get_promise);
    //     $slct_trigger = 'load__event_obj_li';
    //     return;
    // }

    // fulltext_search_qry_str = event.target.value;

    // if ($ae_app.events.fulltext_search_qry_str.length > 2) {
    // } else {
    //     $ae_app.events.fulltext_search_qry_str = '';
    // }
    $slct_trigger = 'load__event_obj_li';
}

async function handle_get_data_store_obj_w_code({code, data_type='text'}) {
    console.log('*** handle_get_data_store_obj_w_code() ***');

    let get_item_result = window.localStorage.getItem(code);
    if (get_item_result) {
        $ae_app.events.ds[code] = get_item_result;
    } else {
        console.log('Get local storage item miss.');
    }

    data_store_obj_get_promise = api.get_data_store_obj_w_code({
        api_cfg: $ae_app.ae_api,
        data_store_code: code,
        data_type: data_type,
        log_lvl: 1
        })
    .then(function (get_data_store_result) {
        if (get_data_store_result) {

            if (data_type == 'text') {
                // console.log(get_data_store_result.text);
                $ae_app.events.ds[code] = get_data_store_result.text;
            } else if (data_type == 'json') {
                // console.log(get_data_store_result.json);
                $ae_app.events.ds[code] = get_data_store_result.json;
            }

            // console.log(`Code: ${$ae_app.events.ds[code]}`);
            // console.log(`Code:`, $ae_app.events.ds[code]);
        }
    })
    .catch(function (error) {
        console.log('No results returned or failed.', error);
    });
}
</script>

<!-- <svelte:window
	bind:outerHeight={ae_iframe_height}
  /> -->


<section id="osit_idaa_recovery_meetings" class="osit_events ae_main c_idaa c_idaa_recovery_meetings ae_main" bind:clientHeight={$ae_app.iframe_height}>
<!-- <header>
    <h1>Recovery Meetings for Healthcare Professionals</h1>
</header> -->

<section class="ae_section ae_meta">
    <p>
    Novi: {$ae_app.novi_full_name}
    <span class="details">
    (
        {$ae_app.novi_email}
        {#if $ae_app.administrator_access}
            <span class="access_type administrator_access">Administrator</span>
        {:else if $ae_app.trusted_access}
            <span class="access_type trusted_access">Trusted</span>
        {/if}
        <span class="novi_uuid">UUID: {$ae_app.novi_uuid}</span>
    )
    </span>
    </p>
    <!-- <p>Page height: {window.innerHeight}</p> -->
</section>

<!-- <div class="ae_overview">
    <p>IDAA-inclusive for all addictions. If a meeting is listed as an IDAA meeting, anyone may attend if they choose and are respectful of others. <strong>The door may be closed, but it is never locked.</strong></p>
    <p>Caduceus—Includes some IDAA members, but other healthcare workers attend depending on the individual meeting. These are listed as a courtesy.</p>
    <p>If a member wishes to start a virtual meeting with specific focus for IDAA members only, that may be indicated in the description. An announcement or invitation can also be placed on the Bulletin Board.</p>

    <p>If you are interested in Starting a Meeting, IDAA has developed a guide. <a href="https://static.oneskyit.com/c/IDAA/files/IDAA_-_How_to_Start_A_Meeting_Guide.pdf" class="ae_btn btn_md btn_outline_info"><span class="fas fa-file-pdf"></span> Open IDAA's How to Start an IDAA Recovery Meeting PDF</a> for additional information.</p>
</div> -->

{#if $ae_app.events.show_main__options}
<section class="ae_section ae_options ae_column event_obj__options">

    <div class="filters_and_search">
        <div class="ae_info recovery_meetings_info note">
            {@html $ae_app.events.ds.recovery_meetings_info}
        </div>
        <!-- <div class="note">Note: The state/province filter only includes states and provinces that are set for at least one meeting. Many virtual/online meetings do not have a state/province set. Some in-person meetings also do not have a state/province set. Please ask one of the meeting contacts to update this information if it is missing.</div> -->

        <form on:submit|preventDefault={handle_submit_form} on:keydown={e => e.key === 'Escape' && handle_cancel_form} class="search_form">
        <!-- <div class="ae_group">
        <input
            type="search"
            placeholder="Search (day of week, location, time, chair, etc.)"
            name="fulltext_search_qry_str"
            on:keyup={handle_oninput_fulltext_search_qry_str}
            bind:value={$ae_app.events.fulltext_search_qry_str}
            >
        </div> -->
        <div class="ae_group">
        <input
            type="search"
            placeholder="Search (day of week, location, chair, etc.)"
            id="meeting_fulltext_search_qry_str"
            name="fulltext_search_qry_str"
            bind:value={$ae_app.events.fulltext_search_qry_str}
            style="width: 50%;"
            class="bs-input"
            >
        <button
            on:click={handle_oninput_fulltext_search_qry_str}
            >
            <span class="fas fa-search"></span> Search
        </button>
        </div>

        <fieldset>
            <legend>Location?</legend>
            <div class="ae_row ae_flex_justify_around ae_width_md">
            <label>Virtual
                <input
                    name="qry_virtual"
                    type="checkbox"
                    bind:checked={$ae_app.events.qry_virtual}
                    on:change={() => {$slct_trigger = 'load__event_obj_li';}}
                    >
            </label>
            <label>In-person
                <input
                    name="qry_physical"
                    type="checkbox"
                    bind:checked={$ae_app.events.qry_physical}
                    on:change={() => {$slct_trigger = 'load__event_obj_li';}}
                    >
            </label>
            </div>
        </fieldset>

        <fieldset>
            <legend>Type?</legend>
            <div class="ae_row ae_flex_justify_around ae_width_100">
            <label>All
                <input
                    name="qry_type"
                    type="radio"
                    value=""
                    bind:group={$ae_app.events.qry_type}
                    on:change={() => {$slct_trigger = 'load__event_obj_li';}}
                    title="Show all meeting types"
                    >
            </label>
            <label>IDAA
                <input
                    name="qry_type"
                    type="radio"
                    value="IDAA"
                    bind:group={$ae_app.events.qry_type}
                    on:change={() => {$slct_trigger = 'load__event_obj_li';}}
                    title="Open to IDAA members only"
                    >
            </label>
            <label>Caduceus
                <input
                    name="qry_type"
                    type="radio"
                    value="Caduceus"
                    bind:group={$ae_app.events.qry_type}
                    on:change={() => {$slct_trigger = 'load__event_obj_li';}}
                    title="Open to all healthcare workers including those who do not qualify for IDAA"
                    >
            </label>
            <label>Family Recovery
                <input
                    name="qry_type"
                    type="radio"
                    value="Family Recovery"
                    bind:group={$ae_app.events.qry_type}
                    on:change={() => {$slct_trigger = 'load__event_obj_li';}}
                    title="Open to spouses, parents, and children of medical professionals who have substance use disorder."
                    >
            </label>
            <!-- {#if $ae_app.administrator_access}
            <label>Al-Anon (old)
                <input
                    name="qry_type"
                    type="radio"
                    value="Al-Anon"
                    bind:group={$ae_app.events.qry_type}
                    on:change={() => {$slct_trigger = 'load__event_obj_li';}}
                    >
            </label>
            <label>Other (old)
                <input
                    name="qry_type"
                    type="radio"
                    value="Other"
                    bind:group={$ae_app.events.qry_type}
                    on:change={() => {$slct_trigger = 'load__event_obj_li';}}
                    >
            </label>
            {/if} -->
            </div>
        </fieldset>

        </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">

        {#if $ae_app.trusted_access && $ae_app.events.hidden == 'not_hidden'}
            <button
                on:click={() => {
                    $ae_app.events.hidden = 'all';
                    $ae_app.events.limit = 150;
                    $slct_trigger = 'load__event_obj_li';
                }}
                class="btn_show_bb_post ae_btn btn btn-info"
                >
                <span class="fas fa-eye"></span> Show Hidden Events
            </button>
        {:else if $ae_app.trusted_access && $ae_app.events.hidden == 'all'}
            <button
                on:click={() => {
                    $ae_app.events.hidden = 'not_hidden';
                    $slct_trigger = 'load__event_obj_li';
                }}
                class="btn_hide_bb_post ae_btn btn btn-info"
                >
                <span class="fas fa-eye-slash"></span> Hide Hidden Events
            </button>
        {/if}

        {#if $ae_app.administrator_access && $ae_app.events.enabled == 'enabled'}
            <button
                on:click={() => {
                    $ae_app.events.hidden = 'all';
                    $ae_app.events.enabled = 'all';
                    $ae_app.events.limit = 500;
                    $slct_trigger = 'load__event_obj_li';
                }}
                class="btn_show_bb_post ae_btn btn btn-warning"
                >
                <span class="fas fa-eye"></span> Show Disabled Events
            </button>
        {:else if $ae_app.administrator_access && $ae_app.events.enabled == 'all'}
            <button
                on:click={() => {
                    $ae_app.events.enabled = 'enabled';
                    $slct_trigger = 'load__event_obj_li';
                }}
                class="btn_hide_bb_post ae_btn btn btn-warning"
                >
                <span class="fas fa-eye-slash"></span> Hide Disabled Events
            </button>
        {/if}

        <button
            on:click={() => {
                $slct.event_id = null;
                $slct.event_obj = {};

                const url = new URL(location);
                url.searchParams.delete('event_id');
                history.pushState({}, '', url);

                $ae_app.events.show_main__options = false;
                $ae_app.events.show_list__event_obj_li = false;
                $ae_app.events.show_view__event_obj = false;
                $ae_app.events.show_edit__event_obj = true;
            }}
            class="btn_new_recovery_meeting ae_btn btn btn-secondary"
            >
                <span class="fas fa-plus"></span> Create new Meeting
        </button>
        </div>

    </div> <!-- END: div filters_and_search -->

</section>
{/if}


{#if $ae_app.events.show_list__event_obj_li}
    <List_event_obj />
{/if}


{#if $ae_app.events.show_edit__event_obj}
<!-- <section class="ae_edit event_obj event_id idaa_event_id_edit"> -->
<Element_modal_v3
        show = { true }
        modal_cover_body = { false }
        report_client_dimensions = { true }
        on:close={ () => {
            $ae_app.events.show_main__options = true;
            $ae_app.events.show_list__event_obj_li = true;
            $ae_app.events.show_edit__event_obj = false;
            $ae_app.events.show_view__event_obj = false;

            const url = new URL(location);
            url.searchParams.delete('event_id');
            history.pushState({}, '', url);

            let message = {'event_id': null};
            window.parent.postMessage(message, "*");
        }}
        on:report__modal_dimensions={ (event) => {
            console.log('*** on:report__modal_dimensions ***');
            console.log(event.detail);

            $ae_app.modal_dimensions = event.detail.modal_dimensions;
        }}
        >
    <span slot="header_title">{@html ($slct.event_obj.name ? $slct.event_obj.name : 'New Recovery Meeting')}</span>

    <span slot="body">

        <Edit_event_obj
            on:created__meeting_obj={handle_created_event_obj}
            on:updated__meeting_obj={handle_updated_event_obj}
            on:deleted__meeting_obj={handle_deleted_event_obj}
            />

    </span>
</Element_modal_v3>
<!-- </section> -->
{/if}


{#if $ae_app.events.show_view__event_obj && $slct.event_obj}
<!-- <section class="ae_view event_obj event_id idaa_event_id_view"> -->
<Element_modal_v3
    show = { true }
    modal_cover_body = { false }
    report_client_dimensions = { true }
    on:close={ () => {
        $slct.event_id = null;
        $slct.event_obj = {};

        $ae_app.events.show_main__options = true;
        $ae_app.events.show_list__event_obj_li = true;
        $ae_app.events.show_view__event_obj = false;
        $ae_app.events.show_edit__event_obj = false;

        const url = new URL(location);
        url.searchParams.delete('event_id');
        history.pushState({}, '', url);

        let message = {'event_id': null};
        window.parent.postMessage(message, "*");
    }}
    on:report__modal_dimensions={ (event) => {
        console.log('*** on:report__modal_dimensions ***');
        console.log(event.detail);

        $ae_app.modal_dimensions = event.detail.modal_dimensions;
    }}
    >
    <span slot="header_title">{@html $slct.event_obj.name}</span>

    <span slot="body">

        <View_event_obj />

    </span>
</Element_modal_v3>
<!-- </section> -->
{/if}

</section>


<style>


/* .ae_meta {
    font-size: smaller;
    color: hsla(0,0%,50%,1);
} */

/* .ae_edit.event_obj {
    border: dashed thin hsla(0,0%,70%,1);

    margin: .5em .25em;
    padding: .5em .25em;

    background-color: hsla(0,0%,90%,1);
} */

/* .ae_view.event_obj {
    border: dashed thin hsla(0,0%,70%,1);

    margin: .5em .25em;
    padding: .5em .25em;

    background-color: hsla(0,0%,90%,1);
} */

/* :global(.ae_edit.event_id .element_ae_modal) {
    border: dashed thin hsla(0,0%,70%,1);

    margin: .5em .25em;
    padding: .5em .25em;

    background-color: hsla(0,0%,90%,1);
} */

</style>
