<?php

/**
 * @package CsvImporterPlusforACF
 * Advanced Settings
 */

use CIPFA\base\CIPFA_BaseController;

// make sure we don't expose any info if called directly
if (!defined('ABSPATH')) exit; // Exit if accessed directly

// Handle save
if (isset($_POST['save'])) {

   // Verify nonce
   if (
      !isset($_POST['cipfa_nonce_advanced'])
      ||
      !wp_verify_nonce(
         sanitize_text_field(wp_unslash($_POST['cipfa_nonce_advanced'])),
         'cipfa_advanced_settings'
      )
   ) {
      echo '<div class="notice notice-error"><p>Security check failed.</p></div>';
   } else {

      // Save Cache Flush (checkbox)
      $cipfa_flush_cache = isset($_POST['flush_cache']) ? 'yes' : 'no';
      update_option('cipfa_flush_cache', $cipfa_flush_cache);

      // Save Logs (checkbox)
      $cipfa_enable_log = isset($_POST['enable_log']) ? 'yes' : 'no';
      update_option('cipfa_enable_log', $cipfa_enable_log);

      // Categories Mode
      if (isset($_POST['cipfa_replace_categories'])) {
         update_option(
            'cipfa_replace_categories',
            sanitize_text_field(wp_unslash($_POST['cipfa_replace_categories']))
         );
      }

      // Tags Mode
      if (isset($_POST['cipfa_replace_tags'])) {
         update_option(
            'cipfa_replace_tags',
            sanitize_text_field(wp_unslash($_POST['cipfa_replace_tags']))
         );
      }

      // Custom Taxonomies Mode
      if (isset($_POST['cipfa_replace_custom_taxonomies'])) {
         update_option(
            'cipfa_replace_custom_taxonomies',
            sanitize_text_field(wp_unslash($_POST['cipfa_replace_custom_taxonomies']))
         );
      }

      // Success Message
      echo '<div class="notice notice-success is-dismissible"><p>Settings saved successfully.</p></div>';
   }
}
?>
<!-- Display Advanced Settings -->
<div class="cipfa">
   <div class="max-w-2xl mx-auto mt-28 p-8 bg-white rounded-lg shadow-lg">

      <h2 class="text-2xl mb-6 text-center font-semibold">
         <?php echo esc_html__('Advanced Settings', 'csv-importer-plus-for-acf'); ?>
      </h2>

      <form method="post">

         <!-- Cache -->
         <div class="mb-6 flex items-center justify-between">
            <label class="font-semibold text-gray-600 mr-2">
               <?php echo esc_html__('Cache', 'csv-importer-plus-for-acf'); ?>
            </label>
            <input type="checkbox" id="flush_cache" name="flush_cache" value="yes" class="mr-2" <?php checked(get_option('cipfa_flush_cache'), 'yes'); ?>>

            <label for="flush_cache" class="text-xs text-gray-500"><?php echo esc_html__('Flush the WP cache after import.', 'csv-importer-plus-for-acf') ?></label>
         </div>

         <!-- Logs -->
         <div class="mb-6 flex items-center justify-between">
            <label class="font-semibold text-gray-600 mr-2">
               <?php echo esc_html__('Logs', 'csv-importer-plus-for-acf'); ?>
            </label>

            <input type="checkbox" id="enable_log" name="enable_log" value="yes" class="mr-2" <?php checked(get_option('cipfa_enable_log'), 'yes'); ?>>

            <label for="enable_log" class="text-xs text-gray-500"><?php echo esc_html__('Enable logs.', 'csv-importer-plus-for-acf') ?></label>
         </div>

         <!-- Categories -->
         <div class="mb-6">
            <div class="flex items-center gap-2">
               <label class="font-semibold text-gray-600 mr-2">
                  <?php echo esc_html__('Categories Update Mode', 'csv-importer-plus-for-acf'); ?>
               </label>
               <select name="cipfa_replace_categories" class="border-gray-300 rounded-md text-gray-700">
                  <option value="replace" <?php selected(get_option('cipfa_replace_categories'), 'replace'); ?>>
                     <?php echo esc_html__('Replace Existing Categories', 'csv-importer-plus-for-acf'); ?>
                  </option>
                  <option value="append" <?php selected(get_option('cipfa_replace_categories'), 'append'); ?>>
                     <?php echo esc_html__('Append Categories', 'csv-importer-plus-for-acf'); ?>
                  </option>
               </select>
               <?php
               CIPFA_BaseController::cipfa_safe_echo_tooltip(
                  CIPFA_BaseController::cipfa_tooltip(
                     'Choose whether to replace existing categories or append new ones.'
                  )
               );
               ?>
            </div>
         </div>

         <!-- Tags -->
         <div class="mb-6">
            <div class="flex items-center gap-2">
               <label class="font-semibold text-gray-600 mr-2">
                  <?php echo esc_html__('Tags Update Mode', 'csv-importer-plus-for-acf'); ?>
               </label>
               <select name="cipfa_replace_tags" class="border-gray-300 rounded-md text-gray-700">
                  <option value="replace" <?php selected(get_option('cipfa_replace_tags'), 'replace'); ?>>
                     <?php echo esc_html__('Replace Existing Tags', 'csv-importer-plus-for-acf'); ?>
                  </option>
                  <option value="append" <?php selected(get_option('cipfa_replace_tags'), 'append'); ?>>
                     <?php echo esc_html__('Append Tags', 'csv-importer-plus-for-acf'); ?>
                  </option>
               </select>
               <?php
               CIPFA_BaseController::cipfa_safe_echo_tooltip(
                  CIPFA_BaseController::cipfa_tooltip(
                     'Choose whether to replace existing tags or append new ones.'
                  )
               );
               ?>
            </div>
         </div>

         <!-- Custom Taxonomies -->
         <div class="mb-6">
            <div class="flex items-center gap-2">
               <label class="font-semibold text-gray-600 mr-2">
                  <?php echo esc_html__('Custom Taxonomies Update Mode', 'csv-importer-plus-for-acf'); ?>
               </label>
               <select name="cipfa_replace_custom_taxonomies" class="border-gray-300 rounded-md text-gray-700">
                  <option value="replace" <?php selected(get_option('cipfa_replace_custom_taxonomies'), 'replace'); ?>>
                     <?php echo esc_html__('Replace Custom Taxonomies', 'csv-importer-plus-for-acf'); ?>
                  </option>
                  <option value="append" <?php selected(get_option('cipfa_replace_custom_taxonomies'), 'append'); ?>>
                     <?php echo esc_html__('Append Custom Taxonomies', 'csv-importer-plus-for-acf'); ?>
                  </option>
               </select>
               <?php
               CIPFA_BaseController::cipfa_safe_echo_tooltip(
                  CIPFA_BaseController::cipfa_tooltip(
                     'Choose whether to replace existing custom taxonomies or append new ones.'
                  )
               );
               ?>
            </div>
         </div>

         <?php wp_nonce_field('cipfa_advanced_settings', 'cipfa_nonce_advanced'); ?>

         <button type="submit" class="inline-flex items-center px-2 py-2 border border-transparent font-medium rounded-md text-white bg-gradient-to-br from-blue-600 to-purple-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 self-end text-sm mr-2" name="save">
            <?php echo esc_html__('Save', 'csv-importer-plus-for-acf'); ?>
         </button>
         <a href="admin.php?page=csv_importer_plus_for_acf" title="<?php echo esc_attr__('Back to Import', 'csv-importer-plus-for-acf'); ?>" class="font-medium">
            <?php echo esc_html__('Back to Import', 'csv-importer-plus-for-acf'); ?>
         </a>
      </form>
   </div>
</div>