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

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$iafwzdesk_menu_param = isset($_GET['menu']) ? sanitize_text_field(wp_unslash($_GET['menu'])) : 'settings';
?>

<div id="iafwzdesk-content-settings" class="iafwzdesk-tab-content" style="display:none;">

    <?php
    if (get_transient('iafwzdesk_settings_saved')) {
        // Display the notice.
        echo '<div class="notice notice-success is-dismissible">
            <p>' . esc_html__('Settings saved successfully.', 'integrate-with-zoho-desk') . '</p>
          </div>';

        // Delete the transient after displaying the notice.
        delete_transient('iafwzdesk_settings_saved');
    }
$iafwzdesk_message = get_transient('iafwzdesk_license_fail');

// If there is a message, display it as an admin notice.
if ($iafwzdesk_message) {
    echo '<div class="notice notice-error is-dismissible">
                <p>' . esc_html($iafwzdesk_message) . '</p>
              </div>';

    // Delete the transient after displaying the message.
    delete_transient('iafwzdesk_license_fail');
}
$iafwzdesk_message = get_transient('iafwzdesk_license_success');

// If there is a message, display it as an admin notice.
if ($iafwzdesk_message) {
    echo '<div class="notice notice-success is-dismissible">
                <p>' . esc_html($iafwzdesk_message) . '</p>
              </div>';

    // Delete the transient after displaying the message.
    delete_transient('iafwzdesk_license_success');
}
?>
    <div style="margin-bottom:30px;">
        <p class="iafwzdesk-settings-heading" id="iafwzdesk-settings-header"><?php esc_html_e('Settings', 'integrate-with-zoho-desk'); ?></p>
        <p class="iafwzdesk-settings-heading" id="iafwzdesk-form-filters-header"><?php esc_html_e('Form Filters', 'integrate-with-zoho-desk'); ?></p>
        <p class="iafwzdesk-settings-heading" id="iafwzdesk-license-header"><?php esc_html_e('License', 'integrate-with-zoho-desk'); ?></p>
    </div>
    <!-- ----------Settings----------- -->
    <div id="iafwzdesk-settings-section" style="display: <?php echo ($iafwzdesk_menu_param === 'settings') ? 'block' : 'none'; ?>;">
        <?php
    // Fetch and sanitize options from the database
    $iafwzdesk_delete_data_on_uninstall = get_option('iafwzdesk_delete_data_on_uninstall', 'no') === 'yes' ? 'yes' : 'no';
$iafwzdesk_error_notification = get_option('iafwzdesk_error_notification', 'no') === 'yes' ? 'yes' : 'no';
$iafwzdesk_error_notification_email = sanitize_text_field(get_option('iafwzdesk_error_notification_email', ''));
$iafwzdesk_error_notification_subject = sanitize_text_field(get_option('iafwzdesk_error_notification_subject', ''));
?>

        <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
            <?php wp_nonce_field('iafwzdesk_settings_data_action', 'iafwzdesk_settings_data_action_nonce'); ?>

            <table class="form-table">
                <tr>
                    <th scope="row">
                        <label for="iafwzdesk_delete_data_on_uninstall"><?php esc_html_e('Delete Data on Uninstall', 'integrate-with-zoho-desk'); ?></label>
                        <div class="iafwzdesk-help-icon-wrapper">
                            <span class="iafwzdesk-help-icon">?</span>
                            <div class="iafwzdesk-help-tooltip">
                                <p>Delete all plugin data automatically when the plugin is uninstalled. </p>
                            </div>
                        </div>
                    </th>
                    <td>
                        <input type="checkbox" id="iafwzdesk_delete_data_on_uninstall" name="iafwzdesk_delete_data_on_uninstall" value="yes" <?php checked($iafwzdesk_delete_data_on_uninstall, 'yes'); ?> />
                        <span><?php esc_html_e('Yes, delete all plugin data upon uninstallation', 'integrate-with-zoho-desk'); ?></span>
                    </td>
                </tr>

                <tr>
                    <th scope="row">
                        <label for="iafwzdesk_error_notification"><?php esc_html_e('Error Email Notification', 'integrate-with-zoho-desk'); ?></label>
                        <div class="iafwzdesk-help-icon-wrapper">
                            <span class="iafwzdesk-help-icon">?</span>
                            <div class="iafwzdesk-help-tooltip">
                                <p>Enable to receive email notifications if data fails to post to Zoho Desk due to errors. </p>
                            </div>
                        </div>
                    </th>
                    <td>
                        <input type="checkbox" id="iafwzdesk_error_notification" name="iafwzdesk_error_notification" value="yes" <?php checked($iafwzdesk_error_notification, 'yes'); ?> />
                        <span><?php esc_html_e('Yes, send email notifications for errors', 'integrate-with-zoho-desk'); ?></span>
                    </td>
                </tr>

                <tr>
                    <th scope="row">
                        <label for="iafwzdesk_error_notification_email"><?php esc_html_e('Email (comma separated)', 'integrate-with-zoho-desk'); ?></label>
                    </th>
                    <td>
                        <input type="text" id="iafwzdesk_error_notification_email" name="iafwzdesk_error_notification_email" value="<?php echo esc_attr($iafwzdesk_error_notification_email); ?>" class="regular-text" />
                        <p><?php esc_html_e('Enter email addresses separated by commas.', 'integrate-with-zoho-desk'); ?></p>
                    </td>
                </tr>

                <tr>
                    <th scope="row">
                        <label for="iafwzdesk_error_notification_subject"><?php esc_html_e('Email Subject', 'integrate-with-zoho-desk'); ?></label>
                    </th>
                    <td>
                        <input type="text" id="iafwzdesk_error_notification_subject" name="iafwzdesk_error_notification_subject" value="<?php echo esc_attr($iafwzdesk_error_notification_subject); ?>" class="regular-text" />
                        <p><?php esc_html_e('Enter the subject line for error notification emails.', 'integrate-with-zoho-desk'); ?></p>
                    </td>
                </tr>
            </table>

            <input type="hidden" name="action" value="iafwzdesk_delete_data_confirm">
            <?php submit_button(__('Save Settings', 'integrate-with-zoho-desk')); ?>
        </form>
    </div>
    <!-- ----------Form Filters (AI Settings)----------- -->
    <div id="iafwzdesk-form-filters-section" style="display: <?php echo ($iafwzdesk_menu_param === 'form-filters') ? 'block' : 'none'; ?>;">
        <?php include plugin_dir_path(__FILE__) . 'ai-settings-tab.php'; ?>
    </div>

    <?php
    wp_add_inline_script('iafwzdesk-ai-settings-js', '
        setupTagInput("iafwzdesk-ai-spam-div", "iafwzdesk-ai-input-spam", "spam");
        setupTagInput("iafwzdesk-email-detection-div", "iafwzdesk-email-detection-input", "email");
        setupTagInput("iafwzdesk-email-validation-div", "iafwzdesk-email-validation-input", "publicEmails");
    ');
?>

    <?php
$iafwzdesk_request_key = get_option('iafwzdesk_request_key', '');
$iafwzdesk_key_is_valid = !empty($iafwzdesk_request_key); // Check if the key exists

$iafwzdesk_license_expiry_time = null;
if ($session) {
    $iafwzdesk_license_expiry_time = get_option('iafwzdesk_session_key', null);
    if ($iafwzdesk_license_expiry_time !== null) {

        $iafwzdesk_license_expiry_time = base64_decode($iafwzdesk_license_expiry_time, true);
        if ($iafwzdesk_license_expiry_time !== false) {

            $iafwzdesk_license_expiry_time = gmdate('d-M-Y', $iafwzdesk_license_expiry_time / 1000); // Convert milliseconds to date time in dd-MMM-yyyy HH:mm am/pm format

        }

    }
} else {
    $iafwzdesk_license_expiry_time = get_option('iafwzdesk_session_key', null);
    if ($iafwzdesk_license_expiry_time !== null) {
        $iafwzdesk_current_time = round(microtime(true) * 1000);
        $iafwzdesk_license_expiry_time = base64_decode($iafwzdesk_license_expiry_time, true);
        if ($iafwzdesk_license_expiry_time !== false && (int)$iafwzdesk_license_expiry_time < $iafwzdesk_current_time) {
            $iafwzdesk_license_expiry_time = gmdate('d-M-Y', (int) $iafwzdesk_license_expiry_time / 1000) . " (Expired)";
        }
    }
}

?>

    <div id="iafwzdesk-license-section" style="display: <?php echo ($iafwzdesk_menu_param === 'license') ? 'block' : 'none'; ?>;">
        <div class="iafwzdesk-license-key">
            <p class="iafwzdesk-license-header"><?php esc_html_e('License Key', 'integrate-with-zoho-desk'); ?></p>
            <?php if (!empty($iafwzdesk_license_expiry_time)) : ?>
                <p>
                    <strong><?php esc_html_e('License Expiry Date: ', 'integrate-with-zoho-desk'); ?></strong>
                    <span class="iafwzdesk-license-highlighted-value"><?php echo esc_html($iafwzdesk_license_expiry_time); ?></span>
                </p>
                <p>
                    <strong><?php esc_html_e('Plan: ', 'integrate-with-zoho-desk'); ?></strong>
                    <span class="iafwzdesk-license-highlighted-value"><?php echo esc_html(get_option('iafwzdesk_session_plan', '--')); ?></span>
                </p>
                <p><?php esc_html_e('Your premium license is active. Enjoy all premium features!', 'integrate-with-zoho-desk'); ?></p>
            <?php else : ?>
                <p>
                    <?php esc_html_e("Unlock more features by ", 'integrate-with-zoho-desk'); ?>
                    <a href="<?php echo esc_url("https://integrazo.com/products/integrate-with-zoho-desk"); ?>" target="_blank">
                        <?php esc_html_e('Upgrade to Premium', 'integrate-with-zoho-desk'); ?>
                    </a>
                </p>
                <p><strong><?php esc_html_e('Already purchased?', 'integrate-with-zoho-desk'); ?></strong>
                    <?php esc_html_e('Simply enter your license key below to enable premium features!', 'integrate-with-zoho-desk'); ?>
                </p>
            <?php endif; ?>

            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
                <?php wp_nonce_field('iafwzdesk_request_key_action', 'iafwzdesk_request_key_nonce'); ?>

                <label for="iafwzdesk_request_key">
                    <input
                        type="text"
                        id="iafwzdesk_request_key"
                        name="iafwzdesk_request_key"
                        placeholder="<?php echo esc_attr__('Paste key here', 'integrate-with-zoho-desk'); ?>"
                        class=""
                        value="<?php echo esc_attr($iafwzdesk_request_key); ?>"
                        size="50"
                        autocomplete="off"
                        required />
                </label>
                <br><br>

                <input type="hidden" name="action" value="iafwzdesk_process_request">

                <?php
            if ($iafwzdesk_key_is_valid) {
                // Deactivate Key Button
                submit_button(
                    __('Deactivate Key', 'integrate-with-zoho-desk'),
                    'secondary',
                    'iafwzdesk_revoke_request_key',
                    false // Do not wrap in <p>
                );
            } else {
                // Process Key Button
                submit_button(
                    __('Process Key', 'integrate-with-zoho-desk'),
                    'primary',
                    'iafwzdesk_process_request_key',
                    false // Do not wrap in <p>
                );
            }
?>
            </form>


        </div>
    </div>


</div>
