/* OVP-Sponsorizare Admin JS */
/* global ovp_admin */
(function() {
    'use strict';

    // ── Confirm delete ──
    document.addEventListener('click', function(e) {
        var el = e.target.closest('.ovp-confirm-del');
        if (!el) return;
        var msg = el.getAttribute('data-confirm') || 'Ești sigur?';
        if (!window.confirm(msg)) {
            e.preventDefault();
        }
    });

    // ── Settings page: Color pickers ──
    function isValidHex(h) { return /^#([0-9A-Fa-f]{3}){1,2}$/.test(h); }

    function initColorPair(pick, hex) {
        var wrap = hex.closest('.ovp-color-wrap');
        var prev = wrap ? wrap.querySelector('.ovp-color-preview') : null;
        pick.addEventListener('input', function() {
            hex.value = pick.value.toUpperCase();
            if (prev) prev.style.background = pick.value;
            updateFormPreview();
        });
        hex.addEventListener('input', function() {
            var val = hex.value.trim();
            if (val && !val.startsWith('#')) val = '#' + val;
            hex.value = val.toUpperCase();
            if (isValidHex(val)) {
                pick.value = val;
                if (prev) prev.style.background = val;
                hex.style.borderColor = '#c8d5e8';
                updateFormPreview();
            } else {
                hex.style.borderColor = '#e74c3c';
            }
        });
        hex.addEventListener('blur', function() {
            var val = hex.value.trim();
            if (val && !val.startsWith('#')) val = '#' + val;
            if (isValidHex(val)) {
                hex.value = val.toUpperCase();
                pick.value = val;
                if (prev) prev.style.background = val;
                hex.style.borderColor = '#c8d5e8';
                updateFormPreview();
            } else {
                hex.value = pick.value.toUpperCase();
                if (prev) prev.style.background = pick.value;
                hex.style.borderColor = '#c8d5e8';
            }
        });
    }

    document.querySelectorAll('.ovp-color-wrap').forEach(function(wrap) {
        var pick = wrap.querySelector('.ovp-cpick');
        var hex  = wrap.querySelector('.ovp-chex');
        if (pick && hex) initColorPair(pick, hex);
    });

    function getColor(name, fallback) {
        var el = document.querySelector('input[name="ovp_sponsorizare_settings[' + name + ']"].ovp-cpick');
        return el ? el.value : fallback;
    }

    function lighten(hex) {
        var r = parseInt(hex.slice(1,3),16), g = parseInt(hex.slice(3,5),16), b = parseInt(hex.slice(5,7),16);
        r = Math.min(255, r+40); g = Math.min(255, g+40); b = Math.min(255, b+40);
        return '#' + r.toString(16).padStart(2,'0') + g.toString(16).padStart(2,'0') + b.toString(16).padStart(2,'0');
    }
    function darken(hex) {
        var r = parseInt(hex.slice(1,3),16), g = parseInt(hex.slice(3,5),16), b = parseInt(hex.slice(5,7),16);
        r = Math.max(0, r-30); g = Math.max(0, g-30); b = Math.max(0, b-30);
        return '#' + r.toString(16).padStart(2,'0') + g.toString(16).padStart(2,'0') + b.toString(16).padStart(2,'0');
    }

    function updateFormPreview() {
        var header   = getColor('culoare_header',  '#0F408A');
        var accent   = getColor('culoare_accent',  '#c8a84b');
        var sectiuni = getColor('culoare_sectiuni','#0F408A');
        var buton    = getColor('culoare_buton',   '#0F408A');
        var text     = getColor('culoare_text',    '#1c2333');

        var hero = document.getElementById('prev_hero');
        if (hero) hero.style.background = 'linear-gradient(135deg,' + header + ' 0%,' + lighten(header) + ' 60%,' + darken(header) + ' 100%)';

        var orgSpan = document.getElementById('prev_orgname');
        if (orgSpan) orgSpan.style.color = accent;

        var h3 = document.getElementById('prev_h3');
        if (h3) { h3.style.borderLeftColor = sectiuni; h3.style.color = sectiuni; }

        var btn = document.getElementById('prev_btn');
        if (btn) btn.style.background = buton;

        var prevBody = document.querySelector('.ovp-prev-body');
        if (prevBody) prevBody.style.color = text;
    }

    var resetBtn = document.getElementById('ovp_reset_colors');
    if (resetBtn) {
        resetBtn.addEventListener('click', function() {
            var defaults = {
                'culoare_header':  '#0F408A',
                'culoare_accent':  '#c8a84b',
                'culoare_sectiuni':'#0F408A',
                'culoare_buton':   '#0F408A',
                'culoare_text':    '#1c2333'
            };
            Object.keys(defaults).forEach(function(name) {
                var pick = document.querySelector('input[name="ovp_sponsorizare_settings[' + name + ']"].ovp-cpick');
                var wrap = pick ? pick.closest('.ovp-color-wrap') : null;
                var hex  = wrap ? wrap.querySelector('.ovp-chex') : null;
                var prev = wrap ? wrap.querySelector('.ovp-color-preview') : null;
                if (pick) pick.value = defaults[name];
                if (hex)  hex.value  = defaults[name].toUpperCase();
                if (prev) prev.style.background = defaults[name];
            });
            updateFormPreview();
        });
    }

    updateFormPreview();

    // ── Adresa preview ──
    function updateAdresaPreview() {
        var fields = ['org_strada','org_info_adresa','org_localitate','org_judet','org_sector','org_cod_postal'];
        var vals = [];
        fields.forEach(function(name) {
            var el = document.querySelector('[name="ovp_sponsorizare_settings[' + name + ']"]');
            if (!el || !el.value.trim()) return;
            if (name === 'org_sector') vals.push('Sector ' + el.value.trim());
            else vals.push(el.value.trim());
        });
        var preview = document.getElementById('ovp_adresa_preview');
        if (preview) {
            preview.value = vals.join(', ');
            preview.style.color = vals.length ? '#1c2333' : '#888';
        }
    }

    ['org_strada','org_info_adresa','org_localitate','org_judet','org_sector','org_cod_postal'].forEach(function(name) {
        var el = document.querySelector('[name="ovp_sponsorizare_settings[' + name + ']"]');
        if (el) {
            el.addEventListener('input', updateAdresaPreview);
            el.addEventListener('change', updateAdresaPreview);
        }
    });
    updateAdresaPreview();

    // ── Tabs ──
    document.querySelectorAll('.ovp-snav-btn').forEach(function(btn) {
        btn.addEventListener('click', function() {
            document.querySelectorAll('.ovp-snav-btn, .ovp-stab').forEach(function(el) { el.classList.remove('active'); });
            btn.classList.add('active');
            var tab = document.getElementById('stab-' + btn.dataset.tab);
            if (tab) tab.classList.add('active');
        });
    });

}());
