<?php
/**
 * Gestión de listas desplegables genéricas (mismo estilo que códigos etiqueta).
 *
 * @package Logistica
 *
 * @var list<string> $catalog_ids
 * @var string       $intro_title
 * @var string       $intro_text
 */

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

if ( ! class_exists( 'Logistica_Roles', false ) || ! Logistica_Roles::user_can_manage_config() ) {
	return;
}

$catalog_ids = isset( $catalog_ids ) && is_array( $catalog_ids ) ? $catalog_ids : array();
$intro_title = isset( $intro_title ) ? (string) $intro_title : __( 'Listas desplegables', 'logistica' );
$intro_text  = isset( $intro_text ) ? (string) $intro_text : __( 'Administra las opciones usadas en los selects del panel operativo. Los cambios se aplican de inmediato.', 'logistica' );

$base_field_defs = class_exists( 'Logistica_Order_Closing', false )
	? Logistica_Order_Closing::base_field_definitions()
	: array();
$base_fields_map = class_exists( 'Logistica_Order_Closing', false )
	? Logistica_Order_Closing::get_base_type_fields_map()
	: array();

$panels = array();
foreach ( $catalog_ids as $catalog_id ) {
	$def = Logistica_Select_Catalog::get_definition( $catalog_id );
	if ( ! $def ) {
		continue;
	}
	$panels[] = array(
		'id'          => $catalog_id,
		'title'       => (string) ( $def['title'] ?? $catalog_id ),
		'description' => (string) ( $def['description'] ?? '' ),
		'mode'        => (string) ( $def['mode'] ?? 'map' ),
		'case'        => (string) ( $def['case'] ?? '' ),
		'fields'      => ! empty( $def['fields'] ),
		'entries'     => Logistica_Select_Catalog::get_admin_entries( $catalog_id ),
	);
}

if ( empty( $panels ) ) {
	return;
}
?>
<div class="logistica-etiqueta-catalog-options logistica-select-catalog-options" data-select-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 echo esc_html( $intro_title ); ?></h3>
			<p><?php echo esc_html( $intro_text ); ?></p>
		</div>
	</div>

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

	<div class="logistica-etiqueta-catalog-options__grid">
		<?php foreach ( $panels as $panel ) : ?>
			<section
				class="logistica-etiqueta-catalog-options__panel<?php echo ! empty( $panel['fields'] ) ? ' has-base-fields' : ''; ?>"
				data-select-catalog-section="<?php echo esc_attr( $panel['id'] ); ?>"
				data-select-catalog-mode="<?php echo esc_attr( $panel['mode'] ); ?>"
				data-select-catalog-case="<?php echo esc_attr( $panel['case'] ); ?>"
				<?php echo ! empty( $panel['fields'] ) ? 'data-select-catalog-fields="1"' : ''; ?>
			>
				<header class="logistica-etiqueta-catalog-options__panel-head">
					<h4><?php echo esc_html( $panel['title'] ); ?></h4>
					<span class="logistica-etiqueta-catalog-options__count"><?php echo esc_html( (string) count( $panel['entries'] ) ); ?></span>
				</header>

				<?php if ( '' !== $panel['description'] ) : ?>
					<p class="logistica-etiqueta-catalog-options__hint"><?php echo esc_html( $panel['description'] ); ?></p>
				<?php endif; ?>

				<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>
								<?php if ( 'map' === $panel['mode'] ) : ?>
									<th scope="col"><?php esc_html_e( 'Clave', 'logistica' ); ?></th>
								<?php endif; ?>
								<th class="is-actions-head" scope="col"><?php esc_html_e( 'Acción', 'logistica' ); ?></th>
							</tr>
						</thead>
						<tbody data-select-catalog-rows>
							<?php foreach ( $panel['entries'] as $entry ) : ?>
								<?php
								$entry_key    = (string) $entry['key'];
								$entry_fields = isset( $base_fields_map[ $entry_key ] ) ? $base_fields_map[ $entry_key ] : array();
								?>
								<tr
									data-select-catalog-row
									data-key="<?php echo esc_attr( '' === $entry_key ? '__empty__' : $entry_key ); ?>"
									data-can-delete="<?php echo ! empty( $entry['can_delete'] ) ? '1' : '0'; ?>"
								>
									<td>
										<input
											type="text"
											class="<?php echo 'upper' === $panel['case'] ? 'logistica-input-uppercase' : 'logistica-input-sentence-case'; ?>"
											value="<?php echo esc_attr( $entry['label'] ); ?>"
											data-select-catalog-label
											autocomplete="off"
										>
										<?php if ( ! empty( $panel['fields'] ) && ! empty( $base_field_defs ) ) : ?>
											<details class="logistica-base-type-fields" data-base-type-fields>
												<summary><?php esc_html_e( 'Campos del tipo', 'logistica' ); ?></summary>
												<p class="logistica-base-type-fields__note"><?php esc_html_e( 'Código, lote y ajuste siempre se muestran.', 'logistica' ); ?></p>
												<div class="logistica-base-type-fields__grid">
													<?php foreach ( $base_field_defs as $field_key => $field_meta ) : ?>
														<label class="logistica-base-type-fields__item">
															<input
																type="checkbox"
																value="<?php echo esc_attr( $field_key ); ?>"
																data-base-type-field
																<?php checked( in_array( $field_key, $entry_fields, true ) ); ?>
															>
															<span><?php echo esc_html( (string) ( $field_meta['label'] ?? $field_key ) ); ?></span>
														</label>
													<?php endforeach; ?>
												</div>
											</details>
										<?php endif; ?>
									</td>
									<?php if ( 'map' === $panel['mode'] ) : ?>
										<td>
											<span class="logistica-etiqueta-catalog-options__key-badge"><?php echo esc_html( '' === $entry_key ? '—' : $entry_key ); ?></span>
										</td>
									<?php endif; ?>
									<td>
										<div class="logistica-etiqueta-catalog-options__actions">
											<button type="button" class="button button-small logistica-table-icon-btn logistica-etiqueta-catalog-options__btn-save" data-select-catalog-save aria-label="<?php esc_attr_e( 'Guardar', 'logistica' ); ?>" title="<?php esc_attr_e( 'Guardar', 'logistica' ); ?>">
												<span class="dashicons dashicons-saved" aria-hidden="true"></span>
											</button>
											<button type="button" class="button button-small logistica-table-icon-btn logistica-etiqueta-catalog-options__btn-delete" data-select-catalog-delete aria-label="<?php esc_attr_e( 'Eliminar', 'logistica' ); ?>" title="<?php esc_attr_e( 'Eliminar', 'logistica' ); ?>">
												<span class="dashicons dashicons-trash" aria-hidden="true"></span>
											</button>
										</div>
									</td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				</div>

				<form class="logistica-etiqueta-catalog-options__add" data-select-catalog-add>
					<p class="logistica-etiqueta-catalog-options__add-title"><?php esc_html_e( 'Agregar opción', 'logistica' ); ?></p>
					<div class="logistica-etiqueta-catalog-options__add-fields">
						<label><?php esc_html_e( 'Nombre', 'logistica' ); ?>
							<input
								type="text"
								class="<?php echo 'upper' === $panel['case'] ? 'logistica-input-uppercase' : 'logistica-input-sentence-case'; ?>"
								data-select-catalog-add-label
								placeholder="<?php esc_attr_e( 'Ej. Nueva opción', '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>
		<?php endforeach; ?>
	</div>
</div>
