<?php
/**
 * TAB: Label Sender — sender address settings for shipping label generation.
 *
 * Free version: InPost sender address fields.
 *
 * @package ParcelSelector
 * @since   4.0.0
 */

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

if ( ! function_exists( 'parcselect_tab_label_sender' ) ) {
function parcselect_tab_label_sender() {
	if ( ! current_user_can( 'manage_options' ) ) return;

	// Handle form save.
	if ( isset( $_POST['parcselect_sender_save'] ) && check_admin_referer( 'parcselect_sender_save', 'parcselect_sender_nonce' ) ) {
		// NS-59 2026-04-30: dodano sender_locker_id (paczkomat nadawczy InPost).
		// Opcjonalne — gdy ustawione, etykieta generowana paczkomat-to-paczkomat
		// (~50% taniej vs kurier pickup pod adres). Walidacja: 5-6 alfanumerycznych.
		$fields = array( 'sender_name', 'sender_company', 'sender_street', 'sender_city', 'sender_postcode', 'sender_phone', 'sender_email', 'sender_locker_id' );
		foreach ( $fields as $f ) {
			if ( isset( $_POST[ $f ] ) ) {
				if ( 'sender_email' === $f ) {
					$val = sanitize_email( wp_unslash( $_POST[ $f ] ) );
				} elseif ( 'sender_locker_id' === $f ) {
					$val = strtoupper( preg_replace( '/[^A-Za-z0-9]/', '', sanitize_text_field( wp_unslash( $_POST[ $f ] ) ) ) );
					if ( '' !== $val && ! preg_match( '/^[A-Z]{3}\d{2}[A-Z]?$/', $val ) ) {
						$val = ''; // invalid format — ignore (e.g. typed garbage)
					}
				} else {
					$val = sanitize_text_field( wp_unslash( $_POST[ $f ] ) );
				}
				update_option( 'parcselect_' . $f, $val, false );
			}
		}
		echo '<div class="notice notice-success is-dismissible"><p><strong>' . esc_html__( 'Sender data saved.', 'parcel-selector' ) . '</strong></p></div>';
	}

	$sender_name     = get_option( 'parcselect_sender_name', get_option( 'blogname' ) );
	$sender_company  = get_option( 'parcselect_sender_company', '' );
	$sender_street   = get_option( 'parcselect_sender_street', get_option( 'woocommerce_store_address' ) );
	$sender_city     = get_option( 'parcselect_sender_city', get_option( 'woocommerce_store_city' ) );
	$sender_postcode = get_option( 'parcselect_sender_postcode', get_option( 'woocommerce_store_postcode' ) );
	$sender_phone     = get_option( 'parcselect_sender_phone', '' );
	$sender_email     = get_option( 'parcselect_sender_email', get_option( 'admin_email' ) );
	$sender_locker_id = get_option( 'parcselect_sender_locker_id', '' );
	?>
	<div class="ps-tab-content" style="max-width:900px;">

		<div style="padding:20px 24px;background:linear-gradient(135deg,#eff6ff 0%,#dbeafe 100%);border:1px solid #93c5fd;border-radius:10px;margin-bottom:20px;">
			<h2 style="margin:0 0 8px;font-size:20px;font-weight:700;color:#1e3a8a;">
				&#128230; <?php echo esc_html__( 'Sender data for label generation', 'parcel-selector' ); ?>
			</h2>
			<p style="margin:0;font-size:14px;color:#1e293b;line-height:1.6;">
				<?php echo esc_html__( 'This data is saved as the default sender for all labels generated through the parcelselector.com service.', 'parcel-selector' ); ?>
			</p>
		</div>

		<!-- InPost — ACTIVE -->
		<div style="background:#fff;border:2px solid #10b981;border-radius:10px;padding:24px;margin-bottom:16px;position:relative;">
			<div style="position:absolute;top:14px;right:14px;background:#10b981;color:#fff;font-size:10px;font-weight:800;padding:3px 10px;border-radius:4px;letter-spacing:.5px;"><?php echo esc_html__( 'ACTIVE', 'parcel-selector' ); ?></div>
			<h3 style="margin:0 0 14px;font-size:18px;font-weight:700;color:#065f46;">
				&#10003; InPost Paczkomaty <span style="font-size:13px;font-weight:400;color:#64748b;">(<?php echo esc_html__( 'Poland', 'parcel-selector' ); ?>)</span>
			</h3>

			<form method="post" action="">
				<?php wp_nonce_field( 'parcselect_sender_save', 'parcselect_sender_nonce' ); ?>
				<div style="display:grid;grid-template-columns:1fr 1fr;gap:14px;">
					<div style="grid-column:1/-1;">
						<label style="display:block;font-size:12px;font-weight:700;color:#374151;text-transform:uppercase;letter-spacing:.3px;margin-bottom:4px;">
							<?php echo esc_html__( 'Name / first and last name', 'parcel-selector' ); ?> <span style="color:#ef4444;">*</span>
						</label>
						<input type="text" name="sender_name" value="<?php echo esc_attr( $sender_name ); ?>" required
							style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:6px;font-size:14px;" />
					</div>
					<div style="grid-column:1/-1;">
						<label style="display:block;font-size:12px;font-weight:700;color:#374151;text-transform:uppercase;letter-spacing:.3px;margin-bottom:4px;">
							<?php echo esc_html__( 'Company (optional)', 'parcel-selector' ); ?>
						</label>
						<input type="text" name="sender_company" value="<?php echo esc_attr( $sender_company ); ?>"
							style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:6px;font-size:14px;" />
					</div>
					<div style="grid-column:1/-1;">
						<label style="display:block;font-size:12px;font-weight:700;color:#374151;text-transform:uppercase;letter-spacing:.3px;margin-bottom:4px;">
							<?php echo esc_html__( 'Street and number', 'parcel-selector' ); ?> <span style="color:#ef4444;">*</span>
						</label>
						<input type="text" name="sender_street" value="<?php echo esc_attr( $sender_street ); ?>" required
							style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:6px;font-size:14px;" />
					</div>
					<div>
						<label style="display:block;font-size:12px;font-weight:700;color:#374151;text-transform:uppercase;letter-spacing:.3px;margin-bottom:4px;">
							<?php echo esc_html__( 'Postal code', 'parcel-selector' ); ?> <span style="color:#ef4444;">*</span>
						</label>
						<input type="text" name="sender_postcode" value="<?php echo esc_attr( $sender_postcode ); ?>" required placeholder="00-000"
							style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:6px;font-size:14px;" />
					</div>
					<div>
						<label style="display:block;font-size:12px;font-weight:700;color:#374151;text-transform:uppercase;letter-spacing:.3px;margin-bottom:4px;">
							<?php echo esc_html__( 'City', 'parcel-selector' ); ?> <span style="color:#ef4444;">*</span>
						</label>
						<input type="text" name="sender_city" value="<?php echo esc_attr( $sender_city ); ?>" required
							style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:6px;font-size:14px;" />
					</div>
					<div>
						<label style="display:block;font-size:12px;font-weight:700;color:#374151;text-transform:uppercase;letter-spacing:.3px;margin-bottom:4px;">
							<?php echo esc_html__( 'Phone', 'parcel-selector' ); ?> <span style="color:#ef4444;">*</span>
						</label>
						<?php $_ps_phone_disp = function_exists( 'parcselect_phone_strip_pl_prefix' ) ? parcselect_phone_strip_pl_prefix( $sender_phone ) : $sender_phone; ?>
						<div style="display:flex;align-items:stretch;border:1px solid #d1d5db;border-radius:6px;overflow:hidden;background:#fff;">
							<span style="background:#f1f5f9;padding:0 12px;display:flex;align-items:center;font-weight:700;color:#475569;font-size:14px;border-right:1px solid #d1d5db;flex-shrink:0;letter-spacing:0.3px;" aria-hidden="true">+48</span>
							<input type="tel" name="sender_phone" value="<?php echo esc_attr( $_ps_phone_disp ); ?>" required placeholder="500 100 200" maxlength="11" inputmode="numeric" autocomplete="tel-national"
								style="border:0;outline:0;padding:10px 12px;flex:1;font-size:14px;background:transparent;min-width:0;" />
						</div>
					</div>
					<div>
						<label style="display:block;font-size:12px;font-weight:700;color:#374151;text-transform:uppercase;letter-spacing:.3px;margin-bottom:4px;">
							<?php echo esc_html__( 'Email', 'parcel-selector' ); ?>
						</label>
						<input type="email" name="sender_email" value="<?php echo esc_attr( $sender_email ); ?>"
							style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:6px;font-size:14px;" />
					</div>
					<!-- NS-59 2026-04-30: Paczkomat nadawczy — OSOBNE POLE.
					     Opcjonalne. Gdy ustawione → etykieta paczkomat-to-paczkomat (~50% taniej).
					     Format: 5-6 znaków alfanumerycznych (np. WAW01M, LUK01M, KRA53A). -->
					<div style="grid-column:1/-1;border-top:1px dashed #cbd5e1;padding-top:14px;margin-top:6px;">
						<label style="display:block;font-size:12px;font-weight:700;color:#0e7490;text-transform:uppercase;letter-spacing:.3px;margin-bottom:4px;">
							📦 <?php echo esc_html__( 'Sending parcel locker (optional)', 'parcel-selector' ); ?>
						</label>
						<input type="text" name="sender_locker_id" value="<?php echo esc_attr( $sender_locker_id ); ?>"
							pattern="[A-Za-z]{3}[0-9]{2}[A-Za-z]?" maxlength="6"
							placeholder="<?php echo esc_attr__( 'e.g. WAW01M, LUK01M (leave empty = courier picks up the parcel at the address)', 'parcel-selector' ); ?>"
							style="width:100%;padding:10px 12px;border:1px solid #67e8f9;border-radius:6px;font-size:14px;font-family:monospace;text-transform:uppercase;background:#ecfeff;" />
						<p style="margin:6px 0 0;font-size:12px;color:#0e7490;line-height:1.5;">
							💡 <strong><?php echo esc_html__( 'Choose the parcel locker you will send parcels from — the label will be about 50% cheaper', 'parcel-selector' ); ?></strong>
							(<?php echo esc_html__( 'locker-to-locker ~6 PLN vs courier pickup ~13 PLN', 'parcel-selector' ); ?>).
							<a href="https://map.parcelselector.com/" target="_blank" rel="noopener" style="color:#0e7490;font-weight:600;text-decoration:underline;"><?php echo esc_html__( 'Find a parcel locker at map.parcelselector.com →', 'parcel-selector' ); ?></a>
						</p>
					</div>
				</div>
				<p style="margin-top:18px;">
					<button type="submit" name="parcselect_sender_save" value="1" class="button button-primary" style="padding:10px 24px;font-size:14px;font-weight:600;">
						<?php echo esc_html__( 'Save sender data', 'parcel-selector' ); ?>
					</button>
				</p>
			</form>
		</div>

	</div>
	<?php
}
}
