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

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

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

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

        // Delete the transient after displaying the notice.
        delete_transient('iafwzcai_settings_saved');
    }
    $message = get_transient('iafwzcai_license_fail');

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

        // Delete the transient after displaying the message.
        delete_transient('iafwzcai_license_fail');
    }
    $message = get_transient('iafwzcai_license_success');

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

        // Delete the transient after displaying the message.
        delete_transient('iafwzcai_license_success');
    }
    ?>
    <div style="margin-bottom:30px;">
        <p class="iafwzcai-settings-heading iafwzcai-settings-heading-active" id="iafwzcai-settings-header"><?php esc_html_e('Settings', 'integrate-with-zoho-campaigns'); ?></p>
        <p class="iafwzcai-settings-heading" id="iafwzcai-license-header"><?php esc_html_e('License', 'integrate-with-zoho-campaigns'); ?></p>
    </div>
    <!-- ----------Settings----------- -->
    <div id="iafwzcai-settings-section" style="display: <?php echo ($menu_param === 'settings') ? 'block' : 'none'; ?>;">
        <?php
        // Fetch and sanitize options from the database
        $delete_data_on_uninstall = get_option('iafwzcai_delete_data_on_uninstall', 'no') === 'yes' ? 'yes' : 'no';
        $error_notification = get_option('iafwzcai_error_notification', 'no') === 'yes' ? 'yes' : 'no';
        $error_notification_email = sanitize_text_field(get_option('iafwzcai_error_notification_email', ''));
        $error_notification_subject = sanitize_text_field(get_option('iafwzcai_error_notification_subject', ''));
        ?>

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

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

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

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

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

            <input type="hidden" name="action" value="iafwzcai_delete_data_confirm">
            <?php submit_button(__('Save Settings', 'integrate-with-zoho-campaigns')); ?>
        </form>
    </div>
    <?php
    $iafwzcai_request_key = get_option('iafwzcai_request_key', '');
    $key_is_valid = !empty($iafwzcai_request_key); // Check if the key exists

    $license_expiry_time = null;
    if ($session) {
        $license_expiry_time = get_option('iafwzcai_session_key', null);
        if ($license_expiry_time !== null) {
            if ($license_expiry_time === "MTExMTExMTExMTExMQ==") {
                $license_expiry_time = "Lifetime";
            } else {
                $license_expiry_time = base64_decode($license_expiry_time, true);
                if ($license_expiry_time !== false) {

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

                }
            }
        }
    } else {
        $license_expiry_time = get_option('iafwzcai_session_key', null);
        if ($license_expiry_time !== null) {
            $current_time = round(microtime(true) * 1000);
            $license_expiry_time = base64_decode($license_expiry_time, true);
            if ((int)$license_expiry_time < $current_time) {
                $license_expiry_time = gmdate('d-M-Y', (int) $license_expiry_time / 1000) . " (Expired)";
            }
        }
    }

    ?>

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

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

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

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

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


        </div>
    </div>


</div>