<?php
/**
 * Trazabilidad: auditoría, papelera e historial de versiones.
 *
 * @package Logistica
 */

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

$lifecycle_modules = Logistica_Module_Registry::all();
$export_base       = admin_url( 'admin-post.php' );
?>
<section id="logistica-config-trazabilidad" class="logistica-configuracion-card logistica-configuracion-card--lifecycle logistica-configuracion-section" data-lifecycle-board>
	<div class="logistica-configuracion-card__head">
		<h3><?php esc_html_e( 'Trazabilidad del sistema', 'logistica' ); ?></h3>
		<p class="description">
			<?php esc_html_e( 'Consulta auditoría, papelera e historial de versiones. Los registros eliminados se envían a la papelera y pueden restaurarse. Solo administradores pueden eliminar definitivamente.', 'logistica' ); ?>
		</p>
	</div>
	<div class="logistica-configuracion-card__body">
		<nav class="logistica-lifecycle-tabs" aria-label="<?php esc_attr_e( 'Vistas de trazabilidad', 'logistica' ); ?>">
			<button type="button" class="button logistica-lifecycle-tabs__btn is-active" data-lifecycle-scope="audit"><?php esc_html_e( 'Auditoría', 'logistica' ); ?></button>
			<button type="button" class="button logistica-lifecycle-tabs__btn" data-lifecycle-scope="trash"><?php esc_html_e( 'Papelera', 'logistica' ); ?></button>
			<button type="button" class="button logistica-lifecycle-tabs__btn" data-lifecycle-scope="versions"><?php esc_html_e( 'Historial de versiones', 'logistica' ); ?></button>
		</nav>

		<div class="logistica-tapas-excel-wrap logistica-lifecycle-table-wrap">
			<div class="logistica-tapas-filters is-active" data-lifecycle-filters>
				<div class="logistica-tapas-filters__head">
					<?php logistica_table_filters_icon(); ?>
					<p class="logistica-tapas-filters__title"><?php esc_html_e( 'Filtrar tabla', 'logistica' ); ?></p>
					<span class="logistica-tapas-filters__badge" data-lifecycle-filter-badge hidden><?php esc_html_e( 'Activo', 'logistica' ); ?></span>
				</div>
				<div class="logistica-tapas-filters__fields">
					<label class="logistica-tapas-filters__field logistica-tapas-filters__field--search">
						<span class="logistica-tapas-filters__label"><?php esc_html_e( 'Buscar', 'logistica' ); ?></span>
						<input type="search" data-lifecycle-filter-search placeholder="<?php esc_attr_e( 'Módulo, resumen, ID…', 'logistica' ); ?>" autocomplete="off" spellcheck="false">
					</label>
					<label class="logistica-tapas-filters__field">
						<span class="logistica-tapas-filters__label"><?php esc_html_e( 'Módulo', 'logistica' ); ?></span>
						<select data-lifecycle-filter-module>
							<option value=""><?php esc_html_e( 'Todos', 'logistica' ); ?></option>
							<?php foreach ( $lifecycle_modules as $module_key => $module_config ) : ?>
								<option value="<?php echo esc_attr( $module_key ); ?>"><?php echo esc_html( (string) ( $module_config['label'] ?? $module_key ) ); ?></option>
							<?php endforeach; ?>
						</select>
					</label>
					<label class="logistica-tapas-filters__field" data-lifecycle-filter-status-wrap hidden>
						<span class="logistica-tapas-filters__label"><?php esc_html_e( 'Estado', 'logistica' ); ?></span>
						<select data-lifecycle-filter-status>
							<option value="deleted"><?php esc_html_e( 'En papelera', 'logistica' ); ?></option>
							<option value="restored"><?php esc_html_e( 'Restaurados', 'logistica' ); ?></option>
							<option value="all"><?php esc_html_e( 'Todos', 'logistica' ); ?></option>
						</select>
					</label>
					<label class="logistica-tapas-filters__field">
						<span class="logistica-tapas-filters__label"><?php esc_html_e( 'Desde', 'logistica' ); ?></span>
						<input type="date" data-lifecycle-filter-date-from>
					</label>
					<label class="logistica-tapas-filters__field">
						<span class="logistica-tapas-filters__label"><?php esc_html_e( 'Hasta', 'logistica' ); ?></span>
						<input type="date" data-lifecycle-filter-date-to>
					</label>
					<div class="logistica-tapas-filters__actions">
						<button type="button" class="button logistica-tapas-filters__btn" data-action="clear-lifecycle-filters" disabled>
							<?php logistica_table_filters_clear_icon(); ?>
							<?php esc_html_e( 'Limpiar filtros', 'logistica' ); ?>
						</button>
						<a class="button logistica-excel-btn logistica-excel-btn--export" data-lifecycle-export href="<?php echo esc_url( $export_base ); ?>">
							<span class="dashicons dashicons-download" aria-hidden="true"></span>
							<span class="logistica-excel-btn__label"><?php esc_html_e( 'Exportar CSV', 'logistica' ); ?></span>
						</a>
					</div>
				</div>
				<p class="logistica-tapas-filters__meta" data-lifecycle-filter-meta hidden></p>
			</div>

			<div class="logistica-lifecycle-table-scroll" data-lifecycle-excel-scroll tabindex="0">
				<table class="logistica-lifecycle-table">
					<thead data-lifecycle-table-head></thead>
					<tbody data-lifecycle-table-body>
						<tr data-lifecycle-loading-row>
							<td colspan="8"><?php esc_html_e( 'Cargando…', 'logistica' ); ?></td>
						</tr>
					</tbody>
				</table>
			</div>
			<?php logistica_table_pagination( 'lifecycle' ); ?>
		</div>
	</div>
</section>
