<?php
 // Exit if accessed directly
 if (!defined('ABSPATH')) {
  exit;
 }

 // Add settings submenu under Portfolio
 function aump_add_settings_menu() {
  add_submenu_page(
  'edit.php?post_type=aump_portfolio',
  __('Portfolio Settings', 'advanced-universal-modern-portfolio-and-gallery'),
  __('Settings', 'advanced-universal-modern-portfolio-and-gallery'),
  'manage_options',
  'aump-portfolio-settings',
  'aump_render_settings_page'
  );
 }
 add_action('admin_menu', 'aump_add_settings_menu');

 // Render settings page
 function aump_render_settings_page() {
  ?>
  <div class="wrap-aump-portfolio-settings">
   <form method="post" action="options.php">
    <?php
	settings_errors(); 	 
    settings_fields('aump_settings_group');
    do_settings_sections('aump-portfolio-settings');
    submit_button();
    ?>
   </form>
  </div>

  <?php
 }

 // Register settings
 function aump_register_settings() {
register_setting('aump_settings_group', 'aump_auto_scroll', array(
    'default' => 'no',
    'sanitize_callback' => 'sanitize_text_field'
));
register_setting('aump_settings_group', 'aump_enable_category_filter', array(
    'default' => 'yes',
    'sanitize_callback' => 'sanitize_text_field'
));
register_setting('aump_settings_group', 'aump_enable_featured_image_on_popup', array(
    'default' => 'no',
    'sanitize_callback' => 'sanitize_text_field'
));

register_setting('aump_settings_group', 'aump_category_filter_bg', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_category_filter_text_colour', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_columns', array('sanitize_callback' => 'absint'));
register_setting('aump_settings_group', 'aump_grid_border_clr', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_posts_per_page', array('sanitize_callback' => 'absint'));
register_setting('aump_settings_group', 'aump_show_open_button', array('sanitize_callback' => 'sanitize_text_field'));
register_setting('aump_settings_group', 'aump_open_button_text', array('sanitize_callback' => 'sanitize_text_field'));
register_setting('aump_settings_group', 'aump_open_button_bg', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_open_button_text_color', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_show_title', array('sanitize_callback' => 'sanitize_text_field'));
register_setting('aump_settings_group', 'aump_title_color', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_title_bg', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_show_description', array('sanitize_callback' => 'sanitize_text_field'));
register_setting('aump_settings_group', 'aump_desc_word_limit', array('sanitize_callback' => 'absint'));
register_setting('aump_settings_group', 'aump_desc_color', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_desc_bg', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_next_text', array('sanitize_callback' => 'sanitize_text_field'));
register_setting('aump_settings_group', 'aump_next_prev_bg', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_next_prev_text_color', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_prev_text', array('sanitize_callback' => 'sanitize_text_field'));
// register_setting('aump_settings_group', 'aump_prev_bg');
// register_setting('aump_settings_group', 'aump_prev_text_color');
register_setting('aump_settings_group', 'aump_close_text_color', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_close_bg', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_open_in_popup', array('sanitize_callback' => 'sanitize_text_field'));
register_setting('aump_settings_group', 'aump_popup_width', array('sanitize_callback' => 'absint'));
register_setting('aump_settings_group', 'aump_show_load_more_button', array('sanitize_callback' => 'sanitize_text_field'));
register_setting('aump_settings_group', 'aump_load_more_button_text', array('sanitize_callback' => 'sanitize_text_field'));
register_setting('aump_settings_group', 'aump_load_more_button_bg', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_load_more_button_text_color', array('sanitize_callback' => 'sanitize_hex_color'));
register_setting('aump_settings_group', 'aump_grid_image_height_desktop', array('sanitize_callback' => 'absint'));
register_setting('aump_settings_group', 'aump_grid_image_height_mobile', array('sanitize_callback' => 'absint'));
register_setting('aump_settings_group', 'aump_hide_title_on_popup', array(
    'default' => 'no',
    'sanitize_callback' => 'sanitize_text_field'
));
register_setting('aump_settings_group', 'aump_hide_categories_on_popup', array(
    'default' => 'no',
    'sanitize_callback' => 'sanitize_text_field'
));
register_setting('aump_settings_group', 'aump_popup_bg_color', array('sanitize_callback' => 'sanitize_hex_color'));



  add_settings_section('aump_general_settings_section', __('General Settings', 'advanced-universal-modern-portfolio-and-gallery'), '__return_false', 'aump-portfolio-settings');
  add_settings_section('aump_filter_settings_section', __('Category Filter Settings', 'advanced-universal-modern-portfolio-and-gallery'), '__return_false', 'aump-portfolio-settings');
  add_settings_section('aump_button_settings_section', __('Button Settings', 'advanced-universal-modern-portfolio-and-gallery'), '__return_false', 'aump-portfolio-settings');
  add_settings_section('aump_title_settings_section', __('Title Settings', 'advanced-universal-modern-portfolio-and-gallery'), '__return_false', 'aump-portfolio-settings');
  add_settings_section('aump_description_settings_section', __('Description Settings', 'advanced-universal-modern-portfolio-and-gallery'), '__return_false', 'aump-portfolio-settings');
  add_settings_section('aump_navigation_settings_section', __('Navigation Settings', 'advanced-universal-modern-portfolio-and-gallery'), '__return_false', 'aump-portfolio-settings');
  add_settings_section('aump_popup_settings_section', __('Popup Settings', 'advanced-universal-modern-portfolio-and-gallery'), '__return_false', 'aump-portfolio-settings');
  add_settings_section('aump_load_more_settings_section', __('Load More Button Settings', 'advanced-universal-modern-portfolio-and-gallery'), '__return_false', 'aump-portfolio-settings');
  add_settings_section('aump_image_settings_section', __('Image Settings', 'advanced-universal-modern-portfolio-and-gallery'), '__return_false', 'aump-portfolio-settings');


  aump_add_toggle_field('aump_general_settings_section', 'aump_auto_scroll', __('Disable Auto Scroll on Hover', 'advanced-universal-modern-portfolio-and-gallery'));
  aump_add_toggle_field('aump_filter_settings_section', 'aump_enable_category_filter', __('Show Category Filter', 'advanced-universal-modern-portfolio-and-gallery'));
  aump_add_toggle_field('aump_general_settings_section', 'aump_enable_featured_image_on_popup', __('Enable Featured Image on Popup', 'advanced-universal-modern-portfolio-and-gallery'));
  aump_add_dropdown_field('aump_general_settings_section', 'aump_columns', __('Number of Grid Columns', 'advanced-universal-modern-portfolio-and-gallery'), range(1, 6));
  aump_add_number_field('aump_general_settings_section', 'aump_posts_per_page', __('Items Per Page', 'advanced-universal-modern-portfolio-and-gallery'), 6, 'Set number of items that will load after clicking on load more button');
  aump_add_color_field('aump_general_settings_section', 'aump_grid_border_clr', __('Portfolio Item Border Color ', 'advanced-universal-modern-portfolio-and-gallery'), '#e4e5e7');


  aump_add_color_field('aump_filter_settings_section', 'aump_category_filter_bg', __('Active Filter Background Color ', 'advanced-universal-modern-portfolio-and-gallery'), '#EB5916');
  aump_add_color_field('aump_filter_settings_section', 'aump_category_filter_text_colour', __('Active Filter Text Color ', 'advanced-universal-modern-portfolio-and-gallery'), '#ffffff');


  aump_add_toggle_field('aump_button_settings_section', 'aump_show_open_button', __('Show Open Button on Portfolio Grid', 'advanced-universal-modern-portfolio-and-gallery'));
  aump_add_text_field('aump_button_settings_section', 'aump_open_button_text', __('Open Project Button Text', 'advanced-universal-modern-portfolio-and-gallery'), 'Open Project');
  aump_add_color_field('aump_button_settings_section', 'aump_open_button_bg', __('Open Button Background Color', 'advanced-universal-modern-portfolio-and-gallery'), '#eb5916');
  aump_add_color_field('aump_button_settings_section', 'aump_open_button_text_color', __('Open Button Text Color', 'advanced-universal-modern-portfolio-and-gallery'), '#ffffff');

  aump_add_toggle_field('aump_title_settings_section', 'aump_show_title', __('Show Title on Portfolio Grid', 'advanced-universal-modern-portfolio-and-gallery'));
  aump_add_color_field('aump_title_settings_section', 'aump_title_color', __('Title Color', 'advanced-universal-modern-portfolio-and-gallery'), '#000000');
  aump_add_color_field('aump_title_settings_section', 'aump_title_bg', __('Title Background Color', 'advanced-universal-modern-portfolio-and-gallery'), '#FFFFFF00');

  aump_add_toggle_field('aump_description_settings_section', 'aump_show_description', __('Show Description on Portfolio Grid', 'advanced-universal-modern-portfolio-and-gallery'));
  aump_add_number_field('aump_description_settings_section', 'aump_desc_word_limit', __('Description Limit', 'advanced-universal-modern-portfolio-and-gallery'), 15, 'Short description lenght in words.');
  aump_add_color_field('aump_description_settings_section', 'aump_desc_color', __('Description Color', 'advanced-universal-modern-portfolio-and-gallery'), '#000000');
  aump_add_color_field('aump_description_settings_section', 'aump_desc_bg', __('Description Background Color', 'advanced-universal-modern-portfolio-and-gallery'), '#FFFFFF00');

  aump_add_text_field('aump_navigation_settings_section', 'aump_next_text', __('Next Button Text', 'advanced-universal-modern-portfolio-and-gallery'), 'Next');
  aump_add_text_field('aump_navigation_settings_section', 'aump_prev_text', __('Previous Button Text', 'advanced-universal-modern-portfolio-and-gallery'), 'Previous');
  aump_add_color_field('aump_navigation_settings_section', 'aump_next_prev_bg', __('Next,Previous Button Background Color', 'advanced-universal-modern-portfolio-and-gallery'), '#ffffff');
  aump_add_color_field('aump_navigation_settings_section', 'aump_next_prev_text_color', __('Next,Previous Button Text Color', 'advanced-universal-modern-portfolio-and-gallery'), '#000000');

  aump_add_color_field('aump_navigation_settings_section', 'aump_close_text_color', __('Close Button color', 'advanced-universal-modern-portfolio-and-gallery'), '#000000');
  aump_add_color_field('aump_navigation_settings_section', 'aump_close_bg', __('Close Button Background Color', 'advanced-universal-modern-portfolio-and-gallery'), '#ffffff');


  aump_add_toggle_field('aump_popup_settings_section', 'aump_open_in_popup', __('Open Portfolio in Popup', 'advanced-universal-modern-portfolio-and-gallery'));
  aump_add_number_field('aump_popup_settings_section', 'aump_popup_width', __('Popup Width (%)', 'advanced-universal-modern-portfolio-and-gallery'), 70, 'Set popup width in percentage (%). Should not exceed 100%.');
  aump_add_toggle_field('aump_popup_settings_section', 'aump_hide_title_on_popup', __('Hide Project Title on Popup', 'advanced-universal-modern-portfolio-and-gallery'));
  aump_add_toggle_field('aump_popup_settings_section', 'aump_hide_categories_on_popup', __('Hide Project Categories on Popup', 'advanced-universal-modern-portfolio-and-gallery'));
  aump_add_color_field('aump_popup_settings_section', 'aump_popup_bg_color', __('Popup Background Color', 'advanced-universal-modern-portfolio-and-gallery'), '#ffffff');

	 
  aump_add_toggle_field('aump_load_more_settings_section', 'aump_show_load_more_button', __('Show Load More button', 'advanced-universal-modern-portfolio-and-gallery'));
  aump_add_text_field('aump_load_more_settings_section', 'aump_load_more_button_text', __('Load More Button Text', 'advanced-universal-modern-portfolio-and-gallery'), 'Load More Items');
  aump_add_color_field('aump_load_more_settings_section', 'aump_load_more_button_bg', __('Load More Button Background', 'advanced-universal-modern-portfolio-and-gallery'), '#eb5916');
  aump_add_color_field('aump_load_more_settings_section', 'aump_load_more_button_text_color', __('Load More Button Text color', 'advanced-universal-modern-portfolio-and-gallery'), '#ffffff');

  aump_add_number_field('aump_image_settings_section', 'aump_grid_image_height_desktop', __('Grid Image Height (Desktop)', 'advanced-universal-modern-portfolio-and-gallery'), 250, 'Set height of the image on portfolio grid in px (Default is 250px).');
  aump_add_number_field('aump_image_settings_section', 'aump_grid_image_height_mobile', __('Grid Image Height (Mobile)', 'advanced-universal-modern-portfolio-and-gallery'), 300, 'Set height of the image on portfolio grid in px (Default is 300px).');

 }

 add_action('admin_init', 'aump_register_settings');

 // Helper function to add toggle switch fields
 function aump_add_toggle_field($section, $id, $label) {
  add_settings_field($id, $label, function() use ($id) {
   $value = get_option($id); // No default here, so it takes the default from register_setting()
   ?>
   <div class="aump-toggle">
    <input type="checkbox" id="<?php echo esc_attr($id); ?>" name="<?php echo esc_attr($id); ?>" value="yes" <?php checked($value, 'yes'); ?>>
    <label for="<?php echo esc_attr($id); ?>"></label>
   </div>
   <?php
  }, 'aump-portfolio-settings', $section);
 }

 // Helper function to add dropdown fields
 function aump_add_dropdown_field($section, $id, $label, $options) {
  add_settings_field($id, $label, function() use ($id, $options) {
   $value = get_option($id, 3);
   ?>
   <select name="<?php echo esc_attr($id); ?>">
    <?php foreach ($options as $option) : ?>
     <option value="<?php echo esc_attr($option); ?>" <?php selected($value, $option); ?>>
      <?php echo esc_html($option); ?>
     </option>
    <?php endforeach; ?>
   </select>
   <?php
  }, 'aump-portfolio-settings', $section);
 }

 // Helper function to add text fields
 function aump_add_text_field($section, $id, $label, $default_value = '') { // Added default value parameter
  add_settings_field($id, $label, function() use ($id, $default_value) { // Use default value
   $value = get_option($id, $default_value); // Get option with default value
   ?>
   <input type="text" name="<?php echo esc_attr($id); ?>" value="<?php echo esc_attr($value); ?>">
   <?php
  }, 'aump-portfolio-settings', $section);
 }

 // Helper function to add color fields
 function aump_add_color_field($section, $id, $label, $default_value = '#000000') {
  add_settings_field($id, $label, function() use ($id, $default_value) {
   $value = get_option($id, $default_value);
   ?>
   <div class="aump-color-picker-container">
    <div class="pickr" id="aump-color-picker-<?php echo esc_attr($id); ?>">
     <button type="button" class="pcr-button" style="--pcr-color: <?php echo esc_attr($value); ?>;"></button>
    </div>
    <input type="hidden" name="<?php echo esc_attr($id); ?>" id="aump-color-input-<?php echo esc_attr($id); ?>" value="<?php echo esc_attr($value); ?>" data-default="<?php echo esc_attr($default_value); ?>">
   </div>
   <?php
  }, 'aump-portfolio-settings', $section);
 }


 // Helper function to add number fields
 function aump_add_number_field($section, $id, $label, $default_value = 100, $description = '') { // Added description parameter
  add_settings_field($id, $label, function() use ($id, $default_value, $description) { // Use description
   $value = get_option($id, $default_value); // Get option with default value
   ?>
   <input type="number" name="<?php echo esc_attr($id); ?>" value="<?php echo esc_attr($value); ?>" min="1" max="1000"> <?php // Added min and max attributes to number field ?>
   <?php if ($description) : ?>
    <p class="description"><?php echo wp_kses_post($description); ?></p>
   <?php endif; ?>
   <?php
  }, 'aump-portfolio-settings', $section);
 }