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

/**
 * ============================================
 * FILE: templates/emails/admin/new-appointment.php
 * Admin New Appointment Email Template
 * ============================================
 */
?>
<?php
do_action( 'rtrb_get_email_header', $email, $booking );
do_action( 'rtrb_email_before_content', $email, $booking );
do_action( 'rtrb_email_content_start', $email, $booking );

?>

<!-- Email Content Text -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td class="main-content">
			<?php if ( ! empty( $settings['additional_content'] ) ) : ?>
				<?php echo wp_kses_post( wpautop( wptexturize( $settings['additional_content'] ) ) ); ?>
			<?php else : ?>
				<p style="margin: 0;"><?php esc_html_e( 'A new appointment has been created in the system. Please review the details below and take necessary action.', 'radius-booking' ); ?></p>
			<?php endif; ?>
		</td>
	</tr>
</table>

<!-- Quick Stats Box -->
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%); border: 1px solid #3b82f6; border-radius: 6px; margin: 16px 0;">
	<tr>
		<td style="padding: 16px;">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td width="50%" style="padding: 8px; text-align: center; border-right: 1px solid #93c5fd;">
						<p style="margin: 0 0 4px 0; font-size: 12px; color: #1e40af; font-weight: 500;">
							<?php esc_html_e( 'Booking ID', 'radius-booking' ); ?>
						</p>
						<p style="margin: 0; font-size: 18px; color: #1e3a8a; font-weight: bold;">
							#<?php echo esc_html( $booking['customer']->id ); ?>
						</p>
					</td>
					<td width="50%" style="padding: 8px; text-align: center;">
						<p style="margin: 0 0 4px 0; font-size: 12px; color: #1e40af; font-weight: 500;">
							<?php esc_html_e( 'Revenue', 'radius-booking' ); ?>
						</p>
						<p style="margin: 0; font-size: 18px; color: #1e3a8a; font-weight: bold;">
							<?php echo esc_html( get_rtrb_currency_symbol() . number_format( $booking['customer']->price, 2 ) ); ?>
						</p>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>

<!-- Customer Information Box -->
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #f9fafb; border: 1px solid #e5e7eb; border-radius: 6px; margin: 16px 0;">
	<tr>
		<td style="padding: 16px;">
			<p style="margin: 0 0 12px 0; font-weight: 600; color: #111827; font-size: 15px; border-bottom: 2px solid #e5e7eb; padding-bottom: 8px;">
				👤 <?php esc_html_e( 'Customer Information', 'radius-booking' ); ?>
			</p>
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td style="padding: 4px 0;">
						<p style="margin: 0; font-size: 14px; color: #374151;">
							<strong style="color: #6b7280;"><?php esc_html_e( 'Name:', 'radius-booking' ); ?></strong>
							<?php echo esc_html( $booking['customer']->customer_info['name'] ?? '' ); ?>
						</p>
					</td>
				</tr>
				<tr>
					<td style="padding: 4px 0;">
						<p style="margin: 0; font-size: 14px; color: #374151;">
							<strong style="color: #6b7280;"><?php esc_html_e( 'Email:', 'radius-booking' ); ?></strong>
							<a href="mailto:<?php echo esc_attr( $booking['customer']->customer_info['email'] ?? '' ); ?>" style="color: #2563eb; text-decoration: none;">
								<?php echo esc_html( $booking['customer']->customer_info['email'] ?? '' ); ?>
							</a>
						</p>
					</td>
				</tr>
				<?php if ( ! empty( $booking['customer']->customer_info['phone'] ) ) : ?>
					<tr>
						<td style="padding: 4px 0;">
							<p style="margin: 0; font-size: 14px; color: #374151;">
								<strong style="color: #6b7280;"><?php esc_html_e( 'Phone:', 'radius-booking' ); ?></strong>
								<a href="tel:<?php echo esc_attr( $booking['customer']->customer_info['phone'] ); ?>" style="color: #2563eb; text-decoration: none;">
									<?php echo esc_html( $booking['customer']->customer_info['phone'] ); ?>
								</a>
							</p>
						</td>
					</tr>
				<?php endif; ?>
			</table>
		</td>
	</tr>
</table>

<!-- Staff Assignment Box -->
<?php if ( ! empty( $booking['employee_name'] ) ) : ?>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #fef3c7; border: 1px solid #fbbf24; border-radius: 6px; margin: 16px 0;">
		<tr>
			<td style="padding: 16px;">
				<p style="margin: 0 0 8px 0; font-weight: 600; color: #78350f; font-size: 14px;">
					👨‍💼 <?php esc_html_e( 'Assigned Staff:', 'radius-booking' ); ?>
				</p>
				<p style="margin: 0; font-size: 14px; color: #92400e;">
					<?php echo esc_html( $booking['employee_name'] ); ?>
					<?php if ( ! empty( $booking['employee_email'] ) ) : ?>
						<br>
						<a href="mailto:<?php echo esc_attr( $booking['employee_email'] ); ?>" style="color: #d97706; text-decoration: none;">
							<?php echo esc_html( $booking['employee_email'] ); ?>
						</a>
					<?php endif; ?>
					<?php if ( ! empty( $booking['employee_phone'] ) ) : ?>
						<br>
						<a href="tel:<?php echo esc_attr( $booking['employee_phone'] ); ?>" style="color: #d97706; text-decoration: none;">
							<?php echo esc_html( $booking['employee_phone'] ); ?>
						</a>
					<?php endif; ?>
				</p>
			</td>
		</tr>
	</table>
<?php endif; ?>

<!-- Custom Fields Box -->
<?php
$custom_fields = $booking['customer']->custom_info ?? array();
if ( is_object( $custom_fields ) ) {
	$custom_fields = (array) $custom_fields;
}
if ( ! empty( $custom_fields ) && is_array( $custom_fields ) ) :
	?>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #eef2ff; border: 1px solid #c7d2fe; border-radius: 6px; margin: 16px 0;">
		<tr>
			<td style="padding: 16px;">
				<p style="margin: 0 0 12px 0; font-weight: 600; color: #3730a3; font-size: 15px; border-bottom: 2px solid #c7d2fe; padding-bottom: 8px;">
					📋 <?php esc_html_e( 'Custom Fields', 'radius-booking' ); ?>
				</p>
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<?php
					foreach ( $custom_fields as $field ) :
						$field = (array) $field;
						$label = isset( $field['label'] ) ? $field['label'] : '';
						$type  = isset( $field['type'] ) ? $field['type'] : '';
						$value = isset( $field['value'] ) ? $field['value'] : '';

						if ( is_array( $value ) ) {
							$value = implode( ', ', $value );
						} elseif ( 'checkbox' === $type ) {
							$value = implode( ', ', array_filter( explode( '||', (string) $value ) ) );
						}

						if ( '' === trim( (string) $label ) && '' === trim( (string) $value ) ) {
							continue;
						}
						?>
						<tr>
							<td style="padding: 4px 0;">
								<p style="margin: 0; font-size: 14px; color: #374151;">
									<strong style="color: #6b7280;"><?php echo esc_html( $label ); ?>:</strong>
									<?php echo esc_html( $value ); ?>
								</p>
							</td>
						</tr>
					<?php endforeach; ?>
				</table>
			</td>
		</tr>
	</table>
<?php endif; ?>

<?php do_action( 'rtrb_email_before_booking_details', $email, $booking ); ?>

<!-- Booking Details Box -->
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-details">
	<tr>
		<td style="padding: 20px;">
			<?php do_action( 'rtrb_email_booking_details', $email, $booking ); ?>
		</td>
	</tr>
</table>

<?php do_action( 'rtrb_email_after_booking_details', $email, $booking ); ?>

<!-- Status Badge -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td align="center" style="padding: 16px 0;">
			<?php
			$booking_status = isset( $booking['status'] ) ? strtolower( $booking['status'] ) : 'pending';
			$badge_colors   = array(
				'confirmed' => array(
					'bg' => '#d1fae5',
					'text' => '#065f46',
					'icon' => '✅',
					'label' => 'CONFIRMED',
				),
				'pending' => array(
					'bg' => '#fef3c7',
					'text' => '#92400e',
					'icon' => '⏳',
					'label' => 'PENDING',
				),
				'cancelled' => array(
					'bg' => '#fee2e2',
					'text' => '#991b1b',
					'icon' => '❌',
					'label' => 'CANCELLED',
				),
				'completed' => array(
					'bg' => '#d1fae5',
					'text' => '#065f46',
					'icon' => '✅',
					'label' => 'COMPLETED',
				),
			);
			$badge = isset( $badge_colors[ $booking_status ] ) ? $badge_colors[ $booking_status ] : $badge_colors['pending'];
			?>
			<span style="background-color: <?php echo esc_attr( $badge['bg'] ); ?>; color: <?php echo esc_attr( $badge['text'] ); ?>; padding: 8px 16px; border-radius: 20px; font-size: 12px; font-weight: 600; display: inline-block;">
				 <?php echo esc_html( $badge['label'] ); ?>
			</span>
		</td>
	</tr>
</table>


<!-- Notes Section -->
<?php if ( ! empty( $booking['notes'] ) ) : ?>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #fef9c3; border: 1px solid #fde047; border-radius: 6px; margin: 16px 0;">
		<tr>
			<td style="padding: 16px;">
				<p style="margin: 0 0 8px 0; font-weight: 600; color: #854d0e; font-size: 14px;">
					<?php esc_html_e( 'Customer Notes:', 'radius-booking' ); ?>
				</p>
				<p style="margin: 0; font-size: 14px; color: #a16207;">
					<?php echo esc_html( $booking['notes'] ); ?>
				</p>
			</td>
		</tr>
	</table>
<?php endif; ?>

<?php if ( ! empty( $settings['footer_text'] ) ) : ?>
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td class="footer-text">
				<?php echo wp_kses_post( wpautop( wptexturize( $settings['footer_text'] ) ) ); ?>
			</td>
		</tr>
	</table>
<?php endif; ?>

<?php do_action( 'rtrb_email_before_button', $email, $booking ); ?>

<?php if ( ! empty( $settings['button_text'] ) ) : ?>
	<?php do_action( 'rtrb_email_button', $email, $booking, $settings ); ?>
<?php endif; ?>


<?php
do_action( 'rtrb_email_content_end', $email, $booking );
do_action( 'rtrb_email_after_content', $email, $booking );
do_action( 'rtrb_get_email_footer', $email, $booking );
?>
