<?php

$gsc_frmd_integration = new GSCFRMDFORM_Main();

// Get the feed ID and form ID from the form
$feed_id = isset($_GET['feed_id']) ? filter_var(wp_unslash($_GET['feed_id']), FILTER_SANITIZE_NUMBER_INT) : '';
$form_id = isset($_GET['form_id']) ? filter_var(wp_unslash($_GET['form_id']), FILTER_SANITIZE_NUMBER_INT) : '';
// Get the saved feed data from the database
$feed_data = get_post_meta($feed_id, 'gscfrm_form_feeds_sheet_settings', true);

$sheet_name = isset($feed_data['sheet-name']) ? esc_attr($feed_data['sheet-name']) : '';
$sheet_id = isset($feed_data['sheet-id']) ? esc_attr($feed_data['sheet-id']) : '';
$tab_name = isset($feed_data['sheet-tab-name']) ? esc_attr($feed_data['sheet-tab-name']) : '';
$tab_id = isset($feed_data['tab-id']) ? esc_attr($feed_data['tab-id']) : '';


?>

<div class="frmn-main-div">
    <div class="frmn-bread-crumb">
        <ul class="breadcrumb_frmntr">
            <li>
                <a href="?page=formidable-form-google-sheet-config&tab=form_feed_settings">
                    <button class="button button-secondary">
                        <span
                            class="back-icon">&#8592;</span><?php echo esc_html('Back to Feeds List', 'gsheetconnector-formidable-forms'); ?>
                    </button>
                </a>
            </li>
        </ul>
    </div>
    <!-- form feed sheet settigns -->
    <form id="edit-feed-form" method="post" action="">

        <?php if (isset($_POST['execute-edit-feed-formidable']) && !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($feed_id); ?>">
        <input type="hidden" id="form-id" name="form_id" value="<?php echo esc_attr($form_id); ?>">

        <!-- MANUAL METHOD OF 1st DiV -->
        <div class="manual-section <?php echo esc_attr($class); ?>">
            <h2 class="info-headers">
                <?php echo esc_html(__('Edit Feed and Integrate with Google Sheets', 'gsheetconnector-formidable-forms')); ?>
            </h2>
            <div class="field-row row">
                <label
                    for="edit-sheet-name"><?php echo esc_html(__('Sheet Name', 'gsheetconnector-formidable-forms')); ?></label>
                <input type="text" id="edit-sheet-name" name="formidable-gs[sheet-name-custom]"
                    value="<?php echo esc_attr($sheet_name); ?>">
              	<span class="tooltip-new" data-tooltip="<?php echo esc_html(__("Go to your google account and click on Google apps icon and than click Sheet, Select the name of the appropriate sheet you want to link your contact form or create new sheet.", "gsheetconnector-formidable-forms")); ?>" data-tooltip-pos="right" data-tooltip-length="medium">
                        <i class="gswoo-helop-icon fa fa-question-circle"></i></span>
				
            </div> <!-- field row #end -->

            <div class="field-row row">
                <label
                    for="edit-sheet-id"><?php echo esc_html(__('Sheet ID', 'gsheetconnector-formidable-forms')); ?></label>
                <input type="text" id="edit-sheet-id" name="formidable-gs[sheet-id-custom]"
                    value="<?php echo esc_attr($sheet_id); ?>">
               <?php /*?> <div class="tooltip-new">
                    <img src="<?php echo esc_url(GSCFRMDFORM_VERSION_URL . 'assets/img/help.png'); ?>"
                        class="help-icon">
                    <span
                        class="tooltiptext tooltip-right-msg"><?php echo esc_html(__("you can get sheet id from your sheet URL", "gsheetconnector-formidable-forms")); ?></span>
                </div><?php */?>
				
				<span class="tooltip-new" data-tooltip="<?php echo esc_html(__("you can get sheet id from your sheet URL", "gsheetconnector-formidable-forms")); ?>" data-tooltip-pos="right" data-tooltip-length="medium"><i class="gswoo-helop-icon fa fa-question-circle"></i></span>
				
            </div> <!-- field row #end -->

            <div class="field-row row">
                <label
                    for="edit-tab-name"><?php echo esc_html(__('Tab Name', 'gsheetconnector-formidable-forms')); ?></label>
                <input type="text" id="edit-tab-name" name="formidable-gs[sheet-tab-name-custom]"
                    value="<?php echo esc_attr($tab_name); ?>">
              
				
				<span class="tooltip-new" data-tooltip="<?php echo esc_html(__("Open your Google Sheet with which you want to link your contact form . You will notice a tab names at bottom of the screen. Copy the tab name where you want to have an entry of contact form.", "gsheetconnector-formidable-forms")); ?>" data-tooltip-pos="right" data-tooltip-length="medium"><i class="gswoo-helop-icon fa fa-question-circle"></i></span>
				
            </div> <!-- field row #end -->

            <div class="field-row row">

                <label
                    for="edit-tab-id"><?php echo esc_html(__('Tab ID', 'gsheetconnector-formidable-forms')); ?></label>
                <input type="text" id="edit-tab-id" name="formidable-gs[tab-id-custom]"
                    value="<?php echo esc_attr($tab_id); ?>">
               
				
				<span class="tooltip-new" data-tooltip="<?php echo esc_html(__("you can get tab id from your sheet URL", "gsheetconnector-formidable-forms")); ?>" data-tooltip-pos="right" data-tooltip-length="medium"><i class="gswoo-helop-icon fa fa-question-circle"></i></span>
				
            </div> <!-- field row #end -->
            <div class="sheet-url" id="sheet-url" style="display: flex;">
                <?php if ((isset($sheet_id) && $sheet_id != "") && (isset($tab_id) && $tab_id != "")) {
                    ?>
                    <label><?php echo esc_html('Google Sheet URL', 'gsheetconnector-formidable-forms'); ?>
                    </label>
                    <?php
                    $sheet_url = sprintf(
                        'https://docs.google.com/spreadsheets/d/%s/edit#gid=%s',
                        rawurlencode($sheet_id),
                        rawurlencode($tab_id)
                    );
                    ?>

                    <a class="sheet-url-formidable" href="<?php echo esc_url($sheet_url); ?>"
                        target="_blank">
                        <?php echo esc_html__('Sheet URL', 'gsheetconnector-formidable-forms'); ?>
                    </a>

                    <?php
                }
                ?>
            </div>

            <input type="hidden" name="gs-ajax-nonce" id="gs-ajax-nonce"
                value="<?php echo esc_attr(wp_create_nonce('gs-ajax-nonce')); ?>" />
            <input type="submit" name="execute-edit-feed-formidable" id="execute-save" class="button button-primary"
    value="<?php echo esc_attr__( 'Save Changes', 'gsheetconnector-formidable-forms' ); ?>">


        </div>

    </form>
    <!-- Free setting End -->

    <div class="system-debug-logs" id="opener">
        <a href="https://www.gsheetconnector.com/formidable-forms-google-sheet-connector-pro" class="pro-link"
            target="_blank" style="text-decoration: none;"></a>
        <div class="auto-section" style="display:block;">

            <h2 class="oneoforall">
                <span><?php echo esc_html(__('Auto Google Sheet Settings ', 'gsheetconnector-formidable-forms')); ?></span>
                <span class="pro-ver"><?php echo esc_html(__('PRO', 'gsheetconnector-formidable-forms')); ?></span>
            </h2>

            <div class="gs-fields">
                <div class="sheet-details <?php echo esc_attr($class); ?>">
                    <p>
                        <label><?php echo esc_html(__('Google Spreadsheet Name', "gsheetconnector-formidable-forms")); ?></label>
                        <select name="formidable-gs[gs-formidable-sheet-id]" id="gs-formidable-sheet-id">
                            <option value=""><?php echo esc_html(__('Select', "gsheetconnector-formidable-forms")); ?>
                            </option>

                            <option value="create_new">
                                <?php echo esc_html(__('Create New', 'gsheetconnector-formidable-forms')); ?>
                            </option>
                        </select> 
						
						<span class="tooltip-new" data-tooltip="><?php echo esc_html(__("Not fetching sheet details to the dropdown than add sheet and tab name manually.", "gsheetconnector-formidable-forms")); ?>" data-tooltip-pos="right" data-tooltip-length="medium"><i class="gswoo-helop-icon fa fa-question-circle"></i></span>
						
                        <span class="error_msg" id="error_spread"></span>
                        <span class="loading-sign">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>

                    </p>
                    <i class="errorSelect errorSelectsheet"></i>

                    <p>
                        <label><?php echo esc_html(__('Google Sheet Tab Name', 'gsheetconnector-formidable-forms')); ?></label>
                        <select name="formidable-gs[gs-sheet-tab-name]" id="formidable-sheet-tab-name">
                            <option value=""><?php echo esc_html(__('Select', 'gsheetconnector-formidable-forms')); ?>
                            </option>

                        </select>

                        
						
						<span class="tooltip-new" data-tooltip="<?php echo esc_html(__("Not fetching sheet Tab details to the dropdown than add sheet and tab name manually.", "gsheetconnector-formidable-forms")); ?>" data-tooltip-pos="right" data-tooltip-length="medium"><i class="gswoo-helop-icon fa fa-question-circle"></i></span>
						
						
                    </p>

                </div>

                <!-- <input type="submit" name=""  class="button button-primary google-setting" value="Save Changes"> -->

                <div class="" id="opener">
                    <i class="errorSelect errorSelecttabs"></i>

                    <div class="create-ss-wrapper" style="display: none;">
                        <label>
                            <?php echo esc_html(__('Create Spreadsheet', 'gsheetconnector-formidable-forms')); ?>
                        </label>
                        <input type="text" name="_gs_formidable_setting_create_sheet" value=""
                            id="_gs_formidable_setting_create_sheet">
                        <span class="error_msg" id="error_new_spread"></span>
                    </div>

                    <p id="gs-validation-message"></p>
                    <p id="gs-valid-message"></p>

                    <?php if (!empty(get_option('gscfrmdform_verify')) && (get_option('gscfrmdform_verify') == "valid")) { ?>
                        <p class="gscfrmdform-sync-row">
                            <a id="gscfrmdform-sync" data-init="yes" class="sync-button">
                                <?php echo esc_html__('Click Here', 'gsheetconnector-formidable-forms'); ?>
                            </a>
                            <?php echo esc_html__(' to fetch spreadsheet details.', 'gsheetconnector-formidable-forms'); ?>
                            <span class="loading-sign">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>

                        </p>
                    <?php } ?>
                </div>
            </div>

        </div>

        <?php
        // Assuming $form_id is the ID of the Formidable form you are working with
        
        // Fetch form fields from Formidable's frm_fields table
        global $wpdb;
        $formidable_fields = $wpdb->get_results($wpdb->prepare(
            "SELECT id, name FROM {$wpdb->prefix}frm_fields WHERE form_id = %d",
            $form_id
        ), OBJECT);

        // Prepare field labels
        $field_labels = [];
        $field_labels['Entry ID'] = 'Entry ID';

        // Populate field labels with Formidable fields
        if (!empty($formidable_fields)) {
            foreach ($formidable_fields as $field) {
                $field_labels[$field->name] = $field->name;
            }
        }

        // Array of default WordPress mail tags
        $default_mail_tags = array(
            'Entry Date' => 'Entry Date',
            'Post ID' => 'Post ID',
            'User Name' => 'User Name',
            'User IP' => 'User IP',
            'User Agent' => 'User Agent',
            'User ID' => 'User ID',
            'Referrer' => 'Referrer',
            // Add more default mail tags as needed
        );

        // Merge Formidable fields with default mail tags
        $merged_fields = array_merge($field_labels, $default_mail_tags);

        // Displaying the fields in the interface
        ?>

        <div class="form-fields-list formidable-list-set">
            <div class="elementorgs-color-code">
                <div class="color-elementorgs">
                    <h2><?php echo esc_html('Field List', 'gsheetconnector-formidable-forms'); ?> |
                        <?php echo esc_html('Special Mail Tags', 'gsheetconnector-formidable-forms'); ?>
                        <span
                            class="pro-ver"><?php echo esc_html(__('PRO', 'gsheetconnector-formidable-forms')); ?></span>
                    </h2>
                </div>

            </div>

            <div class="toggle-button select-all-toggle">
                <label class="switch">
                    <input type="checkbox" id="select-all-checkbox">
                    <span class="slider round"></span>
                </label>
                <span
                    class="label-text"><?php echo esc_html('Select All', 'gsheetconnector-formidable-forms'); ?></span>
            </div>

            <div id="sortable">
                <?php
                // Initialize variables to prevent warnings
                $header_name = ''; // Default value for header name
                $is_selected = ''; // Default value for checkbox status
                $non_sortable_class = ''; // Default value for non-sortable class
                
                foreach ($merged_fields as $key => $field) {
                    $field_label = $key;

                    // Check if the field is a special mail tag
                    if (in_array($key, ['Entry ID', 'Entry Date', 'Post ID', 'User Name', 'User IP', 'User Agent', 'User ID', 'Referrer'])) {
                        $non_sortable_class = ' non-sortable';
                        echo '<div class="field-item special_mail_tags_bg' . esc_attr($non_sortable_class) . '">';
                    } else {
                        $non_sortable_class = ''; // Reset class for non-special fields
                        echo '<div class="field-item field_list_bg' . esc_attr($non_sortable_class) . '">';
                    }

                    // Example logic for checkbox selection (you may modify based on actual logic)
                    $is_selected = isset($_POST['sheet_header'][$key]) ? 'checked' : '';

                    echo '<label class="switch">';
                    echo '<input type="checkbox" name="sheet_header[' . esc_attr($key) . ']" value="1" ' . esc_attr($is_selected) . '>';

                    echo '<span class="slider round"></span>';
                    echo '</label>';
                    echo '<span class="label-text">' . esc_html($key) . '</span>';

                    // Placeholder and value logic for header name
                    $header_name = isset($_POST['sheet_header'][$key])
                        ? sanitize_text_field(wp_unslash($_POST['sheet_header'][$key]))
                        : $key;

                    echo '<input type="text" name="sheet_header[' . esc_attr($key) . ']" placeholder="' . esc_attr($key) . '" value="' . esc_attr($header_name) . '">';
                    echo '</div>';
                }
                ?>
            </div>
        </div>
        <!-- Field List #end -->


        <div class="form-fields-list formidable-list-set">

            <div class="header-manage field-list">

                <h2 class="avada-title lbl_heading">
                    <?php echo esc_html(__('Header Management ', 'gsheetconnector-formidable-forms')); ?>
                    <span class="pro-ver"><?php echo esc_html(__('PRO', 'gsheetconnector-formidable-forms')); ?></span>
                </h2>

                <!-- Freeze header START-->
                <div class="misc-head">

                    <div class="toggle-button sheet_formatting-header-toggle">
                        <label for="freeze-header-option" class="switch">
                            <input type="checkbox" name="formidable-gs[freeze_header]" id="freeze-header-option"
                                class="check-toggle-elemgsc" value="">

                            <span class="slider round"></span>
                        </label>
                        <span
                            class="label-text"><?php echo esc_html('Freeze Header Row', 'gsheetconnector-formidable-forms'); ?></span>

                    </div> 
                    
					<span class="tooltip-new" data-tooltip="<?php echo esc_html(__("Freeze First Header Row.", "gsheetconnector-formidable-forms")); ?>" data-tooltip-pos="right" data-tooltip-length="medium"><i class="gswoo-helop-icon fa fa-question-circle"></i></span>
					
                </div>
                <!-- Freeze header END-->

                <!-- Colors START-->
                <div class="sheet_formatting">
                    <div class="elemgsc-sheet_formatting elemgsc-sheet_formatting">
                        <div class="toggle-button sheet_formatting-header-toggle">
                            <label class="switch" for="sheet_formatting-header-checkbox">
                                <input type="checkbox" id="sheet_formatting-header-checkbox"
                                    name="formidable-gs[sheet_formatting_header]" value="1">
                                <span class="slider round"></span>
                            </label>
                            <span
                                class="label-text"><?php echo esc_html('Header - Font Settings', 'gsheetconnector-formidable-forms'); ?></span>
 	
							<span class="tooltip-new" data-tooltip="<?php echo esc_html(__("This feature locks the top row in your selected sheet, providing a consistent reference point as you scroll through your data.", "gsheetconnector-formidable-forms")); ?>" data-tooltip-pos="right" data-tooltip-length="medium"><i class="gswoo-helop-icon fa fa-question-circle"></i></span>
                        </div>
                    </div>
                </div>
                <!-- Colors END-->

                <!-- 4th Div OF PRO Sheet Background Colors -->
                <div class="back-formating">

                    <div class="toggle-button sheet-bg-toggle">
                        <label class="switch">
                            <input type="checkbox" id="sheet-bg-toggle-checkbox" name="formidable-gs[sheet_bg]"
                                value="1">
                            <span class="slider round"></span>
                        </label>
                        <span
                            class="label-text"><?php echo esc_html('Sheet Background Color', 'gsheetconnector-formidable-forms'); ?></span> 
						
						<span class="tooltip-new" data-tooltip="<?php echo esc_html(__("Apply colors to the entire sheet, distinguishing odd and even rows for enhanced readability.", "gsheetconnector-formidable-forms")); ?>" data-tooltip-pos="right" data-tooltip-length="medium"><i class="gswoo-helop-icon fa fa-question-circle"></i></span>
						
                    </div>
                </div>

                <!--Row Font Setings 4th div of header settigns  -->
                <!-- FOR DATA OF FORM STYLES -->
                <div class="sheet_formatting">
                    <div class="elemgsc-sheet_formatting_row elemgsc-sheet_formatting_row">
                        <div class="toggle-button sheet_formatting-row-toggle">
                            <label class="switch" for="sheet_formatting-row-checkbox">
                                <input type="checkbox" id="sheet_formatting-row-checkbox"
                                    name="formidable-gs[sheet_formatting_row]" value="1">
                                <span class="slider round"></span>
                            </label>
                            <span
                                class="label-text"><?php echo esc_html('Row - Font Settings', 'gsheetconnector-formidable-forms'); ?></span> 
							
							<span class="tooltip-new" data-tooltip="<?php echo esc_html(__("This feature locks the top row in your selected sheet, providing a consistent reference point as you scroll through your data.", "gsheetconnector-formidable-forms")); ?>" data-tooltip-pos="right" data-tooltip-length="medium"><i class="gswoo-helop-icon fa fa-question-circle"></i></span>
							
                        </div>
                    </div>
                </div>
            </div>

        </div> <!-- header management #end -->

        </form>

        <div class="form-fields-list formidable-list-set">
            <!-- HTML for Sync Section -->
            <div class="header-ev-od-sec div-all-sec" style="margin-top:10px;">
                <h2>
                    <?php echo esc_html(__('Sync Formidable Forms entries to Google Sheet', 'gsheetconnector-formidable-forms')); ?>
                    <span class="pro-ver"><?php echo esc_html(__('PRO', 'gsheetconnector-formidable-forms')); ?></span>

                </h2>
                <span class="sync-formidable-msg"></span>

                <!-- Progress Bar -->
                <div class="progress-bar-container" style="display: none;">
                    <div class="progress-bar" style="width: 0%; background-color: #4caf50; height: 20px;"></div>
                </div>

                <div class="sync_div_design syncronous_formidable_entry_gsheet">
                    <?php echo esc_html('Sync Entries with Connected Sheet', 'gsheetconnector-formidable-forms'); ?>

                    <span class="sync-formidable-load">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>

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