<?php
/**
 * Files screen: the registry, with working proxy links.
 *
 * @package Upload_Vault
 */

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

if ( ! current_user_can( 'manage_options' ) ) {
	return;
}

global $wpdb;

// Search/pagination input is honoured only when it arrives with the nonce
// this screen's own form and pagination links carry; anything else falls
// back to the default view (page 1, no search).
$uvgf_nonce_ok = isset( $_GET['uvgf_nonce'] ) && false !== wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['uvgf_nonce'] ) ), 'uvgf_files' );

$uvgf_per_page = 50;
$uvgf_paged    = 1;
$uvgf_search   = '';
if ( $uvgf_nonce_ok ) {
	$uvgf_paged  = isset( $_GET['uvgf_paged'] ) ? max( 1, absint( $_GET['uvgf_paged'] ) ) : 1;
	$uvgf_search = isset( $_GET['uvgf_s'] ) ? sanitize_text_field( wp_unslash( $_GET['uvgf_s'] ) ) : '';
}
$uvgf_offset = ( $uvgf_paged - 1 ) * $uvgf_per_page;

if ( '' !== $uvgf_search ) {
	$uvgf_like  = '%' . $wpdb->esc_like( $uvgf_search ) . '%';
	$uvgf_total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}uvgf_files WHERE relpath LIKE %s", $uvgf_like ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- own table.
	$uvgf_rows  = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}uvgf_files WHERE relpath LIKE %s ORDER BY id DESC LIMIT %d OFFSET %d", $uvgf_like, $uvgf_per_page, $uvgf_offset ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- own table.
} else {
	$uvgf_total = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}uvgf_files" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- own table.
	$uvgf_rows  = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}uvgf_files ORDER BY id DESC LIMIT %d OFFSET %d", $uvgf_per_page, $uvgf_offset ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- own table.
}

$uvgf_titles = Uvgf_Locator::form_titles();
$uvgf_pages  = max( 1, (int) ceil( $uvgf_total / $uvgf_per_page ) );
?>
<div class="wrap uvgf-wrap">
	<h1><?php esc_html_e( 'Upload Vault — Files', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></h1>

	<?php Uvgf_Admin::render_notice(); ?>

	<form method="get" class="uvgf-searchform">
		<input type="hidden" name="page" value="uvgf-files" />
		<input type="hidden" name="uvgf_nonce" value="<?php echo esc_attr( wp_create_nonce( 'uvgf_files' ) ); ?>" />
		<p class="search-box">
			<label class="screen-reader-text" for="uvgf-search-input"><?php esc_html_e( 'Search files', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></label>
			<input type="search" id="uvgf-search-input" name="uvgf_s" value="<?php echo esc_attr( $uvgf_search ); ?>" />
			<button type="submit" class="button"><?php esc_html_e( 'Search files', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></button>
		</p>
	</form>

	<?php if ( empty( $uvgf_rows ) ) : ?>
		<p><?php esc_html_e( 'No files in the registry yet — run an audit first (it indexes every upload it finds).', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></p>
	<?php else : ?>
		<table class="widefat striped uvgf-table">
			<thead>
				<tr>
					<th><?php esc_html_e( 'File', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
					<th><?php esc_html_e( 'Form', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
					<th><?php esc_html_e( 'Size', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
					<th><?php esc_html_e( 'Status', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
					<th><?php esc_html_e( 'First seen', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
					<th><?php esc_html_e( 'Download', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $uvgf_rows as $uvgf_row ) : ?>
					<tr>
						<td><code><?php echo esc_html( $uvgf_row->relpath ); ?></code></td>
						<td>
							<?php
							$uvgf_fid = (int) $uvgf_row->form_id;
							if ( isset( $uvgf_titles[ $uvgf_fid ] ) && '' !== $uvgf_titles[ $uvgf_fid ] ) {
								echo esc_html( $uvgf_titles[ $uvgf_fid ] );
							} elseif ( $uvgf_fid > 0 ) {
								/* translators: %d: form id. */
								printf( esc_html__( 'Form #%d', 'buildsbyluke-upload-vault-for-gravity-forms' ), (int) $uvgf_fid );
							} else {
								echo '&mdash;';
							}
							?>
						</td>
						<td><?php echo esc_html( size_format( (int) $uvgf_row->size ) ); ?></td>
						<td>
							<?php if ( 'exposed' === $uvgf_row->status ) : ?>
								<span class="uvgf-pill uvgf-pill--exposed"><?php esc_html_e( 'exposed', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></span>
							<?php elseif ( 'protected' === $uvgf_row->status ) : ?>
								<span class="uvgf-pill uvgf-pill--ok"><?php esc_html_e( 'protected', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></span>
							<?php else : ?>
								<span class="uvgf-pill"><?php echo esc_html( $uvgf_row->status ); ?></span>
							<?php endif; ?>
						</td>
						<td><?php echo esc_html( mysql2date( get_option( 'date_format' ), $uvgf_row->first_seen ) ); ?></td>
						<td>
							<a class="button button-small" href="<?php echo esc_url( Uvgf_Proxy::url( (int) $uvgf_row->id ) ); ?>"><?php esc_html_e( 'Download', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></a>
							<?php
							/**
							 * Extra row actions (the Pro layer adds "Share…").
							 *
							 * @param object $uvgf_row Registry row.
							 */
							do_action( 'uvgf_file_row_actions', $uvgf_row );
							?>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>

		<?php if ( $uvgf_pages > 1 ) : ?>
			<div class="tablenav"><div class="tablenav-pages">
				<?php
				$uvgf_base_url = add_query_arg(
					array(
						'page'       => 'uvgf-files',
						'uvgf_s'     => rawurlencode( $uvgf_search ),
						'uvgf_nonce' => wp_create_nonce( 'uvgf_files' ),
					),
					admin_url( 'admin.php' )
				);
				for ( $uvgf_i = 1; $uvgf_i <= $uvgf_pages; $uvgf_i++ ) {
					if ( $uvgf_i === $uvgf_paged ) {
						echo '<span class="tablenav-pages-navspan button disabled">' . esc_html( (string) $uvgf_i ) . '</span> ';
					} else {
						echo '<a class="button" href="' . esc_url( add_query_arg( 'uvgf_paged', $uvgf_i, $uvgf_base_url ) ) . '">' . esc_html( (string) $uvgf_i ) . '</a> ';
					}
				}
				?>
			</div></div>
		<?php endif; ?>

		<p class="uvgf-muted">
			<?php
			/* translators: %s: file count. */
			printf( esc_html__( '%s files in the registry. Download links go through the capability-checked proxy and keep working after protection is installed.', 'buildsbyluke-upload-vault-for-gravity-forms' ), esc_html( number_format_i18n( $uvgf_total ) ) );
			?>
		</p>
	<?php endif; ?>
</div>
