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

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

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

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

<?php
// Retrieve necessary data
$form_group_list = $IAFWZCAI_Form_Group_IdInstance->getAllFormGroupID();
$account_group_list = $iafwzcai_accountDBInstance->get_account_data();
$integration_details = null;
$setup_id = null;
$field_mapping_json = null;

$total_integration_count = "Integration #" . intval($IAFWZCAI_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')) {
        $setup_existing_file = plugin_dir_path(__FILE__) . "setup-existing.php";
        if (file_exists($setup_existing_file)) {
            include $setup_existing_file;
        } else {
            wp_die(
                esc_html__('The requested file does not exist. Please contact the administrator.', 'integrate-with-zoho-campaigns')
            );
        }
    } else {
        // Handle invalid nonce
        wp_die(
            esc_html__('Unauthorized request. Please try again.', 'integrate-with-zoho-campaigns')
        );
    }
} else {
    $setup_new_file = plugin_dir_path(__FILE__) . "setup-new.php";
    if (file_exists($setup_new_file)) {
        include $setup_new_file;
    } else {
        wp_die(
            esc_html__('The requested file does not exist. Please contact the administrator.', 'integrate-with-zoho-campaigns')
        );
    }
}
?>