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

?>
<div id="iafwzc-content-setup" class="iafwzc-tab-content" style="display:none;">
    <span class="iafwzc-span-header">
        <?php esc_html_e('Integration Details', 'integrate-any-form-with-zoho-crm'); ?>
    </span>
    <div class="iafwzc-help-icon-wrapper">
        <span class="iafwzc-help-icon">?</span>
        <div class="iafwzc-help-tooltip">
            <p>
                <?php esc_html_e('Connect and manage integrations with Zoho CRM and Zoho Bigin from this page.', 'integrate-any-form-with-zoho-crm'); ?>
            </p>
        </div>
    </div>
    <?php
    $iafwzc_message = get_transient('iafwzc_setup_message');

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

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

<?php
// Retrieve necessary data
$iafwzc_form_group_list = $IAFWZC_Form_Group_IdInstance->getAllFormGroupID();
$iafwzc_account_group_list = $iafwzc_accountDBInstance->get_account_data();
$iafwzc_integration_details = null;
$iafwzc_setup_id = null;
$iafwzc_field_mapping_json = null;

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