/**
 * Dashboard Widgets Manager Scripts
 *
 * @package Polanger_Dashboard_Tools_Pro
 * @since 4.3.0
 */

(function($) {
  'use strict';

  $(document).ready(function() {
    var i18n = window.pdtDashboardWidgetsI18n || {};
    
    // Prevent double initialization
    if (window.pdtDashboardWidgetsInitialized) {
      return;
    }
    window.pdtDashboardWidgetsInitialized = true;

    // Toggle sub-options visibility for Admin Notices
    $('#pdt-hide-all-notices').on('change', function() {
      if ($(this).is(':checked')) {
        $('#pdt-notice-options').slideDown(200);
      } else {
        $('#pdt-notice-options').slideUp(200);
      }
    });

    // Toggle sub-options visibility for Custom Welcome
    $('#pdt-custom-welcome-enabled').on('change', function() {
      if ($(this).is(':checked')) {
        $('#pdt-welcome-options').slideDown(200);
      } else {
        $('#pdt-welcome-options').slideUp(200);
      }
    });

    // Toggle whitelist options for Auto-hide
    $('#pdt-auto-hide-widgets').on('change', function() {
      if ($(this).is(':checked')) {
        $('#pdt-whitelist-options').slideDown(200);
      } else {
        $('#pdt-whitelist-options').slideUp(200);
      }
    });

    // Whitelist item toggle
    $('.pdt-dw-whitelist-item input[type="checkbox"]').on('change', function() {
      var $item = $(this).closest('.pdt-dw-whitelist-item');
      var $icon = $item.find('.dashicons');
      
      if ($(this).is(':checked')) {
        $item.addClass('is-whitelisted');
        $icon.removeClass('dashicons-marker').addClass('dashicons-yes-alt');
      } else {
        $item.removeClass('is-whitelisted');
        $icon.removeClass('dashicons-yes-alt').addClass('dashicons-marker');
      }
    });

    // Widget card toggle state (for hide/show widgets)
    $('.pdt-dw-widget-card input[type="checkbox"]').on('change', function() {
      var $card = $(this).closest('.pdt-dw-widget-card');
      var $label = $(this).siblings('.pdt-dw-toggle-label');
      
      if ($(this).is(':checked')) {
        $card.addClass('is-hidden');
        $label.text(i18n.hiddenLabel || '');
      } else {
        $card.removeClass('is-hidden');
        $label.text(i18n.visibleLabel || '');
      }
    });

    // Custom Widgets - Add new widget
    var widgetIndex = $('#pdt-custom-widgets-list .pdt-dw-custom-widget-card').length;
    
    $('#pdt-add-custom-widget').off('click').on('click', function(e) {
      e.preventDefault();
      var template = $('#pdt-widget-template').html();
      var newWidget = template.replace(/\{\{INDEX\}\}/g, widgetIndex);
      
      $('#pdt-custom-widgets-list').append(newWidget);
      
      // Auto-expand the new widget
      var $newCard = $('#pdt-custom-widgets-list .pdt-dw-custom-widget-card').last();
      $newCard.find('.pdt-dw-widget-card-body').slideDown(200);
      $newCard.find('.pdt-dw-toggle-widget .dashicons').removeClass('dashicons-arrow-down-alt2').addClass('dashicons-arrow-up-alt2');
      
      // Focus on title input
      $newCard.find('.pdt-dw-widget-title-input').focus();
      
      widgetIndex++;
    });

    // Custom Widgets - Toggle expand/collapse
    $(document).on('click', '.pdt-dw-toggle-widget', function() {
      var $card = $(this).closest('.pdt-dw-custom-widget-card');
      var $body = $card.find('.pdt-dw-widget-card-body');
      var $icon = $(this).find('.dashicons');
      
      $body.slideToggle(200);
      
      if ($icon.hasClass('dashicons-arrow-down-alt2')) {
        $icon.removeClass('dashicons-arrow-down-alt2').addClass('dashicons-arrow-up-alt2');
      } else {
        $icon.removeClass('dashicons-arrow-up-alt2').addClass('dashicons-arrow-down-alt2');
      }
    });

    // Custom Widgets - Remove widget
    $(document).on('click', '.pdt-dw-remove-widget', function() {
      var $card = $(this).closest('.pdt-dw-custom-widget-card');
      
      if (confirm(i18n.removeWidgetConfirm || '')) {
        $card.fadeOut(200, function() {
          $(this).remove();
        });
      }
    });

    // Custom Widgets - Update title in header when typing
    $(document).on('input', '.pdt-dw-widget-title-input', function() {
      var $card = $(this).closest('.pdt-dw-custom-widget-card');
      var title = $(this).val() || i18n.untitledWidget || '';
      $card.find('.pdt-dw-widget-name').text(title);
    });

    // Radio button visual feedback
    $('.pdt-dw-radio input[type="radio"]').on('change', function() {
      var $group = $(this).closest('.pdt-dw-radio-group');
      $group.find('.pdt-dw-radio').removeClass('selected');
      $(this).closest('.pdt-dw-radio').addClass('selected');
    });

    // Form unsaved changes warning
    var formChanged = false;
    
    $('.pdt-dw-form').on('change', 'input, select, textarea', function() {
      formChanged = true;
    });

    $(window).on('beforeunload', function() {
      if (formChanged) {
        return i18n.unsavedChanges || '';
      }
    });

    $('.pdt-dw-form').on('submit', function() {
      formChanged = false;
    });

  });

})(jQuery);
