<?php
/**
 * Festivos del checklist (Inicio) — formulario clásico.
 *
 * @package Logistica
 */

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

$panel_holidays = Logistica_Settings::get_panel_holidays();
?>
<section class="logistica-panel logistica-panel-admin-settings">
	<?php if ( isset( $_GET['logistica_status'] ) && 'panel_saved' === $_GET['logistica_status'] ) : ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Configuración de festivos guardada correctamente.', 'logistica' ); ?></p></div>
	<?php endif; ?>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="logistica-panel-compra-statuses-form">
		<input type="hidden" name="action" value="logistica_save_panel_settings">
		<?php wp_nonce_field( 'logistica_save_panel_settings' ); ?>

		<?php
		// Conservar estados de compra actuales al guardar solo festivos.
		foreach ( Logistica_Settings::get_compra_statuses() as $status_key => $status_label ) :
			?>
			<input type="hidden" name="compra_statuses[][key]" value="<?php echo esc_attr( $status_key ); ?>">
			<input type="hidden" name="compra_statuses[][label]" value="<?php echo esc_attr( $status_label ); ?>">
		<?php endforeach; ?>

		<h2><?php esc_html_e( 'Lista chequeo — festivos', 'logistica' ); ?></h2>
		<p><?php esc_html_e( 'Define qué días se marcan automáticamente como «Festivo» en el checklist. Si un usuario cambia un día a «Laborable» en el panel, esa elección prevalece hasta que vuelva a marcarlo como festivo.', 'logistica' ); ?></p>

		<fieldset class="logistica-panel-holidays-options">
			<label>
				<input type="checkbox" name="panel_holidays[auto_sundays]" value="1" <?php checked( ! empty( $panel_holidays['auto_sundays'] ) ); ?>>
				<?php esc_html_e( 'Marcar todos los domingos como festivo', 'logistica' ); ?>
			</label>
		</fieldset>

		<h3><?php esc_html_e( 'Festivos adicionales', 'logistica' ); ?></h3>
		<p class="description"><?php esc_html_e( 'Añade cierres o días especiales de la empresa (paros, jornadas, etc.).', 'logistica' ); ?></p>

		<div class="logistica-table-wrap">
			<table class="widefat striped logistica-panel-holidays-table" data-panel-extra-holidays-table>
				<thead>
					<tr>
						<th scope="col"><?php esc_html_e( 'Fecha', 'logistica' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Descripción (opcional)', 'logistica' ); ?></th>
						<th scope="col" class="logistica-panel-compra-statuses-table__actions"><?php esc_html_e( 'Acciones', 'logistica' ); ?></th>
					</tr>
				</thead>
				<tbody data-panel-extra-holidays-rows>
					<?php foreach ( (array) ( $panel_holidays['extra_holidays'] ?? array() ) as $index => $row ) : ?>
						<tr data-panel-extra-holiday-row>
							<td>
								<input type="date" name="panel_holidays[extra_holidays][<?php echo esc_attr( (string) $index ); ?>][date]" value="<?php echo esc_attr( $row['date'] ?? '' ); ?>" required>
							</td>
							<td>
								<input type="text" class="regular-text" name="panel_holidays[extra_holidays][<?php echo esc_attr( (string) $index ); ?>][label]" value="<?php echo esc_attr( $row['label'] ?? '' ); ?>" placeholder="<?php esc_attr_e( 'Ej. Paro planta', 'logistica' ); ?>">
							</td>
							<td class="logistica-panel-compra-statuses-table__actions">
								<button type="button" class="button button-link-delete" data-panel-extra-holiday-remove><?php esc_html_e( 'Eliminar', 'logistica' ); ?></button>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
		<p class="logistica-panel-compra-statuses-form__tools">
			<button type="button" class="button" data-panel-extra-holiday-add><?php esc_html_e( 'Añadir festivo', 'logistica' ); ?></button>
		</p>

		<p><button type="submit" class="button button-primary"><?php esc_html_e( 'Guardar festivos', 'logistica' ); ?></button></p>
	</form>
</section>

<template id="logistica-panel-extra-holiday-row-template">
	<tr data-panel-extra-holiday-row>
		<td><input type="date" name="panel_holidays[extra_holidays][][date]" value="" required></td>
		<td><input type="text" class="regular-text" name="panel_holidays[extra_holidays][][label]" value="" placeholder="<?php esc_attr_e( 'Ej. Paro planta', 'logistica' ); ?>"></td>
		<td class="logistica-panel-compra-statuses-table__actions">
			<button type="button" class="button button-link-delete" data-panel-extra-holiday-remove><?php esc_html_e( 'Eliminar', 'logistica' ); ?></button>
		</td>
	</tr>
</template>

<script>
(function () {
	'use strict';
	var holidaysTable = document.querySelector('[data-panel-extra-holidays-table]');
	var holidaysTbody = document.querySelector('[data-panel-extra-holidays-rows]');
	var holidayTpl = document.getElementById('logistica-panel-extra-holiday-row-template');
	var addHoliday = document.querySelector('[data-panel-extra-holiday-add]');

	if (addHoliday && holidaysTbody && holidayTpl) {
		addHoliday.addEventListener('click', function () {
			holidaysTbody.appendChild(holidayTpl.content.cloneNode(true));
		});
	}

	if (holidaysTable) {
		holidaysTable.addEventListener('click', function (event) {
			var btn = event.target.closest('[data-panel-extra-holiday-remove]');
			var row;
			if (!btn) {
				return;
			}
			row = btn.closest('[data-panel-extra-holiday-row]');
			if (row) {
				row.remove();
			}
		});
	}
})();
</script>
