<?php
/**
 * Settings page template for Easy Invoice
 *
 * @package Easy_Invoice
 * @since 1.0.0
 */

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

/**
 * Helper function to render a single settings field.
 *
 * @param string $option_key The option key.
 * @param array $field_config The field configuration.
 * @param mixed $current_value The current value of the field.
 */
function easy_invoice_render_field($option_key, $field_config, $current_value) {
    $label = esc_html($field_config['label'] ?? '');
    $type = $field_config['type'] ?? 'text';
    $default = $field_config['default'] ?? '';
    $value = $current_value ?? $default;
    $description_text = $field_config['description'] ?? '';
    $field_id = esc_attr($option_key);
    $field_name = 'settings[' . esc_attr($option_key) . ']';
    $col_span_class = esc_attr($field_config['col_span'] ?? 'sm:col-span-6');
    $placeholder = isset($field_config['placeholder']) ? esc_attr($field_config['placeholder']) : '';
    $required = !empty($field_config['required']) ? 'required' : '';
    $aria_describedby = !empty($description_text) ? 'aria-describedby="' . $field_id . '-description"' : '';

    echo '<div class=" ' . $col_span_class . '">'; // Added 'form-group' class for JS show/hide functionality

    // Special layout for checkboxes with descriptions (e.g., Invoice Numbering type)
    if ($type === 'checkbox' && !empty($description_text) && ($option_key === 'easy_invoice_invoice_numbering')) {
        echo '<div class="flex items-start space-x-3 p-4 bg-gray-50 rounded-lg border border-gray-200">';
        echo '  <div class="flex items-center h-5 mt-0.5">';
        echo '    <input type="checkbox" name="' . $field_name . '" id="' . $field_id . '" value="yes" ' . checked($value, 'yes', false) . ' class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded" ' . $required . '>';
        echo '  </div>';
        echo '  <div class="flex-1">';
        echo '    <label for="' . $field_id . '" class="block text-sm font-medium text-gray-700 mb-1">' . $label . '</label>';
        echo '    <p id="' . $field_id . '-description" class="text-sm text-gray-400 leading-relaxed">' . wp_kses($description_text, array(
            'a' => array(
                'href' => array(),
                'target' => array(),
                'class' => array()
            )
        )) . '</p>';
        echo '  </div>';
        echo '</div>';
    } else {
        // Standard label for other types, or checkboxes without the special description structure
        if ($type !== 'checkbox') {
            echo '<label for="' . $field_id . '" class="block text-sm font-medium text-gray-700 mb-2">' . $label . '</label>';
        }

        $input_class = 'mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm';
        $select_class = 'mt-1 block w-full pl-3 pr-10 py-2 text-base border border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md';


        switch ($type) {
            case 'text':
            case 'email':
            case 'url':
            case 'tel':
                echo '<input type="' . esc_attr($type) . '" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($value) . '" class="' . $input_class . '" placeholder="' . $placeholder . '" ' . $required . ' ' . $aria_describedby . '>';
                break;
            case 'number':
                $min = isset($field_config['min']) ? ' min="' . esc_attr($field_config['min']) . '"' : '';
                $max = isset($field_config['max']) ? ' max="' . esc_attr($field_config['max']) . '"' : '';
                $step = isset($field_config['step']) ? ' step="' . esc_attr($field_config['step']) . '"' : '';
                echo '<input type="number" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($value) . '"' . $min . $max . $step . ' class="' . $input_class . '" placeholder="' . $placeholder . '" ' . $required . ' ' . $aria_describedby . '>';
                break;
            case 'textarea':
                echo '<textarea id="' . $field_id . '" name="' . $field_name . '" rows="3" class="' . $input_class . '" placeholder="' . $placeholder . '" ' . $required . ' ' . $aria_describedby . '>' . wp_kses_post($value) . '</textarea>';
                break;
            case 'select':
                echo '<select id="' . $field_id . '" name="' . $field_name . '" class="' . $select_class . '" ' . $required . ' ' . $aria_describedby . '>';
                if (!empty($field_config['options']) && is_array($field_config['options'])) {
                    foreach ($field_config['options'] as $opt_val => $opt_label) {
                        echo '<option value="' . esc_attr($opt_val) . '" ' . selected($value, $opt_val, false) . '>' . esc_html($opt_label) . '</option>';
                    }
                }
                echo '</select>';
                break;
            case 'multiselect':
                echo '<select id="' . $field_id . '" name="' . $field_name . '[]" class="' . $select_class . '" multiple="multiple" aria-label="' . esc_attr($label) . '">';
                if (!empty($field_config['options']) && is_array($field_config['options'])) {
                    foreach ($field_config['options'] as $opt_val => $opt_label) {
                        $selected = is_array($value) && in_array($opt_val, $value) ? 'selected="selected"' : '';
                        echo '<option value="' . esc_attr($opt_val) . '" ' . $selected . '>' . esc_html($opt_label) . '</option>';
                    }
                }
                echo '</select>';
                break;
            case 'readonly':
                echo '<input type="text" id="' . $field_id . '" value="' . esc_attr($value) . '" class="' . $input_class . ' bg-gray-50" readonly ' . $aria_describedby . '>';
                echo '<input type="hidden" name="' . $field_name . '" value="' . esc_attr($value) . '">';
                break;
            case 'checkbox': // Checkboxes that don't use the special description layout
                echo '<div class="flex items-center">'; // Original 'tax_enabled' was just this simple structure
                echo '  <input type="checkbox" name="' . $field_name . '" id="' . $field_id . '" value="yes" ' . checked($value, 'yes', false) . ' class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded" ' . $required . ' ' . $aria_describedby . '>';
                echo '  <label for="' . $field_id . '" class="ml-2 block text-sm text-gray-900">' . $label . '</label>';
                echo '</div>';
                if (!empty($description_text)) { // Show description below the field
                    echo '<p id="' . $field_id . '-description" class="mt-2 text-sm text-gray-400 leading-relaxed">' . wp_kses($description_text, array(
                        'a' => array(
                            'href' => array(),
                            'target' => array(),
                            'class' => array()
                        )
                    )) . '</p>';
                }
                break;
            case 'image':
                 echo '<div class="mt-1 flex items-center">';
                 echo '  <span class="inline-block h-12 w-12 rounded-full overflow-hidden bg-gray-100">';
                 echo '    <img id="' . $field_id . '-preview" src="' . esc_url($value) . '" alt="' . esc_attr($label) . '" class="h-full w-full object-cover' . (empty($value) ? ' hidden' : '') . '">';
                 echo '  </span>';
                 echo '  <button type="button" id="upload_image_button_' . $field_id . '" class="upload-logo-button ml-5 bg-white py-2 px-3 border border-gray-300 rounded-md shadow-sm text-sm leading-4 font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" data-uploader_title="' . esc_attr__('Choose Logo', 'easy-invoice') . '" data-uploader_button_text="' . esc_attr__('Select Logo', 'easy-invoice') . '">';
                 echo esc_html__('Change', 'easy-invoice');
                 echo '</button>';
                 echo '  <input type="hidden" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($value) . '" ' . $required . ' ' . $aria_describedby . '>';
                 echo '</div>';
                break;
            case 'wp_editor':
                // For wp_editor, the label is usually handled before calling it.
                // The col_span div wraps this.
                wp_editor( $value, $field_id, ['textarea_name' => $field_name, 'teeny' => true, 'media_buttons' => false, 'textarea_rows' => 7, 'editor_class' => 'mt-1'] );
                // Handle description for wp_editor specifically
                if (!empty($description_text)) {
                    echo '<p id="' . $field_id . '-description" class="mt-1 text-sm text-gray-400">' . wp_kses($description_text, array(
                        'a' => array(
                            'href' => array(),
                            'target' => array(),
                            'class' => array()
                        )
                    )) . '</p>';
                }
                break;
            case 'test_email':
                echo '<div class="mt-1">';
                echo '  <div class="flex items-center space-x-3">';
                echo '    <input type="email" id="' . $field_id . '_email" placeholder="' . esc_attr__('Enter email address to test', 'easy-invoice') . '" class="mt-1 block w-[200px] border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" value="' . esc_attr(get_option('admin_email')) . '">';
                echo '    <button type="button" id="' . $field_id . '_button" class="test-email-button inline-flex items-center px-3 py-1.5 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 h-[38px]">';
                echo '      <i class="fas fa-paper-plane mr-2" aria-hidden="true"></i>';
                echo '      ' . esc_html__('Send Test Email', 'easy-invoice');
                echo '    </button>';
                echo '  </div>';
                echo '  <div id="' . $field_id . '_result" class="mt-2" style="display: none;"></div>';
                echo '</div>';
                break;
            case 'color':
                echo '<input type="color" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($value) . '" class="h-10 w-20 border border-gray-300 rounded">';
                break;
            case 'range':
                $min = isset($field_config['min']) ? ' min="' . esc_attr($field_config['min']) . '"' : '';
                $max = isset($field_config['max']) ? ' max="' . esc_attr($field_config['max']) . '"' : '';
                $step = isset($field_config['step']) ? ' step="' . esc_attr($field_config['step']) . '"' : '';
                echo '<input type="range" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($value) . '"' . $min . $max . $step . ' class="w-full">';
                echo '<span class="text-sm text-gray-400">' . esc_html($value) . '%</span>';
                break;
            case 'button':
                $button_text = $field_config['button_text'] ?? $label;
                $button_class = $field_config['button_class'] ?? 'button button-secondary';
                $button_class_name = 'regenerate-invoice-numbers-button';
                if ($option_key === 'easy_invoice_regenerate_quote_numbers') {
                    $button_class_name = 'regenerate-quote-numbers-button';
                }
                echo '<button type="button" id="' . $field_id . '" class="' . esc_attr($button_class) . ' ' . $button_class_name . '">';
                echo esc_html($button_text);
                echo '</button>';
                break;
            case 'image_upload':
                echo '<div class="image-upload-wrap">';
                echo '<div class="flex items-center space-x-3">';
                echo '  <input type="hidden" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($value) . '">';
                echo '  <button type="button" class="upload-image-button bg-white py-2 px-3 border border-gray-300 rounded-md shadow-sm text-sm leading-4 font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" data-target="' . $field_id . '">';
                echo esc_html__('Upload Image', 'easy-invoice');
                echo '</button>';
                echo '</div>';
                echo '<div class="image-preview mt-2' . (empty($value) ? ' hidden' : '') . '">';
                if (!empty($value)) {
                    echo '<img src="' . esc_url($value) . '" alt="Preview" style="max-width: 200px; max-height: 100px;">';
                }
                echo '</div>';
                echo '</div>';
                break;
            default:
                // Special handling for regenerate invoice numbers button
                if ($option_key === 'easy_invoice_regenerate_invoice_numbers') {
                    $button_text = $field_config['button_text'] ?? $label;
                    $button_class = $field_config['button_class'] ?? 'button button-secondary';
                    echo '<button type="button" id="' . $field_id . '" class="' . esc_attr($button_class) . ' regenerate-invoice-numbers-button">';
                    echo esc_html($button_text);
                    echo '</button>';
                } elseif ($option_key === 'easy_invoice_regenerate_quote_numbers') {
                    $button_text = $field_config['button_text'] ?? $label;
                    $button_class = $field_config['button_class'] ?? 'button button-secondary';
                    echo '<button type="button" id="' . $field_id . '" class="' . esc_attr($button_class) . ' regenerate-quote-numbers-button">';
                    echo esc_html($button_text);
                    echo '</button>';
                } else {
                    echo '<input type="text" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($value) . '" class="' . $input_class . '" placeholder="' . $placeholder . '" ' . $required . ' ' . $aria_describedby . '>';
                }
                break;
        }
        // General description for non-checkbox and non-wp_editor fields (if it exists and not handled by special layouts)
        if ($type !== 'checkbox' && $type !== 'wp_editor' && !empty($description_text)) {
             echo '<p id="' . $field_id . '-description" class="mt-1 text-sm text-gray-400">' . wp_kses($description_text, array(
                 'a' => array(
                     'href' => array(),
                     'target' => array(),
                     'class' => array()
                 )
             )) . '</p>';
        }
    }
    echo '</div>'; // End col_span div
}

/**
 * Filter settings config to allow extensions to modify it
 * Note: This filter is already applied in the controller, so this is redundant
 */
// $settings_config = apply_filters('easy_invoice_settings_fields_config', $settings_config);

$active_section = $_GET['section'] ?? key($settings_config);
if (!isset($settings_config[$active_section])) {
    $active_section = key($settings_config);
}
?>

<div class="p-8">
    <!-- Header with Actions -->
    <div class="ei-app-page-header bg-white border-b border-gray-200 px-6 easy-invoice-admin-page-header">
        <div class="flex items-center justify-between">
            <div>
                <h1 class="text-2xl font-bold text-gray-900"><?php esc_html_e('Settings', 'easy-invoice'); ?></h1>
                <p class="mt-1 text-sm text-gray-400"><?php esc_html_e('Configure your invoice management system', 'easy-invoice'); ?></p>
            </div>
            <div class="flex items-center space-x-3">
                <button type="button" id="save-settings" class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" aria-label="<?php esc_attr_e('Save Settings', 'easy-invoice'); ?>">
                    <i class="fas fa-save mr-2" aria-hidden="true"></i>
                    <?php esc_html_e('Save Changes', 'easy-invoice'); ?>
                </button>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-7xl mx-auto">
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-8 mt-8">
                <!-- Left Sidebar - Navigation -->
                <div class="lg:col-span-1">
                    <div class="bg-white shadow-lg rounded-lg sticky top-24 border border-gray-200">
                    <nav class="space-y-2 p-4" aria-label="<?php esc_attr_e('Settings Navigation', 'easy-invoice'); ?>">
                        <?php foreach ($settings_config as $section_id => $section_data) : ?>
                            <div class="space-y-1">
                                <a href="?page=easy-invoice-settings&section=<?php echo esc_attr($section_id); ?>"
                                   class="nav-link group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200 <?php echo ($section_id === $active_section) ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?>"
                                   aria-selected="<?php echo $section_id === $active_section ? 'true' : 'false'; ?>"
                                   role="tab">

                                    <?php if ($section_id === 'company'): ?>
                                        <svg class="<?php echo ($section_id === $active_section) ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <path d="M3 9L12 2L21 9V20C21 20.5304 20.7893 21.0391 20.4142 21.4142C20.0391 21.7893 19.5304 22 19 22H5C4.46957 22 3.96086 21.7893 3.58579 21.4142C3.21071 21.0391 3 20.5304 3 20V9Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <polyline points="9,22 9,12 15,12 15,22" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                        </svg>
                                    <?php elseif ($section_id === 'invoice'): ?>
                                        <svg class="<?php echo ($section_id === $active_section) ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <path d="M14 2H6C4.9 2 4 2.9 4 4V20C4 21.1 4.9 22 6 22H18C19.1 22 20 21.1 20 20V8L14 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M14 2V8H20" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M16 13H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M16 17H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M10 9H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                        </svg>
                                    <?php elseif ($section_id === 'quote'): ?>
                                        <svg class="<?php echo ($section_id === $active_section) ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <path d="M14 2H6C4.9 2 4 2.9 4 4V20C4 21.1 4.9 22 6 22H18C19.1 22 20 21.1 20 20V8L14 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M14 2V8H20" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M9 9H15" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M9 13H15" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M9 17H13" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                        </svg>
                                    <?php elseif ($section_id === 'currency'): ?>
                                        <svg class="<?php echo ($section_id === $active_section) ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M12 1V23" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M17 5H9.5C8.57174 5 7.6815 5.36875 7.02513 6.02513C6.36875 6.6815 6 7.57174 6 8.5C6 9.42826 6.36875 10.3185 7.02513 10.9749C7.6815 11.6313 8.57174 12 9.5 12H14.5C15.4283 12 16.3185 12.3687 16.9749 13.0251C17.6313 13.6815 18 14.5717 18 15.5C18 16.4283 17.6313 17.3185 16.9749 17.9749C16.3185 18.6313 15.4283 19 14.5 19H6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                        </svg>
                                    <?php elseif ($section_id === 'tax'): ?>
                                        <svg class="<?php echo ($section_id === $active_section) ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M8 14L16 8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M8 8L16 14" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M12 6V18" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                        </svg>
                                    <?php elseif ($section_id === 'payment'): ?>
                                        <svg class="<?php echo ($section_id === $active_section) ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <rect x="1" y="4" width="22" height="16" rx="2" ry="2" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <line x1="1" y1="10" x2="23" y2="10" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                        </svg>
                                    <?php elseif ($section_id === 'email'): ?>
                                        <svg class="<?php echo ($section_id === $active_section) ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <path d="M4 4H20C21.1 4 22 4.9 22 6V18C22 19.1 21.1 20 20 20H4C2.9 20 2 19.1 2 18V6C2 4.9 2.9 4 4 4Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <polyline points="22,6 12,13 2,6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                        </svg>
                                    <?php elseif ($section_id === 'advanced'): ?>
                                        <svg class="<?php echo ($section_id === $active_section) ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <circle cx="12" cy="12" r="3" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M19.4 15A1.65 1.65 0 0 0 18 14A6 6 0 0 0 6 14A1.65 1.65 0 0 0 4.6 15A2 2 0 0 0 6 19H18A2 2 0 0 0 19.4 15Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                        </svg>
                                    <?php elseif ($section_id === 'text_settings'): ?>
                                        <svg class="<?php echo ($section_id === $active_section) ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <path d="M4 7V4C4 3.44772 4.44772 3 5 3H19C19.5523 3 20 3.44772 20 4V7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M4 7V20C4 20.5523 4.44772 21 5 21H19C19.5523 21 20 20.5523 20 20V7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M8 11H16" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M8 15H12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M8 19H14" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M12 11L14 13L12 15" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                        </svg>
                                    <?php else: ?>
                                        <svg class="<?php echo ($section_id === $active_section) ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <circle cx="12" cy="12" r="3" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            <path d="M19.4 15A1.65 1.65 0 0 0 18 14A6 6 0 0 0 6 14A1.65 1.65 0 0 0 4.6 15A2 2 0 0 0 6 19H18A2 2 0 0 0 19.4 15Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                        </svg>
                                    <?php endif; ?>

                                    <span class="flex-1"><?php echo esc_html($section_data['title']); ?></span>
                                    <?php if ($section_id === 'email' && !empty($section_data['subsections'])): ?>
                                        <svg class="flex-shrink-0 h-4 w-4 text-gray-400 group-hover:text-gray-500 transition-transform duration-200" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <polyline points="6,9 12,15 18,9" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                        </svg>
                                    <?php endif; ?>
                                </a>

                                <?php if ($section_id === 'email' && !empty($section_data['subsections'])): ?>
                                    <div class="ml-6 space-y-1 email-settings-submenu" style="display: none;">
                                        <?php
                                        // Only show active subsection if we're actually on an email settings page
                                        $active_email_subsection = null;
                                        if ($active_section === 'email') {
                                            if (isset($_GET['subsection']) && isset($section_data['subsections'][$_GET['subsection']])) {
                                                $active_email_subsection = $_GET['subsection'];
                                            } elseif (!empty($_GET['subsection']) && isset($section_data['subsections'][$_GET['subsection']])) {
                                                $active_email_subsection = $_GET['subsection'];
                                            } else {
                                                // Default to 'general' only if we're on email section but no specific subsection
                                                $active_email_subsection = 'general';
                                            }
                                        }
                                        ?>
                                        <?php foreach ($section_data['subsections'] as $subsection_id => $subsection_data): ?>
                                            <?php $is_active_subsection = ($active_email_subsection && $subsection_id === $active_email_subsection); ?>
                                            <a href="?page=easy-invoice-settings&section=email&subsection=<?php echo esc_attr($subsection_id); ?>"
                                               class="email-submenu-link group flex items-center px-3 py-2 text-sm font-medium rounded-md transition-all duration-200 <?php echo $is_active_subsection ? 'text-indigo-700 bg-indigo-50 border-l-2 border-indigo-500' : 'text-gray-500 hover:text-gray-700 hover:bg-gray-50'; ?>"
                                               data-subsection="<?php echo esc_attr($subsection_id); ?>"
                                               data-section="<?php echo esc_attr($section_id); ?>">
                                                <svg class="mr-2 flex-shrink-0 h-4 w-4 <?php echo $is_active_subsection ? 'text-indigo-500' : 'text-gray-400 group-hover:text-gray-500'; ?>" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                    <polyline points="9,18 15,12 9,6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                                </svg>
                                                <?php
                                                // Display shorter names for better UX
                                                $display_name = '';
                                                switch ($subsection_id) {
                                                    case 'general':
                                                        $display_name = 'General';
                                                        break;
                                                    case 'invoice_available':
                                                        $display_name = 'Invoice Available';
                                                        break;
                                                    case 'quote_available':
                                                        $display_name = 'Quote Available';
                                                        break;
                                                    case 'payment_received':
                                                        $display_name = 'Payment Received';
                                                        break;
                                                    default:
                                                        $display_name = $subsection_data['title'];
                                                        break;
                                                }
                                                echo esc_html($display_name);
                                                ?>
                                            </a>
                                        <?php endforeach; ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endforeach; ?>
                    </nav>
                    </div>
                    </div>

                <!-- Right Content - Settings -->
                <div class="lg:col-span-2">
                <form id="settings-form" method="post" action="">
                    <?php wp_nonce_field('easy_invoice_settings', 'easy_invoice_settings_nonce'); ?>
                    <input type="hidden" name="action" value="easy_invoice_save_settings">

                    <?php foreach ($settings_config as $section_id => $section_data) : ?>
                        <div id="<?php echo esc_attr($section_id); ?>"
                             class="settings-section bg-white shadow rounded-lg <?php echo ($section_id !== $active_section) ? 'hidden' : ''; ?>"
                             role="tabpanel"
                             aria-labelledby="tab-<?php echo esc_attr($section_id); ?>">
                            <div class="px-6 py-5 border-b border-gray-200">
                                <div class="flex items-center">
                                    <div class="flex-shrink-0 bg-indigo-100 rounded-lg p-3">
                                        <?php if (isset($section_data['icon']) && !empty($section_data['icon'])): ?>
                                            <?php if (strpos($section_data['icon'], '<svg') === 0): ?>
                                                <?php echo $section_data['icon']; ?>
                                            <?php else: ?>
                                                <i class="<?php echo esc_attr($section_data['icon']); ?> text-indigo-600 text-xl"></i>
                                            <?php endif; ?>
                                        <?php else: ?>
                                            <svg class="text-indigo-600 text-xl h-6 w-6" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                <circle cx="12" cy="12" r="3" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                                <path d="M19.4 15A1.65 1.65 0 0 0 18 14A6 6 0 0 0 6 14A1.65 1.65 0 0 0 4.6 15A2 2 0 0 0 6 19H18A2 2 0 0 0 19.4 15Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            </svg>
                                        <?php endif; ?>
                                    </div>
                                    <div class="ml-4">
                                        <h2 class="text-lg font-medium text-gray-900"><?php echo esc_html($section_data['title']); ?></h2>
                                        <p class="mt-1 text-sm text-gray-400"><?php echo esc_html($section_data['description']); ?></p>
                                    </div>
                                </div>
                            </div>
                            <div class="px-6 py-5">
                                <div class="email-settings">
                                    <?php if ($section_id === 'email' && !empty($section_data['subsections'])): ?>
                                        <?php
                                        // Determine the active subsection from the hash or default to 'general'
                                        $active_email_subsection = 'general';
                                        if (isset($_GET['subsection']) && isset($section_data['subsections'][$_GET['subsection']])) {
                                            $active_email_subsection = $_GET['subsection'];
                                        } elseif (!empty($_GET['subsection']) && isset($section_data['subsections'][$_GET['subsection']])) {
                                            $active_email_subsection = $_GET['subsection'];
                                        }
                                        if (!isset($section_data['subsections'][$active_email_subsection])) {
                                            $active_email_subsection = array_key_first($section_data['subsections']);
                                        }
                                        $subsection_data = $section_data['subsections'][$active_email_subsection];
                                        ?>
                                        <?php
                                        // Render all email subsections so JS can show/hide them
                                        foreach ($section_data['subsections'] as $subsection_key => $subsection_data) {
                                            $is_active = ($subsection_key === $active_email_subsection) ? '' : 'hidden';
                                        ?>
                                        <div id="email--<?php echo esc_attr($subsection_key); ?>-content" class="email-subsection-content <?php echo $is_active; ?>">
                                            <?php
                                            // Display subsection title
                                            $subsection_title = '';
                                            switch ($subsection_key) {
                                                case 'general':
                                                    $subsection_title = 'General Email Settings';
                                                    break;
                                                case 'invoice_available':
                                                    $subsection_title = 'Invoice Available Email Settings';
                                                    break;
                                                case 'quote_available':
                                                    $subsection_title = 'Quote Available Email Settings';
                                                    break;
                                                case 'payment_received':
                                                    $subsection_title = 'Payment Received Email Settings';
                                                    break;
                                                default:
                                                    $subsection_title = $subsection_data['title'];
                                                    break;
                                            }
                                            ?>
                                            <div class="mb-6">
                                                <h3 class="text-lg font-medium text-gray-900 mb-2"><?php echo esc_html($subsection_title); ?></h3>
                                                <?php if (!empty($subsection_data['description'])): ?>
                                                    <p class="text-sm text-gray-400"><?php echo esc_html($subsection_data['description']); ?></p>
                                                <?php endif; ?>
                                            </div>
                                            <div class="space-y-6">
                                                <?php if (!empty($subsection_data['fields'])) {
                                                    foreach ($subsection_data['fields'] as $option_key => $field_config) {
                                                        $current_value = $settings[$option_key] ?? ($field_config['default'] ?? '');
                                                        $field_id = esc_attr($option_key);
                                                        $field_type = $field_config['type'] ?? 'text';
                                                        $field_label = esc_html($field_config['label'] ?? '');
                                                        $field_placeholder = isset($field_config['placeholder']) ? esc_attr($field_config['placeholder']) : '';
                                                        $field_description = $field_config['description'] ?? '';
                                                        $field_name = 'settings[' . esc_attr($option_key) . ']';
                                                        $input_class = 'mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm';
                                                        $select_class = 'mt-1 block w-full pl-3 pr-10 py-2 text-base border border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md';
                                                        $col_span_class = esc_attr($field_config['col_span'] ?? 'sm:col-span-6');
                                                        echo '<div class="field-wrapper form-group">';
                                                        if ($field_type !== 'checkbox') {
                                                            echo '<label for="' . $field_id . '" class="block text-sm font-medium text-gray-700">' . $field_label . '</label>';
                                                        }

                                                        // Define required and aria_describedby variables
                                                        $required = !empty($field_config['required']) ? 'required' : '';
                                                        $aria_describedby = !empty($field_description) ? 'aria-describedby="' . $field_id . '-description"' : '';

                                                        switch ($field_type) {
                                                            case 'text':
                                                            case 'email':
                                                            case 'url':
                                                            case 'tel':
                                                                echo '<input type="' . esc_attr($field_type) . '" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($current_value) . '" class="' . $input_class . '" placeholder="' . $field_placeholder . '" ' . $required . ' ' . $aria_describedby . '>';
                                                                break;
                                                            case 'number':
                                                                $min = isset($field_config['min']) ? ' min="' . esc_attr($field_config['min']) . '"' : '';
                                                                $max = isset($field_config['max']) ? ' max="' . esc_attr($field_config['max']) . '"' : '';
                                                                $step = isset($field_config['step']) ? ' step="' . esc_attr($field_config['step']) . '"' : '';
                                                                echo '<input type="number" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($current_value) . '"' . $min . $max . $step . ' class="' . $input_class . '" placeholder="' . $field_placeholder . '" ' . $required . ' ' . $aria_describedby . '>';
                                                                break;
                                                            case 'textarea':
                                                                echo '<textarea id="' . $field_id . '" name="' . $field_name . '" rows="3" class="' . $input_class . '" placeholder="' . $field_placeholder . '" ' . $required . ' ' . $aria_describedby . '>' . wp_kses_post($current_value) . '</textarea>';
                                                                break;
                                                            case 'select':
                                                                echo '<select id="' . $field_id . '" name="' . $field_name . '" class="' . $select_class . '" ' . $required . ' ' . $aria_describedby . '>';
                                                                if (!empty($field_config['options']) && is_array($field_config['options'])) {
                                                                    foreach ($field_config['options'] as $opt_val => $opt_label) {
                                                                        echo '<option value="' . esc_attr($opt_val) . '" ' . selected($current_value, $opt_val, false) . '>' . esc_html($opt_label) . '</option>';
                                                                    }
                                                                }
                                                                echo '</select>';
                                                                break;
                                                            case 'multiselect':
                                                                echo '<select id="' . $field_id . '" name="' . $field_name . '[]" class="' . $select_class . '" multiple="multiple" aria-label="' . esc_attr($field_label) . '">';
                                                                if (!empty($field_config['options']) && is_array($field_config['options'])) {
                                                                    foreach ($field_config['options'] as $opt_val => $opt_label) {
                                                                        $selected = is_array($current_value) && in_array($opt_val, $current_value) ? 'selected="selected"' : '';
                                                                        echo '<option value="' . esc_attr($opt_val) . '" ' . $selected . '>' . esc_html($opt_label) . '</option>';
                                                                    }
                                                                }
                                                                echo '</select>';
                                                                break;
                                                            case 'readonly':
                                                                echo '<input type="text" id="' . $field_id . '" value="' . esc_attr($current_value) . '" class="' . $input_class . ' bg-gray-50" readonly>';
                                                                echo '<input type="hidden" name="' . $field_name . '" value="' . esc_attr($current_value) . '">';
                                                                break;
                                                            case 'checkbox':
                                                                echo '<div class="flex items-center">';
                                                                echo '  <input type="checkbox" name="' . $field_name . '" id="' . $field_id . '" value="yes" ' . checked($current_value, 'yes', false) . ' class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded">';
                                                                echo '  <label for="' . $field_id . '" class="ml-2 block text-sm text-gray-900">' . $field_label . '</label>';
                                                                echo '</div>';
                                                                if (!empty($field_description)) {
                                                                    echo '<p id="' . $field_id . '-description" class="mt-2 text-sm text-gray-400 leading-relaxed">' . esc_html($field_description) . '</p>';
                                                                }
                                                                break;
                                                            case 'image':
                                                                echo '<div class="mt-1 flex items-center">';
                                                                echo '  <span class="inline-block h-12 w-12 rounded-full overflow-hidden bg-gray-100">';
                                                                echo '    <img id="' . $field_id . '-preview" src="' . esc_url($current_value) . '" alt="' . esc_attr($field_label) . '" class="h-full w-full object-cover' . (empty($current_value) ? ' hidden' : '') . '">';
                                                                echo '  </span>';
                                                                echo '  <button type="button" id="upload_image_button_' . $field_id . '" class="upload-logo-button ml-5 bg-white py-2 px-3 border border-gray-300 rounded-md shadow-sm text-sm leading-4 font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" data-uploader_title="' . esc_attr__('Choose Logo', 'easy-invoice') . '" data-uploader_button_text="' . esc_attr__('Select Logo', 'easy-invoice') . '">';
                                                                echo esc_html__('Change', 'easy-invoice');
                                                                echo '</button>';
                                                                echo '  <input type="hidden" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($current_value) . '">';
                                                                echo '</div>';
                                                                break;
                                                            case 'wp_editor':
                                                                wp_editor( $current_value, $field_id, ['textarea_name' => $field_name, 'teeny' => true, 'media_buttons' => false, 'textarea_rows' => 7, 'editor_class' => 'mt-1'] );
                                                                // Handle description for wp_editor specifically
                                                                if (!empty($field_description)) {
                                                                    echo '<p class="mt-1 text-sm text-gray-400">' . esc_html($field_description) . '</p>';
                                                                }
                                                                break;
                                                            case 'test_email':
                                                                echo '<div class="mt-1">';
                                                                echo '  <div class="flex items-center space-x-3">';
                                                                echo '    <input type="email" id="' . $field_id . '_email" placeholder="' . esc_attr__('Enter email address to test', 'easy-invoice') . '" class="mt-1 block w-[200px] border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" value="' . esc_attr(get_option('admin_email')) . '">';
                                                                echo '    <button type="button" id="' . $field_id . '_button" class="test-email-button inline-flex items-center px-3 py-1.5 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 h-[38px]">';
                                                                echo '      <i class="fas fa-paper-plane mr-2" aria-hidden="true"></i>';
                                                                echo '      ' . esc_html__('Send Test Email', 'easy-invoice');
                                                                echo '    </button>';
                                                                echo '  </div>';
                                                                echo '  <div id="' . $field_id . '_result" class="mt-2" style="display: none;"></div>';
                                                                echo '</div>';
                                                                break;
                                                            case 'test_template_email':
                                                                $template_type = $field_config['template_type'] ?? '';
                                                                $is_payment_test = ($field_id === 'easy_invoice_payment_test_template');
                                                                $width_class = $is_payment_test ? 'w-[200px]' : 'w-64';
                                                                echo '<div class="mt-1">';
                                                                echo '  <div class="flex items-center space-x-3">';
                                                                echo '    <input type="email" id="' . $field_id . '_email" placeholder="' . esc_attr__('Enter email address to test', 'easy-invoice') . '" class="mt-1 block ' . $width_class . ' border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" value="' . esc_attr(get_option('admin_email')) . '">';
                                                                echo '    <button type="button" id="' . $field_id . '_button" class="test-template-email-button inline-flex items-center px-3 py-1.5 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 h-[38px]" data-template-type="' . esc_attr($template_type) . '">';
                                                                echo '      <i class="fas fa-envelope mr-2" aria-hidden="true"></i>';
                                                                echo '      ' . esc_html__('Test Template', 'easy-invoice');
                                                                echo '    </button>';
                                                                echo '  </div>';
                                                                echo '  <div id="' . $field_id . '_result" class="mt-2" style="display: none;"></div>';
                                                                echo '</div>';
                                                                break;
                                                            case 'test_payment_reminder_email':
                                                                echo '<div class="mt-1">';
                                                                echo '  <div class="flex items-center space-x-3">';
                                                                echo '    <input type="email" id="' . $field_id . '_email" placeholder="' . esc_attr__('Enter email address to test', 'easy-invoice') . '" class="mt-1 block w-64 border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" value="' . esc_attr(get_option('admin_email')) . '">';
                                                                echo '    <button type="button" id="' . $field_id . '_button" class="test-payment-reminder-email-button inline-flex items-center px-3 py-1.5 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-orange-600 hover:bg-orange-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-orange-500 h-[38px]">';
                                                                echo '      <i class="fas fa-bell mr-2" aria-hidden="true"></i>';
                                                                echo '      ' . esc_html__('Test Reminder', 'easy-invoice');
                                                                echo '    </button>';
                                                                echo '  </div>';
                                                                echo '  <div id="' . $field_id . '_result" class="mt-2" style="display: none;"></div>';
                                                                echo '</div>';
                                                                break;
                                                            default:
                                                                echo '<input type="text" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($current_value) . '" class="' . $input_class . '" placeholder="' . $field_placeholder . '">';
                                                                break;
                                                        }
                                                        if ($field_type !== 'checkbox' && $field_type !== 'wp_editor' && !empty($field_description)) {
                                                            echo '<p class="mt-1 text-sm text-gray-400">' . esc_html($field_description) . '</p>';
                                                        }
                                                        echo '</div>'; // Close the field wrapper div
                                                    }
                                                }
                                                ?>

                                                <?php
                                                // Show placeholders only for specific email subsections (not General)
                                                if (in_array($subsection_key, ['invoice_available', 'quote_available', 'payment_received', 'payment_reminder'])): ?>
                                                <div class="mt-6">
                                                    <div class="bg-blue-50 border border-blue-200 rounded-lg p-4">
                                                        <p class="text-sm font-medium text-blue-900 mb-3"><?php esc_html_e('Available Placeholders:', 'easy-invoice'); ?></p>
                                                        <div class="grid grid-cols-2 md:grid-cols-3 gap-2">
                                                            <?php if (in_array($subsection_key, ['invoice_available', 'payment_received', 'payment_reminder'])): ?>
                                                                <!-- Invoice-specific placeholders -->
                                                                <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{invoice_number}}</div>
                                                                <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{due_date}}</div>
                                                                <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{invoice_url}}</div>
                                                                <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{payment_url}}</div>
                                                            <?php endif; ?>

                                                            <?php if (in_array($subsection_key, ['quote_available'])): ?>
                                                                <!-- Quote-specific placeholders -->
                                                                <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{quote_number}}</div>
                                                                <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{expiry_date}}</div>
                                                                <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{quote_url}}</div>
                                                            <?php endif; ?>

                                                            <!-- Common placeholders for all templates -->
                                                            <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{client_name}}</div>
                                                            <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{client_first_name}}</div>
                                                            <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{client_last_name}}</div>
                                                            <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{client_email}}</div>
                                                            <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{client_address}}</div>
                                                            <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{company_name}}</div>
                                                            <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{total_amount}}</div>
                                                            <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{issue_date}}</div>
                                                            <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{company_email}}</div>
                                                            <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{company_phone}}</div>
                                                            <div class="bg-white border border-blue-200 rounded px-3 py-2 text-xs text-blue-700 font-mono">{{payment_terms}}</div>
                                                            <?php do_action('easy_invoice_settings_email_placeholders', $settings); ?>
                                                        </div>
                                                    </div>
                                                </div>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                        <?php } ?>
                                    <?php elseif ($section_id === 'payment' && !empty($all_gateways_sorted)): ?>
                                        <div class="space-y-5">
                                            <?php if (count($all_gateways_sorted) > 1): ?>
                                            <div class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-6">
                                                <div class="flex items-center">
                                                    <i class="fas fa-info-circle text-blue-500 mr-2"></i>
                                                    <p class="text-sm text-blue-700">
                                                        <?php _e('Drag and drop payment methods to reorder them. The order will be reflected on payment forms.', 'easy-invoice'); ?>
                                                    </p>
                                                </div>
                                            </div>
                                            <?php endif; ?>

                                            <div id="sortable-gateways" class="space-y-5">
                                                <?php foreach ($all_gateways_sorted as $gateway_id => $gateway) : ?>
                                                    <div class="gateway-item bg-gray-50 p-5 rounded-lg border border-gray-200 cursor-move" data-gateway-id="<?php echo esc_attr($gateway_id); ?>">
                                                        <div class="flex items-center mb-3 space-x-3">
                                                            <!-- Drag Handle -->
                                                            <div class="gateway-handle text-gray-400 hover:text-gray-600 cursor-move mr-2">
                                                                <i class="fas fa-grip-vertical"></i>
                                                            </div>

                                                            <input type="checkbox"
                                                                class="gateway-enable-checkbox h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded"
                                                                name="settings[easy_invoice_payment_methods][]"
                                                                id="gateway-enable-<?php echo esc_attr($gateway_id); ?>"
                                                                value="<?php echo esc_attr($gateway_id); ?>"
                                                                <?php checked(!empty($payment_methods_enabled) && in_array($gateway_id, $payment_methods_enabled)); ?>>

                                                            <div class="flex-1">
                                                                <label for="gateway-enable-<?php echo esc_attr($gateway_id); ?>" class="font-medium text-gray-900">
                                                                    <?php echo esc_html(method_exists($gateway, 'getTitle') ? $gateway->getTitle() : $gateway_id); ?>
                                                                </label>
                                                                <?php if (method_exists($gateway, 'getDescription') && $gateway->getDescription()): ?>
                                                                    <span class="block text-sm text-gray-400"><?php echo esc_html($gateway->getDescription()); ?></span>
                                                                <?php endif; ?>
                                                            </div>
                                                        </div>

                                                        <!-- Custom Display Name Field -->
                                                        <div class="gateway-settings <?php echo empty($payment_methods_enabled) || !in_array($gateway_id, $payment_methods_enabled) ? 'hidden' : ''; ?>">
                                                            <div class="mb-4">
                                                                <label for="gateway-display-name-<?php echo esc_attr($gateway_id); ?>" class="block text-sm font-medium text-gray-700">
                                                                    <?php _e('Display Name', 'easy-invoice'); ?>
                                                                </label>
                                                                <input type="text"
                                                                    id="gateway-display-name-<?php echo esc_attr($gateway_id); ?>"
                                                                    name="settings[easy_invoice_gateway_display_name_<?php echo esc_attr($gateway_id); ?>]"
                                                                    value="<?php echo esc_attr($settings['easy_invoice_gateway_display_name_' . $gateway_id] ?? (method_exists($gateway, 'getTitle') ? $gateway->getTitle() : ucfirst(str_replace('_', ' ', $gateway_id)))); ?>"
                                                                    placeholder="<?php echo esc_attr(method_exists($gateway, 'getTitle') ? $gateway->getTitle() : ucfirst(str_replace('_', ' ', $gateway_id))); ?>"
                                                                    class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                                                                <p class="mt-1 text-xs text-gray-500">
                                                                    <?php _e('Leave blank to use the default name. This name will appear on invoices and payment forms.', 'easy-invoice'); ?>
                                                                </p>
                                                            </div>

                                                            <?php
                                                            $gateway_config = method_exists($gateway, 'getSettingsConfig') ? $gateway->getSettingsConfig() : [];
                                                            if (!empty($gateway_config['fields'])): ?>
                                                                <div class="grid grid-cols-1 md:grid-cols-6 gap-4">
                                                                    <?php foreach ($gateway_config['fields'] as $option_key => $field_config): ?>
                                                                        <?php
                                                                        $current_value = $settings[$option_key] ?? ($field_config['default'] ?? '');
                                                                        easy_invoice_render_field($option_key, $field_config, $current_value);
                                                                        ?>
                                                                    <?php endforeach; ?>
                                                                </div>
                                                            <?php endif; ?>
                                                        </div>

                                                        <input type="hidden" class="gateway-order-input" name="settings[easy_invoice_gateway_order][]" value="<?php echo esc_attr($gateway_id); ?>">
                                                    </div>
                                                <?php endforeach; ?>
                                            </div>
                                        </div>
                                    <?php elseif (!empty($section_data['fields'])): ?>
                                        <?php
                                        // Check if this section has subsections
                                        if (!empty($section_data['subsections'])) {
                                            // Handle sections with subsections (like invoice, quote, etc.)
                                            foreach ($section_data['subsections'] as $subsection_id => $subsection_data) {
                                                $subsection_title = $subsection_data['title'] ?? '';
                                                $subsection_description = $subsection_data['description'] ?? '';
                                                ?>
                                                <div class="subsection-content mb-8">
                                                    <div class="mb-4">
                                                        <h3 class="text-lg font-medium text-gray-900"><?php echo esc_html($subsection_title); ?></h3>
                                                        <?php if (!empty($subsection_description)): ?>
                                                            <p class="text-sm text-gray-400"><?php echo esc_html($subsection_description); ?></p>
                                                        <?php endif; ?>
                                                    </div>
                                                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                                                        <?php if (!empty($subsection_data['fields'])) {
                                                            foreach ($subsection_data['fields'] as $option_key => $field_config) {
                                                                $current_value = $settings[$option_key] ?? ($field_config['default'] ?? '');
                                                                easy_invoice_render_field($option_key, $field_config, $current_value);
                                                            }
                                                        }
                                                        ?>
                                                    </div>
                                                </div>
                                                <?php
                                            }
                                        } else {
                                            // Handle regular sections without subsections
                                            if ($section_id === 'tax') {
                                                // Render Enable Tax checkbox full width
                                                $enable_tax_key = 'easy_invoice_tax_enabled';
                                                $enable_tax_field = $section_data['fields'][$enable_tax_key];
                                                $current_value = $settings[$enable_tax_key] ?? ($enable_tax_field['default'] ?? '');
                                                echo '<div class="mb-4">';
                                                echo '<div class="flex items-center">';
                                                echo '  <input type="checkbox" name="settings[' . esc_attr($enable_tax_key) . ']" id="' . esc_attr($enable_tax_key) . '" value="yes" ' . checked($current_value, 'yes', false) . ' class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded">';
                                                echo '  <label for="' . esc_attr($enable_tax_key) . '" class="ml-2 block text-sm text-gray-900">' . esc_html($enable_tax_field['label']) . '</label>';
                                                echo '</div>';
                                                if (!empty($enable_tax_field['description'])) {
                                                    echo '<p id="' . $field_id . '-description" class="text-gray-400 mt-1 text-xs">' . esc_html($enable_tax_field['description']) . '</p>';
                                                }
                                                echo '</div>';
                                                // Render the other three fields in a 3-column grid
                                                echo '<div class="grid grid-cols-1 md:grid-cols-3 gap-6">';
                                                $tax_keys = ['easy_invoice_tax_entry_method', 'easy_invoice_tax_rate', 'easy_invoice_tax_name'];
                                                $select_class = 'mt-1 block w-full pl-3 pr-10 py-2 text-base border border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md';
                                                $input_class = 'mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm';
                                                foreach ($tax_keys as $tax_key) {
                                                    $field_config = $section_data['fields'][$tax_key];
                                                    $current_value = $settings[$tax_key] ?? ($field_config['default'] ?? '');
                                                    echo '<div>';
                                                    if ($field_config['type'] !== 'checkbox') {
                                                        echo '<label for="' . esc_attr($tax_key) . '" class="block text-sm font-medium text-gray-700">' . esc_html($field_config['label']) . '</label>';
                                                    }
                                                    switch ($field_config['type']) {
                                                        case 'text':
                                                        case 'email':
                                                        case 'url':
                                                        case 'tel':
                                                            echo '<input type="' . esc_attr($field_config['type']) . '" name="settings[' . esc_attr($tax_key) . ']" id="' . esc_attr($tax_key) . '" value="' . esc_attr($current_value) . '" class="' . $input_class . '" placeholder="' . esc_attr($field_config['placeholder'] ?? '') . '">';
                                                            break;
                                                        case 'number':
                                                            $min = isset($field_config['min']) ? ' min="' . esc_attr($field_config['min']) . '"' : '';
                                                            $max = isset($field_config['max']) ? ' max="' . esc_attr($field_config['max']) . '"' : '';
                                                            $step = isset($field_config['step']) ? ' step="' . esc_attr($field_config['step']) . '"' : '';
                                                            echo '<input type="number" name="settings[' . esc_attr($tax_key) . ']" id="' . esc_attr($tax_key) . '" value="' . esc_attr($current_value) . '"' . $min . $max . $step . ' class="' . $input_class . '" placeholder="' . esc_attr($field_config['placeholder'] ?? '') . '">';
                                                            break;
                                                        case 'select':
                                                            echo '<select id="' . esc_attr($tax_key) . '" name="settings[' . esc_attr($tax_key) . ']" class="' . $select_class . '">';
                                                            if (!empty($field_config['options']) && is_array($field_config['options'])) {
                                                                foreach ($field_config['options'] as $opt_val => $opt_label) {
                                                                    echo '<option value="' . esc_attr($opt_val) . '" ' . selected($current_value, $opt_val, false) . '>' . esc_html($opt_label) . '</option>';
                                                                }
                                                            }
                                                            echo '</select>';
                                                            break;
                                                    }
                                                    if (!empty($field_config['description'])) {
                                                        echo '<p class="mt-1 text-xs text-gray-400">' . esc_html($field_config['description']) . '</p>';
                                                    }
                                                    echo '</div>';
                                                }
                                                echo '</div>';

                                                // Allow additional fields to be added to the tax section
                                                $additional_tax_fields = apply_filters('easy_invoice_tax_section_additional_fields', [], $settings);
                                                if (!empty($additional_tax_fields)) {
                                                    echo '<div class="mt-6 pt-6 border-t border-gray-200">';
                                                    echo '<h4 class="text-sm font-medium text-gray-900 mb-4">' . __('Additional Tax Settings', 'easy-invoice-pro') . '</h4>';
                                                    echo '<div class="grid grid-cols-1 md:grid-cols-2 gap-6">';
                                                    foreach ($additional_tax_fields as $field_key => $field_config) {
                                                        $current_value = $settings[$field_key] ?? ($field_config['default'] ?? '');
                                                        easy_invoice_render_field($field_key, $field_config, $current_value);
                                                    }
                                                    echo '</div>';
                                                    echo '</div>';
                                                }
                                            } else {
                                                // Use a two-column grid for currency and company settings
                                                $is_grid = ($section_id === 'currency' || $section_id === 'company');
                                                // Use a 6-column grid for invoice and quote settings to allow specific field pairing
                                                $is_invoice_grid = ($section_id === 'invoice');
                                                $is_quote_grid = ($section_id === 'quote');
                                                // Use a specific grid for advanced settings to improve spacing
                                                $is_advanced_grid = ($section_id === 'advanced');
                                                // Use a 3-column grid for text settings
                                                $is_text_settings_grid = ($section_id === 'text_settings');
                                                if ($is_grid) echo '<div class="grid grid-cols-1 md:grid-cols-2 gap-6">';
                                                if ($is_invoice_grid) echo '<div class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">';
                                                if ($is_quote_grid) echo '<div class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">';
                                                if ($is_advanced_grid) echo '<div class="grid grid-cols-1 gap-6">';
                                                if ($is_text_settings_grid) echo '<div class="grid grid-cols-1 md:grid-cols-3 gap-6">';
                                                foreach ($section_data['fields'] as $option_key => $field_config) {
                                                    $current_value = $settings[$option_key] ?? ($field_config['default'] ?? '');
                                                    $field_id = esc_attr($option_key);
                                                    $field_type = $field_config['type'] ?? 'text';
                                                    $field_label = esc_html($field_config['label'] ?? '');
                                                    $field_placeholder = isset($field_config['placeholder']) ? esc_attr($field_config['placeholder']) : '';
                                                    $field_description = $field_config['description'] ?? '';
                                                    $field_name = 'settings[' . esc_attr($option_key) . ']';
                                                    $input_class = 'mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm';
                                                    $select_class = 'mt-1 block w-full pl-3 pr-10 py-2 text-base border border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md';
                                                    $col_span_class = esc_attr($field_config['col_span'] ?? 'sm:col-span-6');
                                                    $form_group_class = '';
                                                    if ($is_grid) {
                                                        $form_group_class = '';
                                                    } elseif ($is_invoice_grid || $is_quote_grid || $is_advanced_grid || $is_text_settings_grid) {
                                                        $form_group_class = $col_span_class;
                                                    } else {
                                                        $form_group_class = $col_span_class;
                                                    }

                                                    // Handle conditional display
                                                    $conditional_class = '';
                                                    if (isset($field_config['depends_on']) && is_array($field_config['depends_on'])) {
                                                        $conditional_class = ' conditional-field';
                                                        foreach ($field_config['depends_on'] as $depends_key => $depends_value) {
                                                            $depends_field_name = 'settings[' . esc_attr($depends_key) . ']';
                                                            $conditional_class .= ' depends-on-' . esc_attr($depends_key) . '-' . esc_attr($depends_value);
                                                        }
                                                    }

                                                    echo '<div class="form-group ' . $form_group_class . $conditional_class . '">';
                                                    if ($field_type !== 'checkbox') {
                                                        echo '<label for="' . $field_id . '" class="block text-sm font-medium text-gray-700">' . $field_label . '</label>';
                                                    }

                                                    // Define required and aria_describedby variables
                                                    $required = !empty($field_config['required']) ? 'required' : '';
                                                    $aria_describedby = !empty($field_description) ? 'aria-describedby="' . $field_id . '-description"' : '';

                                                    switch ($field_type) {
                                                        case 'text':
                                                        case 'email':
                                                        case 'url':
                                                        case 'tel':
                                                            echo '<input type="' . esc_attr($field_type) . '" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($current_value) . '" class="' . $input_class . '" placeholder="' . $field_placeholder . '" ' . $required . ' ' . $aria_describedby . '>';
                                                            break;
                                                        case 'number':
                                                            $min = isset($field_config['min']) ? ' min="' . esc_attr($field_config['min']) . '"' : '';
                                                            $max = isset($field_config['max']) ? ' max="' . esc_attr($field_config['max']) . '"' : '';
                                                            $step = isset($field_config['step']) ? ' step="' . esc_attr($field_config['step']) . '"' : '';
                                                            echo '<input type="number" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($current_value) . '"' . $min . $max . $step . ' class="' . $input_class . '" placeholder="' . $field_placeholder . '" ' . $required . ' ' . $aria_describedby . '>';
                                                            break;
                                                        case 'textarea':
                                                            echo '<textarea id="' . $field_id . '" name="' . $field_name . '" rows="3" class="' . $input_class . '" placeholder="' . $field_placeholder . '" ' . $required . ' ' . $aria_describedby . '>' . wp_kses_post($current_value) . '</textarea>';
                                                            break;
                                                        case 'select':
                                                            echo '<select id="' . $field_id . '" name="' . $field_name . '" class="' . $select_class . '" ' . $required . ' ' . $aria_describedby . '>';
                                                            if (!empty($field_config['options']) && is_array($field_config['options'])) {
                                                                foreach ($field_config['options'] as $opt_val => $opt_label) {
                                                                    echo '<option value="' . esc_attr($opt_val) . '" ' . selected(strtolower($current_value), strtolower($opt_val), false) . '>' . esc_html($opt_label) . '</option>';
                                                                }
                                                            }
                                                            echo '</select>';
                                                            break;
                                                        case 'multiselect':
                                                            echo '<select id="' . $field_id . '" name="' . $field_name . '[]" class="' . $select_class . '" multiple="multiple" aria-label="' . esc_attr($field_label) . '">';
                                                            if (!empty($field_config['options']) && is_array($field_config['options'])) {
                                                                foreach ($field_config['options'] as $opt_val => $opt_label) {
                                                                    $selected = is_array($current_value) && in_array($opt_val, $current_value) ? 'selected="selected"' : '';
                                                                    echo '<option value="' . esc_attr($opt_val) . '" ' . $selected . '>' . esc_html($opt_label) . '</option>';
                                                                }
                                                            }
                                                            echo '</select>';
                                                            break;
                                                        case 'readonly':
                                                            echo '<input type="text" id="' . $field_id . '" value="' . esc_attr($current_value) . '" class="' . $input_class . ' bg-gray-50" readonly>';
                                                            echo '<input type="hidden" name="' . $field_name . '" value="' . esc_attr($current_value) . '">';
                                                            break;
                                                        case 'checkbox':
                                                            echo '<div class="flex items-center">';
                                                            echo '  <input type="checkbox" name="' . $field_name . '" id="' . $field_id . '" value="yes" ' . checked($current_value, 'yes', false) . ' class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded">';
                                                            echo '  <label for="' . $field_id . '" class="ml-2 block text-sm text-gray-900">' . $field_label . '</label>';
                                                            echo '</div>';
                                                            if (!empty($field_description)) {
                                                                echo '<p id="' . $field_id . '-description" class="mt-2 text-sm text-gray-400 leading-relaxed">' . esc_html($field_description) . '</p>';
                                                            }
                                                            break;
                                                        case 'image':
                                                            echo '<div class="mt-1 flex items-center">';
                                                            echo '  <span class="inline-block h-12 w-12 rounded-full overflow-hidden bg-gray-100">';
                                                            echo '    <img id="' . $field_id . '-preview" src="' . esc_url($current_value) . '" alt="' . esc_attr($field_label) . '" class="h-full w-full object-cover' . (empty($current_value) ? ' hidden' : '') . '">';
                                                            echo '  </span>';
                                                            echo '  <button type="button" id="upload_image_button_' . $field_id . '" class="upload-logo-button ml-5 bg-white py-2 px-3 border border-gray-300 rounded-md shadow-sm text-sm leading-4 font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" data-uploader_title="' . esc_attr__('Choose Logo', 'easy-invoice') . '" data-uploader_button_text="' . esc_attr__('Select Logo', 'easy-invoice') . '">';
                                                            echo esc_html__('Change', 'easy-invoice');
                                                            echo '</button>';
                                                            echo '  <input type="hidden" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($current_value) . '">';
                                                            echo '</div>';
                                                            break;
                                                        case 'wp_editor':
                                                            wp_editor( $current_value, $field_id, ['textarea_name' => $field_name, 'teeny' => true, 'media_buttons' => false, 'textarea_rows' => 7, 'editor_class' => 'mt-1'] );
                                                            // Handle description for wp_editor specifically
                                                            if (!empty($field_description)) {
                                                                echo '<p class="mt-1 text-sm text-gray-400">' . esc_html($field_description) . '</p>';
                                                            }
                                                            break;
                                                        case 'color':
                                                            echo '<input type="color" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($current_value) . '" class="h-10 w-20 border border-gray-300 rounded">';
                                                            break;
                                                        case 'range':
                                                            $min = isset($field_config['min']) ? ' min="' . esc_attr($field_config['min']) . '"' : '';
                                                            $max = isset($field_config['max']) ? ' max="' . esc_attr($field_config['max']) . '"' : '';
                                                            $step = isset($field_config['step']) ? ' step="' . esc_attr($field_config['step']) . '"' : '';
                                                            echo '<input type="range" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($current_value) . '"' . $min . $max . $step . ' class="w-full">';
                                                            echo '<span class="text-sm text-gray-400">' . esc_html($current_value) . '%</span>';
                                                            break;
                                                        case 'image_upload':
                                                            echo '<div class="image-upload-wrap">';
                                                            echo '<div class="flex items-center space-x-3">';
                                                            echo '  <input type="hidden" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($current_value) . '">';
                                                            echo '  <button type="button" class="upload-image-button bg-white py-2 px-3 border border-gray-300 rounded-md shadow-sm text-sm leading-4 font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" data-target="' . $field_id . '">';
                                                            echo esc_html__('Upload Image', 'easy-invoice');
                                                            echo '</button>';
                                                            echo '</div>';
                                                            echo '<div class="image-preview mt-2' . (empty($current_value) ? ' hidden' : '') . '">';
                                                            if (!empty($current_value)) {
                                                                echo '<img src="' . esc_url($current_value) . '" alt="Preview" style="max-width: 200px; max-height: 100px;">';
                                                            }
                                                            echo '</div>';
                                                            echo '</div>';
                                                            break;
                                                        case 'button':
                                                            $button_text = $field_config['button_text'] ?? $field_label;
                                                            $button_class = $field_config['button_class'] ?? 'button button-secondary';
                                                            $button_class_name = 'regenerate-invoice-numbers-button';
                                                            if ($option_key === 'easy_invoice_regenerate_quote_numbers') {
                                                                $button_class_name = 'regenerate-quote-numbers-button';
                                                            }
                                                            echo '<button type="button" id="' . $field_id . '" class="' . esc_attr($button_class) . ' ' . $button_class_name . '">';
                                                            echo esc_html($button_text);
                                                            echo '</button>';
                                                            break;
                                                        default:
                                                            echo '<input type="text" name="' . $field_name . '" id="' . $field_id . '" value="' . esc_attr($current_value) . '" class="' . $input_class . '" placeholder="' . $field_placeholder . '">';
                                                            break;
                                                    }
                                                    if ($field_type !== 'checkbox' && $field_type !== 'wp_editor' && !empty($field_description)) {
                                                        echo '<p class="mt-1 text-sm text-gray-400">' . esc_html($field_description) . '</p>';
                                                    }
                                                    echo '</div>';
                                                }
                                                if ($is_grid) echo '</div>';
                                                if ($is_invoice_grid) echo '</div>';
                                                if ($is_quote_grid) echo '</div>';
                                                if ($is_advanced_grid) echo '</div>';
                                                if ($is_text_settings_grid) echo '</div>';
                                            }
                                        }
                                        ?>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>

                    <!-- Bottom Save Button -->
                    <div class="mt-8 pt-6 border-t border-gray-200">
                        <div class="flex items-center justify-start space-x-3">
                            <button type="button" id="save-settings-bottom" class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition-colors duration-200" aria-label="<?php esc_attr_e('Save Settings', 'easy-invoice'); ?>">
                                <i class="fas fa-save mr-2" aria-hidden="true"></i>
                                <?php esc_html_e('Save Changes', 'easy-invoice'); ?>
                            </button>
                        </div>
                    </div>

                </form>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Enter key functionality to save settings
    const settingsForm = document.getElementById('settings-form');
    const saveButton = document.getElementById('save-settings-bottom');

    if (settingsForm && saveButton) {
        // Get both save buttons
        const topSaveButton = document.getElementById('save-settings');
        const bottomSaveButton = document.getElementById('save-settings-bottom');

        // Function to trigger save
        function triggerSave() {
            // Try bottom button first, then top button
            if (bottomSaveButton) {
                bottomSaveButton.click();
            } else if (topSaveButton) {
                topSaveButton.click();
            }
        }

        // Listen for Enter key on form inputs
        settingsForm.addEventListener('keydown', function(e) {
            // Check if Enter key is pressed and not in a textarea or select
            if (e.key === 'Enter' && e.target.tagName !== 'TEXTAREA' && e.target.tagName !== 'SELECT') {
                e.preventDefault();
                triggerSave();
            }
        });

        // Also listen for Enter key on the entire form
        settingsForm.addEventListener('keypress', function(e) {
            if (e.key === 'Enter' && e.target.tagName !== 'TEXTAREA' && e.target.tagName !== 'SELECT') {
                e.preventDefault();
                triggerSave();
            }
        });
    }

    // Email Settings submenu hover behavior for settings page
    const emailNavLink = document.querySelector('.nav-link[href*="section=email"]');
    const emailSubmenu = document.querySelector('.email-settings-submenu');

    if (emailNavLink && emailSubmenu) {
        // Check if any Email Settings subsection is active
        const activeSubmenuLink = document.querySelector('.email-submenu-link.text-indigo-700');
        const isEmailSectionActive = emailNavLink.classList.contains('text-indigo-700');
        const isAnySubsectionActive = activeSubmenuLink !== null;

        // If any subsection is active, show submenu permanently and disable hover effects
        if (isAnySubsectionActive || isEmailSectionActive) {
            emailSubmenu.style.display = 'block';
            emailSubmenu.style.opacity = '1';
            emailSubmenu.style.visibility = 'visible';
            emailSubmenu.style.maxHeight = '200px';
            emailSubmenu.classList.add('show');
            return; // Exit early, no hover effects needed
        }

        // Only add hover effects if no subsection is active
        // Show submenu on hover
        emailNavLink.addEventListener('mouseenter', function() {
            emailSubmenu.style.display = 'block';
            emailSubmenu.classList.add('show');
            setTimeout(() => {
                emailSubmenu.style.opacity = '1';
                emailSubmenu.style.visibility = 'visible';
                emailSubmenu.style.maxHeight = '200px';
            }, 10);
        });

        // Hide submenu when mouse leaves
        emailNavLink.addEventListener('mouseleave', function() {
            // Only hide if not hovering over submenu
            setTimeout(() => {
                if (!emailSubmenu.matches(':hover')) {
                    emailSubmenu.style.opacity = '0';
                    emailSubmenu.style.visibility = 'hidden';
                    emailSubmenu.style.maxHeight = '0';
                    emailSubmenu.classList.remove('show');
                    setTimeout(() => {
                        emailSubmenu.style.display = 'none';
                    }, 200);
                }
            }, 100);
        });

        // Keep submenu open when hovering over submenu items
        emailSubmenu.addEventListener('mouseenter', function() {
            emailSubmenu.style.opacity = '1';
            emailSubmenu.style.visibility = 'visible';
            emailSubmenu.style.maxHeight = '200px';
            emailSubmenu.classList.add('show');
        });

        emailSubmenu.addEventListener('mouseleave', function() {
            emailSubmenu.style.opacity = '0';
            emailSubmenu.style.visibility = 'hidden';
            emailSubmenu.style.maxHeight = '0';
            emailSubmenu.classList.remove('show');
            setTimeout(() => {
                emailSubmenu.style.display = 'none';
            }, 200);
        });
    }
});
</script>

<style>
    #screen-meta-links{
        display:none;
    }
/* Email Settings Submenu Styles for Settings Page */
.email-settings-submenu {
    transition: all 0.3s ease;
    overflow: hidden;
}

.email-settings-submenu.show {
    display: block !important;
}

.email-settings-submenu.show ~ .nav-link .fas.fa-chevron-down,
.email-settings-submenu:hover ~ .nav-link .fas.fa-chevron-down {
    transform: rotate(180deg);
}

/* Custom color for settings navigation icons */
.nav-link:hover svg {
    color: rgb(79 70 229) !important;
}

/* Active state icon color */
.bg-indigo-50 svg {
    color: rgb(79 70 229) !important;
}

/* Hover state for non-active items */
.nav-link:hover:not(.bg-indigo-50) svg {
    color: rgb(79 70 229) !important;
}

/* Email submenu icon colors */
.email-submenu-link:hover svg {
    color: rgb(79 70 229) !important;
}

.email-submenu-link.bg-indigo-50 svg {
    color: rgb(79 70 229) !important;
}
</style>
