<script setup>
/*
 * MelissaConvenios — Pagina nativa Melissa pra "Convenios".
 *
 * Substitui o embed cfg-convenios. Cadastro de convenios atendidos
 * com seus valores. Layout 2-col padrao (chrome igual aos irmaos).
 */
import { ref, onMounted, onBeforeUnmount } from 'vue';
import MelissaConfigList from './MelissaConfigList.vue';
import ConfiguracoesConveniosPage from '@/layout/configuracoes/ConfiguracoesConveniosPage.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="mcv-drawer-fade">
        <div
            v-show="isMobile && drawerOpen"
            class="mcv-mobile-drawer"
            :class="{ 'is-open': drawerOpen }"
        >
            <div id="mcv-mobile-drawer-target" class="mcv-mobile-drawer__scroll" />
        </div>
    </Transition>
    <Transition name="mcv-drawer-fade">
        <div
            v-show="isMobile && drawerOpen"
            class="mcv-mobile-drawer__backdrop"
            @click="fecharDrawer"
        />
    </Transition>

    <section class="mcv-page">
        <header class="mcv-page__head">
            <button
                class="mcv-menu-btn mcv-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="mcv-page__title">
                <i class="pi pi-id-card mcv-page__title-icon" aria-hidden="true" />
                <span>Convênios</span>
            </div>
            <div class="mcv-page__actions">
                <button
                    class="mcv-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="mcv-subheader" role="note">
            <i class="pi pi-info-circle mcv-subheader__icon" aria-hidden="true" />
            <span class="mcv-subheader__text">
                Cadastre os convênios que você atende e os valores praticados em cada.
                Use ao agendar pacientes que pagam via plano de saúde.
            </span>
        </div>

        <div class="mcv-body">
            <Teleport to="#mcv-mobile-drawer-target" :disabled="!isMobile">
                <aside class="mcv-side">
                    <button
                        class="mcv-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 mcv-cfg-btn__chev" aria-hidden="true" />
                    </button>
                    <div v-if="cfgOpen" class="mcv-side__scroll mcv-side__scroll--cfg">
                        <MelissaConfigList @select="fecharCfg" />
                    </div>
                    <div v-else class="mcv-side__scroll">
                        <div class="mcv-w mcv-w--side">
                            <div class="mcv-w__head">
                                <div class="mcv-w__icon"><i class="pi pi-question-circle" aria-hidden="true" /></div>
                                <div class="mcv-w__title">
                                    <div class="mcv-w__title-text">Como funciona</div>
                                    <div class="mcv-w__sub">Convênios atendidos</div>
                                </div>
                            </div>
                            <div class="mcv-w__body">
                                <ul class="mcv-faq">
                                    <li class="mcv-faq__item">
                                        <i class="pi pi-id-card" aria-hidden="true" />
                                        <span><strong>Cadastro:</strong> nome do convênio, valor por sessão e código TUSS (opcional).</span>
                                    </li>
                                    <li class="mcv-faq__item">
                                        <i class="pi pi-user" aria-hidden="true" />
                                        <span><strong>Vincular:</strong> ao cadastrar paciente, escolha o convênio — valor entra automático.</span>
                                    </li>
                                    <li class="mcv-faq__item">
                                        <i class="pi pi-eye-slash" aria-hidden="true" />
                                        <span><strong>Inativar:</strong> mantém histórico mas remove dos formulários novos.</span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </aside>
            </Teleport>

            <div class="mcv-main">
                <ConfiguracoesConveniosPage />
            </div>
        </div>
    </section>
</template>

<style scoped>
.mcv-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: mcv-page-enter 240ms cubic-bezier(0.2, 0.7, 0.3, 1);
}
@keyframes mcv-page-enter {
    from { opacity: 0; transform: scale(0.985); }
    to   { opacity: 1; transform: scale(1); }
}

.mcv-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;
}
.mcv-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;
}
.mcv-page__title-icon { color: var(--p-primary-color); font-size: 1.05rem; }
.mcv-page__actions { display: flex; align-items: center; gap: 6px; }

.mcv-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;
}
.mcv-close:hover { background: var(--m-bg-soft-hover); color: var(--m-text); }
.mcv-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;
}
.mcv-menu-btn:hover { background: var(--m-bg-soft-hover); }

.mcv-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;
}
.mcv-subheader__icon { color: var(--p-primary-color); font-size: 0.85rem; margin-top: 2px; flex-shrink: 0; }

.mcv-body { flex: 1; min-height: 0; display: flex; overflow: hidden; }
.mcv-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;
}
.mcv-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;
}
.mcv-side__scroll::-webkit-scrollbar { width: 5px; }
.mcv-side__scroll::-webkit-scrollbar-thumb { background: var(--m-border-strong); border-radius: 3px; }

.mcv-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;
}
.mcv-cfg-btn:hover { background: var(--m-bg-soft-hover); border-color: var(--m-border-strong); }
.mcv-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);
}
.mcv-cfg-btn > i:first-child { color: var(--p-primary-color); font-size: 0.92rem; }
.mcv-cfg-btn > span { flex: 1; }
.mcv-cfg-btn__chev { color: var(--m-text-muted); font-size: 0.7rem; }
.mcv-side__scroll--cfg { padding: 8px; gap: 0; }

.mcv-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;
}
.mcv-main::-webkit-scrollbar { width: 5px; }
.mcv-main::-webkit-scrollbar-thumb { background: var(--m-border-strong); border-radius: 3px; }

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

.mcv-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;
}
.mcv-w--side { background: var(--m-bg-medium); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16); }
.mcv-w__head {
    display: flex; align-items: center; gap: 10px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--m-border);
}
.mcv-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);
}
.mcv-w__icon > i { font-size: 0.95rem; }
.mcv-w__title { flex: 1; min-width: 0; }
.mcv-w__title-text { font-size: 0.92rem; font-weight: 700; color: var(--m-text); line-height: 1.2; }
.mcv-w__sub { font-size: 0.74rem; color: var(--m-text-muted); margin-top: 2px; line-height: 1.3; }
.mcv-w__body { padding: 14px; display: flex; flex-direction: column; gap: 14px; }

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

.mcv-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;
}
.mcv-mobile-drawer.is-open { transform: translateX(0); }
.mcv-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;
}
.mcv-mobile-drawer__scroll::-webkit-scrollbar { width: 5px; }
.mcv-mobile-drawer__scroll::-webkit-scrollbar-thumb { background: var(--m-border-strong); border-radius: 3px; }
.mcv-mobile-drawer__scroll .mcv-side { width: 100%; border-right: none; }
.mcv-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;
}
.mcv-drawer-fade-enter-active,
.mcv-drawer-fade-leave-active { transition: opacity 200ms ease; }
.mcv-drawer-fade-enter-from,
.mcv-drawer-fade-leave-to { opacity: 0; }

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