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

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

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

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

        // Delete the transient after displaying the notice.
        delete_transient('iafwzc_settings_saved');
    }
    $iafwzc_message = get_transient('iafwzc_license_fail');

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

        // Delete the transient after displaying the message.
        delete_transient('iafwzc_license_fail');
    }
    $iafwzc_message = get_transient('iafwzc_license_success');

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

        // Delete the transient after displaying the message.
        delete_transient('iafwzc_license_success');
    }
    ?>
    <div style="margin-bottom:30px;">
        <p class="iafwzc-settings-heading iafwzc-settings-heading-active" id="iafwzc-settings-header"><?php esc_html_e('Settings', 'integrate-any-form-with-zoho-crm'); ?></p>
        <p class="iafwzc-settings-heading" id="iafwzc-license-header"><?php esc_html_e('License', 'integrate-any-form-with-zoho-crm'); ?></p>
    </div>
    <!-- ----------Settings----------- -->
    <div id="iafwzc-settings-section" style="display: <?php echo ($iafwzc_menu_param === 'settings') ? 'block' : 'none'; ?>;">
        <?php
        // Fetch and sanitize options from the database
        $iafwzc_delete_data_on_uninstall = get_option('iafwzc_delete_data_on_uninstall', 'no') === 'yes' ? 'yes' : 'no';
        $iafwzc_error_notification = get_option('iafwzc_error_notification', 'no') === 'yes' ? 'yes' : 'no';
        $iafwzc_error_notification_email = sanitize_text_field(get_option('iafwzc_error_notification_email', ''));
        $iafwzc_error_notification_subject = sanitize_text_field(get_option('iafwzc_error_notification_subject', ''));
        // NEW: separate options for deactivate notification
        $iafwzc_deactivate_notification       = get_option( 'iafwzc_deactivate_notification', 'no' ) === 'yes' ? 'yes' : 'no';
        $iafwzc_deactivate_notification_email = sanitize_text_field( get_option( 'iafwzc_deactivate_notification_email', '' ) );
        ?>

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

           <table class="form-table iafwzc-settings-table">

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

    <!-- SEPARATOR -->
    <tr class="iafwzc-divider-row">
        <td colspan="2"><div class="iafwzc-divider"></div></td>
    </tr>

    <!-- GROUP 2: Error Notification -->
    <tr>
        <th scope="row">
            <label for="iafwzc_error_notification"><?php esc_html_e('Error Email Notification', 'integrate-any-form-with-zoho-crm'); ?></label>
            <div class="iafwzc-help-icon-wrapper">
                <span class="iafwzc-help-icon">?</span>
                <div class="iafwzc-help-tooltip">
                    <p><?php esc_html_e('Enable to receive email notifications if data fails to post to ZOHO CRM due to errors.', 'integrate-any-form-with-zoho-crm'); ?></p>
                </div>
            </div>
        </th>
        <td>
            <input type="checkbox" id="iafwzc_error_notification" name="iafwzc_error_notification" value="yes" <?php checked($iafwzc_error_notification, 'yes'); ?> />
            <span><?php esc_html_e('Yes, send email notifications for errors', 'integrate-any-form-with-zoho-crm'); ?></span>
        </td>
    </tr>

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

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

    <!-- SEPARATOR -->
    <tr class="iafwzc-divider-row">
        <td colspan="2"><div class="iafwzc-divider"></div></td>
    </tr>

    <!-- GROUP 3: Deactivation Alert -->
    <tr>
        <th scope="row">
            <label for="iafwzc_deactivate_notification">
                <?php esc_html_e( 'Deactivation Alert Email', 'integrate-any-form-with-zoho-crm' ); ?>
            </label>
            <div class="iafwzc-help-icon-wrapper">
                <span class="iafwzc-help-icon">?</span>
                <div class="iafwzc-help-tooltip">
                    <p>
                        <?php esc_html_e(
                            'Enable this option to receive an automatic email alert whenever this plugin is deactivated from the WordPress admin. This helps you track unauthorized or unexpected plugin deactivations. Add one or more email addresses (comma separated).',
                            'integrate-any-form-with-zoho-crm'
                        ); ?>
                    </p>
                </div>
            </div>
        </th>
        <td>
            <input
                type="checkbox"
                id="iafwzc_deactivate_notification"
                name="iafwzc_deactivate_notification"
                value="yes"
                <?php checked( $iafwzc_deactivate_notification, 'yes' ); ?>
            />
            <span><?php esc_html_e( 'Yes, send an email when the plugin is deactivated', 'integrate-any-form-with-zoho-crm' ); ?></span>
        </td>
    </tr>

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

</table>


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

    $iafwzc_license_expiry_time = null;
    if ($iafwzc_session) {
        $iafwzc_license_expiry_time = get_option('iafwzc_session_key', null);
        if ($iafwzc_license_expiry_time !== null) {
            if ($iafwzc_license_expiry_time === "MTExMTExMTExMTExMQ==") {
                $iafwzc_license_expiry_time = "Lifetime";
            } else {
                $iafwzc_license_expiry_time = base64_decode($iafwzc_license_expiry_time, true);
                if ($iafwzc_license_expiry_time !== false) {

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

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

    ?>

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

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

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

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

                <?php
                if ($iafwzc_key_is_valid) {
                    // Deactivate Key Button
                    submit_button(
                        __('Deactivate Key', 'integrate-any-form-with-zoho-crm'),
                        'secondary',
                        'iafwzc_revoke_request_key',
                        false // Do not wrap in <p>
                    );
                } else {
                    // Process Key Button
                    submit_button(
                        __('Process Key', 'integrate-any-form-with-zoho-crm'),
                        'primary',
                        'iafwzc_process_request_key',
                        false // Do not wrap in <p>
                    );
                }
                ?>
            </form>


        </div>
    </div>


</div>
