/**
 * Login Page Admin Scripts
 *
 * @package Polanger_Dashboard_Tools_Pro
 * @since 4.2.0
 */

(function($) {
    'use strict';

    $(document).ready(function() {
        var i18n = window.pdtLoginPageAdminI18n || {};
        
        // Background type toggle
        function toggleBgSections() {
            var bgType = $('input[name="pdt_login_page_settings[bg_type]"]:checked').val();
            
            $('#pdt-bg-color-section').hide();
            $('#pdt-bg-gradient-section').hide();
            $('#pdt-bg-image-section').hide();
            
            if (bgType === 'color') {
                $('#pdt-bg-color-section').show();
            } else if (bgType === 'gradient') {
                $('#pdt-bg-gradient-section').show();
            } else if (bgType === 'image') {
                $('#pdt-bg-image-section').show();
            }
        }
        
        // Initial toggle
        toggleBgSections();
        
        // On change
        $('input[name="pdt_login_page_settings[bg_type]"]').on('change', toggleBgSections);
        
        // Logo upload
        $('#pdt-upload-login-logo').on('click', function(e) {
            e.preventDefault();
            
            var frame = wp.media({
                title: i18n.selectLogoTitle || '',
                button: { text: i18n.useLogoButton || '' },
                multiple: false,
                library: { type: 'image' }
            });
            
            frame.on('select', function() {
                var attachment = frame.state().get('selection').first().toJSON();
                $('#pdt-login-logo-url').val(attachment.url);
                $('#pdt-login-logo-preview').html('<img src="' + attachment.url + '" alt="' + (i18n.logoAlt || '') + '" />');
                $('#pdt-remove-login-logo').show();
            });
            
            frame.open();
        });
        
        // Remove logo
        $('#pdt-remove-login-logo').on('click', function(e) {
            e.preventDefault();
            $('#pdt-login-logo-url').val('');
            $('#pdt-login-logo-preview').html('<span class="dashicons dashicons-format-image"></span>');
            $(this).hide();
        });
        
        // Background image upload
        $('#pdt-upload-login-bg').on('click', function(e) {
            e.preventDefault();
            
            var frame = wp.media({
                title: i18n.selectBackgroundTitle || '',
                button: { text: i18n.useBackgroundButton || '' },
                multiple: false,
                library: { type: 'image' }
            });
            
            frame.on('select', function() {
                var attachment = frame.state().get('selection').first().toJSON();
                $('#pdt-login-bg-url').val(attachment.url);
                $('#pdt-login-bg-preview').html('<img src="' + attachment.url + '" alt="' + (i18n.backgroundAlt || '') + '" />');
                $('#pdt-remove-login-bg').show();
            });
            
            frame.open();
        });
        
        // Remove background image
        $('#pdt-remove-login-bg').on('click', function(e) {
            e.preventDefault();
            $('#pdt-login-bg-url').val('');
            $('#pdt-login-bg-preview').html('<span class="dashicons dashicons-format-image"></span>');
            $(this).hide();
        });
        
    });
    
})(jQuery);
