<?php
/**
 * Single Booking View
 *
 * @package Freelance_Studio
 */

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

$booking = $data['booking'];
$meeting_type = $data['meeting_type'];
$client = $data['client'];
?>

<div class="wrap fs-wrap">
    <div class="fs-page-header">
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-bookings') ); ?>" class="fs-btn fs-btn-secondary fs-btn-small">
            <span class="dashicons dashicons-arrow-left-alt2"></span>
            <?php esc_html_e('Back', 'freelance-studio'); ?>
        </a>
        <h1><?php esc_html_e('Booking Details', 'freelance-studio'); ?></h1>
    </div>
    
    <?php if (isset($_GET['updated'])): ?>
        <div class="notice notice-success is-dismissible">
            <p><?php esc_html_e('Booking updated successfully.', 'freelance-studio'); ?></p>
        </div>
    <?php endif; ?>
    
    <div class="fs-view-columns">
        <div class="fs-view-main">
            <div class="fs-card">
                <div class="fs-booking-header" style="border-left: 4px solid <?php echo esc_attr($meeting_type->color ?? '#4173B0'); ?>; padding-left: 15px;">
                    <h2 style="margin: 0;"><?php echo esc_html($meeting_type->name ?? 'Unknown Meeting Type'); ?></h2>
                    <?php echo  wp_kses_post( FS_Booking::get_status_badge($booking->status) ); ?>
                </div>
                
                <div class="fs-booking-details" style="margin-top: 20px;">
                    <div class="fs-detail-row">
                        <span class="fs-detail-icon"><span class="dashicons dashicons-calendar-alt"></span></span>
                        <div class="fs-detail-content">
                            <strong><?php echo  esc_html( gmdate('l, F j, Y', strtotime($booking->start_datetime)) ); ?></strong>
                            <br>
                            <?php echo  esc_html( gmdate('g:i A', strtotime($booking->start_datetime)) ); ?> - 
                            <?php echo  esc_html( gmdate('g:i A', strtotime($booking->end_datetime)) ); ?>
                            <span class="fs-timezone">(<?php echo esc_html($booking->timezone); ?>)</span>
                        </div>
                    </div>
                    
                    <div class="fs-detail-row">
                        <span class="fs-detail-icon"><span class="dashicons dashicons-clock"></span></span>
                        <div class="fs-detail-content">
                            <?php echo  esc_html( FS_Meeting_Type::get_duration_display($meeting_type->duration ?? 30) ); ?>
                        </div>
                    </div>
                    
                    <div class="fs-detail-row">
                        <span class="fs-detail-icon"><span class="dashicons dashicons-location"></span></span>
                        <div class="fs-detail-content">
                            <?php echo  esc_html( FS_Meeting_Type::get_location_display($meeting_type->location_type ?? 'video', $meeting_type->location_details ?? '') ); ?>
                            <?php if ($meeting_type && $meeting_type->location_details): ?>
                                <br><span class="fs-location-details"><?php echo esc_html($meeting_type->location_details); ?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <?php if (!empty($booking->meet_link)): ?>
                    <div class="fs-detail-row">
                        <span class="fs-detail-icon"><span class="dashicons dashicons-video-alt2"></span></span>
                        <div class="fs-detail-content">
                            <a href="<?php echo esc_url($booking->meet_link); ?>" target="_blank" class="fs-meet-link">
                                <?php esc_html_e('Join Google Meet', 'freelance-studio'); ?>
                                <span class="dashicons dashicons-external"></span>
                            </a>
                            <br><small class="fs-muted"><?php echo esc_html($booking->meet_link); ?></small>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <div class="fs-card">
                <h3><?php esc_html_e('Attendee', 'freelance-studio'); ?></h3>
                
                <?php if ($client): ?>
                    <div class="fs-attendee-info">
                        <div class="fs-attendee-name">
                            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-clients&action=view&id=' . $client->id) ); ?>">
                                <?php echo esc_html($client->name); ?>
                            </a>
                            <?php if ($client->company_name): ?>
                                <span class="fs-company"><?php echo esc_html($client->company_name); ?></span>
                            <?php endif; ?>
                        </div>
                        
                        <?php if ($client->email): ?>
                            <div class="fs-attendee-detail">
                                <span class="dashicons dashicons-email"></span>
                                <a href="mailto:<?php echo esc_attr($client->email); ?>"><?php echo esc_html($client->email); ?></a>
                            </div>
                        <?php endif; ?>
                        
                        <?php if ($client->phone): ?>
                            <div class="fs-attendee-detail">
                                <span class="dashicons dashicons-phone"></span>
                                <a href="tel:<?php echo esc_attr($client->phone); ?>"><?php echo esc_html($client->phone); ?></a>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php else: ?>
                    <div class="fs-attendee-info">
                        <div class="fs-attendee-name"><?php echo esc_html($booking->guest_name); ?></div>
                        
                        <?php if ($booking->guest_email): ?>
                            <div class="fs-attendee-detail">
                                <span class="dashicons dashicons-email"></span>
                                <a href="mailto:<?php echo esc_attr($booking->guest_email); ?>"><?php echo esc_html($booking->guest_email); ?></a>
                            </div>
                        <?php endif; ?>
                        
                        <?php if ($booking->guest_phone): ?>
                            <div class="fs-attendee-detail">
                                <span class="dashicons dashicons-phone"></span>
                                <a href="tel:<?php echo esc_attr($booking->guest_phone); ?>"><?php echo esc_html($booking->guest_phone); ?></a>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
                
                <?php if ($booking->notes): ?>
                    <div class="fs-notes" style="margin-top: 15px;">
                        <strong><?php esc_html_e('Notes:', 'freelance-studio'); ?></strong>
                        <p><?php echo nl2br(esc_html($booking->notes)); ?></p>
                    </div>
                <?php endif; ?>
            </div>
            
            <?php if ($booking->status === 'cancelled' && $booking->cancellation_reason): ?>
                <div class="fs-card fs-card-warning">
                    <h3><?php esc_html_e('Cancellation Details', 'freelance-studio'); ?></h3>
                    <p><strong><?php esc_html_e('Cancelled:', 'freelance-studio'); ?></strong> <?php echo  esc_html( gmdate('M j, Y g:i A', strtotime($booking->cancelled_at)) ); ?></p>
                    <p><strong><?php esc_html_e('By:', 'freelance-studio'); ?></strong> <?php echo  esc_html( ucfirst($booking->cancelled_by) ); ?></p>
                    <?php if ($booking->cancellation_reason): ?>
                        <p><strong><?php esc_html_e('Reason:', 'freelance-studio'); ?></strong> <?php echo esc_html(stripslashes($booking->cancellation_reason)); ?></p>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        </div>
        
        <div class="fs-view-sidebar">
            <?php if ($booking->status === 'confirmed'): ?>
                <div class="fs-card">
                    <h3><?php esc_html_e('Actions', 'freelance-studio'); ?></h3>
                    
                    <form method="post">
                        <?php wp_nonce_field('fs_update_booking_' . $booking->id); ?>
                        
                        <p>
                            <button type="submit" name="update_status" value="1" class="button button-primary" style="width: 100%;"
                                    onclick="document.getElementById('new_status').value = 'completed';">
                                <span class="dashicons dashicons-yes" style="margin-top: 3px;"></span>
                                <?php esc_html_e('Mark as Completed', 'freelance-studio'); ?>
                            </button>
                        </p>
                        
                        <p>
                            <button type="submit" name="update_status" value="1" class="button" style="width: 100%;"
                                    onclick="document.getElementById('new_status').value = 'no_show';">
                                <span class="dashicons dashicons-dismiss" style="margin-top: 3px;"></span>
                                <?php esc_html_e('Mark as No Show', 'freelance-studio'); ?>
                            </button>
                        </p>
                        
                        <hr style="margin: 15px 0;">
                        
                        <p>
                            <label for="cancel_reason"><?php esc_html_e('Cancel Booking', 'freelance-studio'); ?></label>
                            <input type="text" name="cancel_reason" id="cancel_reason" class="regular-text" style="width: 100%;" 
                                   placeholder="<?php esc_attr_e('Reason (optional)', 'freelance-studio'); ?>">
                        </p>
                        <p>
                            <button type="submit" name="update_status" value="1" class="button button-link-delete" style="width: 100%;"
                                    onclick="document.getElementById('new_status').value = 'cancelled'; return confirm('<?php esc_attr_e('Are you sure you want to cancel this booking?', 'freelance-studio'); ?>');">
                                <?php esc_html_e('Cancel Booking', 'freelance-studio'); ?>
                            </button>
                        </p>
                        
                        <input type="hidden" name="new_status" id="new_status" value="">
                    </form>
                </div>
            <?php endif; ?>
            
            <div class="fs-card">
                <h3><?php esc_html_e('Booking Info', 'freelance-studio'); ?></h3>
                
                <div class="fs-info-list">
                    <div class="fs-info-item">
                        <span class="fs-info-label"><?php esc_html_e('Booked:', 'freelance-studio'); ?></span>
                        <span class="fs-info-value"><?php echo  esc_html( gmdate('M j, Y g:i A', strtotime($booking->created_at)) ); ?></span>
                    </div>
                    
                    <?php if ($booking->gcal_event_id): ?>
                        <div class="fs-info-item">
                            <span class="fs-info-label"><?php esc_html_e('Calendar:', 'freelance-studio'); ?></span>
                            <span class="fs-info-value">
                                <span class="dashicons dashicons-yes-alt" style="color: #4caf50;"></span>
                                <?php esc_html_e('Synced to Google Calendar', 'freelance-studio'); ?>
                            </span>
                        </div>
                    <?php endif; ?>
                    
                    <?php if ($booking->reminder_sent): ?>
                        <div class="fs-info-item">
                            <span class="fs-info-label"><?php esc_html_e('Reminder:', 'freelance-studio'); ?></span>
                            <span class="fs-info-value"><?php esc_html_e('Sent', 'freelance-studio'); ?></span>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
