<?php
/**
 * Team Surveys — queue page.
 *
 * Variables in scope:
 *   - $responses     array  Hydrated rows.
 *   - $status_counts array  ['new' => 3, 'all' => 27, ...]
 *   - $status_filter string Selected filter.
 *   - $holding_only  bool   "holding queue only" view.
 *   - $search        string Search query.
 *   - $page          int    Pagination.
 *   - $nonce         string Bulk-action nonce.
 *   - $base_url      string Base admin URL for filter links.
 *
 * @package BW_AI_Schema_Pro
 * @since 2.2.0
 */

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

/** @var array  $responses */
/** @var array  $status_counts */
/** @var string $status_filter */
/** @var bool   $holding_only */
/** @var string $search */
/** @var int    $page */
/** @var string $nonce */
/** @var string $base_url */

$is_open       = BW_Schema_Survey::is_open();
$public_url    = BW_Schema_Survey::public_url();
$days_left     = BW_Schema_Survey::days_remaining();
$settings_url  = BW_Schema_Survey_Admin::url_settings();
$add_url       = BW_Schema_Survey_Admin::url_add();

$status_labels = array(
	'new'        => __( 'New', 'bw-ai-schema-pro' ),
	'triaged'    => __( 'Triaged', 'bw-ai-schema-pro' ),
	'structured' => __( 'In review', 'bw-ai-schema-pro' ),
	'ready'      => __( 'Ready', 'bw-ai-schema-pro' ),
	'published'  => __( 'Published', 'bw-ai-schema-pro' ),
	'rejected'   => __( 'Rejected', 'bw-ai-schema-pro' ),
);
?>
<div class="wrap">
	<h1 class="wp-heading-inline"><?php esc_html_e( 'Team Surveys', 'bw-ai-schema-pro' ); ?></h1>
	<a href="<?php echo esc_url( $add_url ); ?>" class="page-title-action"><?php esc_html_e( 'Add team info manually', 'bw-ai-schema-pro' ); ?></a>
	<a href="<?php echo esc_url( $settings_url ); ?>" class="page-title-action"><?php esc_html_e( 'Settings', 'bw-ai-schema-pro' ); ?></a>
	<hr class="wp-header-end" />

	<?php settings_errors( 'bw_schema_survey' ); ?>

	<?php if ( BW_Schema_Survey::is_proceeding_without_cpt() ) : ?>
		<div class="notice notice-warning">
			<p>
				<strong><?php esc_html_e( "You're running Team Surveys without a team post type.", 'bw-ai-schema-pro' ); ?></strong>
				<?php esc_html_e( "Submissions can be collected and reviewed, but they can't be published to team-member pages until you connect a team post type.", 'bw-ai-schema-pro' ); ?>
				<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'page' => BW_Schema_Survey_Admin::PAGE_QUEUE, 'bw_schema_undo_proceed_without' => '1' ), admin_url( 'options-general.php' ) ), 'bw_schema_undo_proceed_without' ) ); ?>">
					<?php esc_html_e( 'Connect one now', 'bw-ai-schema-pro' ); ?>
				</a>
			</p>
		</div>
	<?php endif; ?>

	<div class="bw-schema-survey-status-banner notice notice-<?php echo $is_open ? 'success' : 'info'; ?>">
		<p>
			<?php if ( $is_open ) : ?>
				<strong><?php esc_html_e( 'Survey is OPEN.', 'bw-ai-schema-pro' ); ?></strong>
				<?php
				/* translators: %d: number of days remaining */
				printf( esc_html__( 'Closes in %d day(s).', 'bw-ai-schema-pro' ), (int) $days_left );
				?>
				<br />
				<code><?php echo esc_html( $public_url ); ?></code>
			<?php else : ?>
				<strong><?php esc_html_e( 'Survey is CLOSED.', 'bw-ai-schema-pro' ); ?></strong>
				<?php esc_html_e( 'Open it from settings to start collecting submissions.', 'bw-ai-schema-pro' ); ?>
			<?php endif; ?>
		</p>
	</div>

	<ul class="subsubsub">
		<li>
			<a href="<?php echo esc_url( $base_url ); ?>" class="<?php echo ( '' === $status_filter && ! $holding_only ) ? 'current' : ''; ?>">
				<?php esc_html_e( 'All', 'bw-ai-schema-pro' ); ?> <span class="count">(<?php echo (int) $status_counts['all']; ?>)</span>
			</a>
		</li>
		<?php foreach ( $status_labels as $slug => $label ) : ?>
			<li>
				|
				<a href="<?php echo esc_url( add_query_arg( 'status', $slug, $base_url ) ); ?>" class="<?php echo $status_filter === $slug ? 'current' : ''; ?>">
					<?php echo esc_html( $label ); ?> <span class="count">(<?php echo (int) ( $status_counts[ $slug ] ?? 0 ); ?>)</span>
				</a>
			</li>
		<?php endforeach; ?>
		<li>
			|
			<a href="<?php echo esc_url( add_query_arg( 'holding', '1', $base_url ) ); ?>" class="<?php echo $holding_only ? 'current' : ''; ?>">
				<?php esc_html_e( 'Holding queue', 'bw-ai-schema-pro' ); ?>
			</a>
		</li>
	</ul>

	<form method="get" class="search-form" style="margin: 12px 0;">
		<input type="hidden" name="page" value="<?php echo esc_attr( BW_Schema_Survey_Admin::PAGE_QUEUE ); ?>" />
		<?php if ( $status_filter ) : ?>
			<input type="hidden" name="status" value="<?php echo esc_attr( $status_filter ); ?>" />
		<?php endif; ?>
		<?php if ( $holding_only ) : ?>
			<input type="hidden" name="holding" value="1" />
		<?php endif; ?>
		<input type="search" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php esc_attr_e( 'Search by submitter name or email', 'bw-ai-schema-pro' ); ?>" />
		<button type="submit" class="button"><?php esc_html_e( 'Search', 'bw-ai-schema-pro' ); ?></button>
	</form>

	<form method="post">
		<?php wp_nonce_field( BW_Schema_Survey_Admin::NONCE_QUEUE, '_bw_schema_survey_queue_nonce' ); ?>

		<div class="tablenav top">
			<div class="alignleft actions bulkactions">
				<select name="bw_schema_survey_queue_action">
					<option value=""><?php esc_html_e( 'Bulk actions', 'bw-ai-schema-pro' ); ?></option>
					<option value="publish"><?php esc_html_e( 'Publish ready', 'bw-ai-schema-pro' ); ?></option>
					<option value="reject"><?php esc_html_e( 'Reject', 'bw-ai-schema-pro' ); ?></option>
				</select>
				<button type="submit" class="button"><?php esc_html_e( 'Apply', 'bw-ai-schema-pro' ); ?></button>
			</div>
		</div>

		<table class="wp-list-table widefat striped">
			<thead>
				<tr>
					<td class="check-column" style="width: 2.2em;"><input type="checkbox" id="bw-schema-survey-cb-all" /></td>
					<th><?php esc_html_e( 'ID', 'bw-ai-schema-pro' ); ?></th>
					<th><?php esc_html_e( 'Submitted', 'bw-ai-schema-pro' ); ?></th>
					<th><?php esc_html_e( 'Submitter', 'bw-ai-schema-pro' ); ?></th>
					<th><?php esc_html_e( 'Target', 'bw-ai-schema-pro' ); ?></th>
					<th><?php esc_html_e( 'Status', 'bw-ai-schema-pro' ); ?></th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				<?php if ( empty( $responses ) ) : ?>
					<tr>
						<td colspan="7">
							<?php esc_html_e( 'No submissions yet.', 'bw-ai-schema-pro' ); ?>
							<br /><br />
							<a href="<?php echo esc_url( $add_url ); ?>" class="button button-primary">
								<?php esc_html_e( 'Add team info manually', 'bw-ai-schema-pro' ); ?>
							</a>
							<?php if ( $is_open ) : ?>
								<?php esc_html_e( '— or share the survey link above with a team member.', 'bw-ai-schema-pro' ); ?>
							<?php else : ?>
								<a href="<?php echo esc_url( $settings_url ); ?>" class="button">
									<?php esc_html_e( 'Open the public survey', 'bw-ai-schema-pro' ); ?>
								</a>
							<?php endif; ?>
						</td>
					</tr>
				<?php else : ?>
					<?php foreach ( $responses as $r ) : ?>
						<tr>
							<th scope="row" class="check-column">
								<input type="checkbox" name="ids[]" value="<?php echo (int) $r['id']; ?>" />
							</th>
							<td><?php echo (int) $r['id']; ?></td>
							<td>
								<?php
								echo esc_html(
									mysql2date(
										get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
										$r['created_at']
									)
								);
								?>
							</td>
							<td>
								<?php echo esc_html( $r['submitter_name'] ); ?>
								<?php if ( $r['submitter_email'] ) : ?>
									<br /><small><?php echo esc_html( $r['submitter_email'] ); ?></small>
								<?php endif; ?>
							</td>
							<td>
								<?php if ( $r['target_post_id'] ) : ?>
									<?php $tt = get_the_title( $r['target_post_id'] ); ?>
									<a href="<?php echo esc_url( get_edit_post_link( $r['target_post_id'] ) ); ?>">
										<?php echo esc_html( $tt ? $tt : __( '(untitled)', 'bw-ai-schema-pro' ) ); ?>
									</a>
								<?php else : ?>
									<em><?php esc_html_e( 'Holding queue', 'bw-ai-schema-pro' ); ?></em>
								<?php endif; ?>
							</td>
							<td>
								<code><?php echo esc_html( $status_labels[ $r['status'] ] ?? $r['status'] ); ?></code>
							</td>
							<td>
								<a href="<?php echo esc_url( BW_Schema_Survey_Admin::url_detail( $r['id'] ) ); ?>" class="button button-small">
									<?php esc_html_e( 'View', 'bw-ai-schema-pro' ); ?>
								</a>
							</td>
						</tr>
					<?php endforeach; ?>
				<?php endif; ?>
			</tbody>
		</table>
	</form>
</div>

<script>
(function () {
	var cbAll = document.getElementById( 'bw-schema-survey-cb-all' );
	if ( ! cbAll ) {
		return;
	}
	cbAll.addEventListener( 'change', function () {
		var boxes = document.querySelectorAll( 'input[name="ids[]"]' );
		for ( var i = 0; i < boxes.length; i++ ) {
			boxes[ i ].checked = cbAll.checked;
		}
	} );
})();
</script>
