<?php
/**
 * Shipment templates page (Package Templates).
 *
 * @package Inspire_Labs\Alsendo_Connect
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

use Inspire_Labs\Alsendo_Connect\Shipment_Template_Manager;
use Inspire_Labs\Alsendo_Connect\Plugin;

$template_manager   = Shipment_Template_Manager::get_instance();
$templates          = $template_manager->get_templates( true );
$_nonce             = wp_create_nonce( 'alsendo_connect_ajax_shipment_template' );
$_set_default_nonce = wp_create_nonce( 'alsendo_connect_ajax_set_default_template' );
$default_slug       = Shipment_Template_Manager::get_default_template_slug();

if ( 1 === count( $templates ) && empty( $default_slug ) ) {
	$only_item = reset( $templates );
	$only_slug = ! empty( $only_item['template_slug'] ) ? $only_item['template_slug'] : sanitize_title( $only_item['name'] );
	update_option( Shipment_Template_Manager::DEFAULT_TEMPLATE_OPTION, $only_slug );
	$default_slug = $only_slug;
}

$api_type         = alsendo_connect()->get_main_settings_api();
$parcel_types     = Shipment_Template_Manager::get_parcel_types_for_api( $api_type );
$shipping_methods = Shipment_Template_Manager::get_shipping_methods_for_api( $api_type );
$contents_options = Shipment_Template_Manager::get_package_contents_options();
$is_ecolet_api    = ( 'ecolet' === $api_type );
$is_zaslat_api    = ( 'zaslat' === $api_type );

if ( $is_zaslat_api ) {
	$shipping_methods = array_intersect_key(
		$shipping_methods,
		array_flip( array( 'ONDEMAND', 'OCCASIONAL' ) )
	);
}

$ecolet_palette_types = array();
if ( $is_ecolet_api ) {
	foreach ( $parcel_types as $value => $label ) {
		if ( in_array( $value, array( 'package', 'envelope' ), true ) ) {
			continue;
		}
		$ecolet_palette_types[ $value ] = $label;
	}
}

$options_hours = array();
for ( $h = 9; $h < 20; $h++ ) {
	$options_hours[ $h . ':00' ] = $h . ':00';
	if ( $h < 19 ) {
		$options_hours[ $h . ':30' ] = $h . ':30';
	}
}
?>
<style>
	#alsendoAddTemplateDialog { font-size: 13px; }
	#alsendoTemplateFormFields {
		max-height: 70vh;
		overflow-y: auto;
		padding-right: 6px;
	}
	#alsendoAddTemplateDialog .alsendo-tpl-row {
		display: flex;
		flex-wrap: wrap;
		gap: 10px;
		margin-bottom: 12px;
	}
	#alsendoAddTemplateDialog .alsendo-tpl-field {
		display: flex;
		flex-direction: column;
		flex: 1 1 0;
		min-width: 0;
	}
	#alsendoAddTemplateDialog .alsendo-tpl-field.full {
		flex: 1 1 100%;
	}
	#alsendoAddTemplateDialog .alsendo-tpl-row.alsendo-two-cols > .alsendo-tpl-field {
		flex: 1 1 calc(50% - 5px);
		min-width: 240px;
	}
	#alsendoAddTemplateDialog .alsendo-tpl-field label {
		font-weight: 600;
		margin-bottom: 4px;
		font-size: 12px;
		color: #1d2327;
	}
	#alsendoAddTemplateDialog .alsendo-tpl-field input[type="text"],
	#alsendoAddTemplateDialog .alsendo-tpl-field input[type="number"],
	#alsendoAddTemplateDialog .alsendo-tpl-field select {
		width: 100%;
		padding: 5px 8px;
		box-sizing: border-box;
		font-size: 13px;
	}
	#alsendoAddTemplateDialog .alsendo-tpl-radio-group {
		display: flex;
		flex-wrap: wrap;
		gap: 6px 18px;
		padding: 4px 0;
	}
	#alsendoAddTemplateDialog .alsendo-tpl-radio-group label {
		font-weight: normal;
		display: inline-flex;
		align-items: center;
		gap: 4px;
		cursor: pointer;
		font-size: 13px;
	}
	#alsendoAddTemplateDialog .alsendo-tpl-separator {
		border-top: 1px solid #ddd;
		margin: 14px 0 10px;
	}
	#alsendoAddTemplateDialog .alsendo-additional-parcels-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		gap: 10px;
		margin-bottom: 8px;
	}
	#alsendoAddTemplateDialog .alsendo-additional-parcels-header label {
		margin: 0;
	}
	#alsendoAddTemplateDialog .alsendo-additional-parcel-wrap {
		border: 1px solid #ddd;
		padding: 10px;
		margin-bottom: 10px;
		background: #fff;
	}
	#alsendoAddTemplateDialog .alsendo-additional-parcel-title {
		display:flex;
		align-items:center;
		justify-content: space-between;
		gap: 10px;
		margin-bottom: 10px;
		font-weight: 600;
	}
	#alsendoAddTemplateDialog .alsendo-additional-parcel-title > span:first-child {
		display: inline-block;
		background: #f8d7da;
		color: #842029;
		padding: 4px 8px;
		border: 1px solid #f5c2c7;
	}
	.alsendo-tpl-description-cell {
		max-width: 400px;
		word-break: break-word;
	}
	#alsendoAddTemplateDialog .alsendo-parcel-type-cards {
		display: flex;
		gap: 10px;
		flex-wrap: nowrap;
		margin-bottom: 8px;
	}
	#alsendoAddTemplateDialog .alsendo-parcel-type-card {
		position: relative;
		cursor: pointer;
		flex: 1 1 0;
		min-width: 0;
		margin: 0;
	}
	#alsendoAddTemplateDialog .alsendo-parcel-type-card input[type="radio"] {
		position: absolute;
		width: 1px;
		height: 1px;
		margin: -1px;
		padding: 0;
		border: 0;
		clip: rect(0, 0, 0, 0);
		overflow: hidden;
	}
	#alsendoAddTemplateDialog .alsendo-parcel-type-card .alsendo-card-inner {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		min-height: 76px;
		width: 100%;
		box-sizing: border-box;
		border: 1px solid #d4bc81;
		border-radius: 0;
		padding: 8px 10px;
		text-align: center;
		background: #fff;
		color: #c68e2f;
		font-weight: 600;
		line-height: 1.2;
		transition: border-color .15s ease, box-shadow .15s ease, background .15s ease, color .15s ease;
	}
	#alsendoAddTemplateDialog .alsendo-parcel-type-card .dashicons {
		display: block;
		margin: 0 0 6px;
		font-size: 24px;
		width: 24px;
		height: 24px;
		color: #df9726;
		line-height: 1;
	}
	#alsendoAddTemplateDialog .alsendo-parcel-type-card:hover .alsendo-card-inner {
		border-color: #cf9d3e;
		box-shadow: 0 0 0 1px #cf9d3e inset;
	}
	#alsendoAddTemplateDialog .alsendo-parcel-type-card input[type="radio"]:checked + .alsendo-card-inner {
		border-color: #e38f1b;
		box-shadow: 0 0 0 1px #e38f1b inset;
		background: #e38f1b;
		color: #fff;
	}
	#alsendoAddTemplateDialog .alsendo-parcel-type-card input[type="radio"]:checked + .alsendo-card-inner .dashicons {
		color: #fff;
	}
	body.woocommerce_page_wc-settings p.submit {
		display: none !important;
	}
	.alsendo_connect_set_default_template.is-default {
		color: #fff !important;
		background: #00a32a !important;
		border-color: #00a32a !important;
		box-shadow: none !important;
		text-shadow: none !important;
		cursor: default;
		pointer-events: none;
	}
</style>

<div class="wrap">
	<h1 class="wp-heading-inline"><?php esc_html_e( 'Shipment templates', 'alsendo-connect' ); ?></h1>
	<a id="alsendo_connect_add_template" class="page-title-action"><?php esc_html_e( 'Add template', 'alsendo-connect' ); ?></a>
	<hr class="wp-header-end">
	<table id="alsendo_package_templates_table" class="wp-list-table widefat fixed striped table-view-list">
		<thead>
		<tr>
			<th scope="col"><?php esc_html_e( 'Name', 'alsendo-connect' ); ?></th>
			<th scope="col"><?php esc_html_e( 'Description', 'alsendo-connect' ); ?></th>
			<th scope="col"><?php esc_html_e( 'Options', 'alsendo-connect' ); ?></th>
		</tr>
		</thead>
		<tbody id="the-list">
		<?php if ( 0 === count( $templates ) ) : ?>
			<tr class="no-items">
				<td class="colspanchange" colspan="3"><?php esc_html_e( 'No templates were found.', 'alsendo-connect' ); ?></td>
			</tr>
		<?php else : ?>
			<?php
			foreach ( $templates as $item ) :
				$item_slug  = ! empty( $item['template_slug'] ) ? $item['template_slug'] : sanitize_title( $item['name'] );
				$is_default = ( $item_slug === $default_slug );
				?>
				<tr data-template="<?php echo esc_attr( wp_json_encode( $item ) ); ?>">
					<td><?php echo esc_html( $item['name'] ); ?></td>
					<td class="alsendo-tpl-description-cell">
						<?php
						$desc_parts = array();
						if ( ! empty( $item['parcel_type'] ) ) {
							$desc_parts[] = esc_html__( 'Type', 'alsendo-connect' ) . ': ' . esc_html( $parcel_types[ $item['parcel_type'] ] ?? $item['parcel_type'] );
						}
						if ( ! empty( $item['package_weight'] ) ) {
							$desc_parts[] = esc_html__( 'Weight', 'alsendo-connect' ) . ': ' . esc_html( $item['package_weight'] ) . ' kg';
						}
						if ( ! empty( $item['package_length'] ) || ! empty( $item['package_width'] ) || ! empty( $item['package_height'] ) ) {
							$desc_parts[] = esc_html( $item['package_length'] ) . ' x ' . esc_html( $item['package_width'] ) . ' x ' . esc_html( $item['package_height'] ) . ' cm';
						}
						if ( ! empty( $item['declared_content'] ) ) {
							$desc_parts[] = esc_html__( 'Declared value', 'alsendo-connect' ) . ': ' . esc_html( $item['declared_content'] );
						}
						echo implode( ' | ', $desc_parts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						?>
					</td>
					<td>
						<?php if ( $is_default ) : ?>
							<a class="button alsendo_connect_set_default_template is-default" data-slug="<?php echo esc_attr( $item_slug ); ?>"><?php esc_html_e( 'Default template', 'alsendo-connect' ); ?></a>
						<?php else : ?>
							<a class="button alsendo_connect_set_default_template" data-slug="<?php echo esc_attr( $item_slug ); ?>"><?php esc_html_e( 'Set as default template', 'alsendo-connect' ); ?></a>
						<?php endif; ?>
						<a class="button alsendo_connect_edit_template"><?php esc_html_e( 'Edit', 'alsendo-connect' ); ?></a>
						<?php if ( ! $is_default ) : ?>
							<a class="button alsendo_connect_delete_template"><?php esc_html_e( 'Delete', 'alsendo-connect' ); ?></a>
						<?php endif; ?>
					</td>
				</tr>
			<?php endforeach; ?>
		<?php endif; ?>
		</tbody>
		<tfoot>
		<tr>
			<th scope="col"><?php esc_html_e( 'Name', 'alsendo-connect' ); ?></th>
			<th scope="col"><?php esc_html_e( 'Description', 'alsendo-connect' ); ?></th>
			<th scope="col"><?php esc_html_e( 'Options', 'alsendo-connect' ); ?></th>
		</tr>
		</tfoot>
	</table>
	<div class="clear"></div>
</div>

<div id="alsendoAddTemplateDialog" title="Template" style="display: none;" data-api-type="<?php echo esc_attr( $api_type ); ?>">
	<div id="alsendoTemplateFormFields">
		<input type="hidden" name="action" value="alsendo_connect_shipment_template">
		<input type="hidden" name="template[_nonce]" value="<?php echo esc_attr( $_nonce ); ?>">
		<input id="alsendo_connect_template_id" type="hidden" name="template[id]" value="">
		<input id="alsendo_connect_template_legacy_slug" type="hidden" name="template[_legacy_slug]" value="">

		<!-- Name -->
		<div class="alsendo-tpl-row<?php echo $is_ecolet_api ? '' : ' alsendo-two-cols'; ?>">
			<div class="alsendo-tpl-field full">
				<label for="alsendo_connect_template_name"><?php esc_html_e( 'Template name', 'alsendo-connect' ); ?></label>
				<input id="alsendo_connect_template_name" type="text" name="template[name]" value="" required="required">
			</div>
		</div>

		<!-- Parcel type + Non-standard -->
		<?php if ( ! $is_zaslat_api ) : ?>
			<div class="alsendo-tpl-row">
				<?php if ( $is_ecolet_api ) : ?>
					<div class="alsendo-tpl-field">
						<label for="alsendo_connect_template_parcel_type"><?php esc_html_e( 'Parcel type', 'alsendo-connect' ); ?></label>
						<input type="hidden" id="alsendo_connect_template_parcel_type" name="template[parcel_type]" value="">
						<div class="alsendo-parcel-type-cards" id="alsendo_connect_ecolet_parcel_type_mode">
							<label class="alsendo-parcel-type-card">
								<input type="radio" name="template[parcel_type_mode]" value="package">
								<span class="alsendo-card-inner">
									<span class="dashicons dashicons-archive"></span>
									<?php esc_html_e( 'Package', 'alsendo-connect' ); ?>
								</span>
							</label>
							<label class="alsendo-parcel-type-card">
								<input type="radio" name="template[parcel_type_mode]" value="envelope">
								<span class="alsendo-card-inner">
									<span class="dashicons dashicons-email"></span>
									<?php esc_html_e( 'Envelope', 'alsendo-connect' ); ?>
								</span>
							</label>
							<label class="alsendo-parcel-type-card">
								<input type="radio" name="template[parcel_type_mode]" value="palette">
								<span class="alsendo-card-inner">
									<span class="dashicons dashicons-screenoptions"></span>
									<?php esc_html_e( 'Pallet', 'alsendo-connect' ); ?>
								</span>
							</label>
						</div>
						<div id="alsendo_connect_ecolet_nonstd_wrapper" style="display:none;">
							<label for="alsendo_connect_template_is_nstd"><?php esc_html_e( 'Non standard package', 'alsendo-connect' ); ?></label>
							<select id="alsendo_connect_template_is_nstd" name="template[is_nstd]">
								<option value="no"><?php esc_html_e( 'No', 'alsendo-connect' ); ?></option>
								<option value="yes"><?php esc_html_e( 'Yes', 'alsendo-connect' ); ?></option>
							</select>
						</div>
						<div id="alsendo_connect_ecolet_palette_size_wrapper" style="display:none;">
							<label for="alsendo_connect_ecolet_palette_type"><?php esc_html_e( 'Pallet size', 'alsendo-connect' ); ?></label>
							<select id="alsendo_connect_ecolet_palette_type">
								<?php foreach ( $ecolet_palette_types as $value => $label ) : ?>
									<option value="<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $label ); ?></option>
								<?php endforeach; ?>
							</select>
						</div>
					</div>
				<?php else : ?>
					<div class="alsendo-tpl-field">
						<label for="alsendo_connect_template_parcel_type"><?php esc_html_e( 'Parcel type', 'alsendo-connect' ); ?></label>
						<select id="alsendo_connect_template_parcel_type" name="template[parcel_type]">
							<?php foreach ( $parcel_types as $value => $label ) : ?>
								<option value="<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
					</div>
					<div class="alsendo-tpl-field" id="alsendo_connect_nonstd_wrapper">
						<label for="alsendo_connect_template_is_nstd"><?php esc_html_e( 'Non standard package', 'alsendo-connect' ); ?></label>
						<select id="alsendo_connect_template_is_nstd" name="template[is_nstd]">
							<option value="no"><?php esc_html_e( 'No', 'alsendo-connect' ); ?></option>
							<option value="yes"><?php esc_html_e( 'Yes', 'alsendo-connect' ); ?></option>
						</select>
					</div>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<!-- Dimensions + Weight -->
		<div class="alsendo-tpl-row" id="alsendo_connect_dimensions_row">
			<div class="alsendo-tpl-field">
				<label for="alsendo_connect_template_package_length"><?php esc_html_e( 'Length [cm]', 'alsendo-connect' ); ?></label>
				<input id="alsendo_connect_template_package_length" type="number" name="template[package_length]" value="" min="0" step="1">
			</div>
			<div class="alsendo-tpl-field">
				<label for="alsendo_connect_template_package_width"><?php esc_html_e( 'Width [cm]', 'alsendo-connect' ); ?></label>
				<input id="alsendo_connect_template_package_width" type="number" name="template[package_width]" value="" min="0" step="1">
			</div>
			<div class="alsendo-tpl-field">
				<label for="alsendo_connect_template_package_height"><?php esc_html_e( 'Height [cm]', 'alsendo-connect' ); ?></label>
				<input id="alsendo_connect_template_package_height" type="number" name="template[package_height]" value="" min="0" step="1">
			</div>
			<div class="alsendo-tpl-field">
				<label for="alsendo_connect_template_package_weight"><?php esc_html_e( 'Weight [kg]', 'alsendo-connect' ); ?></label>
				<input id="alsendo_connect_template_package_weight" type="number" name="template[package_weight]" value="" min="0" step="any">
			</div>
		</div>

		<?php if ( ! $is_zaslat_api ) : ?>
			<!-- Package contents + Custom text + Declared value -->
			<div class="alsendo-tpl-row">
				<div class="alsendo-tpl-field">
					<label for="alsendo_connect_template_package_contents"><?php esc_html_e( 'Default package contents', 'alsendo-connect' ); ?></label>
					<select id="alsendo_connect_template_package_contents" name="template[package_contents]">
						<?php foreach ( $contents_options as $value => $label ) : ?>
							<option value="<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $label ); ?></option>
						<?php endforeach; ?>
					</select>
				</div>
				<div class="alsendo-tpl-field" id="alsendo_connect_custom_text_wrapper" style="display:none;">
					<label for="alsendo_connect_template_custom_text_content"><?php esc_html_e( 'My package contents', 'alsendo-connect' ); ?></label>
					<input id="alsendo_connect_template_custom_text_content" type="text" name="template[custom_text_content]" value="">
				</div>
				<div class="alsendo-tpl-field">
					<label for="alsendo_connect_template_declared_content"><?php esc_html_e( 'Declared value', 'alsendo-connect' ); ?></label>
					<input id="alsendo_connect_template_declared_content" type="text" name="template[declared_content]" value="">
				</div>
			</div>
		<?php endif; ?>

		<?php if ( ! empty( $shipping_methods ) ) : ?>
			<!-- Shipping method (radio) -->
			<div class="alsendo-tpl-separator"></div>
			<?php if ( 'apaczka' === $api_type ) : ?>
				<div class="alsendo-tpl-row">
					<div class="alsendo-tpl-field full">
						<div class="alsendo-additional-parcels-header">
							<label><?php esc_html_e( 'Additional parcels', 'alsendo-connect' ); ?></label>
							<button type="button" class="button" id="alsendo_connect_add_template_parcel"><?php esc_html_e( 'Add parcel', 'alsendo-connect' ); ?></button>
						</div>
						<div id="alsendo_connect_additional_parcels_container"></div>
					</div>
				</div>
				<div class="alsendo-tpl-separator"></div>
			<?php endif; ?>
			<div class="alsendo-tpl-row">
				<div class="alsendo-tpl-field full">
					<label><?php esc_html_e( 'Default way to send a parcel', 'alsendo-connect' ); ?></label>
					<div class="alsendo-tpl-radio-group">
						<?php foreach ( $shipping_methods as $value => $label ) : ?>
							<label>
								<input type="radio" name="template[shipping_method]" value="<?php echo esc_attr( $value ); ?>">
								<?php echo esc_html( $label ); ?>
							</label>
						<?php endforeach; ?>
					</div>
				</div>
			</div>

			<?php if ( ! $is_zaslat_api ) : ?>
				<!-- Pickup hours (visible only for COURIER) -->
				<div class="alsendo-tpl-row" id="alsendo_connect_pickup_hours_wrapper" style="display:none;">
					<div class="alsendo-tpl-field">
						<label for="alsendo_connect_template_pickup_hour_from"><?php esc_html_e( 'Pickup hour from', 'alsendo-connect' ); ?></label>
						<select id="alsendo_connect_template_pickup_hour_from" name="template[pickup_hour_from]">
							<option value=""><?php esc_html_e( '— select —', 'alsendo-connect' ); ?></option>
							<?php foreach ( $options_hours as $value => $label ) : ?>
								<option value="<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
					</div>
					<div class="alsendo-tpl-field">
						<label for="alsendo_connect_template_pickup_hour_to"><?php esc_html_e( 'Pickup hour to', 'alsendo-connect' ); ?></label>
						<select id="alsendo_connect_template_pickup_hour_to" name="template[pickup_hour_to]">
							<option value=""><?php esc_html_e( '— select —', 'alsendo-connect' ); ?></option>
							<?php foreach ( $options_hours as $value => $label ) : ?>
								<option value="<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
					</div>
				</div>
			<?php endif; ?>
		<?php endif; ?>
	</div>
</div>
