<?php

if (!defined('ABSPATH'))
    exit;

require_once plugin_dir_path(__FILE__) . '/class-wpsslc-forms-list-table.php';

$wpsslc_view = isset($_GET['view']) ? sanitize_text_field(wp_unslash($_GET['view'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only routing param; no data mutated.
$wpsslc_form_id = isset($_GET['form_id']) ? intval($_GET['form_id']) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only routing param; no data mutated.
$wpsslc_entry_id = isset($_GET['entryid']) ? intval($_GET['entryid']) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only routing param; no data mutated.

if ('entries' !== $wpsslc_view) {
    $wpsslc_table = new WPSSLC_Forms_List_Table();
    $wpsslc_table->process_bulk_action();
    $wpsslc_table->prepare_items();
}
?>
    <div class="wpssc-top-subtext generalSetting-section">
        <div class="generalSetting-left">
        <?php
if ('entries' === $wpsslc_view && $wpsslc_form_id) { ?>
                <?php
    $wpsslc_back_forms_url = add_query_arg([
        'page' => 'wpsyncsheets-for-contact-form-7',
        'view' => 'forms',
    ], admin_url('admin.php'));
?>
                <h4><?php echo esc_html__('Form Entries', 'contactsheets-lite'); ?></h4>
                <p><?php echo esc_html__('View and manage all submissions for the selected Contact Form 7 form. Filter, search, and export entries in CSV or Excel format with ease.', 'contactsheets-lite'); ?></p>
            <?php
}
else if ('entry' === $wpsslc_view && $wpsslc_form_id && $wpsslc_entry_id) {
    $wpsslc_back_url = admin_url('admin.php?page=wpsyncsheets-for-contact-form-7&form_id=' . $wpsslc_form_id . '&view=entries');
?>
                <h4><?php echo esc_html__('Entry Details', 'contactsheets-lite'); ?></h4>
                <p><?php echo esc_html__('View complete submission data including form fields and tracking information. Access metadata like IP address, user agent, and submission time.', 'contactsheets-lite'); ?></p>
            <?php
}
else { ?>
                    <h4><?php echo esc_html__('Contact Form 7 Database Settings', 'contactsheets-lite'); ?></h4>
                    <p><?php echo esc_html__('Automatically store all Contact Form 7 entries securely inside your WordPress database & Google Spreadsheets. View, manage, and access submissions anytime directly from your dashboard.', 'contactsheets-lite'); ?></p>
                <?php
}?>
        </div>
    </div>
    <div class="wrap">
        <?php
if ('entries' === $wpsslc_view && $wpsslc_form_id) {
    require_once plugin_dir_path(__FILE__) . '/class-wpsslc-entries-page.php';
}
else if ('entry' === $wpsslc_view && $wpsslc_form_id && $wpsslc_entry_id) {
    $wpsslc_selected_form = isset($_GET['form_id']) ? absint($_GET['form_id']) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only navigation param used to build back-link URL; no data mutated.
    $wpsslc_back_forms_url = add_query_arg([
        'page' => 'wpsyncsheets-for-contact-form-7',
        'form_id' => $wpsslc_form_id,
        'view' => 'entries'
    ], admin_url('admin.php'));

?>
                <div class="top-section wpssc-single-entry-top-section">
                    <a href="<?php echo esc_url($wpsslc_back_forms_url); ?>" class="backtoform-btn"><span>←</span> Back</a>
                    <div class="commontopbar">
                        <form method="get" class="formlisting">
                            <input type="hidden" name="page" value="<?php echo esc_attr(isset($_REQUEST['page']) ? sanitize_key(wp_unslash($_REQUEST['page'])) : ''); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Read-only URL param for building admin link; no data mutated. ?>" />
                            <input type="hidden" name="view" value="entries" />
                            <select name="form_id" onchange="this.form.submit()">
                                <option value="">Select Contact Form</option>
                                <?php
    $wpsslc_forms = get_posts([
        'post_type' => 'wpcf7_contact_form',
        'numberposts' => -1,
    ]);

    if (!empty($wpsslc_forms)) {
        foreach ($wpsslc_forms as $wpsslc_form) {
            echo '<option value="' . esc_attr($wpsslc_form->ID) . '" ' .
                selected($wpsslc_selected_form, $wpsslc_form->ID, false) . '>' .
                esc_html($wpsslc_form->post_title) .
                '</option>';
        }
    }
?>
                            </select>
                        </form>
                    </div>
                </div>
                <?php
    require_once plugin_dir_path(__FILE__) . '/class-wpsslc-single-entry.php';
}
else {
?>
                <form method="post">
                    <input type="hidden" name="page" value="wpsyncsheets-for-contact-form-7" />
                    <input type="hidden" name="view" value="forms" />
                    <?php wp_nonce_field('bulk-' . $wpsslc_table->_args['plural']); ?>
                    <?php
    $wpsslc_table->search_box('Search Forms', 'form');
    $wpsslc_table->display();
?>
                </form>
                <?php
}
?>
    </div>

    <?php
?>
