<?php

/**
 * @package CsvImporterPlusforACF
 * CSV Upload Form
 */
?>

<?php

use CIPFA\base\CIPFA_BaseController;

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

// Retrieve the filtered post types
$cipfa_filtered_post_types = CIPFA_BaseController::cipfa_get_filtered_post_types();

// Gets the first filtered post type
$post_type = reset($cipfa_filtered_post_types);

// Check if it is an ACF options page
$cipfa_is_options_page = strpos($post_type->name, 'acf_option_') === 0;

// Get taxonomies associated with this post type
$cipfa_taxonomies = get_object_taxonomies($post_type->name, 'objects');

// List of post types to exclude
$cipfa_excluded_post_types = array(
   'elementor_library',
   'e-landing-page',
   'attachment',
   'e-floating-buttons',
);
?>

<!-- Display import form -->
<div class="cipfa">
   <div class="max-w-3xl mx-auto mt-28 p-8 bg-white rounded-lg shadow-lg">
      <h2 class="text-2xl mb-1 text-center font-semibold"><?php echo esc_html__('CSV Importer Plus for ACF', 'csv-importer-plus-for-acf') ?></h2>
      <p class="mb-8 text-center">
         <?php
         echo esc_html__('Imports posts/custom posts, pages, products, and more with or without ACF fields.', 'csv-importer-plus-for-acf');
         ?>
      </p>
      <form method="post" enctype="multipart/form-data" class="flex flex-col" id="csv-upload-form">
         <div class="mb-4 flex items-center">
            <label for="post_type_select" class="block text-sm font-semibold text-gray-600 mr-16"><?php echo esc_html__('Post Type:', 'csv-importer-plus-for-acf') ?></label>
            <select name="post_type" id="post_type_select" class="block w-1/2 border border-gray-300 rounded-md focus:outline-none focus:border-indigo-500">
               <?php foreach ($cipfa_filtered_post_types as $post_type) :
                  // Skip if this post type is in the excluded array
                  if (in_array($post_type->name, $cipfa_excluded_post_types)) {
                     continue;
                  }
                  if ($post_type->name === 'product') : ?>
                     <optgroup label="Product">
                        <option value="product">Simple Products</option>
                        <option value="all_products_type" disabled style="color: #999;">Variable, Grouped & External Products - Pro Access</option>
                     </optgroup>
                  <?php else : ?>
                     <option value="<?php echo esc_attr($post_type->name); ?>"><?php echo esc_html($post_type->label); ?></option>
                  <?php endif; ?>
               <?php endforeach; ?>
            </select>
            <?php
            CIPFA_BaseController::cipfa_safe_echo_tooltip(
               CIPFA_BaseController::cipfa_tooltip(
                  'Select where you want to import: posts, custom posts, pages, or WooCommerce products.'
               )
            );
            ?>
         </div>
         <div class="mb-4 flex items-center">
            <label for="chunk-size" class="block text-sm font-semibold text-gray-600 mr-14"><?php echo esc_html__('Chunk Size:', 'csv-importer-plus-for-acf') ?></label>
            <select id="chunk-size" name="chunk_size" class="block border border-gray-300 rounded-md focus:outline-none focus:border-indigo-500">
               <option value="<?php echo esc_attr(10); ?>"><?php echo esc_html__('10', 'csv-importer-plus-for-acf') ?></option>
               <option value="<?php echo esc_attr(20); ?>"><?php echo esc_html__('20', 'csv-importer-plus-for-acf') ?></option>
               <option value="<?php echo esc_attr(30); ?>"><?php echo esc_html__('30', 'csv-importer-plus-for-acf') ?></option>
               <option value="<?php echo esc_attr(40); ?>"><?php echo esc_html__('40', 'csv-importer-plus-for-acf') ?></option>
               <option value="<?php echo esc_attr(50); ?>"><?php echo esc_html__('50', 'csv-importer-plus-for-acf') ?></option>
               <option value="<?php echo esc_attr(60); ?>"><?php echo esc_html__('60', 'csv-importer-plus-for-acf') ?></option>
               <option value="<?php echo esc_attr(70); ?>"><?php echo esc_html__('70', 'csv-importer-plus-for-acf') ?></option>
               <option value="<?php echo esc_attr(80); ?>"><?php echo esc_html__('80', 'csv-importer-plus-for-acf') ?></option>
               <option value="<?php echo esc_attr(90); ?>"><?php echo esc_html__('90', 'csv-importer-plus-for-acf') ?></option>
               <option value="<?php echo esc_attr(100); ?>"><?php echo esc_html__('100', 'csv-importer-plus-for-acf') ?></option>
            </select>
            <?php
            CIPFA_BaseController::cipfa_safe_echo_tooltip(
               CIPFA_BaseController::cipfa_tooltip(
                  'Plugin automatically splits the uploaded large CSV into smaller chunks for better performance during import.'
               )
            );
            ?>
         </div>
         <div class="flex items-center">
            <label for="csv_file" class="block text-sm font-semibold text-gray-600 mr-6"><?php echo esc_html__('Upload CSV File:', 'csv-importer-plus-for-acf') ?></label>
            <input type="file" name="csv_file" id="csv-file" accept=".csv" />
         </div>
         <span class="ml-8 mb-6 column-match-warning">
            <?php
            echo '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" style="vertical-align: middle; margin-right: 4px;"><defs>
            <linearGradient id="infoGradient" x1="0%" y1="0%" x2="100%" y2="100%">
            <stop offset="0%" stop-color="#2563EB"/>
            <stop offset="100%" stop-color="#9333EA"/>
            </linearGradient>
            </defs>
            <circle cx="12" cy="12" r="10" fill="url(#infoGradient)"/>
            <rect x="11" y="10" width="2" height="6" fill="#FFFFFF"/>
            <circle cx="12" cy="7" r="1" fill="#FFFFFF"/>
            </svg>' . esc_html__('Auto-mapping', 'csv-importer-plus-for-acf');
            ?>
            <a href="https://csvimporterplusforacf.com/auto-mapping-csv-column-guidelines/" target="_blank" title="<?php echo esc_attr__(' CSV column guidelines', 'csv-importer-plus-for-acf'); ?>">
               <?php echo esc_html__(' CSV column guidelines', 'csv-importer-plus-for-acf'); ?>
            </a>
         </span>
         <?php wp_nonce_field('cipfa_nonce', 'cipfa_nonce_field'); ?>
         <input type="hidden" name="step" value="2">
         <div class="mb-4 flex items-center">
            <label class="block text-sm font-semibold text-gray-600 mr-20"><?php echo esc_html__('Update', 'csv-importer-plus-for-acf') ?></label>
            <?php  ?>
            <input type="checkbox" id="edit_existing" name="edit_existing" value="<?php echo esc_attr(1); ?>" class="mr-2" <?php echo $cipfa_is_options_page ? 'disabled' : ''; ?>>
            <label for="edit_existing" class="text-xs text-gray-500" id="id_column_text"><?php echo esc_html__('Check only to update existing posts. Must include post_id column in CSV.', 'csv-importer-plus-for-acf') ?></label>
         </div>
         <div class="mb-4 flex items-center">
            <label class="block text-sm font-semibold text-gray-600 mr-19"><?php echo esc_html__('Settings', 'csv-importer-plus-for-acf') ?></label>
            <a href="admin.php?page=csv_importer_plus_for_acf_advanced_settings" title="<?php echo esc_attr__('More Advance Settings', 'csv-importer-plus-for-acf'); ?>">
               <?php echo esc_html__('Advanced Settings', 'csv-importer-plus-for-acf'); ?>
            </a>
         </div>
         <button type="submit" class="inline-flex items-center px-4 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-lg" name="next"><?php echo esc_html__('Next', 'csv-importer-plus-for-acf') ?></button>
      </form>
   </div>
</div>