<?php
/** @var array<int,array<string,mixed>> $routes */
/** @var array<string,mixed>|null $edit */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$route_form_open = (bool) $edit;

$routes_for_list = $routes;
usort(
	$routes_for_list,
	static function ( array $a, array $b ): int {
		return (int) $b['id'] <=> (int) $a['id'];
	}
);
?>
<div class="wrap">
	<h1><?php esc_html_e( 'Tuyến', 'datvetaucaotoc-booking' ); ?></h1>
	<?php if ( ! empty( $_GET['saved'] ) ) : ?><div class="notice notice-success"><p><?php esc_html_e( 'Đã lưu.', 'datvetaucaotoc-booking' ); ?></p></div><?php endif; ?>
	<?php if ( ! empty( $_GET['deleted'] ) ) : ?><div class="notice notice-success"><p><?php esc_html_e( 'Đã xóa tuyến.', 'datvetaucaotoc-booking' ); ?></p></div><?php endif; ?>

	<details
		class="datvetau-admin-card datvetau-admin-card--route-form"
		id="datvetau-route-form-details"
		<?php echo $route_form_open ? 'open' : ''; ?>
	>
		<summary class="datvetau-admin-card__title datvetau-admin-card__summary">
			<?php echo $edit ? esc_html__( 'Sửa tuyến', 'datvetaucaotoc-booking' ) : esc_html__( 'Thêm tuyến', 'datvetaucaotoc-booking' ); ?>
			<?php if ( ! $edit ) : ?>
				<span class="datvetau-admin-card__summary-hint"><?php esc_html_e( '(bấm để mở form)', 'datvetaucaotoc-booking' ); ?></span>
			<?php endif; ?>
		</summary>
		<div class="datvetau-admin-card__body">
	<form method="post">
		<?php wp_nonce_field( 'datvetau_route_save' ); ?>
		<input type="hidden" name="datvetau_route_save" value="1" />
		<?php if ( $edit ) : ?><input type="hidden" name="route_id" value="<?php echo (int) $edit['id']; ?>" /><?php endif; ?>
		<table class="form-table">
			<tr><th><label for="slug">Slug</label></th><td><input name="slug" id="slug" class="regular-text" required value="<?php echo esc_attr( (string) ( $edit['slug'] ?? '' ) ); ?>" /></td></tr>
			<tr><th><label for="label">Tên hiển thị</label></th><td><input name="label" id="label" class="regular-text" required value="<?php echo esc_attr( (string) ( $edit['label'] ?? '' ) ); ?>" /></td></tr>
			<tr>
				<th scope="row"><label for="reverse_route_id"><?php esc_html_e( 'Tuyến chiều về (khứ hồi)', 'datvetaucaotoc-booking' ); ?></label></th>
				<td>
					<select name="reverse_route_id" id="reverse_route_id">
						<option value=""><?php esc_html_e( '— Không có / một chiều —', 'datvetaucaotoc-booking' ); ?></option>
						<?php
						$edit_id          = $edit ? (int) $edit['id'] : 0;
						$reverse_selected = (int) ( $edit['reverse_route_id'] ?? 0 );
						foreach ( $routes as $r ) :
							if ( (int) $r['id'] === $edit_id ) {
								continue;
							}
							?>
							<option value="<?php echo (int) $r['id']; ?>" <?php selected( $reverse_selected, (int) $r['id'] ); ?>>
								<?php echo esc_html( (string) $r['label'] . ' (ID ' . (int) $r['id'] . ')' ); ?>
							</option>
						<?php endforeach; ?>
					</select>
					<p class="description"><?php esc_html_e( 'Khi khách chọn khứ hồi trên FE, chiều về tìm chuyến theo tuyến này (ví dụ: Hà Nội → Lào Cai thì chiều về là Lào Cai → Hà Nội).', 'datvetaucaotoc-booking' ); ?></p>
				</td>
			</tr>
			<tr><th><label for="sort_order">Thứ tự</label></th><td><input name="sort_order" id="sort_order" type="number" value="<?php echo esc_attr( (string) ( $edit['sort_order'] ?? '0' ) ); ?>" /></td></tr>
			<tr><th><?php esc_html_e( 'Kích hoạt', 'datvetaucaotoc-booking' ); ?></th><td><label><input type="checkbox" name="is_active" value="1" <?php checked( ! $edit || ! empty( $edit['is_active'] ) ); ?> /> <?php esc_html_e( 'Hiển thị', 'datvetaucaotoc-booking' ); ?></label></td></tr>
		</table>
		<?php submit_button( __( 'Lưu tuyến', 'datvetaucaotoc-booking' ) ); ?>
	</form>
		</div>
	</details>

	<section class="datvetau-admin-card datvetau-admin-card--route-list">
		<h2 class="datvetau-admin-card__title"><?php esc_html_e( 'Danh sách tuyến', 'datvetaucaotoc-booking' ); ?></h2>
	<table class="widefat striped">
		<thead>
			<tr>
				<th><?php esc_html_e( 'ID', 'datvetaucaotoc-booking' ); ?></th>
				<th><?php esc_html_e( 'Slug', 'datvetaucaotoc-booking' ); ?></th>
				<th><?php esc_html_e( 'Tên hiển thị', 'datvetaucaotoc-booking' ); ?></th>
				<th><?php esc_html_e( 'Chiều về (khứ hồi)', 'datvetaucaotoc-booking' ); ?></th>
				<th></th>
			</tr>
		</thead>
		<tbody>
		<?php
		$route_by_id = array();
		foreach ( $routes as $r ) {
			$route_by_id[ (int) $r['id'] ] = (string) $r['label'];
		}
		foreach ( $routes_for_list as $r ) :
			$rev_id = ! empty( $r['reverse_route_id'] ) ? (int) $r['reverse_route_id'] : 0;
			?>
			<tr>
				<td><?php echo (int) $r['id']; ?></td>
				<td><?php echo esc_html( (string) $r['slug'] ); ?></td>
				<td><?php echo esc_html( (string) $r['label'] ); ?></td>
				<td><?php echo $rev_id ? esc_html( $route_by_id[ $rev_id ] ?? (string) $rev_id ) : '—'; ?></td>
				<td>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=datvetau-booking-routes&edit=' . (int) $r['id'] ) ); ?>"><?php esc_html_e( 'Sửa', 'datvetaucaotoc-booking' ); ?></a>
					| <a class="datvetau-admin-action-delete" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=datvetau-booking-routes&datvetau_delete_route=' . (int) $r['id'] ), 'datvetau_delete_route_' . (int) $r['id'] ) ); ?>" onclick="return confirm('<?php echo esc_js( __( 'Xóa tuyến này?', 'datvetaucaotoc-booking' ) ); ?>');"><?php esc_html_e( 'Xóa', 'datvetaucaotoc-booking' ); ?></a>
				</td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>
	</section>
</div>
