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

    <div class="iafwzdesk-ai-settings-tabs">
        <div class="iafwzdesk-ai-settings-tab active" onclick="showTab('iafwzdesk-ai-settings-email-validation')">Email Validation</div>
        <div class="iafwzdesk-ai-settings-tab" onclick="showTab('iafwzdesk-ai-settings-email-detection')">Email Detection</div>
        <div class="iafwzdesk-ai-settings-tab" onclick="showTab('iafwzdesk-ai-settings-spam-detection')">Spam Detection</div>
        <!-- <div class="iafwzdesk-ai-settings-tab" onclick="showTab('iafwzdesk-ai-settings-other-detection')">Other Detection</div> -->
    </div>
    <!-- Email Validation -->
    <?php
    // Load saved options
    $iafwzdesk_email_keywords = get_option('iafwzdesk_email_validation_keywords', '');
    $iafwzdesk_email_handling = get_option('iafwzdesk_email_validation_handling', 'send_mailchimp');
    $iafwzdesk_email_keywords_array = !empty($iafwzdesk_email_keywords) ? array_map('trim', explode(',', sanitize_text_field($iafwzdesk_email_keywords))) : [];
    ?>

    <div class="iafwzdesk-ai-settings-tab-content active" id="iafwzdesk-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 Desk.', 'integrate-with-zoho-desk'); ?></p>

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

                </td>
            </tr>

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

        <button type="button" class="button button-primary" onclick="iafwzdeskAiSettingsSubmitTags('Email_Validation')">
            <?php esc_html_e('Save Settings', 'integrate-with-zoho-desk'); ?>
        </button>
    </div>
    <!-- Email Detection -->
    <?php
    // Load saved options
    $iafwzdesk_spam_keywords = get_option('iafwzdesk_email_detection_keywords', '');
    $iafwzdesk_spam_handling = get_option('iafwzdesk_email_detection_handling', 'send_mailchimp');
    $iafwzdesk_spam_keywords_array = !empty($iafwzdesk_spam_keywords) ? array_map('trim', explode(',', sanitize_text_field($iafwzdesk_spam_keywords))) : [];
    ?>

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

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

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

        <button type="button" class="button button-primary" onclick="iafwzdeskAiSettingsSubmitTags('Email_Detection')">
            <?php esc_html_e('Save Settings', 'integrate-with-zoho-desk'); ?>
        </button>
    </div>
    <!-- Spam Detection -->
    <div class="iafwzdesk-ai-settings-tab-content" id="iafwzdesk-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 Desk. If any field contains a listed keyword, it will be handled according to your selected action.', 'integrate-with-zoho-desk'); ?></p>

        <?php
        // Load saved options with proper escaping
        $iafwzdesk_spam_keywords = get_option('iafwzdesk_spam_detection_keywords', ''); // Get stored spam keywords
        $iafwzdesk_spam_handling = get_option('iafwzdesk_spam_detection_handling', 'send_mailchimp'); // Get stored spam handling

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

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

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

        <button type="button" class="button button-primary" onclick="iafwzdeskAiSettingsSubmitTags('Spam_Detection')">
            <?php esc_html_e('Save Settings', 'integrate-with-zoho-desk'); ?>
        </button>
    </div>
