<script lang="ts">
    import { getDBEntries } from "../../api"
    import { socialIcons } from "../../config"
    import { spaLink } from "../actions"
    import { login, navigationCache } from "../store"
    import CrinkledSection from "./CrinkledSection.svelte"
    import Input from "./pagebuilder/blocks/form/Input.svelte"
    import { submitNewsletter } from "../functions/CommerceAPIs/tibiEndpoints/actions"
    import { onChange } from "./pagebuilder/profile/helper"
    enum NavigationType {
        MainNavigation = 0,
        ServiceNavigation = 2,
        LegalNavigation = 1,
    }
    let emailIsSubscribed = false
    let navigationEntries: NavigationEntry[] = []

    function elementsToCache(elements: NavigationElement[]) {
        elements.forEach((el) => {
            if (!el.external) {
                if (!$navigationCache[el.page]) $navigationCache[el.page] = el
                if (el.elements?.length > 0) elementsToCache(el.elements)
            }
        })
    }

    getDBEntries("navigation").then((navs) => {
        navigationEntries = navs.sort((a, b) => a.type - b.type)
        navigationEntries.forEach((nav) => elementsToCache(nav.elements))
    })
    export let className = ""
    let sections: {
        title: string
        links: NavigationElement[]
    }[] = []
    $: if (navigationEntries.length) sections = []

    let email = ""
    let dataProt = false
</script>

<CrinkledSection>
    <footer class="{'footer ' + className}">
        <section id="content-section">
            <section id="content-link-section">
                {#each sections as section}
                    <section class="content">
                        <div class="row">
                            <div class="col">
                                <small class="service"><em>{section?.title}</em></small>
                                <nav class="sub-points">
                                    <ul>
                                        {#each section?.links || [] as link}
                                            <li>
                                                <a
                                                    class="footer-nav-point"
                                                    use:spaLink
                                                    href="{link.page}"><small>{link.name}</small></a
                                                >
                                            </li>
                                        {/each}
                                    </ul>
                                </nav>
                            </div>
                        </div>
                    </section>
                {/each}
            </section>
            <section id="newsletter-section">
                <h4>Newsletter</h4>
                <form
                    on:submit|preventDefault|stopPropagation="{() => {
                        submitNewsletter(email, dataProt).then(() => {
                            emailIsSubscribed = true
                        })
                    }}"
                >
                    {#if emailIsSubscribed}
                        <p>Du hast dich zum Newsletter angemeldet!</p>
                    {:else}
                        <Input
                            type="text"
                            placeholder="E-Mail"
                            bind:value="{email}"
                            id="email"
                            onChange="{onChange}"
                        />
                        <div class="data-protection">
                            <Input
                                type="checkbox"
                                bind:value="{dataProt}"
                                id="dataprot"
                                onChange="{onChange}"
                            />
                            <p>
                                <a
                                    href="/datenschutz"
                                    use:spaLink>Datenschutz</a
                                > zum Newsletterversand akzeptieren
                            </p>
                        </div>
                        <button
                            class="btn cta primary"
                            type="submit"
                        >
                            Anmelden</button
                        >
                    {/if}
                </form>
            </section>
        </section>
        <section id="icons-section">
            <div class="line">
                <div class="line-1"></div>
                <img
                    alt="Symbol"
                    class="symbol"
                    src="../../../logo/logoShort.svg"
                />
                <div class="line-2"></div>
            </div>
            <ul class="social">
                {#each Object.keys(socialIcons) as icon}
                    <li>
                        <a
                            href="{socialIcons[icon]}"
                            use:spaLink
                            target="_blank"
                            aria-label="{icon}"
                        >
                            <figure class="footer-icon">
                                <img
                                    alt="{icon}"
                                    src="../../../media/{icon}.svg"
                                />
                            </figure></a
                        >
                    </li>
                {/each}
            </ul>
        </section>
        <section id="legal-section">
            <div class="wrapper">
                <small class="">© 2024 | BKDF - Bin Krass Du Fass | Alle Rechte vorbehalten.</small>
                <nav class="nav-points">
                    <ul>
                        {#each navigationEntries.length ? navigationEntries[NavigationType.LegalNavigation].elements : [] as link}
                            <li>
                                <a
                                    class="footer-nav-point-bottom"
                                    use:spaLink
                                    href="{link.page}"><small>{link.name}</small></a
                                >
                            </li>
                        {/each}
                    </ul>
                </nav>
            </div>
        </section>
    </footer>
</CrinkledSection>

<style lang="less">
    @import "../../lib/assets/css/variables.less";
    .footer {
        &,
        & * {
            box-sizing: border-box;
        }
        background: var(--neutral-white);
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: flex-start;
        position: relative;
        width: 100%;
        gap: 1.5rem;
        #content-section {
            padding: 0px var(--horizontal-default-margin);
            max-width: var(--small-max-width);
            width: 100%;
            display: flex;
            flex-direction: row;
            gap: 1.5rem;
            align-items: flex-start;
            justify-content: flex-start;
            position: relative;
            #content-link-section {
                display: flex;
                flex-direction: row;
                gap: 1.5rem;
                align-items: flex-start;
                justify-content: flex-start;
                flex: 1;
                position: relative;
                .content {
                    display: flex;
                    flex-direction: column;
                    gap: 1.125rem;
                    align-items: flex-start;
                    justify-content: flex-start;
                    width: 8rem;
                    position: relative;
                }
            }
            @media @mobile {
                flex-direction: column;
                align-items: flex-start;
                #content-link-section {
                    flex-direction: column;
                }
            }
        }
        #newsletter-section {
            display: flex;
            flex-direction: column;
            gap: 18px;
            min-width: 300px;
            form {
                button {
                    width: fit-content;
                }
            }
        }
        #icons-section {
            padding: 0px var(--horizontal-default-margin);
            max-width: var(--small-max-width);
            width: 100%;
            margin: 0 0 0 -0.0625rem;
            display: flex;
            gap: 0.625rem;
            align-items: flex-start;
            justify-content: flex-start;
            flex: 1;
            position: relative;
            overflow: hidden;
            .payments,
            .social {
                display: flex;
                gap: 0.75rem;
                figure {
                    height: 1.2rem;
                    img {
                        height: 100%;
                        width: auto;
                    }
                }
            }
            .line {
                padding: 0rem 1.5rem 0rem 1.5rem;
                display: flex;
                flex-direction: row;
                gap: 1.5rem;
                align-items: center;
                justify-content: flex-start;
                flex: 1;
                position: relative;
                overflow: hidden;
                .line-1,
                .line-2 {
                    border-style: solid;
                    border-color: var(--text-invers-100);
                    border-width: 0.0625rem 0 0 0;
                    flex: 1;
                    height: 0rem;
                    position: relative;
                }
            }
            @media @mobile {
                flex-direction: column-reverse;
                align-items: center;
                gap: 2rem;
                .line {
                    width: 100%;
                    padding: 0px;
                }
            }
        }

        #legal-section {
            display: flex;
            justify-content: center;
            width: 100%;
            background: var(--bg-100);
            gap: 1.2rem;
            .wrapper {
                max-width: var(--small-max-width);

                width: 100%;
                a,
                small {
                    color: var(--text-100);
                }

                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: space-between;
                flex-shrink: 0;
                position: relative;
                gap: 1.2rem;
                padding: 1.5rem var(--horizontal-default-margin);
                @media @mobile {
                    flex-direction: column;
                    align-items: flex-start;
                }

                .nav-points {
                    ul {
                        display: flex;
                        flex-wrap: wrap;
                        gap: 0.6rem;
                        align-items: center;
                        a {
                            font-weight: 400;
                            font-family: Outfit;
                        }
                    }
                }
            }
        }
    }
</style>
