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

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

    // *** Import Svelte specific
    import { goto } from '$app/navigation';

    // *** Import other supporting libraries

    // *** Import Aether specific variables and functions
    // import type { key_val } from '$lib/ae_stores';
    import {
        ae_snip,
        ae_loc,
        ae_sess,
        ae_api,
        ae_trig,
        slct,
        slct_trigger
    } from '$lib/stores/ae_stores';
    import {
        idaa_loc,
        idaa_sess,
        idaa_slct,
        idaa_trig,
        idaa_prom
    } from '$lib/stores/ae_idaa_stores';
    import { posts_func } from '$lib/ae_posts/ae_posts_functions';
    import Help_tech from '$lib/app_components/e_app_help_tech.svelte';

    // let ae_promises: key_val = {};
    // let ae_tmp: key_val = {};
    // let ae_trigger: any = null;
    // let ae_triggers: key_val = {};

    // let search_submit_results: any = null;

    // if (log_lvl) console.log('** Component Loaded: ** Post Options');
</script>

<!-- preset-filled-surface-500 -->
<!-- text-white -->
<div
    class="
        ae_section ae_options filters_and_search
        novi_bg_light
        flex flex-row flex-wrap gap-1
        items-center justify-center
        my-2 p-2 border rounded-md

        preset-outlined-surface-800-200

        w-full max-w-4xl
        m-auto
        "
>
    <div
        class="
            ae_group ae_row
            flex flex-row flex-wrap gap-1
            w-full max-w-2xl items-center justify-center
            "
    >
        <!-- Max posts select options -->
        <span class="flex flex-row gap-1 items-center justify-around">
            <label
                class="
                    novi_label
                    text-sm text-gray-700 dark:text-gray-300 w-32 md:w-42 form-group
                    "
                for="qry_limit__posts"
            >
                Max posts:

                <select
                    id="qry_limit__posts"
                    bind:value={$idaa_loc.bb.qry__limit}
                    onchange={() => {
                        $idaa_trig.post_li = 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} class:hidden={!$ae_loc.trusted_access}>75</option>
                    <option value={100} class:hidden={!$ae_loc.trusted_access}>100</option>
                    <option value={150} class:hidden={!$ae_loc.trusted_access}>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>

        {#if $ae_loc.trusted_access && (!$idaa_loc.bb.qry__hidden || $idaa_loc.bb.qry__hidden == 'not_hidden')}
            <button type="button"
                onclick={() => {
                    $idaa_loc.bb.qry__hidden = 'all';
                    $idaa_loc.bb.qry__limit = 100;
                    $idaa_trig.post_li = true;
                }}
                class="
                    novi_btn btn_hide_bb_post 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 Posts
            </button>
        {:else if $ae_loc.trusted_access && $idaa_loc.bb.qry__hidden != 'not_hidden'}
            <button type="button"
                onclick={() => {
                    $idaa_loc.bb.qry__hidden = 'not_hidden';
                    $idaa_trig.post_li = true;
                }}
                class="
                    novi_btn btn_hide_bb_post 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 Posts
            </button>
        {/if}

        {#if $ae_loc.edit_mode && $ae_loc.manager_access && (!$idaa_loc.bb.qry__enabled || $idaa_loc.bb.qry__enabled == 'enabled')}
            <button type="button"
                onclick={() => {
                    $idaa_loc.bb.qry__enabled = 'all';
                    $idaa_loc.bb.qry__hidden = 'all';
                    $idaa_loc.bb.qry__limit = 500;
                    $idaa_trig.post_li = true;
                }}
                class="
                    novi_btn btn_show_bb_post 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 Posts
            </button>
        {:else if $ae_loc.edit_mode && $ae_loc.manager_access && $idaa_loc.bb.qry__enabled != 'enabled'}
            <button type="button"
                onclick={() => {
                    $idaa_loc.bb.qry__enabled = 'enabled';
                    $idaa_trig.post_li = true;
                }}
                class="
                    novi_btn btn_hide_bb_post 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 Posts
            </button>
        {/if}

        {#if ($ae_loc.trusted_access && $ae_loc.edit_mode) || $idaa_loc.novi_uuid}
            <button type="button"
                disabled={!$ae_loc.authenticated_access}
                onclick={() => {
                    if (!confirm('Create new post?')) {
                        return false;
                    }

                    $idaa_slct.post_obj = {};

                    let data_kv = {
                        external_person_id: $idaa_loc.novi_uuid,
                        title: '',
                        full_name: $idaa_loc.novi_full_name,
                        email: $idaa_loc.novi_email,
                        enable: true
                    };
                    if (log_lvl) {
                        console.log('Creating new post with data_kv:', data_kv);
                    }

                    posts_func
                        .create_ae_obj__post({
                            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 post created:', results);
                            }
                            // V3 Standard: Use clean ID field names
                            const clean_post_id = results?.id || results?.post_id;
                            $idaa_slct.post_id = clean_post_id;
                            $idaa_slct.post_obj = {
                                ...results
                            };
                            $idaa_sess.bb.edit__post_obj = clean_post_id;
                            $idaa_loc.bb.edit__post_obj = clean_post_id;
                        })
                        .catch((error) => {
                            console.error('Error creating post:', error);
                            alert('Failed to create post.');
                        })
                        .finally(() => {
                            const final_id = $idaa_slct.post_id;
                            if (final_id) {
                                if (log_lvl) console.log(`Redirecting to new post: ${final_id}`);
                                goto(`/idaa/bb/${final_id}`);
                            } else {
                                alert('Failed to create new post.');
                            }
                        });

                    // $idaa_slct.post_id = '';
                    // $idaa_slct.post_id = undefined;
                    // $idaa_slct.post_id = null;
                    // $idaa_slct.post_obj = {};

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

                    // $idaa_loc.bb.show_main__options = false;
                    // $idaa_loc.bb.show_list__post_obj_li = false;
                    // $idaa_loc.bb.show_view__post_obj = false;
                    // $idaa_loc.bb.show_edit__post_obj = true;

                    // $idaa_sess.bb.show__modal_view__post_id = true;
                    // $idaa_sess.bb.show__inline_edit__post_obj = true;
                }}
                class="
                novi_btn novi_white btn-tertiary
                text-xs
                btn btn-sm
                preset-tonal-warning preset-outlined-warning-200-800 hover:preset-filled-warning-200-800
                transition
                "
                title="Create new post"
            >
                <span class="fas fa-plus m-1"></span> Create New Post
            </button>
        {/if}
    </div>

    <Help_tech
        e_class=""
        e_class_h1="novi_m0"
        e_class_h2="novi_m0"
        e_class_form_hidden="float-right"
        e_class_form_showing="inline-block"
        btn_class="novi_btn"
        show_btn_class="btn-info"
        additional_kv={{
            novi_uuid: $idaa_loc.novi_uuid,
            novi_email: $idaa_loc.novi_email,
            novi_full_name: $idaa_loc.novi_full_name
        }}
    ></Help_tech>
</div>
<!-- END: div filters_and_search -->
