<?php
/**
 * Gestión de listas desplegables del catálogo de etiquetas (solo backend).
 *
 * @package Logistica
 *
 * @var array<string, string> $type_options
 * @var array<string, string> $market_options
 * @var list<string>          $tamano_options
 */

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

if ( ! logistica_can_manage_etiqueta_catalog_options() ) {
	return;
}

$tipo_entries    = Logistica_Label_Etiqueta_Tipos::get_admin_entries();
$mercado_entries = Logistica_Label_Barcode::get_market_admin_entries();
$tamano_entries  = Logistica_Label_Etiqueta_Tamanos::get_admin_entries();
$hidden_entries  = Logistica_Label_Etiqueta_Option_Usage::hidden_catalog_entries();
?>
<div class="logistica-etiqueta-catalog-options" data-etiqueta-catalog-options>
	<div class="logistica-etiqueta-catalog-options__intro">
		<span class="logistica-etiqueta-catalog-options__icon" aria-hidden="true">
			<span class="dashicons dashicons-admin-settings"></span>
		</span>
		<div>
			<h3><?php esc_html_e( 'Listas del catálogo', 'logistica' ); ?></h3>
			<p><?php esc_html_e( 'Administra las opciones de Tipo producto, Mercado y Tamaño usadas en el panel operativo. Los cambios se aplican de inmediato en formularios e importación Excel.', 'logistica' ); ?></p>
		</div>
	</div>

	<p class="logistica-etiqueta-catalog-options__status" data-etiqueta-options-status role="status" aria-live="polite"></p>

	<div class="logistica-etiqueta-catalog-options__grid">
		<section class="logistica-etiqueta-catalog-options__panel" data-etiqueta-options-section="tipos">
			<header class="logistica-etiqueta-catalog-options__panel-head">
				<h4><?php esc_html_e( 'Tipos de producto', 'logistica' ); ?></h4>
				<span class="logistica-etiqueta-catalog-options__count"><?php echo esc_html( (string) count( $tipo_entries ) ); ?></span>
			</header>

			<div class="logistica-etiqueta-catalog-options__table-wrap">
				<table class="widefat striped logistica-etiqueta-catalog-options__table">
					<thead>
						<tr>
							<th scope="col"><?php esc_html_e( 'Nombre', 'logistica' ); ?></th>
							<th scope="col"><?php esc_html_e( 'Cód. EAN tipo', 'logistica' ); ?></th>
							<th class="is-actions-head" scope="col"><?php esc_html_e( 'Acción', 'logistica' ); ?></th>
						</tr>
					</thead>
					<tbody data-etiqueta-options-tipo-rows>
						<?php foreach ( $tipo_entries as $entry ) : ?>
							<tr
								data-etiqueta-options-row="tipo"
								data-key="<?php echo esc_attr( $entry['key'] ); ?>"
								data-built-in="<?php echo ! empty( $entry['built_in'] ) ? '1' : '0'; ?>"
								data-can-delete="<?php echo ! empty( $entry['can_delete'] ) ? '1' : '0'; ?>"
							>
								<td>
									<input type="text" class="logistica-input-sentence-case" value="<?php echo esc_attr( $entry['label'] ); ?>" data-etiqueta-options-label autocomplete="off">
								</td>
								<td>
									<input
										type="text"
										value="<?php echo esc_attr( $entry['type_code'] ); ?>"
										data-etiqueta-options-type-code
										inputmode="numeric"
										maxlength="2"
										readonly
										class="logistica-readonly-field"
										title="<?php esc_attr_e( 'El código EAN no se puede modificar tras crearlo.', 'logistica' ); ?>"
									>
								</td>
								<td>
									<?php logistica_etiqueta_options_icon_actions( 'tipo', ! empty( $entry['can_delete'] ) ); ?>
								</td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			</div>

			<form class="logistica-etiqueta-catalog-options__add" data-etiqueta-options-add="tipo">
				<p class="logistica-etiqueta-catalog-options__add-title"><?php esc_html_e( 'Agregar tipo', 'logistica' ); ?></p>
				<div class="logistica-etiqueta-catalog-options__add-fields">
					<label><?php esc_html_e( 'Nombre', 'logistica' ); ?>
						<input type="text" class="logistica-input-sentence-case" data-etiqueta-options-add-label placeholder="<?php esc_attr_e( 'Ej. Copa, bandeja', 'logistica' ); ?>" autocomplete="off">
					</label>
					<label><?php esc_html_e( 'Cód. EAN tipo (opcional)', 'logistica' ); ?>
						<input type="text" data-etiqueta-options-add-type-code inputmode="numeric" maxlength="2" placeholder="<?php esc_attr_e( 'Auto', 'logistica' ); ?>" autocomplete="off">
					</label>
					<button type="submit" class="button button-primary logistica-etiqueta-catalog-options__btn-add"><?php esc_html_e( 'Agregar', 'logistica' ); ?></button>
				</div>
			</form>
		</section>

		<section class="logistica-etiqueta-catalog-options__panel logistica-etiqueta-catalog-options__panel--mercados" data-etiqueta-options-section="mercados">
			<header class="logistica-etiqueta-catalog-options__panel-head">
				<h4><?php esc_html_e( 'Mercados', 'logistica' ); ?></h4>
				<span class="logistica-etiqueta-catalog-options__count"><?php echo esc_html( (string) count( $mercado_entries ) ); ?></span>
			</header>

			<div class="logistica-etiqueta-catalog-options__table-wrap">
				<table class="widefat striped logistica-etiqueta-catalog-options__table">
					<thead>
						<tr>
							<th scope="col"><?php esc_html_e( 'Clave', 'logistica' ); ?></th>
							<th scope="col"><?php esc_html_e( 'Etiqueta', 'logistica' ); ?></th>
							<th scope="col"><?php esc_html_e( 'Dígito EAN', 'logistica' ); ?></th>
							<th class="is-actions-head" scope="col"><?php esc_html_e( 'Acción', 'logistica' ); ?></th>
						</tr>
					</thead>
					<tbody data-etiqueta-options-mercado-rows>
						<?php foreach ( $mercado_entries as $entry ) : ?>
							<tr
								data-etiqueta-options-row="mercado"
								data-key="<?php echo esc_attr( $entry['key'] ); ?>"
								data-built-in="<?php echo ! empty( $entry['built_in'] ) ? '1' : '0'; ?>"
								data-can-delete="<?php echo ! empty( $entry['can_delete'] ) ? '1' : '0'; ?>"
							>
								<td><span class="logistica-etiqueta-catalog-options__key-badge"><?php echo esc_html( $entry['key'] ); ?></span></td>
								<td>
									<input type="text" class="logistica-input-sentence-case" value="<?php echo esc_attr( $entry['label'] ); ?>" data-etiqueta-options-label autocomplete="off">
								</td>
								<td>
									<input
										type="text"
										value="<?php echo esc_attr( $entry['market_digit'] ); ?>"
										data-etiqueta-options-market-digit
										inputmode="numeric"
										maxlength="1"
										readonly
										class="logistica-readonly-field"
										title="<?php esc_attr_e( 'El dígito EAN no se puede modificar tras crearlo.', 'logistica' ); ?>"
									>
								</td>
								<td>
									<?php logistica_etiqueta_options_icon_actions( 'mercado', ! empty( $entry['can_delete'] ) ); ?>
								</td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			</div>

			<form class="logistica-etiqueta-catalog-options__add" data-etiqueta-options-add="mercado">
				<p class="logistica-etiqueta-catalog-options__add-title"><?php esc_html_e( 'Agregar mercado', 'logistica' ); ?></p>
				<div class="logistica-etiqueta-catalog-options__add-fields">
					<label><?php esc_html_e( 'Clave', 'logistica' ); ?>
						<input type="text" class="logistica-input-uppercase" data-etiqueta-options-add-key placeholder="<?php esc_attr_e( 'EJ. BIO, EU', 'logistica' ); ?>" autocomplete="off" maxlength="12">
					</label>
					<label><?php esc_html_e( 'Etiqueta', 'logistica' ); ?>
						<input type="text" class="logistica-input-sentence-case" data-etiqueta-options-add-label placeholder="<?php esc_attr_e( 'Ej. Bio exportación', 'logistica' ); ?>" autocomplete="off">
					</label>
					<label><?php esc_html_e( 'Dígito EAN (opcional)', 'logistica' ); ?>
						<input type="text" data-etiqueta-options-add-market-digit inputmode="numeric" maxlength="1" placeholder="<?php esc_attr_e( 'Auto', 'logistica' ); ?>" autocomplete="off">
					</label>
					<button type="submit" class="button button-primary logistica-etiqueta-catalog-options__btn-add"><?php esc_html_e( 'Agregar', 'logistica' ); ?></button>
				</div>
			</form>
			<p class="logistica-etiqueta-catalog-options__hint"><?php esc_html_e( 'NAC y EXP son base del EAN-13 (dígitos 1 y 2). Los mercados nuevos usan dígitos 3–9.', 'logistica' ); ?></p>
		</section>

		<section class="logistica-etiqueta-catalog-options__panel" data-etiqueta-options-section="tamanos">
			<header class="logistica-etiqueta-catalog-options__panel-head">
				<h4><?php esc_html_e( 'Tamaños', 'logistica' ); ?></h4>
				<span class="logistica-etiqueta-catalog-options__count"><?php echo esc_html( (string) count( $tamano_entries ) ); ?></span>
			</header>

			<div class="logistica-etiqueta-catalog-options__table-wrap">
				<table class="widefat striped logistica-etiqueta-catalog-options__table">
					<thead>
						<tr>
							<th scope="col"><?php esc_html_e( 'Tamaño', 'logistica' ); ?></th>
							<th class="is-actions-head" scope="col"><?php esc_html_e( 'Acción', 'logistica' ); ?></th>
						</tr>
					</thead>
					<tbody data-etiqueta-options-tamano-rows>
						<?php foreach ( $tamano_entries as $tamano_entry ) : ?>
							<tr data-etiqueta-options-row="tamano" data-key="<?php echo esc_attr( $tamano_entry['label'] ); ?>" data-can-delete="<?php echo ! empty( $tamano_entry['can_delete'] ) ? '1' : '0'; ?>">
								<td>
									<input type="text" class="logistica-input-uppercase" value="<?php echo esc_attr( $tamano_entry['label'] ); ?>" data-etiqueta-options-label autocomplete="off">
								</td>
								<td>
									<?php logistica_etiqueta_options_icon_actions( 'tamano', ! empty( $tamano_entry['can_delete'] ) ); ?>
								</td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			</div>

			<form class="logistica-etiqueta-catalog-options__add" data-etiqueta-options-add="tamano">
				<p class="logistica-etiqueta-catalog-options__add-title"><?php esc_html_e( 'Agregar tamaño', 'logistica' ); ?></p>
				<div class="logistica-etiqueta-catalog-options__add-fields">
					<label><?php esc_html_e( 'Tamaño', 'logistica' ); ?>
						<input type="text" class="logistica-input-uppercase" data-etiqueta-options-add-label placeholder="<?php esc_attr_e( 'EJ. 8 OZ', 'logistica' ); ?>" autocomplete="off">
					</label>
					<button type="submit" class="button button-primary logistica-etiqueta-catalog-options__btn-add"><?php esc_html_e( 'Agregar', 'logistica' ); ?></button>
				</div>
			</form>
		</section>
	</div>

	<section class="logistica-etiqueta-catalog-options__panel logistica-etiqueta-catalog-options__panel--hidden" data-etiqueta-options-section="ocultos">
		<header class="logistica-etiqueta-catalog-options__panel-head">
			<h4><?php esc_html_e( 'Opciones ocultas', 'logistica' ); ?></h4>
			<span class="logistica-etiqueta-catalog-options__count" data-etiqueta-options-hidden-count><?php echo esc_html( (string) count( $hidden_entries ) ); ?></span>
		</header>
		<p class="logistica-etiqueta-catalog-options__hint"><?php esc_html_e( 'Las opciones eliminadas se ocultan de los formularios. Restáuralas aquí cuando las necesites de nuevo.', 'logistica' ); ?></p>

		<p class="logistica-etiqueta-catalog-options__empty" data-etiqueta-options-hidden-empty<?php echo empty( $hidden_entries ) ? '' : ' hidden'; ?>><?php esc_html_e( 'No hay opciones ocultas.', 'logistica' ); ?></p>

		<div class="logistica-etiqueta-catalog-options__table-wrap" data-etiqueta-options-hidden-wrap<?php echo empty( $hidden_entries ) ? ' hidden' : ''; ?>>
			<table class="widefat striped logistica-etiqueta-catalog-options__table logistica-etiqueta-catalog-options__table--hidden">
				<thead>
					<tr>
						<th scope="col"><?php esc_html_e( 'Lista', 'logistica' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Nombre / clave', 'logistica' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Detalle', 'logistica' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Uso catálogo', 'logistica' ); ?></th>
						<th class="is-actions-head" scope="col"><?php esc_html_e( 'Acción', 'logistica' ); ?></th>
					</tr>
				</thead>
				<tbody data-etiqueta-options-hidden-rows>
					<?php foreach ( $hidden_entries as $hidden_entry ) : ?>
						<tr
							data-etiqueta-options-row="<?php echo esc_attr( $hidden_entry['kind'] ); ?>"
							data-key="<?php echo esc_attr( $hidden_entry['key'] ); ?>"
						>
							<td><span class="logistica-etiqueta-catalog-options__kind-badge"><?php echo esc_html( logistica_etiqueta_option_kind_label( $hidden_entry['kind'] ) ); ?></span></td>
							<td><?php echo esc_html( 'mercado' === $hidden_entry['kind'] ? $hidden_entry['key'] . ' · ' . $hidden_entry['label'] : $hidden_entry['label'] ); ?></td>
							<td><?php echo esc_html( (string) ( $hidden_entry['meta'] ?? '' ) ); ?></td>
							<td><?php echo esc_html( (int) ( $hidden_entry['usage'] ?? 0 ) > 0 ? (string) (int) $hidden_entry['usage'] : '—' ); ?></td>
							<td><?php logistica_etiqueta_options_restore_button( $hidden_entry['kind'] ); ?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
	</section>
</div>
