<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
include_once SFTCY_WORDFORM_PLUGIN_DIR . 'admin/views/pages-header-template.php';
$nonce = SftcyWordformBase::sc_wordform_get_nonce();
$form_id = isset( $_GET['form_id'] ) ? sanitize_text_field( $_GET['form_id'] ) : '';
$form_name = '';
$form_data = ! empty( $form_id ) ? SftcyWordformBase::sc_wordform_db_query( $form_id ) : array();
$export_nonce = ! empty( $form_id ) ? wp_create_nonce( 'sc_wordform_export_form_submissions_' . $form_id ) : '';
$submission_export_available = ! class_exists( 'SftcyWordformFeatures' ) || SftcyWordformFeatures::wordform_is_feature_available( 'form_submission_export' );
$wordform_upgrade_url = defined( 'SFTCY_WORDFORM_PRODUCT_URL' ) ? SFTCY_WORDFORM_PRODUCT_URL : 'https://softcoy.com/products/wordform-pro';

if ( ! empty( $form_data[0]['form_name'] ) ) {
	$form_name = sanitize_text_field( $form_data[0]['form_name'] );
}

$back_to_submission_report_url = admin_url( 'admin.php?page=sc-wordform-user-submission-data' );
?>
<div id="wordformUsersFormSubmissionDataWrapperTable" class="wrap" data-form-id="<?php echo  $form_id; ?>" >
	<div class="wordform-page-header-with-action">
		<div class="wordform-submission-detail-header-copy">
			<h3><i class="dashicons dashicons-editor-table dashicon-custom-color"></i>&nbsp;<?php esc_html_e( 'Users Form Submission Data', 'wordform-pro' ); ?></h3>
			<?php if ( ! empty( $form_name ) ) : ?>
				<p class="wordform-submission-detail-selected-form">
					<?php
					printf(
						/* translators: 1: form name, 2: form id */
						esc_html__( 'Showing submissions for %1$s (%2$s)', 'wordform-pro' ),
						$form_name,
						$form_id
					);
					?>
				</p>
			<?php endif; ?>
		</div>
		<div class="wordform-page-header-action-group">
			<?php if ( ! empty( $export_nonce ) ) : ?>
				<div class="wordform-report-export-popover-wrapper">
					<button type="button" class="button button-primary wordform-report-export-btn" aria-expanded="false" aria-controls="wordformSubmissionExportPopover">
						<span class="dashicons dashicons-download" aria-hidden="true"></span>
						<?php esc_html_e( 'Export Excel', 'wordform-pro' ); ?>
					</button>
					<div class="wordform-report-export-popover" id="wordformSubmissionExportPopover" hidden>
						<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wordform-report-export-form">
							<input type="hidden" name="action" value="sc_wordform_export_form_submissions" />
							<input type="hidden" name="form_id" value="<?php echo esc_attr( $form_id ); ?>" />
							<input type="hidden" name="_wpnonce" value="<?php echo esc_attr( $export_nonce ); ?>" />
							<p class="wordform-report-export-popover-title"><?php esc_html_e( 'Select sheets to include', 'wordform-pro' ); ?></p>
							<?php if ( $submission_export_available ) : ?>
								<label class="wordform-report-export-sheet-option">
									<input type="checkbox" name="export_sheets[]" value="submissions" checked />
									<span><?php esc_html_e( 'Submissions', 'wordform-pro' ); ?></span>
								</label>
							<?php else : ?>
								<div class="wordform-report-export-sheet-option wordform-report-export-sheet-option-locked" role="button" tabindex="0" data-upgrade-url="<?php echo esc_url( $wordform_upgrade_url ); ?>">
									<span class="wordform-report-export-sheet-option-text">
										<input type="checkbox" name="export_sheets_locked[]" value="submissions" checked disabled aria-disabled="true" />
										<span><?php esc_html_e( 'Submissions', 'wordform-pro' ); ?></span>
									</span>
									<span class="wordform-report-export-sheet-pro-badge"><?php esc_html_e( 'Pro', 'wordform-pro' ); ?></span>
								</div>
							<?php endif; ?>
							<label class="wordform-report-export-sheet-option">
								<input type="checkbox" name="export_sheets[]" value="field_map" checked />
								<span><?php esc_html_e( 'Field Map', 'wordform-pro' ); ?></span>
							</label>
							<label class="wordform-report-export-sheet-option">
								<input type="checkbox" name="export_sheets[]" value="form_architecture" checked />
								<span><?php esc_html_e( 'Form Architecture', 'wordform-pro' ); ?></span>
							</label>
							<p class="wordform-report-export-sheet-error" hidden><?php esc_html_e( 'Select at least one sheet to export.', 'wordform-pro' ); ?></p>
							<div class="wordform-report-export-popover-actions">
								<button type="submit" class="button button-primary"><?php esc_html_e( 'Download Excel', 'wordform-pro' ); ?></button>
							</div>
						</form>
					</div>
				</div>
			<?php endif; ?>
			<a href="<?php echo esc_url( $back_to_submission_report_url ); ?>" class="button button-secondary">
				<span class="dashicons dashicons-arrow-left-alt2" style="font-size:16px;line-height:1.3;"></span>
				<?php esc_html_e( 'Back to Submission Report', 'wordform-pro' ); ?>
			</a>
		</div>
	</div>

	<div class="wordform-submission-data-summary-grid" id="wordformSubmissionDataSummaryGrid">
		<div class="wordform-submission-data-summary-card">
			<span class="wordform-submission-data-summary-label"><?php esc_html_e( 'Total Submissions', 'wordform-pro' ); ?></span>
			<strong class="wordform-submission-data-summary-value" id="wordformSubmissionSummaryTotal">0</strong>
		</div>
		<div class="wordform-submission-data-summary-card">
			<span class="wordform-submission-data-summary-label"><?php esc_html_e( 'Today', 'wordform-pro' ); ?></span>
			<strong class="wordform-submission-data-summary-value" id="wordformSubmissionSummaryToday">0</strong>
		</div>
		<div class="wordform-submission-data-summary-card">
			<span class="wordform-submission-data-summary-label"><?php esc_html_e( 'This Week', 'wordform-pro' ); ?></span>
			<strong class="wordform-submission-data-summary-value" id="wordformSubmissionSummaryWeek">0</strong>
		</div>
		<div class="wordform-submission-data-summary-card">
			<span class="wordform-submission-data-summary-label"><?php esc_html_e( 'Latest Submission', 'wordform-pro' ); ?></span>
			<strong class="wordform-submission-data-summary-value" id="wordformSubmissionSummaryLatest"><?php esc_html_e( 'No submissions yet', 'wordform-pro' ); ?></strong>
		</div>
	</div>

	<table id="wordformUsersFormSubmissionDataTable" class="display hover stripe wp-list-table widefat fixed striped table-view-list wordform-users-submission-data-table">
		<thead>
			<tr>
				<th><?php esc_html_e( 'Submitter', 'wordform-pro' ); ?></th>
				<th><?php esc_html_e( 'Preview', 'wordform-pro' ); ?></th>
				<th><?php esc_html_e( 'Submission Date', 'wordform-pro' ); ?></th>
				<th><?php esc_html_e( 'Details', 'wordform-pro' ); ?></th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>						
</div> <!-- #wordformUsersFormSubmissionDataWrapperTable -->
<div id="WordFormSubmissionDetailModal" title="<?php esc_attr_e( 'Submission Details', 'wordform-pro' ); ?>" style="display:none;"></div>
