<?php
/**
 * Entity browser view shell.
 *
 * @var string $browser_type
 */

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

use DatabaseCleanup\Includes\Entity_Browser;

$config = Entity_Browser::browser_config( $browser_type );
if ( $config === [] ) {
	return;
}
?>
<div class="wpps-dc-browser" data-browser-type="<?php echo esc_attr( $browser_type ); ?>">
	<div class="wpps-dc-panel">
		<h2 class="wpps-dc-panel-title"><?php echo esc_html( (string) ( $config['title'] ?? '' ) ); ?></h2>
		<div class="wpps-dc-panel-body">
			<?php if ( ! empty( $config['description'] ) ) : ?>
				<p class="wpps-dc-browser-desc description"><?php echo esc_html( (string) $config['description'] ); ?></p>
			<?php endif; ?>

			<div class="wpps-dc-browser-summary" aria-live="polite"></div>

			<div class="wpps-dc-browser-toolbar">
				<div class="wpps-dc-browser-toolbar-left">
					<input type="search" class="wpps-dc-browser-search regular-text" placeholder="<?php esc_attr_e( 'Search…', 'wp-powersuite' ); ?>" />
					<?php foreach ( (array) ( $config['filters'] ?? [] ) as $filter_group ) : ?>
						<?php if ( empty( $filter_group['options'] ) || ! is_array( $filter_group['options'] ) ) { continue; } ?>
						<select class="wpps-dc-browser-filter" data-filter-key="<?php echo esc_attr( (string) ( $filter_group['key'] ?? 'filter' ) ); ?>" aria-label="<?php echo esc_attr( (string) ( $filter_group['label'] ?? __( 'Filter', 'wp-powersuite' ) ) ); ?>">
							<?php foreach ( $filter_group['options'] as $value => $label ) : ?>
								<?php if ( is_array( $label ) && isset( $label['options'] ) && is_array( $label['options'] ) ) : ?>
									<optgroup label="<?php echo esc_attr( (string) ( $label['label'] ?? '' ) ); ?>">
										<?php foreach ( $label['options'] as $opt_value => $opt_label ) : ?>
											<option value="<?php echo esc_attr( (string) $opt_value ); ?>"><?php echo esc_html( (string) $opt_label ); ?></option>
										<?php endforeach; ?>
									</optgroup>
								<?php else : ?>
									<option value="<?php echo esc_attr( (string) $value ); ?>"><?php echo esc_html( (string) $label ); ?></option>
								<?php endif; ?>
							<?php endforeach; ?>
						</select>
					<?php endforeach; ?>
				</div>
				<div class="wpps-dc-browser-toolbar-right">
					<span class="wpps-dc-owner-scan-status" aria-live="polite"></span>
					<button type="button" class="button wpps-dc-owner-scan" data-state="idle">
						<span class="dashicons dashicons-search" aria-hidden="true"></span>
						<span class="wpps-dc-owner-scan-label"><?php esc_html_e( 'Detect owners', 'wp-powersuite' ); ?></span>
					</button>
					<span class="wpps-dc-browser-selected" aria-live="polite" hidden></span>
					<?php foreach ( (array) ( $config['actions'] ?? [] ) as $action ) : ?>
						<button type="button"
							class="button wpps-dc-browser-action<?php echo ! empty( $action['destructive'] ) ? ' wpps-dc-browser-action--danger' : ''; ?>"
							data-action="<?php echo esc_attr( (string) ( $action['id'] ?? '' ) ); ?>"
							data-confirm="<?php echo esc_attr( (string) ( $action['confirm'] ?? '' ) ); ?>"
							disabled>
							<?php echo esc_html( (string) ( $action['label'] ?? '' ) ); ?>
						</button>
					<?php endforeach; ?>
					<?php if ( ! empty( $config['bulk'] ) && is_array( $config['bulk'] ) ) : ?>
						<button type="button"
							class="button wpps-dc-browser-bulk wpps-dc-browser-action--danger"
							data-confirm="<?php echo esc_attr( (string) ( $config['bulk']['confirm'] ?? '' ) ); ?>">
							<span class="dashicons dashicons-trash" aria-hidden="true"></span>
							<?php echo esc_html( (string) ( $config['bulk']['label'] ?? '' ) ); ?>
						</button>
					<?php endif; ?>
					<button type="button" class="button wpps-dc-browser-refresh"><?php esc_html_e( 'Refresh', 'wp-powersuite' ); ?></button>
				</div>
			</div>

			<div class="wpps-dc-owner-progress" hidden aria-live="polite">
				<div class="wpps-dc-owner-progress__head">
					<span class="wpps-dc-owner-progress__label"><?php esc_html_e( 'Scanning source files…', 'wp-powersuite' ); ?></span>
					<button type="button" class="button-link wpps-dc-owner-cancel"><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
				</div>
				<div class="wpps-dc-progress-bar"><span class="wpps-dc-owner-progress__fill"></span></div>
				<span class="wpps-dc-owner-progress__meta"></span>
			</div>

			<div class="wpps-dc-browser-status" aria-live="polite"></div>

			<div class="wpps-dc-table-wrap">
				<div class="wpps-dc-table-loading" aria-hidden="true">
					<span class="wpps-dc-spinner"></span>
					<span class="wpps-dc-table-loading__text"><?php esc_html_e( 'Loading…', 'wp-powersuite' ); ?></span>
				</div>
				<table class="wp-list-table widefat fixed striped table-view-list wpps-dc-browser-table">
					<thead>
						<tr>
							<td class="manage-column column-cb check-column">
								<label class="screen-reader-text wpps-dc-browser-select-all-label"><?php esc_html_e( 'Select all', 'wp-powersuite' ); ?></label>
								<input type="checkbox" class="wpps-dc-browser-select-all" />
							</td>
							<?php foreach ( (array) ( $config['columns'] ?? [] ) as $column ) : ?>
								<?php
								$column_key = sanitize_key( (string) ( $column['key'] ?? '' ) );
								$is_primary = $column_key === 'name' || $column_key === 'meta_key' || $column_key === 'hook';
								?>
								<th scope="col" class="manage-column column-<?php echo esc_attr( $column_key ); ?><?php echo $is_primary ? ' column-primary' : ''; ?>">
									<?php if ( ! empty( $column['sortable'] ) ) : ?>
										<button type="button" class="wpps-dc-sort" data-sort="<?php echo esc_attr( $column_key ); ?>">
											<?php echo esc_html( (string) ( $column['label'] ?? '' ) ); ?>
										</button>
									<?php else : ?>
										<?php echo esc_html( (string) ( $column['label'] ?? '' ) ); ?>
									<?php endif; ?>
								</th>
							<?php endforeach; ?>
							<?php if ( ! empty( $config['viewable'] ) ) : ?>
								<th scope="col" class="manage-column column-actions"><?php esc_html_e( 'Actions', 'wp-powersuite' ); ?></th>
							<?php endif; ?>
						</tr>
					</thead>
					<tbody></tbody>
					<tfoot>
						<tr>
							<td class="manage-column column-cb check-column">
								<input type="checkbox" class="wpps-dc-browser-select-all wpps-dc-browser-select-all-foot" aria-label="<?php esc_attr_e( 'Select all', 'wp-powersuite' ); ?>" />
							</td>
							<?php foreach ( (array) ( $config['columns'] ?? [] ) as $column ) : ?>
								<?php
								$column_key = sanitize_key( (string) ( $column['key'] ?? '' ) );
								$is_primary = $column_key === 'name' || $column_key === 'meta_key' || $column_key === 'hook';
								?>
								<th scope="col" class="manage-column column-<?php echo esc_attr( $column_key ); ?><?php echo $is_primary ? ' column-primary' : ''; ?>">
									<?php echo esc_html( (string) ( $column['label'] ?? '' ) ); ?>
								</th>
							<?php endforeach; ?>
							<?php if ( ! empty( $config['viewable'] ) ) : ?>
								<th scope="col" class="manage-column column-actions"><?php esc_html_e( 'Actions', 'wp-powersuite' ); ?></th>
							<?php endif; ?>
						</tr>
					</tfoot>
				</table>
			</div>

			<div class="wpps-dc-browser-pagination tablenav bottom">
				<div class="tablenav-pages">
					<span class="displaying-num"></span>
					<label class="wpps-dc-browser-per-page-label">
						<span class="screen-reader-text"><?php esc_html_e( 'Items per page', 'wp-powersuite' ); ?></span>
						<select class="wpps-dc-browser-per-page" aria-label="<?php esc_attr_e( 'Items per page', 'wp-powersuite' ); ?>">
							<?php foreach ( Entity_Browser::per_page_options() as $per_page_option ) : ?>
								<option value="<?php echo esc_attr( (string) $per_page_option ); ?>"<?php selected( $per_page_option, Entity_Browser::DEFAULT_PER_PAGE ); ?>>
									<?php echo esc_html( (string) $per_page_option ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</label>
					<span class="pagination-links">
						<button type="button" class="button wpps-dc-browser-prev" disabled>&laquo;</button>
						<span class="paging-input"></span>
						<button type="button" class="button wpps-dc-browser-next" disabled>&raquo;</button>
					</span>
				</div>
			</div>
		</div>
	</div>
</div>
