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

?>
<div id="iafwzdesk-content-setup" class="iafwzdesk-tab-content" style="display:none;">
    <span class="iafwzdesk-span-header">
        <?php esc_html_e('Integration Details', 'integrate-with-zoho-desk'); ?>
    </span>
    <div class="iafwzdesk-help-icon-wrapper">
        <span class="iafwzdesk-help-icon">?</span>
        <div class="iafwzdesk-help-tooltip">
            <p>
                <?php esc_html_e('Connect and manage integrations with Zoho Desk from this page.', 'integrate-with-zoho-desk'); ?>
            </p>
        </div>
    </div>
    <?php
    $iafwzdesk_message = get_transient('iafwzdesk_setup_message');

    // 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_setup_message');
    }
    // Display the setup table
    $iafwzdesk_setup_list_table = new IAFWZDESK_Setup_Extend();
    $iafwzdesk_setup_list_table->prepare_items(); // Prepare setup table items
    $iafwzdesk_setup_list_table->display();       // Display setup table

    // Include banner file if exists
    $iafwzdesk_banner_file = plugin_dir_path(__FILE__) . "banner.php";
    if (file_exists($iafwzdesk_banner_file)) {
        if (!$session) {
            include $iafwzdesk_banner_file;
        }
    }
    ?>
</div>

<?php
// Retrieve necessary data
$iafwzdesk_form_group_list = $IAFWZDESK_Form_Group_IdInstance->getAllFormGroupID();
$iafwzdesk_account_group_list = $iafwzdesk_accountDBInstance->get_account_data();
$iafwzdesk_integration_details = null;
$iafwzdesk_setup_id = null;
$iafwzdesk_field_mapping_json = null;

$iafwzdesk_total_integration_count = "Integration #" . intval($IAFWZDESK_Field_MappingDBInstance->getFieldMappingTotalCount() + 1);

if (isset($_GET['action'], $_GET['setup_id']) && $_GET['action'] === 'edit' && !empty($_GET['setup_id'])) {

    // Check and verify nonce only if necessary parameters are present
    if (isset($_GET['_wpnonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'edit_setup_nonce')) {
        $iafwzdesk_setup_existing_file = plugin_dir_path(__FILE__) . "setup-existing.php";
        if (file_exists($iafwzdesk_setup_existing_file)) {
            include $iafwzdesk_setup_existing_file;
        } else {
            wp_die(
                esc_html__('The requested file does not exist. Please contact the administrator.', 'integrate-with-zoho-desk')
            );
        }
    } else {
        // Handle invalid nonce
        wp_die(
            esc_html__('Unauthorized request. Please try again.', 'integrate-with-zoho-desk')
        );
    }
} else {
    $iafwzdesk_setup_new_file = plugin_dir_path(__FILE__) . "setup-new.php";
    if (file_exists($iafwzdesk_setup_new_file)) {
        include $iafwzdesk_setup_new_file;
    } else {
        wp_die(
            esc_html__('The requested file does not exist. Please contact the administrator.', 'integrate-with-zoho-desk')
        );
    }
}
?>
