<?php
/**
 * Meeting Type Form View
 *
 * @package Freelance_Studio
 */

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

$meeting_type = $data['meeting_type'];
$is_edit = $data['is_edit'];
?>

<div class="wrap fs-wrap">
    <h1 class="wp-heading-inline">
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-bookings&tab=meeting-types') ); ?>" class="fs-back-link">
            <span class="dashicons dashicons-arrow-left-alt2"></span>
        </a>
        <?php echo  esc_html( $is_edit ? __('Edit Meeting Type', 'freelance-studio') : __('New Meeting Type', 'freelance-studio') ); ?>
    </h1>
    
    <form method="post" class="fs-form">
        <?php wp_nonce_field('fs_save_meeting_type', 'fs_meeting_type_nonce'); ?>
        
        <div class="fs-form-columns">
            <div class="fs-form-main">
                <div class="fs-card">
                    <h3><?php esc_html_e('Basic Information', 'freelance-studio'); ?></h3>
                    
                    <div class="fs-form-group">
                        <label for="name"><?php esc_html_e('Name', 'freelance-studio'); ?> <span class="required">*</span></label>
                        <input type="text" name="name" id="name" class="regular-text" required
                               value="<?php echo esc_attr($meeting_type->name ?? ''); ?>"
                               placeholder="<?php esc_attr_e('e.g., Discovery Call, Consultation', 'freelance-studio'); ?>">
                    </div>
                    
                    <div class="fs-form-group">
                        <label for="description"><?php esc_html_e('Description', 'freelance-studio'); ?></label>
                        <textarea name="description" id="description" rows="3" class="large-text"
                                  placeholder="<?php esc_attr_e('Briefly describe what this meeting is about...', 'freelance-studio'); ?>"><?php echo esc_textarea($meeting_type->description ?? ''); ?></textarea>
                    </div>
                    
                    <div class="fs-form-row">
                        <div class="fs-form-group">
                            <label for="duration"><?php esc_html_e('Duration (minutes)', 'freelance-studio'); ?> <span class="required">*</span></label>
                            <select name="duration" id="duration" required>
                                <?php
                                $durations = array(15, 30, 45, 60, 90, 120);
                                $current_duration = $meeting_type->duration ?? 30;
                                foreach ($durations as $d):
                                ?>
                                    <option value="<?php echo  esc_attr( $d ); ?>" <?php selected($current_duration, $d); ?>>
                                        <?php echo  esc_html( FS_Meeting_Type::get_duration_display($d) ); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        
                        <div class="fs-form-group">
                            <label for="color"><?php esc_html_e('Color', 'freelance-studio'); ?></label>
                            <input type="color" name="color" id="color" 
                                   value="<?php echo esc_attr($meeting_type->color ?? '#4173B0'); ?>">
                        </div>
                        
                        <div class="fs-form-group">
                            <label for="price"><?php esc_html_e('Price (optional)', 'freelance-studio'); ?></label>
                            <input type="number" name="price" id="price" step="0.01" min="0"
                                   value="<?php echo esc_attr($meeting_type->price ?? ''); ?>"
                                   placeholder="<?php esc_attr_e('Free', 'freelance-studio'); ?>">
                        </div>
                    </div>
                </div>
                
                <div class="fs-card">
                    <h3><?php esc_html_e('Location', 'freelance-studio'); ?></h3>
                    
                    <div class="fs-form-group">
                        <label for="location_type"><?php esc_html_e('Meeting Location', 'freelance-studio'); ?></label>
                        <select name="location_type" id="location_type">
                            <option value="video" <?php selected($meeting_type->location_type ?? 'video', 'video'); ?>>
                                <?php esc_html_e('Video Call (Zoom, Google Meet, etc.)', 'freelance-studio'); ?>
                            </option>
                            <option value="phone" <?php selected($meeting_type->location_type ?? '', 'phone'); ?>>
                                <?php esc_html_e('Phone Call', 'freelance-studio'); ?>
                            </option>
                            <option value="in_person" <?php selected($meeting_type->location_type ?? '', 'in_person'); ?>>
                                <?php esc_html_e('In Person', 'freelance-studio'); ?>
                            </option>
                            <option value="custom" <?php selected($meeting_type->location_type ?? '', 'custom'); ?>>
                                <?php esc_html_e('Custom / To Be Determined', 'freelance-studio'); ?>
                            </option>
                        </select>
                    </div>
                    
                    <div class="fs-form-group">
                        <label for="location_details"><?php esc_html_e('Location Details', 'freelance-studio'); ?></label>
                        <textarea name="location_details" id="location_details" rows="2" class="large-text"
                                  placeholder="<?php esc_attr_e('Meeting link, address, or additional instructions...', 'freelance-studio'); ?>"><?php echo esc_textarea($meeting_type->location_details ?? ''); ?></textarea>
                        <p class="description"><?php esc_html_e('This will be shared with the attendee after booking.', 'freelance-studio'); ?></p>
                    </div>
                </div>
                
                <div class="fs-card">
                    <h3><?php esc_html_e('Scheduling Rules', 'freelance-studio'); ?></h3>
                    
                    <div class="fs-form-row">
                        <div class="fs-form-group">
                            <label for="buffer_before"><?php esc_html_e('Buffer Before (minutes)', 'freelance-studio'); ?></label>
                            <input type="number" name="buffer_before" id="buffer_before" min="0" step="5"
                                   value="<?php echo esc_attr($meeting_type->buffer_before ?? 0); ?>">
                            <p class="description"><?php esc_html_e('Time to prepare before the meeting.', 'freelance-studio'); ?></p>
                        </div>
                        
                        <div class="fs-form-group">
                            <label for="buffer_after"><?php esc_html_e('Buffer After (minutes)', 'freelance-studio'); ?></label>
                            <input type="number" name="buffer_after" id="buffer_after" min="0" step="5"
                                   value="<?php echo esc_attr($meeting_type->buffer_after ?? 0); ?>">
                            <p class="description"><?php esc_html_e('Time to wrap up after the meeting.', 'freelance-studio'); ?></p>
                        </div>
                    </div>
                    
                    <div class="fs-form-row">
                        <div class="fs-form-group">
                            <label for="min_notice_hours"><?php esc_html_e('Minimum Notice (hours)', 'freelance-studio'); ?></label>
                            <input type="number" name="min_notice_hours" id="min_notice_hours" min="0"
                                   value="<?php echo esc_attr($meeting_type->min_notice_hours ?? 24); ?>">
                            <p class="description"><?php esc_html_e('How far in advance must bookings be made.', 'freelance-studio'); ?></p>
                        </div>
                        
                        <div class="fs-form-group">
                            <label for="max_days_advance"><?php esc_html_e('Maximum Days in Advance', 'freelance-studio'); ?></label>
                            <input type="number" name="max_days_advance" id="max_days_advance" min="1" max="365"
                                   value="<?php echo esc_attr($meeting_type->max_days_advance ?? 60); ?>">
                            <p class="description"><?php esc_html_e('How far out can bookings be scheduled.', 'freelance-studio'); ?></p>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="fs-form-sidebar">
                <div class="fs-card">
                    <h3><?php esc_html_e('Visibility', 'freelance-studio'); ?></h3>
                    
                    <div class="fs-form-group">
                        <label>
                            <input type="checkbox" name="is_active" value="1" 
                                   <?php checked($meeting_type->is_active ?? 1, 1); ?>>
                            <?php esc_html_e('Active', 'freelance-studio'); ?>
                        </label>
                        <p class="description"><?php esc_html_e('Inactive meeting types cannot be booked.', 'freelance-studio'); ?></p>
                    </div>
                    
                    <div class="fs-form-group">
                        <label>
                            <input type="checkbox" name="is_public" value="1" 
                                   <?php checked($meeting_type->is_public ?? 1, 1); ?>>
                            <?php esc_html_e('Show on public booking page', 'freelance-studio'); ?>
                        </label>
                        <p class="description"><?php esc_html_e('Private meeting types can only be booked via direct link.', 'freelance-studio'); ?></p>
                    </div>
                </div>
                
                <?php if ($is_edit && $meeting_type): ?>
                    <div class="fs-card">
                        <h3><?php esc_html_e('Direct Link', 'freelance-studio'); ?></h3>
                        <input type="text" readonly class="large-text" 
                               value="<?php echo esc_url(home_url('/book/' . $meeting_type->slug)); ?>"
                               onclick="this.select();">
                        <p class="description"><?php esc_html_e('Share this link to let people book this specific meeting type.', 'freelance-studio'); ?></p>
                    </div>
                <?php endif; ?>
                
                <div class="fs-card">
                    <button type="submit" class="button button-primary button-large" style="width: 100%;">
                        <?php echo  esc_html( $is_edit ? __('Update Meeting Type', 'freelance-studio') : __('Create Meeting Type', 'freelance-studio') ); ?>
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>
