<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
// Setup Tab Content
?>
<div id="integrazo_fwcrm_form-content-setup" class="integrazo_fwcrm_form-tab-content" style="display:none;">
    <span class="integrazo_fwcrm_form-span-header">
        <?php esc_html_e('Integration Details', 'crm-integration-freshworks-any-form'); ?>
    </span>
    <div class="integrazo_fwcrm_form-help-icon-wrapper">
        <span class="integrazo_fwcrm_form-help-icon">?</span>
        <div class="integrazo_fwcrm_form-help-tooltip">
            <p>
                <?php esc_html_e('Connect and manage integrations with Freshworks CRM from this page.', 'crm-integration-freshworks-any-form'); ?>
            </p>
        </div> 
    </div>
    <?php
    $message = get_transient('integrazo_fwcrm_form_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('integrazo_fwcrm_form_setup_message');
    }
    // Display the setup table
    $setup_list_table = new integrazo_fwcrm_form_Setup();
    $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 = $integrazo_fwcrm_form_formGroupIDInstance->getAllFormGroupID();
$account_group_list = $integrazo_fwcrm_form_accountDBInstance->get_account_data();
$integration_details = null;
$setup_id = null;
$field_mapping_json = null;

$total_integration_count = "Integration #" . intval($integrazo_fwcrm_form_fieldMappingDBInstance->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.', 'crm-integration-freshworks-any-form')
            );
        }
    } else {
        // Handle invalid nonce
        wp_die(
            esc_html__('Unauthorized request. Please try again.', 'crm-integration-freshworks-any-form')
        );
    }
} 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.', 'crm-integration-freshworks-any-form')
        );
    }
}
?>