<?php
/**
 * Tabla: fabricacion poliestireno en rollos (35 kg) para tapas.
 *
 * @package Logistica
 */

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

/**
 * @return array<string, string>
 */
function logistica_tapas_status_options() {
	if ( class_exists( 'Logistica_Select_Catalog', false ) ) {
		$options = Logistica_Select_Catalog::get_options( 'tapas_status' );
		if ( is_array( $options ) && ! empty( $options ) ) {
			return $options;
		}
	}

	return array(
		'pendiente'   => __( 'Pendiente', 'logistica' ),
		'fabricacion' => __( 'Fabricación', 'logistica' ),
		'formado'     => __( 'Formado', 'logistica' ),
		'finalizado'  => __( 'Finalizado', 'logistica' ),
		'cancelado'   => __( 'Cancelado', 'logistica' ),
		'otro'        => __( 'Otro...', 'logistica' ),
	);
}

/**
 * Cabecera de tabla Excel con salto de linea opcional.
 *
 * @param string|array<int, string> $lines Lineas del titulo.
 * @param string                    $class Clase de columna.
 */
function logistica_tapas_excel_th( $lines, $class = '' ) {
	if ( is_string( $lines ) ) {
		$lines = array( $lines );
	}

	$class_attr = $class ? ' class="' . esc_attr( $class ) . '"' : '';
	?>
	<th scope="col"<?php echo $class_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
		<span class="logistica-tapas-excel-th__label">
			<?php
			foreach ( $lines as $index => $line ) {
				if ( $index > 0 ) {
					echo '<br />';
				}
				echo esc_html( $line );
			}
			?>
		</span>
	</th>
	<?php
}

/**
 * Cabecera agrupada de tabla Excel.
 *
 * @param string $label   Titulo del grupo.
 * @param int    $colspan Columnas que abarca.
 * @param string $class   Clases CSS.
 */
function logistica_tapas_excel_th_group( $label, $colspan, $class = '' ) {
	$class_attr = $class ? ' class="' . esc_attr( $class ) . '"' : '';
	?>
	<th scope="colgroup" colspan="<?php echo (int) $colspan; ?>"<?php echo $class_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
		<?php echo esc_html( $label ); ?>
	</th>
	<?php
}

/**
 * @param string               $label Etiqueta visible.
 * @param string               $field Nombre del campo data-field.
 * @param string|int|float     $value Valor inicial.
 * @param array<string, mixed> $args  Atributos extra.
 */
function logistica_tapas_field_input( $label, $field, $value, $args = array() ) {
	$type       = isset( $args['type'] ) ? (string) $args['type'] : 'text';
	$extra      = isset( $args['attrs'] ) ? (string) $args['attrs'] : '';
	$list       = isset( $args['list'] ) ? (string) $args['list'] : '';
	$classes    = isset( $args['class'] ) ? (string) $args['class'] : '';
	$list_attr  = $list ? ' list="' . esc_attr( $list ) . '"' : '';
	$class_attr = $classes ? ' class="' . esc_attr( $classes ) . '"' : '';
	?>
	<label class="logistica-tapas-field is-editable">
		<span class="logistica-tapas-field__label"><?php echo esc_html( $label ); ?></span>
		<input
			type="<?php echo esc_attr( $type ); ?>"
			data-field="<?php echo esc_attr( $field ); ?>"
			value="<?php echo esc_attr( (string) $value ); ?>"
			<?php echo $extra; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			<?php echo $list_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			<?php echo $class_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
		/>
	</label>
	<?php
}

/**
 * @param string               $label Etiqueta.
 * @param string               $field Campo calculado.
 * @param int|false            $decimals Decimales.
 * @param array<string, mixed> $args Clases extra.
 */
function logistica_tapas_field_output( $label, $field, $decimals = 2, $args = array() ) {
	$classes    = isset( $args['class'] ) ? (string) $args['class'] : '';
	$class_attr = $classes ? ' ' . esc_attr( $classes ) : '';
	?>
	<div class="logistica-tapas-field is-auto<?php echo $class_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>" data-output-wrap="<?php echo esc_attr( $field ); ?>">
		<span class="logistica-tapas-field__label"><?php echo esc_html( $label ); ?></span>
		<strong class="logistica-tapas-field__value" data-output="<?php echo esc_attr( $field ); ?>" data-decimals="<?php echo esc_attr( (string) $decimals ); ?>"></strong>
	</div>
	<?php
}

/**
 * @param array<string, mixed> $row Datos del registro.
 */
function logistica_render_tapas_record( $row = array() ) {
	$defaults = array(
		'status'       => 'fabricacion',
		'status_other' => '',
		'product'      => '',
		'color'        => '',
		'pedido'       => 0,
		'stock'        => 0,
		'calibre'      => 20,
		'ancho'        => 0,
		'largo'        => 0,
		'und_molde'    => 12,
		'peso_gr'      => 0,
		'resina_pct'   => 40,
		'retal_sent'   => 0,
		'resina_sent'  => 0,
		'material_llega' => 0,
		'precio_kg'      => 0,
	);
	if ( ! isset( $row['precio_kg'] ) && isset( $row['precio_fabricacion'] ) ) {
		$row['precio_kg'] = $row['precio_fabricacion'];
	}
	$row            = wp_parse_args( $row, $defaults );
	$status_options = logistica_tapas_status_options();
	$status         = (string) $row['status'];
	$status_other   = (string) $row['status_other'];
	$is_known       = array_key_exists( $status, $status_options );
	$select_status  = $is_known ? $status : 'otro';

	if ( ! $is_known && '' !== $status ) {
		$status_other = $status;
	}

	$close_label  = esc_attr__( 'Cerrar', 'logistica' );
	$remove_label = esc_attr__( 'Eliminar', 'logistica' );
	$dup_label    = esc_attr__( 'Duplicar referencia', 'logistica' );
	?>
	<article class="logistica-tapas-card" data-tapas-record>
		<header class="logistica-tapas-card__head">
			<div class="logistica-tapas-card__status-wrap">
				<label class="logistica-tapas-field is-editable logistica-tapas-field--status">
					<span class="logistica-tapas-field__label"><?php esc_html_e( 'Estado', 'logistica' ); ?></span>
					<select data-field="status" class="logistica-tapas-status-select">
						<?php foreach ( $status_options as $value => $label ) : ?>
							<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $select_status, $value ); ?>><?php echo esc_html( $label ); ?></option>
						<?php endforeach; ?>
					</select>
				</label>
				<label class="logistica-tapas-field is-editable logistica-tapas-field--status-other<?php echo 'otro' === $select_status ? '' : ' is-hidden'; ?>">
					<span class="logistica-tapas-field__label"><?php esc_html_e( 'Estado personalizado', 'logistica' ); ?></span>
					<input
						type="text"
						data-field="status_other"
						value="<?php echo esc_attr( $status_other ); ?>"
						placeholder="<?php esc_attr_e( 'Escribe el estado', 'logistica' ); ?>"
					/>
				</label>
			</div>
			<div class="logistica-tapas-card__identity">
				<?php
				logistica_tapas_field_input(
					__( 'Producto', 'logistica' ),
					'product',
					$row['product']
				);
				logistica_tapas_field_input(
					__( 'Color', 'logistica' ),
					'color',
					$row['color']
				);
				?>
			</div>
			<div class="logistica-tapas-card__actions">
				<button type="button" class="logistica-table-icon-btn logistica-tapas-icon-btn" data-action="duplicate-tapas-row" aria-label="<?php echo esc_attr( $dup_label ); ?>" title="<?php echo esc_attr( $dup_label ); ?>">
					<span class="logistica-tapas-icon logistica-tapas-icon--duplicate" aria-hidden="true"></span>
				</button>
				<button type="button" class="logistica-table-icon-btn logistica-tapas-icon-btn" data-action="close-tapas-row" aria-label="<?php echo esc_attr( $close_label ); ?>" title="<?php echo esc_attr( $close_label ); ?>">
					<span class="logistica-tapas-icon logistica-tapas-icon--close" aria-hidden="true"></span>
				</button>
				<button type="button" class="logistica-table-icon-btn logistica-tapas-icon-btn logistica-tapas-icon-btn--danger" data-action="remove-tapas-row" aria-label="<?php echo esc_attr( $remove_label ); ?>" title="<?php echo esc_attr( $remove_label ); ?>">
					<span class="logistica-tapas-icon logistica-tapas-icon--delete" aria-hidden="true"></span>
				</button>
			</div>
		</header>

		<div class="logistica-tapas-card__body">
			<section class="logistica-tapas-section">
				<h2 class="logistica-tapas-section__title"><?php esc_html_e( 'Pedido y producción', 'logistica' ); ?></h2>
				<div class="logistica-tapas-grid logistica-tapas-grid--3">
					<?php
					logistica_tapas_field_input( __( 'Pedido (und)', 'logistica' ), 'pedido', $row['pedido'], array( 'type' => 'number', 'attrs' => 'step="1" min="0"' ) );
					logistica_tapas_field_input( __( 'Stock (und)', 'logistica' ), 'stock', $row['stock'], array( 'type' => 'number', 'attrs' => 'step="1" min="0"' ) );
					logistica_tapas_field_output( __( 'Fabricar (und)', 'logistica' ), 'fabricar', 0 );
					?>
				</div>
			</section>

			<section class="logistica-tapas-section">
				<h2 class="logistica-tapas-section__title"><?php esc_html_e( 'Molde y material', 'logistica' ); ?></h2>
				<div class="logistica-tapas-grid logistica-tapas-grid--3">
					<?php
					logistica_tapas_field_input( __( 'Calibre (milésimas)', 'logistica' ), 'calibre', $row['calibre'], array( 'type' => 'number', 'attrs' => 'step="0.1" min="0"' ) );
					logistica_tapas_field_input( __( 'Ancho (cm)', 'logistica' ), 'ancho', $row['ancho'], array( 'type' => 'number', 'attrs' => 'step="0.01" min="0"' ) );
					logistica_tapas_field_input( __( 'Largo (cm)', 'logistica' ), 'largo', $row['largo'], array( 'type' => 'number', 'attrs' => 'step="0.01" min="0"' ) );
					logistica_tapas_field_input( __( 'Und x molde (und)', 'logistica' ), 'und_molde', $row['und_molde'], array( 'type' => 'number', 'attrs' => 'step="1" min="1"' ) );
					logistica_tapas_field_input( __( 'Peso tamaño (gr)', 'logistica' ), 'peso_gr', $row['peso_gr'], array( 'type' => 'number', 'attrs' => 'step="0.01" min="0"' ) );
					logistica_tapas_field_input( __( '% Resina', 'logistica' ), 'resina_pct', $row['resina_pct'], array( 'type' => 'number', 'attrs' => 'step="1" min="0" max="100"' ) );
					?>
				</div>
			</section>

			<section class="logistica-tapas-section logistica-tapas-section--results">
				<h2 class="logistica-tapas-section__title"><?php esc_html_e( 'Resultados (kg)', 'logistica' ); ?></h2>
				<div class="logistica-tapas-results-groups">
					<div class="logistica-tapas-results-group logistica-tapas-results-group--base">
						<h6 class="logistica-tapas-results-group__title"><?php esc_html_e( 'Peso calculado (sin merma)', 'logistica' ); ?></h6>
						<div class="logistica-tapas-grid logistica-tapas-grid--3">
							<?php
							logistica_tapas_field_output( __( 'Resina (kg)', 'logistica' ), 'resina_kg', 2, array( 'class' => 'is-base-kg' ) );
							logistica_tapas_field_output( __( 'Retal (kg)', 'logistica' ), 'retal_kg', 2, array( 'class' => 'is-base-kg' ) );
							logistica_tapas_field_output( __( 'Total (kg)', 'logistica' ), 'total_kg', 2, array( 'class' => 'is-base-kg is-total' ) );
							?>
						</div>
					</div>
					<div class="logistica-tapas-results-group logistica-tapas-results-group--envio">
						<h6 class="logistica-tapas-results-group__title"><?php esc_html_e( 'Cantidad a enviar (con merma)', 'logistica' ); ?></h6>
						<div class="logistica-tapas-grid logistica-tapas-grid--3">
							<?php
							logistica_tapas_field_output( __( 'Resina + merma', 'logistica' ), 'resina_merma', 2, array( 'class' => 'is-envio is-envio-resina' ) );
							logistica_tapas_field_output( __( 'Retal + merma', 'logistica' ), 'retal_merma', 2, array( 'class' => 'is-envio is-envio-retal' ) );
							logistica_tapas_field_output( __( 'Total c/merma', 'logistica' ), 'total_merma', 2, array( 'class' => 'is-envio is-envio-total is-total' ) );
							?>
						</div>
					</div>
					<div class="logistica-tapas-results-group logistica-tapas-results-group--rollos">
						<h6 class="logistica-tapas-results-group__title"><?php esc_html_e( 'Resina y rollos', 'logistica' ); ?></h6>
						<div class="logistica-tapas-grid logistica-tapas-grid--2">
							<?php
							logistica_tapas_field_output( __( 'Mult. 25 resina', 'logistica' ), 'resina_multiplo', 0 );
							logistica_tapas_field_output( __( 'Rollos', 'logistica' ), 'rollos', 0 );
							?>
						</div>
					</div>
				</div>
			</section>

			<section class="logistica-tapas-section logistica-tapas-section--control">
				<h2 class="logistica-tapas-section__title"><?php esc_html_e( 'Control de materiales y servicio', 'logistica' ); ?></h2>
				<div class="logistica-tapas-control-groups">
					<div class="logistica-tapas-control-group">
						<h6 class="logistica-tapas-control-group__title"><?php esc_html_e( 'Material enviado', 'logistica' ); ?></h6>
						<div class="logistica-tapas-grid logistica-tapas-grid--3">
							<?php
							logistica_tapas_field_input( __( 'Retal enviado (kg)', 'logistica' ), 'retal_sent', $row['retal_sent'], array( 'type' => 'number', 'attrs' => 'step="0.01" min="0"' ) );
							logistica_tapas_field_input( __( 'Resina enviada (kg)', 'logistica' ), 'resina_sent', $row['resina_sent'], array( 'type' => 'number', 'attrs' => 'step="0.01" min="0"' ) );
							logistica_tapas_field_input( __( 'Material que llega (kg)', 'logistica' ), 'material_llega', $row['material_llega'], array( 'type' => 'number', 'attrs' => 'step="0.01" min="0"' ) );
							?>
						</div>
					</div>
					<div class="logistica-tapas-control-group logistica-tapas-control-group--due">
						<h6 class="logistica-tapas-control-group__title"><?php esc_html_e( 'Falta por enviar', 'logistica' ); ?></h6>
						<div class="logistica-tapas-grid logistica-tapas-grid--3">
							<?php
							logistica_tapas_field_output( __( 'Retal debiendo (kg)', 'logistica' ), 'retal_due', 2 );
							logistica_tapas_field_output( __( 'Resina debiendo (kg)', 'logistica' ), 'resina_due', 2 );
							logistica_tapas_field_output( __( 'Total debiendo (kg)', 'logistica' ), 'total_due', 2, array( 'class' => 'is-total' ) );
							?>
						</div>
					</div>
					<div class="logistica-tapas-control-group logistica-tapas-control-group--service">
						<h6 class="logistica-tapas-control-group__title"><?php esc_html_e( 'Servicio de fabricación', 'logistica' ); ?></h6>
						<div class="logistica-tapas-grid logistica-tapas-grid--2">
							<?php
							logistica_tapas_field_input( __( 'Precio fabricación (COP/kg)', 'logistica' ), 'precio_kg', $row['precio_kg'], array( 'type' => 'text', 'class' => 'logistica-money-field', 'attrs' => 'inputmode="decimal" autocomplete="off"' ) );
							logistica_tapas_field_output( __( 'Costo servicio (COP)', 'logistica' ), 'service_total', 0, array( 'class' => 'is-total' ) );
							?>
						</div>
					</div>
				</div>
			</section>
		</div>
	</article>
	<?php
}

$default_rows = array(
	array( 'product' => '16oz', 'color' => 'Negro', 'pedido' => 90000, 'stock' => 10000, 'calibre' => 20, 'ancho' => 43, 'largo' => 34, 'und_molde' => 8, 'peso_gr' => 76, 'resina_pct' => 60 ),
	array( 'product' => '9oz y 12oz', 'color' => 'Amarillo', 'pedido' => 50000, 'stock' => 0, 'calibre' => 20, 'ancho' => 39.5, 'largo' => 29.5, 'und_molde' => 12, 'peso_gr' => 61, 'resina_pct' => 60 ),
	array( 'product' => '7oz', 'color' => 'Blanco', 'pedido' => 120000, 'stock' => 0, 'calibre' => 18, 'ancho' => 36, 'largo' => 27, 'und_molde' => 12, 'peso_gr' => 47, 'resina_pct' => 40 ),
	array( 'product' => '9oz y 12oz', 'color' => 'blanco cal. 20', 'pedido' => 50000, 'stock' => 0, 'calibre' => 20, 'ancho' => 39.5, 'largo' => 29.5, 'und_molde' => 12, 'peso_gr' => 61, 'resina_pct' => 40 ),
	array( 'product' => '9oz y 12oz', 'color' => 'negro cal. 20', 'pedido' => 90000, 'stock' => 0, 'calibre' => 20, 'ancho' => 39.5, 'largo' => 29.5, 'und_molde' => 12, 'peso_gr' => 61, 'resina_pct' => 40 ),
	array( 'product' => '9oz y 12oz', 'color' => 'magenta cal. 20', 'pedido' => 56000, 'stock' => 0, 'calibre' => 20, 'ancho' => 39.5, 'largo' => 29.5, 'und_molde' => 12, 'peso_gr' => 61, 'resina_pct' => 40 ),
	array( 'product' => '9oz y 12oz', 'color' => 'blanco cal. 20', 'pedido' => 150000, 'stock' => 0, 'calibre' => 20, 'ancho' => 39.5, 'largo' => 29.5, 'und_molde' => 12, 'peso_gr' => 61, 'resina_pct' => 40 ),
	array( 'product' => '9oz y 12oz', 'color' => 'negro cal. 20', 'pedido' => 130000, 'stock' => 0, 'calibre' => 20, 'ancho' => 39.5, 'largo' => 29.5, 'und_molde' => 12, 'peso_gr' => 61, 'resina_pct' => 40 ),
);
?>
<section class="logistica-conversion-board logistica-tapas-board">
	<?php if ( ! logistica_is_tabbed_shell() ) : ?>
		<?php logistica_tab_loading_overlay( logistica_tab_loading_message( 'logistica-calculo-tapas' ) ); ?>
	<?php endif; ?>
	<div class="logistica-close-toolbar logistica-conversion-intro">
		<?php
		logistica_page_toolbar_intro(
			__( 'Poliestireno en rollos para tapas', 'logistica' ),
			__( 'Calcula fabricación de tapas en poliestireno, consumo de resina y retal, merma, redondeos y resumen editable por referencia.', 'logistica' ),
			'dashicons-forms'
		);
		?>
	</div>

	<div class="logistica-close-legend logistica-conversion-legend">
		<span class="is-editable"><?php esc_html_e( 'Editable', 'logistica' ); ?></span>
		<span class="is-auto"><?php esc_html_e( 'Calculado', 'logistica' ); ?></span>
	</div>

	<div class="logistica-sheet logistica-sheet--tapas" data-sheet="tapas-poliestireno">
		<div class="logistica-tapas-settings">
			<label>
				<?php esc_html_e( 'Peso rollo resina (kg)', 'logistica' ); ?>
				<input type="number" step="0.1" min="0" data-sheet-setting="roll_kg" value="35" />
			</label>
			<label>
				<?php esc_html_e( 'Merma resina/retal (%)', 'logistica' ); ?>
				<input type="number" step="0.1" min="0" data-sheet-setting="waste_pct" value="5" />
			</label>
			<label>
				<?php esc_html_e( 'Múltiplo redondeo resina (kg)', 'logistica' ); ?>
				<input type="number" step="1" min="1" data-sheet-setting="resin_step" value="25" />
			</label>
		</div>

		<div class="logistica-tapas-excel-wrap">
			<h5 class="logistica-tapas-section__title"><?php esc_html_e( 'Vista hoja de cálculo (Excel)', 'logistica' ); ?></h5>
			<?php logistica_excel_table_zoom_toolbar(); ?>
			<p class="logistica-tapas-excel__intro"><?php esc_html_e( 'Vista tipo Excel. Pulsa el icono de editar en cada fila para modificar solo esa referencia.', 'logistica' ); ?></p>
			<div class="logistica-tapas-filters" data-tapas-filters>
				<div class="logistica-tapas-filters__head">
					<span class="logistica-tapas-filters__icon" aria-hidden="true">
						<svg width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
							<path d="M4 5h16l-6.2 7.35v5.9L10 20v-7.65L4 5z" stroke="currentColor" stroke-width="1.8" stroke-linejoin="round"/>
						</svg>
					</span>
					<p class="logistica-tapas-filters__title"><?php esc_html_e( 'Filtrar tabla', 'logistica' ); ?></p>
					<span class="logistica-tapas-filters__badge" data-tapas-filter-badge hidden><?php esc_html_e( 'Activo', 'logistica' ); ?></span>
				</div>
				<div class="logistica-tapas-filters__fields">
					<label class="logistica-tapas-filters__field">
						<span class="logistica-tapas-filters__label"><?php esc_html_e( 'Estado', 'logistica' ); ?></span>
						<select data-tapas-filter="status">
							<option value=""><?php esc_html_e( 'Todos', 'logistica' ); ?></option>
							<?php foreach ( logistica_tapas_status_options() as $status_key => $status_label ) : ?>
								<option value="<?php echo esc_attr( $status_key ); ?>"><?php echo esc_html( $status_label ); ?></option>
							<?php endforeach; ?>
						</select>
					</label>
					<label class="logistica-tapas-filters__field">
						<span class="logistica-tapas-filters__label"><?php esc_html_e( 'Producto', 'logistica' ); ?></span>
						<select data-tapas-filter="product">
							<option value=""><?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( 'Color', 'logistica' ); ?></span>
						<select data-tapas-filter="color">
							<option value=""><?php esc_html_e( 'Todos', 'logistica' ); ?></option>
						</select>
					</label>
					<div class="logistica-tapas-filters__actions">
						<button type="button" class="button logistica-tapas-filters__btn" data-action="clear-tapas-filters">
							<span class="logistica-tapas-filters__btn-icon" aria-hidden="true">
								<svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
									<path d="M6 6l12 12M18 6L6 18" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
								</svg>
							</span>
							<?php esc_html_e( 'Limpiar filtros', 'logistica' ); ?>
						</button>
					</div>
				</div>
				<p class="logistica-tapas-filters__meta" data-tapas-filter-meta hidden></p>
			</div>
			<div class="logistica-tapas-excel-toolbar">
				<button type="button" class="button logistica-excel-btn logistica-excel-btn--export" data-action="export-tapas-excel">
					<span class="dashicons dashicons-media-spreadsheet" aria-hidden="true"></span>
					<span class="logistica-excel-btn__label"><?php esc_html_e( 'Exportar Excel (.xlsx)', 'logistica' ); ?></span>
				</button>
				<button type="button" class="button logistica-excel-btn logistica-excel-btn--import" data-action="import-tapas-excel">
					<span class="dashicons dashicons-upload" aria-hidden="true"></span>
					<span class="logistica-excel-btn__label"><?php esc_html_e( 'Importar Excel', 'logistica' ); ?></span>
				</button>
				<input
					type="file"
					data-tapas-import-file
					accept=".xls,.xlsx,.csv,text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
					hidden
				/>
			</div>
			<ul class="logistica-tapas-status-legend" aria-hidden="true">
				<li><span class="logistica-tapas-status-legend__swatch is-row-status-pendiente"></span><?php esc_html_e( 'Pendiente', 'logistica' ); ?></li>
				<li><span class="logistica-tapas-status-legend__swatch is-row-status-fabricacion"></span><?php esc_html_e( 'Fabricación', 'logistica' ); ?></li>
				<li><span class="logistica-tapas-status-legend__swatch is-row-status-formado"></span><?php esc_html_e( 'Formado', 'logistica' ); ?></li>
				<li><span class="logistica-tapas-status-legend__swatch is-row-status-finalizado"></span><?php esc_html_e( 'Finalizado', 'logistica' ); ?></li>
				<li><span class="logistica-tapas-status-legend__swatch is-row-status-cancelado"></span><?php esc_html_e( 'Cancelado', 'logistica' ); ?></li>
				<li><span class="logistica-tapas-status-legend__swatch is-row-status-otro"></span><?php esc_html_e( 'Otro', 'logistica' ); ?></li>
			</ul>
			<ul class="logistica-tapas-kg-legend" aria-hidden="true">
				<li><span class="logistica-tapas-kg-legend__swatch is-col-base-kg"></span><?php esc_html_e( 'Peso calculado (sin merma)', 'logistica' ); ?></li>
				<li><span class="logistica-tapas-kg-legend__swatch is-col-envio-resina"></span><?php esc_html_e( 'Resina + merma (a enviar)', 'logistica' ); ?></li>
				<li><span class="logistica-tapas-kg-legend__swatch is-col-envio-retal"></span><?php esc_html_e( 'Retal + merma (a enviar)', 'logistica' ); ?></li>
				<li><span class="logistica-tapas-kg-legend__swatch is-col-envio-total"></span><?php esc_html_e( 'Total c/merma (a enviar)', 'logistica' ); ?></li>
				<li><span class="logistica-tapas-status-legend__swatch is-calc-missing"></span><?php esc_html_e( 'Faltan datos', 'logistica' ); ?></li>
			</ul>
			<?php logistica_table_sheet_actions( __( 'Añadir referencia', 'logistica' ), 'add-tapas-row' ); ?>

			<div id="logistica-tapas-cards-panel" class="logistica-tapas-cards-panel logistica-tapas-inline-form-slot" data-tapas-cards-panel hidden>
				<h2 class="logistica-tapas-section__title logistica-tapas-cards-title" hidden><?php esc_html_e( 'Todas las referencias', 'logistica' ); ?></h2>

				<div class="logistica-tapas-list" data-tapas-rows>
					<?php foreach ( $default_rows as $row ) : ?>
						<?php logistica_render_tapas_record( $row ); ?>
					<?php endforeach; ?>
				</div>
				<div class="logistica-tapas-form-actions" data-tapas-form-actions hidden>
					<button type="button" class="button button-primary logistica-calc-form-btn logistica-calc-form-btn--confirm" data-action="confirm-tapas-form">
						<span class="dashicons dashicons-saved" aria-hidden="true"></span>
						<?php esc_html_e( 'Agregar referencia', 'logistica' ); ?>
					</button>
					<button type="button" class="button logistica-calc-form-btn logistica-calc-form-btn--cancel" data-action="cancel-tapas-form">
						<span class="dashicons dashicons-no-alt" aria-hidden="true"></span>
						<?php esc_html_e( 'Cancelar', 'logistica' ); ?>
					</button>
				</div>
			</div>

			<div class="logistica-tapas-scroll-host">
				<div class="logistica-sheet-scroll logistica-tapas-excel-scroll" data-tapas-excel-scroll tabindex="0">
				<table class="logistica-tapas-excel-table">
					<colgroup>
						<col class="logistica-tapas-col logistica-tapas-col--validacion" />
						<col class="logistica-tapas-col logistica-tapas-col--estado" />
						<col class="logistica-tapas-col logistica-tapas-col--producto" />
						<col class="logistica-tapas-col logistica-tapas-col--color" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num-sm" />
						<col class="logistica-tapas-col logistica-tapas-col--num-sm" />
						<col class="logistica-tapas-col logistica-tapas-col--num-sm" />
						<col class="logistica-tapas-col logistica-tapas-col--num-sm" />
						<col class="logistica-tapas-col logistica-tapas-col--num-sm" />
						<col class="logistica-tapas-col logistica-tapas-col--num-sm" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--num" />
						<col class="logistica-tapas-col logistica-tapas-col--actions" />
					</colgroup>
					<thead>
						<tr class="logistica-tapas-excel-groups">
							<?php logistica_tapas_excel_th_group( __( 'Validación', 'logistica' ), 1, 'is-col-validacion-group' ); ?>
							<?php logistica_tapas_excel_th_group( __( 'Referencia', 'logistica' ), 3 ); ?>
							<?php logistica_tapas_excel_th_group( __( 'Producción', 'logistica' ), 3 ); ?>
							<?php logistica_tapas_excel_th_group( __( 'Medidas', 'logistica' ), 6 ); ?>
							<?php logistica_tapas_excel_th_group( __( 'Peso calculado (kg)', 'logistica' ), 3, 'is-col-base-kg-group' ); ?>
							<?php logistica_tapas_excel_th_group( __( 'Cantidad a enviar (kg)', 'logistica' ), 3, 'is-col-envio-group' ); ?>
							<?php logistica_tapas_excel_th_group( __( 'Resina / rollos', 'logistica' ), 2 ); ?>
							<?php logistica_tapas_excel_th_group( __( 'Control', 'logistica' ), 8, 'is-col-control-group' ); ?>
							<?php logistica_tapas_excel_th_group( '', 1, 'is-actions-head' ); ?>
						</tr>
						<tr>
							<?php logistica_tapas_excel_th( __( 'Validación', 'logistica' ), 'logistica-tapas-col--validacion' ); ?>
							<?php logistica_tapas_excel_th( __( 'Estado', 'logistica' ), 'logistica-tapas-col--estado' ); ?>
							<?php logistica_tapas_excel_th( __( 'Producto', 'logistica' ), 'logistica-tapas-col--producto' ); ?>
							<?php logistica_tapas_excel_th( __( 'Color', 'logistica' ), 'logistica-tapas-col--color' ); ?>
							<?php logistica_tapas_excel_th( __( 'Pedido (und)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Stock (und)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Fabricar (und)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( array( __( 'Calibre', 'logistica' ), __( '(milésimas)', 'logistica' ) ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Ancho (cm)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Largo (cm)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Und x molde (und)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Peso tamaño (gr)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( '% Resina', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Resina (kg)', 'logistica' ), 'is-col-base-kg is-col-base-kg--resina' ); ?>
							<?php logistica_tapas_excel_th( __( 'Retal (kg)', 'logistica' ), 'is-col-base-kg is-col-base-kg--retal' ); ?>
							<?php logistica_tapas_excel_th( __( 'Total (kg)', 'logistica' ), 'is-col-base-kg is-col-base-kg--total' ); ?>
							<?php logistica_tapas_excel_th( array( __( 'Resina +', 'logistica' ), __( 'merma', 'logistica' ) ), 'is-col-envio is-col-envio-resina' ); ?>
							<?php logistica_tapas_excel_th( array( __( 'Retal +', 'logistica' ), __( 'merma', 'logistica' ) ), 'is-col-envio is-col-envio-retal' ); ?>
							<?php logistica_tapas_excel_th( array( __( 'Total', 'logistica' ), __( 'c/merma', 'logistica' ) ), 'is-col-envio is-col-envio-total' ); ?>
							<?php logistica_tapas_excel_th( array( __( 'Mult.', 'logistica' ), __( '25 resina', 'logistica' ) ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Rollos', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Retal enviado (kg)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Resina enviada (kg)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Material que llega (kg)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Retal debiendo (kg)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Resina debiendo (kg)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Total debiendo (kg)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Precio fabricación (COP/kg)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Costo servicio (COP)', 'logistica' ) ); ?>
							<?php logistica_tapas_excel_th( __( 'Acción', 'logistica' ), 'is-actions-head' ); ?>
						</tr>
					</thead>
					<tbody data-tapas-table-body></tbody>
					<tfoot>
						<tr class="logistica-tapas-excel-summary" data-tapas-table-summary-inputs>
							<td></td>
							<th colspan="3"><?php esc_html_e( 'Totales', 'logistica' ); ?></th>
							<td data-output="sum_pedido"></td>
							<td data-output="sum_stock"></td>
							<td data-output="sum_fabricar"></td>
							<td colspan="6"></td>
							<td colspan="16"></td>
							<td></td>
						</tr>
						<tr class="logistica-tapas-excel-summary logistica-tapas-excel-summary--kg" data-tapas-table-summary>
							<td></td>
							<th colspan="12"><?php esc_html_e( 'Totales kg y rollos', 'logistica' ); ?></th>
							<td class="is-col-base-kg is-col-base-kg--resina" data-output="sum_resina_kg"></td>
							<td class="is-col-base-kg is-col-base-kg--retal" data-output="sum_retal_kg"></td>
							<td class="is-col-base-kg is-col-base-kg--total" data-output="sum_total_kg"></td>
							<td class="is-col-envio is-col-envio-resina" data-output="sum_resina_merma"></td>
							<td class="is-col-envio is-col-envio-retal" data-output="sum_retal_merma"></td>
							<td class="is-col-envio is-col-envio-total" data-output="sum_total_merma"></td>
							<td data-output="sum_resina_multiplo"></td>
							<td data-output="sum_rollos"></td>
							<td colspan="8"></td>
							<td></td>
						</tr>
						<tr class="logistica-tapas-excel-summary" data-tapas-table-summary-control>
							<td></td>
							<th colspan="20"><?php esc_html_e( 'Control de materiales y servicio', 'logistica' ); ?></th>
							<td data-output="sum_retal_sent"></td>
							<td data-output="sum_resina_sent"></td>
							<td data-output="sum_material_llega"></td>
							<td data-output="sum_retal_due"></td>
							<td data-output="sum_resina_due"></td>
							<td data-output="sum_total_due"></td>
							<td></td>
							<td data-output="sum_service_total"></td>
							<td></td>
						</tr>
					</tfoot>
				</table>
				</div>
				<?php logistica_table_pagination( 'tapas' ); ?>
			</div>
			<p class="logistica-tapas-scroll-hint" data-tapas-scroll-hint hidden><?php esc_html_e( 'Desliza horizontalmente para ver todas las columnas.', 'logistica' ); ?></p>
		</div>

		<aside class="logistica-tapas-totals" aria-label="<?php esc_attr_e( 'Totales del cálculo', 'logistica' ); ?>">
			<h2 class="logistica-tapas-totals__title"><?php esc_html_e( 'Totales', 'logistica' ); ?></h2>
			<div class="logistica-tapas-totals__groups">
				<div class="logistica-tapas-totals__group" data-tapas-summary-inputs>
					<h2 class="logistica-tapas-section__title"><?php esc_html_e( 'Producción', 'logistica' ); ?></h2>
					<div class="logistica-tapas-grid logistica-tapas-grid--3">
						<?php
						logistica_tapas_field_output( __( 'Pedido (und)', 'logistica' ), 'sum_pedido', 0 );
						logistica_tapas_field_output( __( 'Stock (und)', 'logistica' ), 'sum_stock', 0 );
						logistica_tapas_field_output( __( 'Fabricar (und)', 'logistica' ), 'sum_fabricar', 0 );
						?>
					</div>
				</div>
				<div class="logistica-tapas-totals__group logistica-tapas-totals__group--results" data-tapas-summary>
					<h2 class="logistica-tapas-section__title"><?php esc_html_e( 'Kg y rollos', 'logistica' ); ?></h2>
					<div class="logistica-tapas-results-groups">
						<div class="logistica-tapas-results-group logistica-tapas-results-group--base">
							<h6 class="logistica-tapas-results-group__title"><?php esc_html_e( 'Peso calculado (sin merma)', 'logistica' ); ?></h6>
							<div class="logistica-tapas-grid logistica-tapas-grid--3">
								<?php
								logistica_tapas_field_output( __( 'Resina (kg)', 'logistica' ), 'sum_resina_kg', 2, array( 'class' => 'is-base-kg' ) );
								logistica_tapas_field_output( __( 'Retal (kg)', 'logistica' ), 'sum_retal_kg', 2, array( 'class' => 'is-base-kg' ) );
								logistica_tapas_field_output( __( 'Total (kg)', 'logistica' ), 'sum_total_kg', 2, array( 'class' => 'is-base-kg is-total' ) );
								?>
							</div>
						</div>
						<div class="logistica-tapas-results-group logistica-tapas-results-group--envio">
							<h6 class="logistica-tapas-results-group__title"><?php esc_html_e( 'Cantidad a enviar (con merma)', 'logistica' ); ?></h6>
							<div class="logistica-tapas-grid logistica-tapas-grid--3">
								<?php
								logistica_tapas_field_output( __( 'Resina + merma', 'logistica' ), 'sum_resina_merma', 2, array( 'class' => 'is-envio is-envio-resina' ) );
								logistica_tapas_field_output( __( 'Retal + merma', 'logistica' ), 'sum_retal_merma', 2, array( 'class' => 'is-envio is-envio-retal' ) );
								logistica_tapas_field_output( __( 'Total c/merma', 'logistica' ), 'sum_total_merma', 2, array( 'class' => 'is-envio is-envio-total is-total' ) );
								?>
							</div>
						</div>
						<div class="logistica-tapas-results-group logistica-tapas-results-group--rollos">
							<h6 class="logistica-tapas-results-group__title"><?php esc_html_e( 'Resina y rollos', 'logistica' ); ?></h6>
							<div class="logistica-tapas-grid logistica-tapas-grid--2">
								<?php
								logistica_tapas_field_output( __( 'Mult. 25 resina', 'logistica' ), 'sum_resina_multiplo', 0 );
								logistica_tapas_field_output( __( 'Rollos', 'logistica' ), 'sum_rollos', 0 );
								?>
							</div>
						</div>
					</div>
				</div>
				<div class="logistica-tapas-totals__group logistica-tapas-totals__group--control" data-tapas-summary-control>
					<h2 class="logistica-tapas-section__title"><?php esc_html_e( 'Control de materiales y servicio', 'logistica' ); ?></h2>
					<div class="logistica-tapas-control-groups">
						<div class="logistica-tapas-control-group">
							<h6 class="logistica-tapas-control-group__title"><?php esc_html_e( 'Material enviado', 'logistica' ); ?></h6>
							<div class="logistica-tapas-grid logistica-tapas-grid--3">
								<?php
								logistica_tapas_field_output( __( 'Retal enviado (kg)', 'logistica' ), 'sum_retal_sent', 2 );
								logistica_tapas_field_output( __( 'Resina enviada (kg)', 'logistica' ), 'sum_resina_sent', 2 );
								logistica_tapas_field_output( __( 'Material que llega (kg)', 'logistica' ), 'sum_material_llega', 2 );
								?>
							</div>
						</div>
						<div class="logistica-tapas-control-group logistica-tapas-control-group--due">
							<h6 class="logistica-tapas-control-group__title"><?php esc_html_e( 'Falta por enviar', 'logistica' ); ?></h6>
							<div class="logistica-tapas-grid logistica-tapas-grid--3">
								<?php
								logistica_tapas_field_output( __( 'Retal debiendo (kg)', 'logistica' ), 'sum_retal_due', 2 );
								logistica_tapas_field_output( __( 'Resina debiendo (kg)', 'logistica' ), 'sum_resina_due', 2 );
								logistica_tapas_field_output( __( 'Total debiendo (kg)', 'logistica' ), 'sum_total_due', 2, array( 'class' => 'is-total' ) );
								?>
							</div>
						</div>
						<div class="logistica-tapas-control-group logistica-tapas-control-group--service">
							<h6 class="logistica-tapas-control-group__title"><?php esc_html_e( 'Servicio de fabricación', 'logistica' ); ?></h6>
							<div class="logistica-tapas-grid logistica-tapas-grid--2">
								<?php
								logistica_tapas_field_output( __( 'Costo servicio (COP)', 'logistica' ), 'sum_service_total', 0, array( 'class' => 'is-total' ) );
								?>
							</div>
						</div>
					</div>
				</div>
			</div>
		</aside>

	</div>

</section>
