<?php
// Exit if accessed directly to ensure security.
if (!defined('ABSPATH')) {
    exit;
}
?>

<div id="iafwzcai-content-ai-settings" class="iafwzcai-tab-content" style="display:none;">
    <!-- <div class="notice notice-success is-dismissible">
        <h3 style="color: #333; margin-bottom: 5px;">AI Process</h3>
        <p style="color: #555; margin: 0;">
            After form submission, AI checks the inputs based on the settings below. If detected, it logs the data instead of sending it.
        </p>
    </div> -->

    <div class="iafwzcai-ai-settings-tabs">
        <div class="iafwzcai-ai-settings-tab active" onclick="showTab('iafwzcai-ai-settings-email-validation')">Email Validation</div>
        <div class="iafwzcai-ai-settings-tab" onclick="showTab('iafwzcai-ai-settings-email-detection')">Email Detection</div>
        <div class="iafwzcai-ai-settings-tab" onclick="showTab('iafwzcai-ai-settings-spam-detection')">Spam Detection</div>
        <!-- <div class="iafwzcai-ai-settings-tab" onclick="showTab('iafwzcai-ai-settings-other-detection')">Other Detection</div> -->
    </div>
    <!-- Email Validation -->
    <?php
    // Load saved options
    $email_keywords = get_option('iafwzcai_email_validation_keywords', '');
    $email_handling = get_option('iafwzcai_email_validation_handling', 'send_mailchimp');
    $email_keywords_array = !empty($email_keywords) ? array_map('trim', explode(',', sanitize_text_field($email_keywords))) : [];
    ?>

    <div class="iafwzcai-ai-settings-tab-content active" id="iafwzcai-ai-settings-email-validation">
        <p><?php esc_html_e('Block submissions from public email domains to prevent unwanted form entries. If a submitted email contains any of the listed domains, it will be handled according to your selected action before being sent to Zoho Campaigns.', 'integrate-with-zoho-campaigns'); ?></p>

        <table class="form-table">
            <tr>
                <th scope="row">
                    <label for="iafwzcai_email_validation_keywords"><?php esc_html_e('Blocked Domains', 'integrate-with-zoho-campaigns'); ?></label>
                </th>
                <td>
                    <div class="iafwzcai-ai-settings-tag-container" id="iafwzcai-email-validation-div">
                        <?php foreach ($email_keywords_array as $keyword) : ?>
                            <span class="iafwzcai-ai-settings-tag">
                                <?php echo esc_html($keyword); ?>
                                <span class="iafwzcai-ai-settings-close" data-tag="<?php echo esc_attr($keyword); ?>">&times;</span>
                            </span>
                        <?php endforeach; ?>
                        <input type="text" class="iafwzcai-ai-settings-input" id="iafwzcai-email-validation-input" name="iafwzcai_email_validation_keywords" placeholder="<?php esc_attr_e('Enter keywords and press Enter...', 'integrate-with-zoho-campaigns'); ?>">
                    </div>
                    <div class="iafwzcai-ai-settings-reset-div"><button onclick="resetTags('iafwzcai-email-validation-div', 'publicEmails')">Reset</button> <button onclick="removeAllTags('iafwzcai-email-validation-div')">Clear All</button></div>
                    <input type="hidden" id="iafwzcai-email-hidden-field" name="iafwzcai_email_validation_keywords" value="<?php echo esc_attr($email_keywords); ?>">

                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="iafwzcai_email_validation_handling"><?php esc_html_e('Action', 'integrate-with-zoho-campaigns'); ?></label>
                </th>
                <td>
                    <select id="iafwzcai_email_validation_handling" name="iafwzcai_email_validation_handling">
                        <option value="send_mailchimp" <?php selected($email_handling, 'send_mailchimp'); ?>>
                            <?php esc_html_e('Send to Zoho Campaigns (Ignore AI Detection)', 'integrate-with-zoho-campaigns'); ?>
                        </option>
                        <option value="store_logs" <?php selected($email_handling, 'store_logs'); ?>>
                            <?php esc_html_e('Store in Logs (Review & Sync Later)', 'integrate-with-zoho-campaigns'); ?>
                        </option>
                    </select>
                </td>
            </tr>
        </table>

        <button type="button" class="button button-primary" onclick="iafwzcaiAiSettingsSubmitTags('Email_Validation')">
            <?php esc_html_e('Save Settings', 'integrate-with-zoho-campaigns'); ?>
        </button>
    </div>
    <!-- Email Detection -->
    <?php
    // Load saved options
    $spam_keywords = get_option('iafwzcai_email_detection_keywords', '');
    $spam_handling = get_option('iafwzcai_email_detection_handling', 'send_mailchimp');
    $spam_keywords_array = !empty($spam_keywords) ? array_map('trim', explode(',', sanitize_text_field($spam_keywords))) : [];
    ?>

    <div class="iafwzcai-ai-settings-tab-content" id="iafwzcai-ai-settings-email-detection">
        <p><?php esc_html_e('Detect and filter fake or disposable emails before they are sent to Zoho Campaigns. If a submitted email contains any of the listed keywords, it will be handled according to your selected action', 'integrate-with-zoho-campaigns'); ?></p>

        <table class="form-table">
            <tr>
                <th scope="row">
                    <label for="iafwzcai_email_keywords"><?php esc_html_e('Spam Email Keywords', 'integrate-with-zoho-campaigns'); ?></label>
                </th>
                <td>
                    <div class="iafwzcai-ai-settings-tag-container" id="iafwzcai-email-detection-div">
                        <?php foreach ($spam_keywords_array as $keyword) : ?>
                            <span class="iafwzcai-ai-settings-tag">
                                <?php echo esc_html($keyword); ?>
                                <span class="iafwzcai-ai-settings-close" data-tag="<?php echo esc_attr($keyword); ?>">&times;</span>
                            </span>
                        <?php endforeach; ?>
                        <input type="text" class="iafwzcai-ai-settings-input" id="iafwzcai-email-detection-input" name="iafwzcai_email_keywords" placeholder="<?php esc_attr_e('Enter keywords and press Enter...', 'integrate-with-zoho-campaigns'); ?>">
                    </div>
                    <div class="iafwzcai-ai-settings-reset-div"><button onclick="resetTags('iafwzcai-email-detection-div', 'email')">Reset</button> <button onclick="removeAllTags('iafwzcai-email-detection-div')">Clear All</button></div>
                    <input type="hidden" id="iafwzcai-email-hidden-field" name="iafwzcai_email_keywords" value="<?php echo esc_attr($spam_keywords); ?>">
                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="iafwzcai_email_handling"><?php esc_html_e('Action', 'integrate-with-zoho-campaigns'); ?></label>
                </th>
                <td>
                    <select id="iafwzcai_email_handling" name="iafwzcai_email_handling">
                        <option value="send_mailchimp" <?php selected($spam_handling, 'send_mailchimp'); ?>>
                            <?php esc_html_e('Send to Zoho Campaigns (Ignore AI Detection)', 'integrate-with-zoho-campaigns'); ?>
                        </option>
                        <option value="store_logs" <?php selected($spam_handling, 'store_logs'); ?>>
                            <?php esc_html_e('Store in Logs (Review & Sync Later)', 'integrate-with-zoho-campaigns'); ?>
                        </option>
                    </select>
                </td>
            </tr>
        </table>

        <button type="button" class="button button-primary" onclick="iafwzcaiAiSettingsSubmitTags('Email_Detection')">
            <?php esc_html_e('Save Settings', 'integrate-with-zoho-campaigns'); ?>
        </button>
    </div>
    <!-- Spam Detection -->
    <div class="iafwzcai-ai-settings-tab-content" id="iafwzcai-ai-settings-spam-detection">
        <p><?php esc_html_e('Detect and filter spam submissions by scanning all form fields for specific keywords before they are sent to Zoho Campaigns. If any field contains a listed keyword, it will be handled according to your selected action.', 'integrate-with-zoho-campaigns'); ?></p>

        <?php
        // Load saved options with proper escaping
        $spam_keywords = get_option('iafwzcai_spam_detection_keywords', ''); // Get stored spam keywords
        $spam_handling = get_option('iafwzcai_spam_detection_handling', 'send_mailchimp'); // Get stored spam handling

        // Convert keywords to an array
        $spam_keywords_array = ! empty($spam_keywords) ? array_map('trim', explode(',', sanitize_text_field($spam_keywords))) : [];
        ?>

        <table class="form-table">
            <tr>
                <th scope="row">
                    <label for="iafwzcai_spam_keywords"><?php esc_html_e('Blocked Spam Keywords', 'integrate-with-zoho-campaigns'); ?></label>
                </th>
                <td>
                    <div class="iafwzcai-ai-settings-tag-container" id="iafwzcai-ai-spam-div">
                        <?php foreach ($spam_keywords_array as $keyword) : ?>
                            <span class="iafwzcai-ai-settings-tag">
                                <?php echo esc_html($keyword); ?>
                                <span class="iafwzcai-ai-settings-close" data-tag="<?php echo esc_attr($keyword); ?>">&times;</span>
                            </span>
                        <?php endforeach; ?>
                        <input type="text" class="iafwzcai-ai-settings-input" id="iafwzcai-ai-input-spam" name="iafwzcai_spam_keywords" placeholder="<?php esc_attr_e('Enter keywords and press Enter...', 'integrate-with-zoho-campaigns'); ?>">
                    </div>
                    <div class="iafwzcai-ai-settings-reset-div"><button onclick="resetTags('iafwzcai-ai-spam-div', 'spam')">Reset</button> <button onclick="removeAllTags('iafwzcai-ai-spam-div')">Clear All</button></div>
                    <input type="hidden" id="iafwzcai-spam-hidden-field" name="iafwzcai_spam_keywords" value="<?php echo esc_attr($spam_keywords); ?>">
                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="iafwzcai_spam_handling"><?php esc_html_e('Action', 'integrate-with-zoho-campaigns'); ?></label>
                </th>
                <td>
                    <select id="iafwzcai_spam_handling" name="iafwzcai_spam_handling">
                        <option value="send_mailchimp" <?php selected($spam_handling, 'send_mailchimp'); ?>>
                            <?php esc_html_e('Send to Zoho Campaigns (Ignore AI Detection)', 'integrate-with-zoho-campaigns'); ?>
                        </option>
                        <option value="store_logs" <?php selected($spam_handling, 'store_logs'); ?>>
                            <?php esc_html_e('Store in Logs (Review & Sync Later)', 'integrate-with-zoho-campaigns'); ?>
                        </option>
                    </select>
                </td>
            </tr>
        </table>

        <button type="button" class="button button-primary" onclick="iafwzcaiAiSettingsSubmitTags('Spam_Detection')">
            <?php esc_html_e('Save Settings', 'integrate-with-zoho-campaigns'); ?>
        </button>
    </div>
</div>
<?php
// Register a dummy script handle
wp_register_script('iafwzcai-ai-settings', false, [], '1.0', true);
wp_enqueue_script('iafwzcai-ai-settings');

// Add the inline JavaScript for alert and redirect
wp_add_inline_script('iafwzcai-ai-settings', '
    setupTagInput("iafwzcai-ai-spam-div", "iafwzcai-ai-input-spam", "spam");
    setupTagInput("iafwzcai-email-detection-div", "iafwzcai-email-detection-input", "email");
    setupTagInput("iafwzcai-email-validation-div", "iafwzcai-email-validation-input", "publicEmails");
');
?>