<?php
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="integrazo_fwcrm_form-content-settings" class="integrazo_fwcrm_form-tab-content" style="display:none;">

    <?php
    if (get_transient('integrazo_fwcrm_form_settings_saved')) {
        // Display the notice.
        echo '<div class="notice notice-success is-dismissible">
            <p>' . esc_html__('Settings saved successfully.', 'crm-integration-freshworks-any-form') . '</p>
          </div>';

        // Delete the transient after displaying the notice.
        delete_transient('integrazo_fwcrm_form_settings_saved');
    }
    $message = get_transient('integrazo_fwcrm_form_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('integrazo_fwcrm_form_license_fail');
    }
    $message = get_transient('integrazo_fwcrm_form_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('integrazo_fwcrm_form_license_success');
    }
    ?>
    <div style="margin-bottom:30px;">
        <p class="integrazo_fwcrm_form-settings-heading integrazo_fwcrm_form-settings-heading-active" id="integrazo_fwcrm_form-settings-header"><?php esc_html_e('Settings', 'crm-integration-freshworks-any-form'); ?></p>
        <p class="integrazo_fwcrm_form-settings-heading" id="integrazo_fwcrm_form-license-header"><?php esc_html_e('License', 'crm-integration-freshworks-any-form'); ?></p>
    </div>
    <!-- ----------Settings----------- -->
    <div id="integrazo_fwcrm_form-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('integrazo_fwcrm_form_delete_data_on_uninstall', 'no') === 'yes' ? 'yes' : 'no';
        $error_notification = get_option('integrazo_fwcrm_form_error_notification', 'no') === 'yes' ? 'yes' : 'no';
        $error_notification_email = sanitize_text_field(get_option('integrazo_fwcrm_form_error_notification_email', ''));
        $error_notification_subject = sanitize_text_field(get_option('integrazo_fwcrm_form_error_notification_subject', ''));
        ?>

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

            <table class="form-table">
                <tr>
                    <th scope="row">
                        <label for="integrazo_fwcrm_form_delete_data_on_uninstall"><?php esc_html_e('Delete Data on Uninstall', 'crm-integration-freshworks-any-form'); ?></label>
                        <div class="integrazo_fwcrm_form-help-icon-wrapper">
                            <span class="integrazo_fwcrm_form-help-icon">?</span>
                            <div class="integrazo_fwcrm_form-help-tooltip">
                                <p>Delete all plugin data automatically when the plugin is uninstalled. </p>
                            </div>
                        </div>
                    </th>
                    <td>
                        <input type="checkbox" id="integrazo_fwcrm_form_delete_data_on_uninstall" name="integrazo_fwcrm_form_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', 'crm-integration-freshworks-any-form'); ?></span>
                    </td>
                </tr>

                <tr>
                    <th scope="row">
                        <label for="integrazo_fwcrm_form_error_notification"><?php esc_html_e('Error Email Notification', 'crm-integration-freshworks-any-form'); ?></label>
                        <div class="integrazo_fwcrm_form-help-icon-wrapper">
                            <span class="integrazo_fwcrm_form-help-icon">?</span>
                            <div class="integrazo_fwcrm_form-help-tooltip">
                                <p>Enable to receive email notifications if data fails to post to Freshworks CRM due to errors. </p>
                            </div>
                        </div>
                    </th>
                    <td>
                        <input type="checkbox" id="integrazo_fwcrm_form_error_notification" name="integrazo_fwcrm_form_error_notification" value="yes" <?php checked($error_notification, 'yes'); ?> />
                        <span><?php esc_html_e('Yes, send email notifications for errors', 'crm-integration-freshworks-any-form'); ?></span>
                    </td>
                </tr>

                <tr>
                    <th scope="row">
                        <label for="integrazo_fwcrm_form_error_notification_email"><?php esc_html_e('Email (comma separated)', 'crm-integration-freshworks-any-form'); ?></label>
                    </th>
                    <td>
                        <input type="text" id="integrazo_fwcrm_form_error_notification_email" name="integrazo_fwcrm_form_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.', 'crm-integration-freshworks-any-form'); ?></p>
                    </td>
                </tr>

                <tr>
                    <th scope="row">
                        <label for="integrazo_fwcrm_form_error_notification_subject"><?php esc_html_e('Email Subject', 'crm-integration-freshworks-any-form'); ?></label>
                    </th>
                    <td>
                        <input type="text" id="integrazo_fwcrm_form_error_notification_subject" name="integrazo_fwcrm_form_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.', 'crm-integration-freshworks-any-form'); ?></p>
                    </td>
                </tr>
            </table>

            <input type="hidden" name="action" value="integrazo_fwcrm_form_delete_data_confirm">
            <?php submit_button(__('Save Settings', 'crm-integration-freshworks-any-form')); ?>
        </form>
    </div>
    <?php
    $integrazo_fwcrm_form_request_key = get_option('integrazo_fwcrm_form_request_key', '');
    $key_is_valid = !empty($integrazo_fwcrm_form_request_key); // Check if the key exists

    $license_expiry_time = null;
    if ($session) {
        $license_expiry_time = get_option('integrazo_fwcrm_form_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('integrazo_fwcrm_form_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="integrazo_fwcrm_form-license-section" style="display: <?php echo ($menu_param === 'license') ? 'block' : 'none'; ?>;">
        <div class="integrazo_fwcrm_form-license-key">
            <p class="integrazo_fwcrm_form-license-header"><?php esc_html_e('License Key', 'crm-integration-freshworks-any-form'); ?></p>
            <?php if (!empty($license_expiry_time)) : ?>
                <p>
                    <strong><?php esc_html_e('License Expiry Date: ', 'crm-integration-freshworks-any-form'); ?></strong>
                    <span class="integrazo_fwcrm_form-license-highlighted-value"><?php echo esc_html($license_expiry_time); ?></span>
                </p>
                <p>
                    <strong><?php esc_html_e('Plan: ', 'crm-integration-freshworks-any-form'); ?></strong>
                    <span class="integrazo_fwcrm_form-license-highlighted-value"><?php echo esc_html(get_option('integrazo_fwcrm_form_session_plan', '--')); ?></span>
                </p>
                <p><?php esc_html_e('Your premium license is active. Enjoy all premium features!', 'crm-integration-freshworks-any-form'); ?></p>
            <?php else : ?>
                <p>
                    <?php esc_html_e("Unlock more features by ", 'crm-integration-freshworks-any-form'); ?>
                    <a href="<?php echo esc_url("https://integrazo.com/products/freshworks-crm-integration-with-any-form?utm_source=plugin&utm_medium=license_page_link&utm_campaign=freshworks_integration"); ?>" target="_blank">
                        <?php esc_html_e('Upgrade to Premium', 'crm-integration-freshworks-any-form'); ?>
                    </a>
                </p>
                <p><strong><?php esc_html_e('Already purchased?', 'crm-integration-freshworks-any-form'); ?></strong>
                    <?php esc_html_e('Simply enter your license key below to enable premium features!', 'crm-integration-freshworks-any-form'); ?>
                </p>
            <?php endif; ?>

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

                <label for="integrazo_fwcrm_form_request_key">
                    <input
                        type="text"
                        id="integrazo_fwcrm_form_request_key"
                        name="integrazo_fwcrm_form_request_key"
                        placeholder="<?php echo esc_attr__('Paste key here', 'crm-integration-freshworks-any-form'); ?>"
                        class="integrazo-fwcrm-form-regular-text"
                        value="<?php echo esc_attr($integrazo_fwcrm_form_request_key); ?>"
                        size="50"
                        required />
                </label>
                <br><br>

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

                <?php
                if ($key_is_valid) {
                    // Deactivate Key Button
                    submit_button(
                        __('Deactivate Key', 'crm-integration-freshworks-any-form'),
                        'secondary',
                        'integrazo_fwcrm_form_revoke_request_key',
                        false // Do not wrap in <p>
                    );
                } else {
                    // Process Key Button
                    submit_button(
                        __('Process Key', 'crm-integration-freshworks-any-form'),
                        'primary',
                        'integrazo_fwcrm_form_process_request_key',
                        false // Do not wrap in <p>
                    );
                }
                ?>
            </form>


        </div>
    </div>


</div>