<?php

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

if (
    !isset($_GET['_wpnonce']) ||
    !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'gshfjf-ajax-nonce')
) {
    wp_die('Security check failed');
}
if (!current_user_can('manage_options')) {
    wp_die('You are not allowed to access this page');
}

$gshfjf_feed_id = isset($_GET['feed_id'])
    ? absint(wp_unslash($_GET['feed_id']))
    : 0;

$gshfjf_form_id = isset($_GET['form_id'])
    ? absint(wp_unslash($_GET['form_id']))
    : 0;

if (!$gshfjf_feed_id || !$gshfjf_form_id) {
    return;
}

global $wpdb;
$gshfjf_feed_row = $wpdb->get_row(
    $wpdb->prepare(
        "SELECT * FROM {$wpdb->prefix}gshfjf_jetform_db_settings WHERE id = %d AND form_id = %d",
        $gshfjf_feed_id,
        $gshfjf_form_id
    ),
    ARRAY_A
);


$gshfjf_sheet_details = array();
if (isset($gshfjf_feed_row['sheet_details']) && '' !== $gshfjf_feed_row['sheet_details']) {
    $gshfjf_maybe_unserialize = maybe_unserialize($gshfjf_feed_row['sheet_details']);
    if (is_array($gshfjf_maybe_unserialize)) {
        $gshfjf_sheet_details = $gshfjf_maybe_unserialize;
    } else {
        $gshfjf_json = json_decode($gshfjf_feed_row['sheet_details'], true);
        if (is_array($gshfjf_json)) {
            $gshfjf_sheet_details = $gshfjf_json;
        } else {
            $gshfjf_sheet_details = array();
        }
    }
}

$gshfjf_sheet_name = isset($gshfjf_sheet_details['sheet-name']) ? esc_attr($gshfjf_sheet_details['sheet-name']) : '';
$gshfjf_sheet_id = isset($gshfjf_sheet_details['sheet-id']) ? esc_attr($gshfjf_sheet_details['sheet-id']) : '';
$gshfjf_tab_name = isset($gshfjf_sheet_details['sheet-tab-name']) ? esc_attr($gshfjf_sheet_details['sheet-tab-name']) : '';
$gshfjf_tab_id = isset($gshfjf_sheet_details['tab-id']) ? esc_attr($gshfjf_sheet_details['tab-id']) : '';
$gshfjf_class = '';

?>
<div class="wrap w-100 m-0">
    <div class="inner-wrap  w-100 bg-white p-40">
        <div class="gshfjf-main-div">
            <div class="gsc-msg gsc-success d-none fw-400 text-dark text-center pt-10 pb-10"></div>
            <div class="gsc-msg gsc-error fw-400 d-none text-dark text-center pt-10 pb-10"></div>
            <!-- form feed sheet settigns -->
            <form id="edit-feed-form" method="post" action="">
                <!-- < ?php esc_attr(wp_nonce_field('gsc_edit_feed_action', 'gsc_edit_feed_nonce')); ?> -->
                <!--  < ?php wp_nonce_field('gshfjf_edit_feed_action', 'gshfjf-ajax-nonce'); ?> -->
                <?php wp_nonce_field('gshfjf_edit_feed_action', 'gshfjf-ajax-nonce'); ?>

                <?php
                $gshfjf_is_valid_request = false;

                if (
                    isset($_POST['gshfjf-ajax-nonce']) &&
                    wp_verify_nonce(
                        sanitize_text_field(wp_unslash($_POST['gshfjf-ajax-nonce'])),
                        'gshfjf_edit_feed_action'
                    )
                ) {
                    $gshfjf_is_valid_request = true;
                }
                ?>

                <?php if (
                    $gshfjf_is_valid_request &&
                    isset($_POST['gshfjf-execute-edit-feed']) &&
                    !empty($success_message)
                ): ?>
                    <div class="notice notice-success is-dismissible">
                        <p><?php echo esc_html($success_message); ?></p>
                    </div>
                <?php endif; ?>
                <!-- < ?php if (isset($_POST['gshfjf-execute-edit-feed']) && !empty($success_message)): ?> -->
                <!-- <div class="notice notice-success is-dismissible"> -->
                <!-- <p>< ?php echo esc_html($success_message); ?></p> -->
                <!-- </div> -->
                <!-- < ?php endif; ?> -->
                <input type="hidden" id="feed-id" name="feed_id" value="<?php echo esc_attr($gshfjf_feed_id); ?>">
                <input type="hidden" id="form-id" name="form_id" value="<?php echo esc_attr($gshfjf_form_id); ?>">
                <input type="hidden"
                    id="feed-name"
                    name="feed-name"
                    value="<?php echo esc_attr($gshfjf_feed_row['feed_name']); ?>">
                <!-- MANUAL METHOD OF 1st DiV -->
                <div class="gshfjf-manual-section <?php echo esc_html($gshfjf_class); ?>">
                    <div class="gshfjf-bread-crumb">
                        <a href="?post_type=jet-form-builder&page=jetform-google-sheet-config&tab=google-sheet" class="back-btn btn-spacer btn btn-primary text-decoration-none d-inline-flex align-center gap-10">
                            <svg width="10" height="10" viewBox="0 0 6 8" fill="#fff" xmlns="http://www.w3.org/2000/svg" class="back-icon">
                                <path d="M5.27203 0.94L4.33203 0L0.332031 4L4.33203 8L5.27203 7.06L2.2187 4L5.27203 0.94Z" fill="#fff"></path>
                            </svg>
                            <?php echo esc_html__('Back to Feeds List', 'gsheetconnector-for-jetform'); ?>
                        </a>
                    </div>
                    <div class="feed-informtion-inner shadow-box mt-40 p-30">
                        <div class="heading mt-0"><?php echo esc_html(__('Feed Details', 'gsheetconnector-for-jetform')); ?></div>
                        <p><?php echo esc_html(__("Displays the form and feed information used for Google Sheets synchronization.", 'gsheetconnector-for-jetform')); ?>

                        <div class="gsc-info-grid mt-20">
                            <div>
                                <div class="feed-info-heading mt-20" for="edit-sheet-name"><?php echo esc_html(__('Form ID', 'gsheetconnector-for-jetform')); ?></div>
                                <div class="feed-info-sub-head fw-700">
                                    <?php echo isset($gshfjf_feed_row['form_id']) ? esc_attr($gshfjf_feed_row['form_id']) : ''; ?>
                                </div>
                            </div>
                            <div>
                                <div class="feed-info-heading mt-20" for="edit-sheet-name"><?php echo esc_html(__('Feed Name', 'gsheetconnector-for-jetform')); ?></div>
                                <div class="feed-info-sub-head fw-700">
                                    <?php echo isset($gshfjf_feed_row['feed_name']) ? esc_attr($gshfjf_feed_row['feed_name']) : ''; ?>
                                </div>
                            </div>
                            <div>

                                <?php
                                $gshfjf_form_name = '';

                                if (!empty($gshfjf_feed_row['form_id'])) {

                                    $gshfjf_form = get_post((int) $gshfjf_feed_row['form_id']);

                                    if ($gshfjf_form && $gshfjf_form->post_type === 'jet-form-builder') {

                                        $gshfjf_form_name = trim((string) $gshfjf_form->post_title);

                                        // fallback if title is empty
                                        if ($gshfjf_form_name === '') {
                                            $gshfjf_form_name = 'Untitled Form (#' . $gshfjf_feed_row['form_id'] . ')';
                                        }
                                    }
                                }
                                ?>

                                <div>
                                    <div class="feed-info-heading mt-20" for="edit-sheet-name">
                                        <?php echo esc_html__('Form Name', 'gsheetconnector-for-jetform'); ?>
                                    </div>
                                    <div class="form-name-value feed-info-sub-head fw-700">
                                        <?php echo esc_html($gshfjf_form_name); ?>
                                    </div>
                                </div>

                            </div>
                            <?php
                            $gshfjf_fetch_date = $gshfjf_feed_row['created_at'];
                            $gshfjf_formatted_date = '';

                            if ($gshfjf_fetch_date) {
                                $gshfjf_formatted_date = date_i18n(
                                    get_option('date_format') . ' ' . get_option('time_format'),
                                    strtotime($gshfjf_fetch_date)
                                );
                            }

                            $gshfjf_timezone_string = get_option('timezone_string');
                            if (!empty($gshfjf_timezone_string)) {
                                $gshfjf_display_timezone = $gshfjf_timezone_string;
                            } else {
                                $gshfjf_offset = get_option('gmt_offset');
                                $gshfjf_hours   = (int) $gshfjf_offset;
                                $gshfjf_minutes = abs(($gshfjf_offset - $gshfjf_hours) * 60);
                                $gshfjf_sign = ($gshfjf_offset < 0) ? '-' : '+';
                                $gshfjf_display_timezone = 'UTC' . $gshfjf_sign . abs($gshfjf_hours);
                                if ($gshfjf_minutes > 0) {
                                    $gshfjf_display_timezone .= ':' . str_pad($gshfjf_minutes, 2, '0', STR_PAD_LEFT);
                                }
                            }
                            ?>
                            <div>
                                <div class="feed-info-heading mt-20" for="edit-sheet-name">
                                    <?php echo esc_html__('Timestamp', 'gsheetconnector-for-jetform'); ?>
                                </div>
                                <div class="form-name-value feed-info-sub-head fw-700">
                                    <?php echo esc_html($gshfjf_formatted_date . " (" . $gshfjf_display_timezone . ")"); ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="shadow-box mt-40 p-30">
                        <div class="heading">
                            <?php echo esc_html(__('Manually Google Sheets Configuration', 'gsheetconnector-for-jetform')); ?>
                        </div>
                        <p><?php echo esc_html(__("Connect your Google Sheet by entering the required sheet information.", 'gsheetconnector-for-jetform')); ?>
                        <div class="row">
                            <div class="col-6 res-top-20">
                                <div class="form-group field-row mr-10">
                                    <label
                                        for="edit-sheet-name"><?php echo esc_html(__('Sheet Name', 'gsheetconnector-for-jetform')); ?>
                                        <span class="tooltip"
                                            data-tooltip="<?php echo esc_html(__("Enter the exact name of your Google Spreadsheet (as shown in Google Sheets).", 'gsheetconnector-for-jetform')); ?>"
                                            data-tooltip-pos="right" data-tooltip-length="medium">
                                            <i class="fa-solid fa-circle-question help-icon"></i>
                                        </span>
                                    </label>
                                    <input type="text" class="form-control" id="edit-sheet-name" name="gshf-jf[sheet-name-custom]"
                                        value="<?php echo esc_attr($gshfjf_sheet_name); ?>">
                                </div>
                            </div>
                            <div class="col-6 res-top-20">
                                <div class="form-group field-row mr-10">
                                    <label
                                        for="edit-sheet-id"><?php echo esc_html(__('Sheet ID', 'gsheetconnector-for-jetform')); ?>
                                        <span class="tooltip"
                                            data-tooltip="<?php echo esc_html(__("Paste the Spreadsheet ID from your Google Sheet URL. (Example: ", 'gsheetconnector-for-jetform')); ?> https://docs.google.com/spreadsheets/d/**SPREADSHEET_ID**/edit)"
                                            data-tooltip-pos="right" data-tooltip-length="medium">
                                            <i class="fa-solid fa-circle-question help-icon"></i>
                                        </span>
                                    </label>
                                    <input type="text" class="form-control" id="edit-sheet-id" name="gshf-jf[sheet-id-custom]"
                                        value="<?php echo esc_attr($gshfjf_sheet_id); ?>">
                                </div>
                            </div>
                            <div class="col-6 mt-20">
                                <div class="form-group field-row mr-10">
                                    <label
                                        for="edit-tab-name"><?php echo esc_html(__('Tab Name', 'gsheetconnector-for-jetform')); ?>
                                        <span class="tooltip"
                                            data-tooltip="<?php echo esc_html(__("Enter the exact sheet tab name (e.g., Sheet1) from the bottom of your Google Spreadsheet.", 'gsheetconnector-for-jetform')); ?>"
                                            data-tooltip-pos="right" data-tooltip-length="medium">
                                            <i class="fa-solid fa-circle-question help-icon"></i>
                                        </span>
                                    </label>
                                    <input type="text" class="form-control" id="edit-tab-name" name="gshf-jf[sheet-tab-name-custom]"
                                        value="<?php echo esc_attr($gshfjf_tab_name); ?>">
                                </div>
                            </div>
                            <div class="col-6 mt-20">
                                <div class="form-group field-row mr-10">
                                    <label
                                        for="edit-tab-id"><?php echo esc_html(__('Tab ID', 'gsheetconnector-for-jetform')); ?>
                                        <span class="tooltip"
                                            data-tooltip="<?php echo esc_html(__("Get the Tab ID from your sheet URL after gid=.", 'gsheetconnector-for-jetform')); ?>"
                                            data-tooltip-pos="right" data-tooltip-length="medium">
                                            <i class="fa-solid fa-circle-question help-icon"></i>
                                        </span>
                                    </label>
                                    <input type="text" class="form-control" id="edit-tab-id" name="gshf-jf[tab-id-custom]"
                                        value="<?php echo esc_attr($gshfjf_tab_id); ?>">
                                </div> <!-- field row #end -->
                            </div>

                            <div class="sheet-url field-row" id="sheet-url">
                                <?php if ((isset($gshfjf_sheet_id) && $gshfjf_sheet_id != "") && (isset($gshfjf_tab_id) && $gshfjf_tab_id != "")) {
                                ?>
                                    <a class="sheet-url-jetform common-sheet-url btn mr-10 text-dark text-decoration-none mt-30 blinking-button"
                                        href="https://docs.google.com/spreadsheets/d/<?php echo esc_html($gshfjf_sheet_id); ?>/edit#gid=<?php echo esc_html($gshfjf_tab_id); ?>"
                                        target="_blank"><?php echo esc_html__('Get Sheet URL', 'gsheetconnector-for-jetform'); ?></a>
                                <?php
                                }
                                ?>
                            </div>
                            <!-- < ?php $auth_method = get_option('gshfjf_jetform_manual_setting', '0'); ?> -->
                            <div class="sheet-url field-row" id="sheet-url">
                                <label></label>
                                <input type="submit"
                                    name="gshfjf-execute-edit-feed"
                                    id="gshfjf-execute-save"
                                    class="btn btn-primary mt-30 common-disable"
                                    value="<?php echo esc_attr__('Save', 'gsheetconnector-for-jetform'); ?>">
                                <?php if ((isset($gshfjf_sheet_id) && $gshfjf_sheet_id != "") && (isset($gshfjf_tab_id) && $gshfjf_tab_id != "")) { ?>
                                    <input type="submit"
                                        name="execute-reset"
                                        id="execute-reset-free"
                                        class="gshfjf-reset btn btn-reset ml-5"
                                        value="<?php echo esc_attr__('Reset', 'gsheetconnector-for-jetform'); ?>">
                                    <div class="loading-sign-reset"></div>
                                <?php } ?>
                                <input type="hidden" name="gshfjf-ajax-nonce" id="gshfjf-ajax-nonce"
                                    value="<?php echo esc_attr(wp_create_nonce('gshfjf-ajax-nonce')); ?>" />
                            </div>
                        </div>
                    </div>

                    <div class="feed-informtion-inner shadow-box mt-40 p-30">
                        <div class="gsc-pro-promo">
                            <div class="gsc-pro-header">
                                <div class="gsc-pro-icon">
                                    <svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="#28a745" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <path d="M5 19c-1 1-2 1-3 1 0-1 0-2 1-3l4-4"></path>
                                        <path d="M14 3l7 7"></path>
                                        <path d="M9 18l-4 4"></path>
                                        <path d="M15 3c2 0 6 4 6 6-2 2-6 6-8 8l-6-6c2-2 6-8 8-8z"></path>
                                        <circle cx="15" cy="9" r="1.5"></circle>
                                    </svg>

                                </div>
                                <div>
                                    <div class="unlock-header"><?php echo esc_html(__('Unlock Advanced Features with Form Feeds', 'gsheetconnector-for-jetform')); ?></div>
                                    <span class="gsc-pro-badge"><?php echo esc_html(__('FREE users get special upgrade pricing', 'gsheetconnector-for-jetform')); ?></span>
                                </div>
                            </div>

                            <!-- Feature Tabs -->
                            <div class="gsc-pro-tabs pt-20 pb-20 pl-20 pr-20">
                                <div>
                                    <div class="mb-20"><a href="#auto-googlesheet-configuration"><?php echo esc_html(__('Automatically Google Sheet Configuration', 'gsheetconnector-for-jetform')); ?></a></div>
                                    <div class="gsc-pro-grid">
                                        <ul>
                                            <li><?php esc_html_e('Auto fetch Google Sheets list', 'gsheetconnector-for-jetform'); ?></li>
                                            <li><?php esc_html_e('Auto detect sheet tabs', 'gsheetconnector-for-jetform'); ?> </li>
                                            <li><?php esc_html_e('One-click configuration', 'gsheetconnector-for-jetform'); ?></li>
                                            <li><?php esc_html_e('Real-time entry sync', 'gsheetconnector-for-jetform'); ?></li>
                                        </ul>
                                    </div>
                                </div>

                                <div>
                                    <div class="mb-20"><a href="#field-mapping"><?php echo esc_html(__('Select Fields to Sync', 'gsheetconnector-for-jetform')); ?></a></div>
                                    <div class="gsc-pro-grid">
                                        <ul>
                                            <li><?php esc_html_e('Drag & drop field reordering', 'gsheetconnector-for-jetform'); ?></li>
                                            <li><?php esc_html_e('Rename column headers', 'gsheetconnector-for-jetform'); ?></li>
                                            <li><?php esc_html_e('Select specific fields to sync', 'gsheetconnector-for-jetform'); ?></li>
                                        </ul>
                                    </div>
                                </div>

                                <div>
                                    <div class="mb-20"><a href="#header-settings-sheet-sorting"><?php echo esc_html(__('Header Settings & Sheet Sorting', 'gsheetconnector-for-jetform')); ?></a></div>
                                    <div class="gsc-pro-grid">
                                        <ul>
                                            <li><?php esc_html_e('Freeze header row', 'gsheetconnector-for-jetform'); ?></li>
                                            <li><?php esc_html_e('Custom font styling', 'gsheetconnector-for-jetform'); ?></li>
                                            <li><?php esc_html_e('Header & row color control', 'gsheetconnector-for-jetform'); ?></li>
                                            <li><?php esc_html_e('Sort by any column', 'gsheetconnector-for-jetform'); ?></li>
                                        </ul>
                                    </div>
                                </div>

                                <div>
                                    <div class="mb-20"><a href="#spreadsheet-download-sync"><?php echo esc_html(__('Spreadsheet Download & Google Sheets Sync', 'gsheetconnector-for-jetform')); ?></a></div>
                                    <div class="gsc-pro-grid">
                                        <ul>
                                            <li><?php esc_html_e('Download spreadsheet as file', 'gsheetconnector-for-jetform'); ?></li>
                                            <li><?php esc_html_e('Bulk sync past entries', 'gsheetconnector-for-jetform'); ?></li>
                                            <li><?php esc_html_e('Date range sync', 'gsheetconnector-for-jetform'); ?></li>
                                            <li><?php esc_html_e('Real-time entry updates', 'gsheetconnector-for-jetform'); ?></li>
                                        </ul>
                                    </div>
                                </div>
                            </div>

                            <!-- CTA -->
                            <div class="gsc-pro-footer text-center">
                                <a href="#"
                                    target="_blank"
                                    class="btn btn-primary text-decoration-none link-hover-white">
                                    <?php echo esc_html(__('Upgrade to Unlock', 'gsheetconnector-for-jetform')); ?>
                                </a>
                            </div>

                        </div>

                        <div class="system-debug-logs blur-pro-feature" id="opener">
                            <a href="#" class="pro-link"
                                target="_blank" style="text-decoration: none;"></a>
                            <div class="auto-section shadow-box mt-40 p-30" id="auto-googlesheet-configuration" name="auto-googlesheet-configuration" style="display:block;">
                                <div class="gsc-fields">
                                    <div class="sheet-details <?php echo esc_html($gshfjf_class); ?>">
                                        <div class="heading mt-0"> <?php echo esc_html(__('Automatically Google Sheet Configuration', 'gsheetconnector-for-jetform')); ?></div>
                                        <p><?php echo esc_html(__("Automatically configure your Google Sheet and start syncing form submissions in real time.", 'gsheetconnector-for-jetform')); ?></P>
                                        <div class="row">
                                            <div class="col-4">
                                                <div class="mr-10">
                                                    <label><?php echo esc_html(__('Sheet Name', 'gsheetconnector-for-jetform')); ?></label>
                                                    <select name="gshf-jf[gsc-jetform-sheet-id]" class="auto-select-display w-100 mt-5" id="gsc-jetform-sheet-id">
                                                        <option value="">
                                                            <?php echo esc_html(__('Select', 'gsheetconnector-for-jetform')); ?>
                                                        </option>
                                                        <option value="create_new">
                                                            <?php echo esc_html(__('Create New', 'gsheetconnector-for-jetform')); ?>
                                                        </option>
                                                    </select>
                                                </div>
                                            </div>
                                            <span class="error_msg" id="error_spread"></span>
                                            <span class="loading-sign">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                            <i class="errorSelect errorSelectsheet"></i>
                                            <div class="col-4">
                                                <label><?php echo esc_html(__('Sheet Tab Name', 'gsheetconnector-for-jetform')); ?></label>
                                                <select name="gshf-jf[gs-sheet-tab-name]" class="auto-select-display w-100 mt-5" id="gshfjf-sheet-tab-name">
                                                    <option value="">
                                                        <?php echo esc_html(__('Select', 'gsheetconnector-for-jetform')); ?>
                                                    </option>

                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="feed-setting-pro-wrapper">
                                <div class="form-fields-list gshfjf-list-set shadow-box mt-40 p-30" id="field-mapping" name="field-mapping">
                                    <div class="gshfjf-color-code">
                                        <div class="color-ffgs">
                                            <div class="heading gsc-pro-header"><?php echo esc_html('Select Fields to Sync', 'gsheetconnector-for-jetform'); ?>
                                            </div>
                                            <p class="gsc-pro-desc"><?php echo esc_html(__('Enable the fields you want to send to Google Sheets and rename columns if needed.', 'gsheetconnector-for-jetform')); ?></p>

                                        </div>
                                        <div class="gshfjf-color-code d-flex align-center gap-20 pt-10">
                                            <div class="color-ffgs field-type-form">
                                                <span class="field-list-pill align-center fw-700"><?php echo esc_html(__('Field List', 'gsheetconnector-for-jetform')); ?></span>

                                            </div>
                                            <div class="color-ffgs field-type-system">
                                                <span class="align-center fw-700"><?php echo esc_html(__('Submission Info', 'gsheetconnector-for-jetform')); ?></span>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="toggle-button select-all-toggle gsc-pro-content">
                                        <div class="pt-30 select-all-field">
                                            <label class="switch">
                                                <input type="checkbox" id="select-all-checkbox" checked>
                                                <span class="slider round button-toggle"></span>
                                            </label>
                                            <span
                                                class="label-text"><?php echo esc_html('Select All Fields', 'gsheetconnector-for-jetform'); ?></span>
                                        </div>
                                        <div id="gshfjf-sortable" class="ui-sortable">
                                            <?php
                                            global $wpdb;
                                            $gshfjf_form_post = get_post($gshfjf_form_id);

                                            $gshfjf_form_config = array();
                                            $gshfjf_form_config['fields'] = array();

                                            if ($gshfjf_form_post && $gshfjf_form_post->post_type === 'jet-form-builder') {
                                                $gshfjf_blocks = parse_blocks($gshfjf_form_post->post_content);
                                                $gshfjf_stack = $gshfjf_blocks;

                                                while (!empty($gshfjf_stack)) {
                                                    $gshfjf_block = array_shift($gshfjf_stack);
                                                    if (!empty($gshfjf_block['blockName']) && strpos($gshfjf_block['blockName'], 'jet-forms/') !== false) {
                                                        $gshfjf_attrs = $gshfjf_block['attrs'] ?? [];
                                                        $gshfjf_name  = $gshfjf_attrs['name'] ?? '';
                                                        $gshfjf_label = $gshfjf_attrs['label'] ?? ($gshfjf_attrs['field_label'] ?? '');

                                                        if (!empty($gshfjf_name) || !empty($gshfjf_label)) {
                                                            $gshfjf_form_config['fields'][] = array(
                                                                'settings' => array(
                                                                    'label' => $gshfjf_label,
                                                                    'admin_field_label' => $gshfjf_name
                                                                )
                                                            );
                                                        }
                                                    }

                                                    if (!empty($gshfjf_block['innerBlocks'])) {
                                                        foreach ($gshfjf_block['innerBlocks'] as $gshfjf_inner) {
                                                            $gshfjf_stack[] = $gshfjf_inner;
                                                        }
                                                    }
                                                }
                                            }
                                            $gshfjf_submission_tags = array(
                                                'Submission ID' => 'Submission ID',
                                                'Form' => 'Form',
                                                'Referrer' => 'Referrer',
                                                'Status' => 'Status',
                                                'Submitted By' => 'Submitted By',
                                                'Ip Address' => 'Ip Address',
                                                'User Agent' => 'User Agent',
                                                'Created' => 'Created',
                                            );

                                            // Start with submission tags
                                            $gshfjf_field_labels =  array();
                                            // Then add form fields
                                            if (!empty($gshfjf_form_config['fields']) && is_array($gshfjf_form_config['fields'])) {
                                                foreach ($gshfjf_form_config['fields'] as $gshfjf_field) {
                                                    $gshfjf_admin_label = $gshfjf_field['settings']['admin_field_label'] ?? '';
                                                    $gshfjf_label = $gshfjf_field['settings']['label'] ?? '';
                                                    $gshfjf_final_label = !empty($gshfjf_label) ? $gshfjf_label : $gshfjf_admin_label;
                                                    if (!empty($gshfjf_final_label)) {
                                                        $gshfjf_field_labels[$gshfjf_final_label] = $gshfjf_final_label;
                                                    }
                                                }
                                            }
                                            // Merge both arrays — default fields first, then form fields
                                            $gshfjf_merged_array = array_merge($gshfjf_submission_tags, $gshfjf_field_labels);
                                            $gshfjf_final_array = array();
                                            if (!empty($saved_header_names)) {

                                                // Loop through $saved_header_names and keep its order
                                                foreach ($saved_header_names as $gshfjf_key => $gshfjf_value) {
                                                    if (isset($gshfjf_merged_array[$gshfjf_key])) {
                                                        $gshfjf_final_array[$gshfjf_key] = $saved_header_names[$gshfjf_key];
                                                    }
                                                }

                                                // Add any additional fields from $gshfjf_merged_array that are not in $saved_header_names
                                                foreach ($gshfjf_merged_array as $gshfjf_key => $gshfjf_value) {
                                                    if (!isset($gshfjf_final_array[$gshfjf_key])) {
                                                        $gshfjf_final_array[$gshfjf_key] = $gshfjf_merged_array[$gshfjf_key];
                                                    }
                                                }
                                            }
                                            if (!empty($gshfjf_final_array)) {
                                                $gshfjf_merged_array = $gshfjf_final_array;
                                            } else {
                                                $gshfjf_merged_array = $gshfjf_merged_array;
                                            }
                                            foreach ($gshfjf_merged_array as $gshfjf_key => $gshfjf_label) {
                                                $gshfjf_field_label = $gshfjf_key;
                                                $gshfjf_is_selected = $gshfjf_header_name_value = $gshfjf_header_name = "";
                                                // $is_disabled = '';
                                                $gshfjf_cursor_not_allow = '';

                                                if (!empty($saved_header) && array_key_exists($gshfjf_key, $saved_header) && $saved_header[$gshfjf_key] == 1) {
                                                    $gshfjf_is_selected = 'checked';
                                                }

                                                if (!empty($saved_header_names) && array_key_exists($gshfjf_field_label, $saved_header_names)) {
                                                    $gshfjf_header_name_value = $saved_header_names[$gshfjf_field_label];
                                                }

                                                if (!empty($gshfjf_header_name_value)) {
                                                    $gshfjf_header_name = $gshfjf_header_name_value;
                                                } else {
                                                    $gshfjf_header_name = $gshfjf_field_label;
                                                }
                                                if ($gshfjf_key == "Submission ID") {
                                                    $gshfjf_cursor_not_allow = 'disable-cursor';
                                                }
                                                $gshfjf_non_sortable_class = ($gshfjf_key == "Submission ID") ? ' non-sortable disable-submission-id ui-sortable-handle' : '';
                                                echo '<div class="card form-field-toggle non-sortable disable-submission-id ui-sortable-handle">';
                                                echo '<div class="card-content">';

                                                if ($gshfjf_key == "Submission ID" || $gshfjf_key == "Form" || $gshfjf_key == "Referrer" || $gshfjf_key == "Status" || $gshfjf_key == "Submitted By" || $gshfjf_key == "Ip Address" || $gshfjf_key == "User Agent" || $gshfjf_key == "Created") {
                                                    echo '<div class="toggle-button special_mail_tags_bg">';
                                                } else {
                                                    echo '<div class="toggle-button field_list_bg">';
                                                }
                                                echo '<label class="switch">';
                                                echo '<input type="checkbox" name="sheet_header[' . esc_attr($gshfjf_key) . ']" class="toggle-input" value="1" ' . esc_attr($gshfjf_is_selected) . ' checked >';
                                                echo '<span class="slider round button-toggle"></span>';
                                                echo '</label>';

                                                echo '<span class="label-text card-label">' . esc_attr($gshfjf_key) . '</span>';

                                                echo '<div class="info-row">';
                                                echo '<input type="text" class="field-input" value="' . esc_attr($gshfjf_field_label) . '">';
                                                echo '</div>';

                                                echo '<input type="text" class="field-input ' . esc_attr($gshfjf_cursor_not_allow) . '" name="sheet_header_names[' . esc_attr($gshfjf_field_label) . ']"value="' . esc_attr($gshfjf_header_name) . '">';
                                                echo '</div></div></div>';
                                            }
                                            ?>
                                        </div>
                                    </div>
                                </div>
                                <div class="freez_order_sort form-fields-list gshfjf-list-set shadow-box mt-40 p-30">
                                    <div id="header-settings-sheet-sorting" name="header-settings-sheet-sorting">
                                        <div class="heading mt-0"><?php echo esc_html(__('Header Settings', 'gsheetconnector-for-jetform')); ?></div>
                                        <p><?php echo esc_html(__('Customize the appearance and behavior of your sheet headers and rows for better readability and organization.', 'gsheetconnector-for-jetform')); ?></p>
                                        <!-- SECTION 1: Header Behavior -->
                                        <div class="header-styling-sheet">
                                            <div class="settings-card mb-20 w-100 bg-white">
                                                <div class="mt-0 header-settings-ineer-size fw-600 mb-20"><?php echo esc_html(__('Header Behavior', 'gsheetconnector-for-jetform')); ?></div>
                                                <div class="gscfrmnt-cards gscfrmnt-card setting-row">
                                                    <div class="toggle-button freeze-header-toggle d-flex align-items-center justify-between mb-15">
                                                        <span class="label-text fw-400"><?php echo esc_html(__('Freeze Header', 'gsheetconnector-for-jetform')); ?></span>
                                                        <label class="switch">
                                                            <input type="checkbox" id="freeze-header-checkbox" name="gshf-jf[freeze_header]"
                                                                value="true" checked>
                                                            <span class="slider round button-toggle"></span>
                                                        </label>
                                                    </div>
                                                </div>
                                                <div class="sheet_sorting sheet_formatting">
                                                    <div class="gscfrmnt-cards">
                                                        <div class="toggle-button sheet-sorting-toggle d-flex align-items-center justify-between">
                                                            <span class="label-text fw-400"><?php echo esc_html(__('Sheet Sorting', 'gsheetconnector-for-jetform')); ?></span>
                                                            <label class="switch" for="sheet-sorting-checkbox">
                                                                <input type="checkbox" id="sheet-sorting-checkbox"
                                                                    name="gshf-jf[sheet_sorting]" value="1" checked>
                                                                <span class="slider round button-toggle"></span>
                                                            </label>
                                                        </div>
                                                    </div>
                                                    <div class="sheet-sorting-settings" id="sheet-sorting-settings">
                                                        <div class="settings-grid">
                                                            <div class="gscfrmnt-row form-group">
                                                                <label for="sort-column-name">
                                                                    <?php echo esc_html__('Sort Column', 'gsheetconnector-for-jetform'); ?>
                                                                </label>
                                                                <select id="sort-column-name" name="gshf-jf[sort_column]">
                                                                </select>
                                                            </div>
                                                            <div class="gscfrmnt-row form-group">
                                                                <label for="sort-order"><?php echo esc_html__('Sort Order', 'gsheetconnector-for-jetform'); ?></label>
                                                                <select id="sort-order" name="gshf-jf[sort_order]">
                                                                    <option value="ASCENDING"><?php echo esc_html__('Ascending', 'gsheetconnector-for-jetform'); ?></option>
                                                                    <option value="DESCENDING"><?php echo esc_html__('Descending', 'gsheetconnector-for-jetform'); ?></option>
                                                                </select>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <!-- SECTION 2: Header Appearance -->
                                            <div class="settings-card  mb-20 w-100 bg-white">
                                                <div class="mt-0 header-settings-ineer-size fw-600 mb-20"><?php echo esc_html__('Header Styling', 'gsheetconnector-for-jetform'); ?></div>
                                                <div class="sheet_formatting setting-row">
                                                    <div class="gscfrmnt-sheet_formatting gscfrmnt-sheet_formatting">
                                                        <div class="toggle-button sheet_formatting-header-toggle d-flex align-items-center justify-between mb-15">
                                                            <span
                                                                class="label-texts  fw-400"><?php echo esc_html(__('Header Appearance', 'gsheetconnector-for-jetform')); ?>
                                                            </span>
                                                            <label class="switch" for="sheet_formatting-header-checkbox">
                                                                <input type="checkbox" id="sheet_formatting-header-checkbox"
                                                                    name="gshf-jf[sheet_formatting_header]" value="1" checked>
                                                                <span class="slider round button-toggle"></span>
                                                            </label>
                                                        </div>
                                                    </div>
                                                    <div class="font-styling-settings" id="font-styling-settings">
                                                        <div class="settings-grid">
                                                            <div class="font-style row-format form-group">
                                                                <label
                                                                    for="font-size"><?php echo esc_html__('Font Style', 'gsheetconnector-for-jetform'); ?></label>
                                                                <div class="d-flex gap-5">
                                                                    <label class="style-btn active"><input type="checkbox" name="font_styles[]" class="toggle-input active" value="normal">
                                                                        <?php echo esc_html__('Normal', 'gsheetconnector-for-jetform'); ?></label>
                                                                    <label class="style-btn"><input type="checkbox" name="font_styles[]" value="italic">
                                                                        <?php echo esc_html__('Italic', 'gsheetconnector-for-jetform'); ?></label>
                                                                    <label class="style-btn"><input type="checkbox" name="font_styles[]" value="bold"> <?php echo esc_html__('Bold', 'gsheetconnector-for-jetform'); ?></label>
                                                                </div>
                                                            </div>

                                                            <div class="font-size row-format form-group">
                                                                <label
                                                                    for="font-size"><?php echo esc_html__('Font Size', 'gsheetconnector-for-jetform'); ?></label>
                                                                <input type="number" id="font-size" name="gshf-jf[font_size]"
                                                                    value=""> px
                                                            </div>
                                                            <div class="font-color row-format form-group">
                                                                <label
                                                                    for="font-color"><?php echo esc_html__('Font Color', 'gsheetconnector-for-jetform'); ?></label>
                                                                <input type="color" id="font-color" name="gshf-jf[font_color]" class="bg-color-set-input"
                                                                    value="">
                                                            </div>
                                                            <div class="gscfrmnt-cards-sbg gscfrmnt-cards-sbg form-group">

                                                                <label for="gscfrmnt-header-color" class="button-gscfrmnt-toggle-color"></label>
                                                                <label>
                                                                    <?php echo esc_html__('Header Background', 'gsheetconnector-for-jetform'); ?>
                                                                </label>
                                                                <input type="color" id="header-color" name="gshf-jf[header-color]" class="bg-color-set-input"
                                                                    value="< ?php echo esc_attr($header_color); ?>">
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <!-- SECTION 3: Row Appearance -->
                                                <div class="sheet_formatting">
                                                    <div class="gscfrmnt-sheet_formatting_row gscfrmnt-sheet_formatting_row">
                                                        <div class="toggle-button sheet_formatting-row-toggle d-flex align-items-center justify-between">
                                                            <span
                                                                class="label-texts  fw-400"><?php echo esc_html__('Row Appearance', 'gsheetconnector-for-jetform'); ?>
                                                            </span>
                                                            <label class="switch" for="sheet_formatting-row-checkbox">
                                                                <input type="checkbox" id="sheet_formatting-row-checkbox"
                                                                    name="gshf-jf[sheet_formatting_row]" value="1" checked>
                                                                <span class="slider round button-toggle"></span>
                                                            </label>
                                                        </div>
                                                    </div>
                                                    <div class="font-styling-settings-row" id="font-styling-settings-row">
                                                        <div class="settings-grid">
                                                            <div class="font-style row-format form-group">
                                                                <label><?php echo esc_html__('Font Style', 'gsheetconnector-for-jetform'); ?></label>
                                                                <div class="d-flex gap-5">
                                                                    <label class="style-btn active"><input type="checkbox" name="font_styles[]" class="toggle-input active" value="normal">
                                                                        <?php echo esc_html__('Normal', 'gsheetconnector-for-jetform'); ?></label>
                                                                    <label class="style-btn"><input type="checkbox" name="font_styles[]" value="italic">
                                                                        <?php echo esc_html__('Italic', 'gsheetconnector-for-jetform'); ?></label>
                                                                    <label class="style-btn"><input type="checkbox" name="font_styles[]" value="bold"> <?php echo esc_html__('Bold', 'gsheetconnector-for-jetform'); ?></label>
                                                                </div>
                                                            </div>

                                                            <div class="font-size row-format form-group">
                                                                <label
                                                                    for="font-size-row"><?php echo esc_html__('Font Size', 'gsheetconnector-for-jetform'); ?></label>
                                                                <input type="number" id="font-size-row" name="gshf-jf[font_size_row]"
                                                                    value=""> px
                                                            </div>
                                                            <div class="font-color row-format form-group">
                                                                <label
                                                                    for="font-color-row"><?php echo esc_html__('Font Color', 'gsheetconnector-for-jetform'); ?></label>
                                                                <input type="color" id="font-color-row" name="gshf-jf[font_color_row]" class="bg-color-set-input"
                                                                    value="">
                                                            </div>
                                                            <div class="gscfrmnt-cards-sbg gscfrmnt-cards-sbg form-group">

                                                                <label for="gscfrmnt-odd-color" class="button-gscfrmnt-toggle-color"></label>
                                                                <label>
                                                                    <?php echo esc_html__('Odd Row Color', 'gsheetconnector-for-jetform'); ?>
                                                                </label>
                                                                <input type="color" id="odd-color" name="gshf-jf[odd-color]" class="bg-color-set-input"
                                                                    value="< ?php echo esc_attr($odd_color); ?>">
                                                            </div>
                                                            <div class="gscfrmnt-cards-sbg gscfrmnt-cards-sbg form-group">
                                                                <label for="gscfrmnt-even-color" class="button-gscfrmnt-toggle-color"></label>
                                                                <label>
                                                                    <?php echo esc_html__('Even Row Color', 'gsheetconnector-for-jetform'); ?>
                                                                </label>
                                                                <input type="color" id="even-color" name="gshf-jf[even-color]" class="bg-color-set-input"
                                                                    value="< ?php echo esc_attr($even_color); ?>">
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- SECTION 4: Spreadsheet Download -->
                                    <div id="spreadsheet-download-sync" name="spreadsheet-download-sync">
                                        <div class="settings-card mb-20 w-100 bg-white">
                                            <div class="toggle-button sheet-sorting-toggle d-flex align-items-center justify-between" id="downloads-toggle-checkbox">
                                                <span class="label-text  fw-400"><?php echo esc_html(__('Spreadsheet Download', 'gsheetconnector-for-jetform')); ?></span>
                                                <label class="switch" for="download-toggle-checkbox">
                                                    <input type="checkbox"
                                                        id="download-toggle-checkbox"
                                                        name="gshf-jf[download_spreadsheet]"
                                                        value="1" checked>
                                                    <span class="slider round button-toggle"></span>
                                                </label>
                                            </div>
                                            <div id="download-button-wrapper" class="mt-15">
                                                <!-- style="display:none;"> -->
                                                <a href="#"
                                                    class="sheet-url-jetform common-sheet-url text-dark fw-700 download-spreadsheet"
                                                    hover-tooltip="Spreadsheet Download">
                                                    <i class="fa-regular fa-file-zipper text-dark fw-500 mr-5"></i><?php echo esc_html(__('Download', 'gsheetconnector-for-jetform')); ?>
                                                </a>
                                            </div>
                                        </div>

                                        <div class="settings-card mb-20 w-100 bg-white">
                                            <div class="heading mt-0"><?php echo esc_html(__('Google Sheets Data Sync', 'gsheetconnector-for-jetform')); ?></div>
                                            <p><?php echo esc_html(__('Easily sync past form submissions to your connected Google Sheet. Choose a date range and ensure your spreadsheet stays complete and up to date.', 'gsheetconnector-for-jetform')); ?></p>
                                            <form method="post" action="">
                                                <div class="gs-wpcore-sync-entries">
                                                    <div class="d-flex gap-10">
                                                        <div class="form-group w-100">
                                                            <label for="sync-from-date"><?php echo esc_html__('From Date', 'gsheetconnector-for-jetform'); ?></label>
                                                            <input type="date" id="sync-from-date" name="sync_from_date" value="" class="wpgs-date-picker">
                                                        </div>

                                                        <div class="form-group w-100">
                                                            <label for="sync-to-date"><?php echo esc_html__('To Date', 'gsheetconnector-for-jetform'); ?></label>
                                                            <input type="date" id="sync-to-date" name="sync_to_date" value="" class="wpgs-date-picker">
                                                        </div>
                                                    </div>

                                                    <a id="gs-sync-entries" data-init="yes" class="sync-button-entries back-btn btn btn-primary text-decoration-none mt-20">
                                                        <?php echo esc_html__('Sync Entries', 'gsheetconnector-for-jetform'); ?>
                                                    </a>
                                                    <span class="loading-sign">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                                    <input type="hidden" name="gshfjf-ajax-nonce" id="gshfjf-ajax-nonce"
                                                        value="<?php echo esc_attr(wp_create_nonce('gshfjf-ajax-nonce')); ?>" />

                                                    <div id="gs-message-popup" style="display:none;">
                                                        <div class="popup-content">
                                                            <p id="popup-message"></p>
                                                            <button id="popup-close"><?php echo esc_html__('Close', 'gsheetconnector-for-jetform'); ?></button>
                                                        </div>
                                                    </div>

                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>
            </form>
        </div>
    </div>
</div>
</div>
</div>