<script setup>
/*
 * MelissaExcecoes — Pagina nativa Melissa pra "Excecoes Financeiras".
 *
 * Substitui o embed cfg-excecoes. Define o que cobrar em situacoes
 * excepcionais (faltas, cancelamentos com pouca antecedencia, etc).
 * Layout 2-col padrao (chrome igual aos irmaos).
 */
import { ref, onMounted, onBeforeUnmount } from 'vue';
import MelissaConfigList from './MelissaConfigList.vue';
import ConfiguracoesExcecoesFinanceirasPage from '@/layout/configuracoes/ConfiguracoesExcecoesFinanceirasPage.vue';

const emit = defineEmits(['close']);

const drawerOpen = ref(false);
const isMobile = ref(false);
let _mqMobile = null;
function _onMqMobileChange(e) {
    isMobile.value = e.matches;
    if (!e.matches) drawerOpen.value = false;
}
function toggleDrawer() { drawerOpen.value = !drawerOpen.value; }
function fecharDrawer() { drawerOpen.value = false; }

const cfgOpen = ref(false);
function toggleCfg() { cfgOpen.value = !cfgOpen.value; }
function fecharCfg() { cfgOpen.value = false; }

onMounted(() => {
    if (typeof window !== 'undefined' && window.matchMedia) {
        _mqMobile = window.matchMedia('(max-width: 1023px)');
        isMobile.value = _mqMobile.matches;
        try { _mqMobile.addEventListener('change', _onMqMobileChange); }
        catch { _mqMobile.addListener(_onMqMobileChange); }
    }
});
onBeforeUnmount(() => {
    if (_mqMobile) {
        try { _mqMobile.removeEventListener('change', _onMqMobileChange); }
        catch { _mqMobile.removeListener(_onMqMobileChange); }
    }
});
</script>

<template>
    <Transition name="mxc-drawer-fade">
        <div
            v-show="isMobile && drawerOpen"
            class="mxc-mobile-drawer"
            :class="{ 'is-open': drawerOpen }"
        >
            <div id="mxc-mobile-drawer-target" class="mxc-mobile-drawer__scroll" />
        </div>
    </Transition>
    <Transition name="mxc-drawer-fade">
        <div
            v-show="isMobile && drawerOpen"
            class="mxc-mobile-drawer__backdrop"
            @click="fecharDrawer"
        />
    </Transition>

    <section class="mxc-page">
        <header class="mxc-page__head">
            <button
                class="mxc-menu-btn mxc-menu-btn--mobile-only"
                v-tooltip.bottom="'Configurações'"
                aria-label="Abrir menu de configurações"
                @click="toggleDrawer"
            >
                <i class="pi pi-bars" aria-hidden="true" />
                <span>Menu</span>
            </button>
            <div class="mxc-page__title">
                <i class="pi pi-exclamation-triangle mxc-page__title-icon" aria-hidden="true" />
                <span>Exceções Financeiras</span>
            </div>
            <div class="mxc-page__actions">
                <button
                    class="mxc-close"
                    v-tooltip.bottom="'Voltar (Esc)'"
                    aria-label="Voltar (Esc)"
                    @click="emit('close')"
                >
                    <i class="pi pi-times" aria-hidden="true" />
                </button>
            </div>
        </header>

        <div class="mxc-subheader" role="note">
            <i class="pi pi-info-circle mxc-subheader__icon" aria-hidden="true" />
            <span class="mxc-subheader__text">
                Defina o que cobrar em faltas sem aviso, cancelamentos com pouca
                antecedência e outras situações excepcionais.
            </span>
        </div>

        <div class="mxc-body">
            <Teleport to="#mxc-mobile-drawer-target" :disabled="!isMobile">
                <aside class="mxc-side">
                    <button
                        class="mxc-cfg-btn"
                        :class="{ 'is-open': cfgOpen }"
                        @click="toggleCfg"
                    >
                        <i :class="cfgOpen ? 'pi pi-arrow-left' : 'pi pi-cog'" aria-hidden="true" />
                        <span>{{ cfgOpen ? 'Voltar' : 'Configurações' }}</span>
                        <i v-if="!cfgOpen" class="pi pi-chevron-down mxc-cfg-btn__chev" aria-hidden="true" />
                    </button>
                    <div v-if="cfgOpen" class="mxc-side__scroll mxc-side__scroll--cfg">
                        <MelissaConfigList @select="fecharCfg" />
                    </div>
                    <div v-else class="mxc-side__scroll">
                        <div class="mxc-w mxc-w--side">
                            <div class="mxc-w__head">
                                <div class="mxc-w__icon"><i class="pi pi-question-circle" aria-hidden="true" /></div>
                                <div class="mxc-w__title">
                                    <div class="mxc-w__title-text">Como funciona</div>
                                    <div class="mxc-w__sub">Regras de exceção</div>
                                </div>
                            </div>
                            <div class="mxc-w__body">
                                <ul class="mxc-faq">
                                    <li class="mxc-faq__item">
                                        <i class="pi pi-times-circle" aria-hidden="true" />
                                        <span><strong>Falta sem aviso:</strong> cobrar 100%, 50% ou nada da sessão.</span>
                                    </li>
                                    <li class="mxc-faq__item">
                                        <i class="pi pi-clock" aria-hidden="true" />
                                        <span><strong>Cancelamento tardio:</strong> janela em horas e percentual cobrado.</span>
                                    </li>
                                    <li class="mxc-faq__item">
                                        <i class="pi pi-bolt" aria-hidden="true" />
                                        <span><strong>Aplicação:</strong> automática quando o motivo é registrado na sessão.</span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </aside>
            </Teleport>

            <div class="mxc-main">
                <ConfiguracoesExcecoesFinanceirasPage />
            </div>
        </div>
    </section>
</template>

<style scoped>
.mxc-page {
    position: absolute;
    inset: 6px 6px calc(var(--m-dock-h, 76px) + 6px) var(--m-config-aside-left, 6px);
    z-index: 40;
    display: flex;
    flex-direction: column;
    background: var(--m-bg-medium);
    backdrop-filter: blur(32px) saturate(160%);
    -webkit-backdrop-filter: blur(32px) saturate(160%);
    border: 1px solid var(--m-border);
    border-radius: 18px;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
    overflow: hidden;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    color: var(--m-text);
    animation: mxc-page-enter 240ms cubic-bezier(0.2, 0.7, 0.3, 1);
}
@keyframes mxc-page-enter {
    from { opacity: 0; transform: scale(0.985); }
    to   { opacity: 1; transform: scale(1); }
}

.mxc-page__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid var(--m-border);
    flex-shrink: 0;
    gap: 10px;
}
.mxc-page__title {
    flex: 1; min-width: 0;
    display: flex; align-items: center; gap: 10px;
    font-size: 1rem; font-weight: 700; color: var(--m-text);
    flex-wrap: wrap;
}
.mxc-page__title-icon { color: var(--p-primary-color); font-size: 1.05rem; }
.mxc-page__actions { display: flex; align-items: center; gap: 6px; }

.mxc-close {
    width: 32px; height: 32px;
    display: grid; place-items: center;
    background: transparent;
    border: 1px solid var(--m-border);
    color: var(--m-text-muted);
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 120ms ease, color 120ms ease;
}
.mxc-close:hover { background: var(--m-bg-soft-hover); color: var(--m-text); }
.mxc-menu-btn {
    display: none;
    align-items: center;
    gap: 6px;
    padding: 7px 12px;
    border-radius: 8px;
    border: 1px solid var(--m-border);
    background: var(--m-bg-soft);
    color: var(--m-text);
    cursor: pointer;
    font-size: 0.82rem;
    font-weight: 600;
    flex-shrink: 0;
}
.mxc-menu-btn:hover { background: var(--m-bg-soft-hover); }

.mxc-subheader {
    display: flex; align-items: flex-start; gap: 8px;
    padding: 10px 18px;
    background: var(--m-bg-soft);
    border-bottom: 1px solid var(--m-border);
    color: var(--m-text-muted);
    font-size: 0.78rem;
    flex-shrink: 0;
}
.mxc-subheader__icon { color: var(--p-primary-color); font-size: 0.85rem; margin-top: 2px; flex-shrink: 0; }

.mxc-body { flex: 1; min-height: 0; display: flex; overflow: hidden; }
.mxc-side {
    width: 320px; flex-shrink: 0;
    display: flex; flex-direction: column;
    background: var(--m-bg-soft);
    border-right: 1px solid var(--m-border);
    overflow: hidden;
}
.mxc-side__scroll {
    flex: 1; min-height: 0;
    overflow-y: auto; overflow-x: hidden;
    padding: 12px;
    display: flex; flex-direction: column; gap: 12px;
    scrollbar-width: thin;
    scrollbar-color: var(--m-border-strong) transparent;
}
.mxc-side__scroll::-webkit-scrollbar { width: 5px; }
.mxc-side__scroll::-webkit-scrollbar-thumb { background: var(--m-border-strong); border-radius: 3px; }

.mxc-cfg-btn {
    display: flex; align-items: center; gap: 8px;
    width: calc(100% - 24px);
    margin: 12px 12px 0;
    padding: 10px 12px;
    background: var(--m-bg-medium);
    border: 1px solid var(--m-border);
    border-radius: 9px;
    color: var(--m-text);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.84rem; font-weight: 600;
    text-align: left;
    transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
    flex-shrink: 0;
}
.mxc-cfg-btn:hover { background: var(--m-bg-soft-hover); border-color: var(--m-border-strong); }
.mxc-cfg-btn.is-open {
    background: color-mix(in srgb, var(--p-primary-color) 14%, transparent);
    border-color: color-mix(in srgb, var(--p-primary-color) 38%, transparent);
    color: var(--p-primary-color);
}
.mxc-cfg-btn > i:first-child { color: var(--p-primary-color); font-size: 0.92rem; }
.mxc-cfg-btn > span { flex: 1; }
.mxc-cfg-btn__chev { color: var(--m-text-muted); font-size: 0.7rem; }
.mxc-side__scroll--cfg { padding: 8px; gap: 0; }

.mxc-main {
    flex: 1; min-width: 0; min-height: 0;
    overflow-y: auto; overflow-x: hidden;
    padding: 16px;
    display: flex; flex-direction: column; gap: 12px;
    scrollbar-width: thin;
    scrollbar-color: var(--m-border-strong) transparent;
}
.mxc-main::-webkit-scrollbar { width: 5px; }
.mxc-main::-webkit-scrollbar-thumb { background: var(--m-border-strong); border-radius: 3px; }

@media (min-width: 1024px) {
    .mxc-page { right: max(6px, min(50%, calc(100% - 1006px))); }
    .mxc-side > .mxc-side__scroll > .mxc-w--side {
        flex-shrink: 0;
        height: auto;
        display: flex;
        flex-direction: column;
    }
}

.mxc-w {
    background: var(--m-bg-soft);
    border: 1px solid var(--m-border);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    display: flex; flex-direction: column;
}
.mxc-w--side { background: var(--m-bg-medium); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16); }
.mxc-w__head {
    display: flex; align-items: center; gap: 10px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--m-border);
}
.mxc-w__icon {
    width: 36px; height: 36px;
    display: grid; place-items: center;
    border-radius: 9px;
    flex-shrink: 0;
    background: color-mix(in srgb, var(--p-primary-color) 15%, transparent);
    color: var(--p-primary-color);
}
.mxc-w__icon > i { font-size: 0.95rem; }
.mxc-w__title { flex: 1; min-width: 0; }
.mxc-w__title-text { font-size: 0.92rem; font-weight: 700; color: var(--m-text); line-height: 1.2; }
.mxc-w__sub { font-size: 0.74rem; color: var(--m-text-muted); margin-top: 2px; line-height: 1.3; }
.mxc-w__body { padding: 14px; display: flex; flex-direction: column; gap: 14px; }

.mxc-faq {
    display: flex; flex-direction: column; gap: 10px;
    list-style: none; margin: 0; padding: 0;
}
.mxc-faq__item {
    display: flex; gap: 10px;
    font-size: 0.78rem;
    color: var(--m-text-muted);
    line-height: 1.4;
}
.mxc-faq__item > i { color: var(--p-primary-color); font-size: 0.78rem; margin-top: 3px; flex-shrink: 0; }
.mxc-faq__item strong { color: var(--m-text); font-weight: 600; }

.mxc-mobile-drawer {
    position: fixed;
    top: 0; left: 0;
    height: 100vh; height: 100dvh;
    width: min(360px, 88vw);
    z-index: 80;
    background: var(--m-bg-medium);
    backdrop-filter: blur(28px) saturate(160%);
    -webkit-backdrop-filter: blur(28px) saturate(160%);
    border-right: 1px solid var(--m-border);
    transform: translateX(-100%);
    transition: transform 250ms cubic-bezier(0.4, 0, 0.2, 1);
    color: var(--m-text);
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    display: flex; flex-direction: column;
}
.mxc-mobile-drawer.is-open { transform: translateX(0); }
.mxc-mobile-drawer__scroll {
    flex: 1; min-height: 0;
    overflow-y: auto; overflow-x: hidden;
    padding: 12px;
    display: flex; flex-direction: column; gap: 12px;
    scrollbar-width: thin;
    scrollbar-color: var(--m-border-strong) transparent;
}
.mxc-mobile-drawer__scroll::-webkit-scrollbar { width: 5px; }
.mxc-mobile-drawer__scroll::-webkit-scrollbar-thumb { background: var(--m-border-strong); border-radius: 3px; }
.mxc-mobile-drawer__scroll .mxc-side { width: 100%; border-right: none; }
.mxc-mobile-drawer__backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 79;
}
.mxc-drawer-fade-enter-active,
.mxc-drawer-fade-leave-active { transition: opacity 200ms ease; }
.mxc-drawer-fade-enter-from,
.mxc-drawer-fade-leave-to { opacity: 0; }

@media (max-width: 1023px) {
    .mxc-body { flex-direction: column; padding: 0; }
    .mxc-body > .mxc-side { display: none; }
    .mxc-main { width: 100%; padding: 8px; }
    .mxc-page__title > span:first-of-type { display: none; }
    .mxc-page__title-icon { display: none; }
    .mxc-menu-btn--mobile-only { display: inline-flex; }
}
</style>
