<?php
/**
 * List Cards View
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$cloudi7_icg_message = isset( $_GET['message'] ) ? sanitize_text_field( wp_unslash( $_GET['message'] ) ) : '';
?>
<div class="wrap">
	<h1 class="wp-heading-inline"><?php esc_html_e( 'All ID Cards', 'cloudi7-id-card-generator' ); ?></h1>
	<a href="<?php echo esc_url( admin_url( 'admin.php?page=cloudi7-icg-create-card' ) ); ?>" class="page-title-action"><?php esc_html_e( 'Add New', 'cloudi7-id-card-generator' ); ?></a>
	<hr class="wp-header-end">

	<?php if ( 'created' === $cloudi7_icg_message ) : ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'ID Card created successfully.', 'cloudi7-id-card-generator' ); ?></p></div>
	<?php elseif ( 'updated' === $cloudi7_icg_message ) : ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'ID Card updated successfully.', 'cloudi7-id-card-generator' ); ?></p></div>
	<?php elseif ( 'deleted' === $cloudi7_icg_message ) : ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'ID Card deleted.', 'cloudi7-id-card-generator' ); ?></p></div>
	<?php endif; ?>
	<div class="tablenav top">
		<div class="alignleft actions bulkactions">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=cloudi7-icg-create-card' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Add New Card', 'cloudi7-id-card-generator' ); ?></a>
		</div>
		<div class="tablenav-pages">
			<span class="displaying-num">
				<?php 
				/* translators: %s: number of items */
				$cloudi7_icg_item_text = _n( '%s item', '%s items', $cloudi7_icg_total_items, 'cloudi7-id-card-generator' ); 
				echo esc_html( sprintf( $cloudi7_icg_item_text, number_format_i18n( $cloudi7_icg_total_items ) ) ); 
				?>
			</span>
			<?php
			echo wp_kses_post( paginate_links( array(
				'base'      => add_query_arg( 'paged', '%#%' ),
				'format'    => '',
				'prev_text' => esc_html__( '&laquo;', 'cloudi7-id-card-generator' ),
				'next_text' => esc_html__( '&raquo;', 'cloudi7-id-card-generator' ),
				'total'     => $cloudi7_icg_total_pages,
				'current'   => $cloudi7_icg_current_page,
			) ) );
			?>
		</div>
	</div>

	<table class="wp-list-table widefat fixed striped table-view-list cards">
		<thead>
			<tr>
				<th scope="col" class="manage-column column-id-number"><?php esc_html_e( 'ID Number', 'cloudi7-id-card-generator' ); ?></th>
				<th scope="col" class="manage-column column-photo"><?php esc_html_e( 'Photo', 'cloudi7-id-card-generator' ); ?></th>
				<th scope="col" class="manage-column column-name"><?php esc_html_e( 'Name', 'cloudi7-id-card-generator' ); ?></th>
				<th scope="col" class="manage-column column-org"><?php esc_html_e( 'Organization', 'cloudi7-id-card-generator' ); ?></th>
				<th scope="col" class="manage-column column-date"><?php esc_html_e( 'Issue Date', 'cloudi7-id-card-generator' ); ?></th>
				<th scope="col" class="manage-column column-actions"><?php esc_html_e( 'Actions', 'cloudi7-id-card-generator' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( $cloudi7_icg_cards ) : ?>
				<?php foreach ( $cloudi7_icg_cards as $cloudi7_icg_card_item ) : ?>
					<tr>
						<td><strong><?php echo esc_html( $cloudi7_icg_card_item->id_number ); ?></strong></td>
						<td>
							<?php if ( $cloudi7_icg_card_item->photo ) : ?>
								<img src="<?php echo esc_url( $cloudi7_icg_card_item->photo ); ?>" width="40" height="40" style="object-fit: cover; border-radius: 4px;">
							<?php else : ?>
								<span class="dashicons dashicons-admin-users" style="font-size: 40px; width: 40px; height: 40px;"></span>
							<?php endif; ?>
						</td>
						<td><?php echo esc_html( $cloudi7_icg_card_item->name ); ?></td>
						<td><?php echo esc_html( $cloudi7_icg_card_item->organization_type ); ?></td>
						<td><?php echo esc_html( $cloudi7_icg_card_item->issue_date ?: '-' ); ?></td>
						<td>
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=cloudi7-icg-all-cards&action=view&id=' . $cloudi7_icg_card_item->id ) ); ?>"><?php esc_html_e( 'View', 'cloudi7-id-card-generator' ); ?></a> |
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=cloudi7-icg-create-card&id=' . $cloudi7_icg_card_item->id ) ); ?>"><?php esc_html_e( 'Edit', 'cloudi7-id-card-generator' ); ?></a> |
							<a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=cloudi7-icg-all-cards&action=delete&id=' . $cloudi7_icg_card_item->id ), 'cloudi7_icg_action', 'cloudi7_icg_nonce' ) ); ?>" class="submitdelete" onclick="return confirm('<?php echo esc_js( esc_html__( 'Are you sure you want to delete this card?', 'cloudi7-id-card-generator' ) ); ?>')"><?php esc_html_e( 'Delete', 'cloudi7-id-card-generator' ); ?></a>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php else : ?>
				<tr>
					<td colspan="6"><?php esc_html_e( 'No ID cards found.', 'cloudi7-id-card-generator' ); ?></td>
				</tr>
			<?php endif; ?>
		</tbody>
	</table>

	<div class="tablenav bottom">
		<div class="tablenav-pages">
			<span class="displaying-num">
				<?php 
				/* translators: %s: number of items */
				$cloudi7_icg_item_text_bottom = _n( '%s item', '%s items', $cloudi7_icg_total_items, 'cloudi7-id-card-generator' ); 
				echo esc_html( sprintf( $cloudi7_icg_item_text_bottom, number_format_i18n( $cloudi7_icg_total_items ) ) ); 
				?>
			</span>
			<?php
			echo wp_kses_post( paginate_links( array(
				'base'      => add_query_arg( 'paged', '%#%' ),
				'format'    => '',
				'prev_text' => esc_html__( '&laquo;', 'cloudi7-id-card-generator' ),
				'next_text' => esc_html__( '&raquo;', 'cloudi7-id-card-generator' ),
				'total'     => $cloudi7_icg_total_pages,
				'current'   => $cloudi7_icg_current_page,
			) ) );
			?>
		</div>
	</div>
</div>
