<?php
$title = 'Edit Aircraft';
$active = 'aircraft';
require_once __DIR__ . '/../../layouts/header.php';
require_once __DIR__ . '/../../layouts/sidebar.php';

$errors = Session::get('errors', []);
Session::remove('errors');

// Get all states for dropdown
$states = Database::fetchAll("SELECT * FROM states ORDER BY state_name");
if ($states === false) $states = [];

// Get all zones for dropdown
$zones = Database::fetchAll("SELECT * FROM zones ORDER BY zone_name");
if ($zones === false) $zones = [];

// Document types for the dropdown
$documentTypes = [
    'registration' => 'Registration Certificate',
    'airworthiness' => 'Airworthiness Certificate',
    'insurance' => 'Insurance Document',
    'maintenance' => 'Maintenance Log',
    'photo' => 'Aircraft Photo',
    'other' => 'Other Document'
];
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-edit"></i>
                Edit Aircraft: <?php echo htmlspecialchars($aircraft['tail_number'] ?? ''); ?>
            </h1>
            <p>Update aircraft information</p>
        </div>
        <div class="header-actions">
            <a href="<?php echo BASE_URL; ?>/fleet/aircraft/show/<?php echo $aircraft['id'] ?? ''; ?>" class="btn btn-info">
                <i class="fas fa-eye"></i> View Details
            </a>
            <a href="<?php echo BASE_URL; ?>/fleet/aircraft" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back to Aircraft
            </a>
        </div>
    </div>

    <!-- Form -->
    <div class="form-section">
        <form method="POST" action="<?php echo BASE_URL; ?>/fleet/aircraft/update/<?php echo $aircraft['id'] ?? ''; ?>" 
              enctype="multipart/form-data" id="aircraftForm">
            <?php echo Security::csrfField(); ?>
            
            <!-- Basic Information -->
            <div class="form-section-inner">
                <div class="section-title">
                    <h3><i class="fas fa-info-circle"></i> Basic Information</h3>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Asset Code</label>
                        <input type="text" value="<?php echo htmlspecialchars($aircraft['asset_code'] ?? ''); ?>" 
                               class="form-control" readonly disabled>
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Use/Purpose</label>
                        <select name="use_purpose" required class="form-control <?php echo isset($errors['use_purpose']) ? 'error' : ''; ?>">
                            <option value="">Select Purpose</option>
                            <option value="Operational" <?php echo ($aircraft['use_purpose'] ?? '') == 'Operational' ? 'selected' : ''; ?>>Operational</option>
                            <option value="Transport" <?php echo ($aircraft['use_purpose'] ?? '') == 'Transport' ? 'selected' : ''; ?>>Transport</option>
                            <option value="Training" <?php echo ($aircraft['use_purpose'] ?? '') == 'Training' ? 'selected' : ''; ?>>Training</option>
                            <option value="Surveillance" <?php echo ($aircraft['use_purpose'] ?? '') == 'Surveillance' ? 'selected' : ''; ?>>Surveillance</option>
                        </select>
                        <?php if (isset($errors['use_purpose'])): ?>
                            <small class="error-text"><?php echo $errors['use_purpose']; ?></small>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Ownership Type</label>
                        <select name="ownership_type" required class="form-control <?php echo isset($errors['ownership_type']) ? 'error' : ''; ?>">
                            <option value="">Select Ownership</option>
                            <option value="FGN-Owned" <?php echo ($aircraft['ownership_type'] ?? '') == 'FGN-Owned' ? 'selected' : ''; ?>>FGN-Owned</option>
                            <option value="Donor" <?php echo ($aircraft['ownership_type'] ?? '') == 'Donor' ? 'selected' : ''; ?>>Donor</option>
                            <option value="Leased" <?php echo ($aircraft['ownership_type'] ?? '') == 'Leased' ? 'selected' : ''; ?>>Leased</option>
                        </select>
                        <?php if (isset($errors['ownership_type'])): ?>
                            <small class="error-text"><?php echo $errors['ownership_type']; ?></small>
                        <?php endif; ?>
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Aircraft Type</label>
                        <select name="aircraft_type" id="aircraft_type" required class="form-control <?php echo isset($errors['aircraft_type']) ? 'error' : ''; ?>">
                            <option value="">Select Type</option>
                            <option value="Helicopter" <?php echo ($aircraft['aircraft_type'] ?? '') == 'Helicopter' ? 'selected' : ''; ?>>Helicopter</option>
                            <option value="Fixed Wing" <?php echo ($aircraft['aircraft_type'] ?? '') == 'Fixed Wing' ? 'selected' : ''; ?>>Fixed Wing</option>
                            <option value="Jet" <?php echo ($aircraft['aircraft_type'] ?? '') == 'Jet' ? 'selected' : ''; ?>>Jet</option>
                            <option value="Propeller" <?php echo ($aircraft['aircraft_type'] ?? '') == 'Propeller' ? 'selected' : ''; ?>>Propeller</option>
                            <option value="Other">Other</option>
                        </select>
                        <?php if (isset($errors['aircraft_type'])): ?>
                            <small class="error-text"><?php echo $errors['aircraft_type']; ?></small>
                        <?php endif; ?>
                    </div>
                    
                    <div class="form-group" id="typeOtherWrapper" style="display: none;">
                        <label>Specify Aircraft Type</label>
                        <input type="text" name="aircraft_type_other" id="aircraft_type_other" class="form-control" 
                               value="<?php echo htmlspecialchars($aircraft['aircraft_type_other'] ?? ''); ?>" placeholder="Enter aircraft type">
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Model/Manufacturer</label>
                        <input type="text" name="model_manufacturer" value="<?php echo htmlspecialchars($aircraft['model_manufacturer'] ?? ''); ?>" 
                               required maxlength="100" class="form-control <?php echo isset($errors['model_manufacturer']) ? 'error' : ''; ?>">
                        <?php if (isset($errors['model_manufacturer'])): ?>
                            <small class="error-text"><?php echo $errors['model_manufacturer']; ?></small>
                        <?php endif; ?>
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Year of Manufacture</label>
                        <input type="number" name="year_manufacture" value="<?php echo htmlspecialchars($aircraft['year_manufacture'] ?? ''); ?>" 
                               required min="1900" max="<?php echo date('Y'); ?>" 
                               class="form-control <?php echo isset($errors['year_manufacture']) ? 'error' : ''; ?>">
                        <?php if (isset($errors['year_manufacture'])): ?>
                            <small class="error-text"><?php echo $errors['year_manufacture']; ?></small>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Tail Number</label>
                        <input type="text" name="tail_number" value="<?php echo htmlspecialchars($aircraft['tail_number'] ?? ''); ?>" 
                               required maxlength="50" class="form-control <?php echo isset($errors['tail_number']) ? 'error' : ''; ?>">
                        <?php if (isset($errors['tail_number'])): ?>
                            <small class="error-text"><?php echo $errors['tail_number']; ?></small>
                        <?php endif; ?>
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Chassis/Serial Number</label>
                        <input type="text" name="chassis_serial" value="<?php echo htmlspecialchars($aircraft['chassis_serial'] ?? ''); ?>" 
                               required maxlength="100" class="form-control <?php echo isset($errors['chassis_serial']) ? 'error' : ''; ?>">
                        <?php if (isset($errors['chassis_serial'])): ?>
                            <small class="error-text"><?php echo $errors['chassis_serial']; ?></small>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Engine Type</label>
                        <input type="text" name="engine_type" value="<?php echo htmlspecialchars($aircraft['engine_type'] ?? ''); ?>" 
                               required maxlength="100" class="form-control <?php echo isset($errors['engine_type']) ? 'error' : ''; ?>">
                        <?php if (isset($errors['engine_type'])): ?>
                            <small class="error-text"><?php echo $errors['engine_type']; ?></small>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            
            <!-- Operational Details -->
            <div class="form-section-inner">
                <div class="section-title">
                    <h3><i class="fas fa-chart-line"></i> Operational Details</h3>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Flight Hours</label>
                        <input type="number" step="0.1" name="flight_hours" value="<?php echo htmlspecialchars($aircraft['flight_hours'] ?? ''); ?>" 
                               min="0" class="form-control">
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Operational Status</label>
                        <select name="operational_status" required class="form-control <?php echo isset($errors['operational_status']) ? 'error' : ''; ?>">
                            <option value="">Select Status</option>
                            <option value="Operational" <?php echo ($aircraft['operational_status'] ?? '') == 'Operational' ? 'selected' : ''; ?>>Operational</option>
                            <option value="Maintenance" <?php echo ($aircraft['operational_status'] ?? '') == 'Maintenance' ? 'selected' : ''; ?>>Maintenance</option>
                            <option value="Grounded" <?php echo ($aircraft['operational_status'] ?? '') == 'Grounded' ? 'selected' : ''; ?>>Grounded</option>
                        </select>
                        <?php if (isset($errors['operational_status'])): ?>
                            <small class="error-text"><?php echo $errors['operational_status']; ?></small>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Installed Equipment</label>
                        <textarea name="installed_equipment" rows="3" class="form-control"><?php echo htmlspecialchars($aircraft['installed_equipment'] ?? ''); ?></textarea>
                    </div>
                </div>
            </div>
            
            <!-- Acquisition Details -->
            <div class="form-section-inner">
                <div class="section-title">
                    <h3><i class="fas fa-file-invoice-dollar"></i> Acquisition Details</h3>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Acquisition Type</label>
                        <select name="acquisition_type" required class="form-control <?php echo isset($errors['acquisition_type']) ? 'error' : ''; ?>">
                            <option value="">Select Type</option>
                            <option value="Purchase" <?php echo ($aircraft['acquisition_type'] ?? '') == 'Purchase' ? 'selected' : ''; ?>>Purchase</option>
                            <option value="Transfer" <?php echo ($aircraft['acquisition_type'] ?? '') == 'Transfer' ? 'selected' : ''; ?>>Transfer</option>
                            <option value="Donation" <?php echo ($aircraft['acquisition_type'] ?? '') == 'Donation' ? 'selected' : ''; ?>>Donation</option>
                            <option value="Lease" <?php echo ($aircraft['acquisition_type'] ?? '') == 'Lease' ? 'selected' : ''; ?>>Lease</option>
                        </select>
                        <?php if (isset($errors['acquisition_type'])): ?>
                            <small class="error-text"><?php echo $errors['acquisition_type']; ?></small>
                        <?php endif; ?>
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Acquisition Date</label>
                        <input type="date" name="acquisition_date" value="<?php echo htmlspecialchars($aircraft['acquisition_date'] ?? ''); ?>" 
                               required class="form-control <?php echo isset($errors['acquisition_date']) ? 'error' : ''; ?>">
                        <?php if (isset($errors['acquisition_date'])): ?>
                            <small class="error-text"><?php echo $errors['acquisition_date']; ?></small>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Capital Value (₦)</label>
                        <input type="number" step="0.01" name="capital_value" value="<?php echo htmlspecialchars($aircraft['capital_value'] ?? ''); ?>" 
                               required class="form-control <?php echo isset($errors['capital_value']) ? 'error' : ''; ?>">
                        <?php if (isset($errors['capital_value'])): ?>
                            <small class="error-text"><?php echo $errors['capital_value']; ?></small>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            
            <!-- Location Information -->
            <div class="form-section-inner">
                <div class="section-title">
                    <h3><i class="fas fa-map-marker-alt"></i> Location & Assignment</h3>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">State</label>
                        <select name="state_id" id="state_id" required 
                                class="form-control <?php echo isset($errors['state_id']) ? 'error' : ''; ?>">
                            <option value="">Select State</option>
                            <?php foreach ($states as $state): ?>
                            <option value="<?php echo $state['id']; ?>" 
                                    <?php echo ($aircraft['state_id'] ?? '') == $state['id'] ? 'selected' : ''; ?>>
                                <?php echo htmlspecialchars($state['state_name']); ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                        <?php if (isset($errors['state_id'])): ?>
                            <small class="error-text"><?php echo $errors['state_id']; ?></small>
                        <?php endif; ?>
                    </div>
                    
                    <div class="form-group">
                        <label class="required">LGA</label>
                        <select name="lga_id" id="lga_id" required 
                                class="form-control <?php echo isset($errors['lga_id']) ? 'error' : ''; ?>">
                            <option value="">Select State First</option>
                        </select>
                        <?php if (isset($errors['lga_id'])): ?>
                            <small class="error-text"><?php echo $errors['lga_id']; ?></small>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Zone</label>
                        <select name="zone_id" id="zone_id" required 
                                class="form-control <?php echo isset($errors['zone_id']) ? 'error' : ''; ?>">
                            <option value="">Select Zone</option>
                            <?php foreach ($zones as $zone): ?>
                            <option value="<?php echo $zone['id']; ?>" 
                                    <?php echo ($aircraft['zone_id'] ?? '') == $zone['id'] ? 'selected' : ''; ?>>
                                <?php echo htmlspecialchars($zone['zone_name']); ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                        <?php if (isset($errors['zone_id'])): ?>
                            <small class="error-text"><?php echo $errors['zone_id']; ?></small>
                        <?php endif; ?>
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Command</label>
                        <select name="command_id" id="command_id" required 
                                class="form-control <?php echo isset($errors['command_id']) ? 'error' : ''; ?>">
                            <option value="">Select Zone First</option>
                        </select>
                        <?php if (isset($errors['command_id'])): ?>
                            <small class="error-text"><?php echo $errors['command_id']; ?></small>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Storage Location</label>
                        <input type="text" name="storage_location" value="<?php echo htmlspecialchars($aircraft['storage_location'] ?? ''); ?>" 
                               required class="form-control">
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Assigned Unit/Pilot</label>
                        <input type="text" name="assigned_unit_pilot" value="<?php echo htmlspecialchars($aircraft['assigned_unit_pilot'] ?? ''); ?>" 
                               required class="form-control">
                    </div>
                </div>
            </div>
            
            <!-- Insurance & Maintenance -->
            <div class="form-section-inner">
                <div class="section-title">
                    <h3><i class="fas fa-shield-alt"></i> Insurance & Maintenance</h3>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Insurance Type</label>
                        <select name="insurance_type" required class="form-control <?php echo isset($errors['insurance_type']) ? 'error' : ''; ?>">
                            <option value="">Select Type</option>
                            <option value="Hull" <?php echo ($aircraft['insurance_type'] ?? '') == 'Hull' ? 'selected' : ''; ?>>Hull</option>
                            <option value="Liability" <?php echo ($aircraft['insurance_type'] ?? '') == 'Liability' ? 'selected' : ''; ?>>Liability</option>
                            <option value="Comprehensive" <?php echo ($aircraft['insurance_type'] ?? '') == 'Comprehensive' ? 'selected' : ''; ?>>Comprehensive</option>
                        </select>
                        <?php if (isset($errors['insurance_type'])): ?>
                            <small class="error-text"><?php echo $errors['insurance_type']; ?></small>
                        <?php endif; ?>
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Insurance Status</label>
                        <select name="insurance_status" id="insurance_status" required class="form-control <?php echo isset($errors['insurance_status']) ? 'error' : ''; ?>">
                            <option value="">Select Status</option>
                            <option value="Valid" <?php echo ($aircraft['insurance_status'] ?? '') == 'Valid' ? 'selected' : ''; ?>>Valid</option>
                            <option value="Expired" <?php echo ($aircraft['insurance_status'] ?? '') == 'Expired' ? 'selected' : ''; ?>>Expired</option>
                            <option value="Not Insured" <?php echo ($aircraft['insurance_status'] ?? '') == 'Not Insured' ? 'selected' : ''; ?>>Not Insured</option>
                        </select>
                        <?php if (isset($errors['insurance_status'])): ?>
                            <small class="error-text"><?php echo $errors['insurance_status']; ?></small>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Insurance Expiry Date</label>
                        <input type="date" name="insurance_expiry" id="insurance_expiry" 
                               value="<?php echo htmlspecialchars($aircraft['insurance_expiry'] ?? ''); ?>" 
                               class="form-control">
                    </div>
                    
                    <div class="form-group">
                        <label>Last Maintenance Date</label>
                        <input type="date" name="last_maintenance" value="<?php echo htmlspecialchars($aircraft['last_maintenance'] ?? ''); ?>" 
                               class="form-control">
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Next Overhaul Date</label>
                        <input type="date" name="next_overhaul" value="<?php echo htmlspecialchars($aircraft['next_overhaul'] ?? ''); ?>" 
                               class="form-control">
                    </div>
                </div>
            </div>
            
            <!-- Remarks -->
            <div class="form-section-inner">
                <div class="section-title">
                    <h3><i class="fas fa-sticky-note"></i> Remarks</h3>
                </div>
                
                <div class="form-group">
                    <textarea name="remarks" rows="3" class="form-control"><?php echo htmlspecialchars($aircraft['remarks'] ?? ''); ?></textarea>
                </div>
            </div>
            
            <!-- Existing Documents -->
            <?php if (!empty($documents)): ?>
            <div class="form-section-inner">
                <div class="section-title">
                    <h3><i class="fas fa-file-alt"></i> Existing Documents</h3>
                    <span class="badge badge-info"><?php echo count($documents); ?> document(s)</span>
                </div>
                
                <div class="existing-documents">
                    <?php foreach ($documents as $index => $doc): ?>
                    <div class="existing-doc-item" id="doc_<?php echo $doc['id']; ?>">
                        <input type="hidden" name="existing_docs[<?php echo $index; ?>][id]" value="<?php echo $doc['id']; ?>">
                        <div class="doc-info">
                            <i class="fas <?php 
                                $ext = pathinfo($doc['file_name'], PATHINFO_EXTENSION);
                                if ($ext == 'pdf') echo 'fa-file-pdf';
                                elseif (in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) echo 'fa-file-image';
                                elseif (in_array($ext, ['doc', 'docx'])) echo 'fa-file-word';
                                elseif (in_array($ext, ['xls', 'xlsx'])) echo 'fa-file-excel';
                                else echo 'fa-file';
                            ?>"></i>
                            <span><?php echo htmlspecialchars($doc['file_name']); ?></span>
                            <small>(<?php echo round($doc['file_size'] / 1024, 2); ?> KB)</small>
                            <?php if (!empty($doc['document_type'])): ?>
                            <span class="doc-type-badge"><?php echo htmlspecialchars($doc['document_type']); ?></span>
                            <?php endif; ?>
                        </div>
                        <div class="doc-actions">
                            <a href="<?php echo BASE_URL . '/' . $doc['file_path']; ?>" target="_blank" class="btn-icon" title="View">
                                <i class="fas fa-eye"></i>
                            </a>
                            <button type="button" class="btn-icon delete" onclick="removeDocument(this, <?php echo $doc['id']; ?>)">
                                <i class="fas fa-times"></i>
                            </button>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
            <?php endif; ?>
            
            <!-- New Document Upload Section -->
            <div class="form-section-inner">
                <div class="section-title">
                    <h3><i class="fas fa-file-upload"></i> Add New Documents</h3>
                    <span class="badge badge-info">Optional - Max 5MB per file</span>
                </div>
                
                <div class="document-upload-container">
                    <div class="document-types-grid" id="documentTypesContainer">
                        <!-- Document type rows will be added here dynamically -->
                    </div>
                    
                    <div class="document-actions">
                        <button type="button" class="btn btn-outline-primary" id="addDocumentBtn">
                            <i class="fas fa-plus-circle"></i> Add Another Document
                        </button>
                    </div>
                    
                    <div class="upload-info">
                        <div class="info-item">
                            <i class="fas fa-info-circle text-info"></i>
                            <span>Allowed file types: PDF, JPG, PNG, DOC, DOCX</span>
                        </div>
                        <div class="info-item">
                            <i class="fas fa-database text-warning"></i>
                            <span>Maximum file size: 5MB per file</span>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Form Actions -->
            <div class="form-actions">
                <button type="submit" class="btn btn-success submit-btn">
                    <i class="fas fa-save"></i> Update Aircraft
                </button>
                <a href="<?php echo BASE_URL; ?>/fleet/aircraft/show/<?php echo $aircraft['id'] ?? ''; ?>" class="btn btn-outline">
                    <i class="fas fa-times"></i> Cancel
                </a>
            </div>
        </form>
    </div>
</div>

<style>
:root {
    --primary-color: #2c3e50;
    --primary-light: #27ae60;
    --secondary-color: #27ae60;
    --secondary-dark: #219a52;
    --success-color: #27ae60;
    --danger-color: #e53e3e;
    --warning-color: #f39c12;
    --info-color: #3498db;
    --light-bg: #f8f9fa;
    --border-color: #e2e8f0;
    --text-primary: #2c3e50;
    --text-secondary: #7f8c8d;
}

/* Form Styles */
.form-section {
    background: white;
    border-radius: 10px;
    padding: 25px;
    margin-bottom: 25px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}

.form-section-inner {
    margin-bottom: 30px;
    padding-bottom: 20px;
    border-bottom: 1px solid var(--border-color);
}

.form-section-inner:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.section-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.section-title h3 {
    margin: 0;
    font-size: 1.2rem;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 8px;
}

.form-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    margin-bottom: 20px;
}

.form-group {
    display: flex;
    flex-direction: column;
}

.form-group label {
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.required::after {
    content: " *";
    color: var(--danger-color);
}

.form-control, .form-group select, .form-group input {
    padding: 10px 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 0.95rem;
    transition: all 0.3s;
    width: 100%;
}

.form-control:focus, .form-group select:focus, .form-group input:focus {
    outline: none;
    border-color: var(--success-color);
    box-shadow: 0 0 0 3px rgba(39, 174, 96, 0.2);
}

.form-control.error, .form-group select.error, .form-group input.error {
    border-color: var(--danger-color);
    background-color: #fff5f5;
}

.error-text {
    color: var(--danger-color);
    font-size: 0.85rem;
    margin-top: 5px;
}

/* Professional Document Upload Styles */
.document-upload-container {
    background: #f8fafc;
    border-radius: 12px;
    padding: 20px;
    border: 1px solid #e2e8f0;
    margin-top: 15px;
}

.document-types-grid {
    display: flex;
    flex-direction: column;
    gap: 15px;
    margin-bottom: 20px;
}

.document-row {
    display: grid;
    grid-template-columns: 250px 1fr 40px;
    gap: 15px;
    align-items: center;
    background: white;
    padding: 15px;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
    transition: all 0.3s ease;
    position: relative;
}

.document-row:hover {
    border-color: #27ae60;
    box-shadow: 0 2px 8px rgba(39, 174, 96, 0.1);
}

.document-type-select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    font-size: 0.95rem;
    background: white;
    cursor: pointer;
    transition: all 0.3s ease;
}

.document-type-select:hover,
.document-type-select:focus {
    border-color: #27ae60;
    outline: none;
    box-shadow: 0 0 0 3px rgba(39, 174, 96, 0.1);
}

.file-input-wrapper {
    position: relative;
    width: 100%;
}

.file-input-custom {
    width: 100%;
    padding: 10px 12px;
    border: 1px dashed #e2e8f0;
    border-radius: 6px;
    background: #f8fafc;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 10px;
    color: #64748b;
}

.file-input-custom:hover {
    border-color: #27ae60;
    background: #f0f9f4;
    color: #27ae60;
}

.file-input-custom i {
    font-size: 1.2rem;
}

.file-input-custom span {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-input-element {
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
}

.remove-document-btn {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    border: 1px solid #fee2e2;
    background: #fef2f2;
    color: #ef4444;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
}

.remove-document-btn:hover {
    background: #ef4444;
    color: white;
    border-color: #ef4444;
}

.remove-document-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background: #f1f5f9;
    color: #94a3b8;
    border-color: #e2e8f0;
}

.document-actions {
    display: flex;
    justify-content: center;
    margin: 15px 0;
}

.btn-outline-primary {
    background: white;
    border: 1px solid #27ae60;
    color: #27ae60;
    padding: 10px 20px;
    border-radius: 6px;
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.btn-outline-primary:hover {
    background: #27ae60;
    color: white;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(39, 174, 96, 0.2);
}

.upload-info {
    margin-top: 20px;
    padding: 15px;
    background: #f1f5f9;
    border-radius: 8px;
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
}

.info-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.9rem;
    color: #475569;
}

/* Existing Documents Styles */
.existing-documents {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.existing-doc-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 15px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    transition: all 0.3s ease;
}

.existing-doc-item:hover {
    border-color: #27ae60;
    background: #f0f9f4;
}

.doc-info {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
}

.doc-info i {
    font-size: 1.2rem;
    color: #27ae60;
}

.doc-info span {
    font-weight: 500;
    color: #2c3e50;
}

.doc-info small {
    color: #7f8c8d;
    font-size: 0.85rem;
}

.doc-type-badge {
    background: #e3f2fd;
    color: #1565c0;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    margin-left: 8px;
}

.doc-actions {
    display: flex;
    gap: 5px;
}

.btn-icon {
    width: 32px;
    height: 32px;
    border-radius: 4px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #666;
    text-decoration: none;
    transition: all 0.3s;
    background: transparent;
    border: none;
    cursor: pointer;
}

.btn-icon:hover {
    background: #f0f0f0;
    color: #27ae60;
}

.btn-icon.delete:hover {
    background: #fee;
    color: #e53e3e;
}

/* Badge styles */
.badge {
    display: inline-block;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 500;
}

.badge-info {
    background: #e0f2f1;
    color: #00695c;
}

/* Form Actions */
.form-actions {
    display: flex;
    gap: 15px;
    margin-top: 30px;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    text-decoration: none;
}

.btn-success {
    background: var(--success-color);
    color: white;
}

.btn-success:hover {
    background: var(--secondary-dark);
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(39, 174, 96, 0.3);
}

.btn-secondary {
    background: var(--text-secondary);
    color: white;
}

.btn-secondary:hover {
    background: #6c757d;
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-primary);
}

.btn-outline:hover {
    background: var(--light-bg);
    border-color: var(--success-color);
    color: var(--success-color);
}

.btn-info {
    background: var(--info-color);
    color: white;
}

.btn-info:hover {
    background: #2980b9;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(52, 152, 219, 0.3);
}

.submit-btn {
    min-width: 150px;
}

@media (max-width: 768px) {
    .form-row {
        grid-template-columns: 1fr;
        gap: 15px;
    }
    
    .form-actions {
        flex-direction: column;
    }
    
    .form-actions .btn {
        width: 100%;
    }
    
    .document-row {
        grid-template-columns: 1fr;
    }
    
    .existing-doc-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }
    
    .doc-actions {
        width: 100%;
        justify-content: flex-end;
    }
}
</style>

<script>
// Define base URL for API calls
const baseUrl = '<?php echo BASE_URL; ?>';
const documentTypes = <?php echo json_encode($documentTypes); ?>;

let documentCount = 0;

document.addEventListener('DOMContentLoaded', function() {
    // Add first document row by default
    addDocumentRow();
    
    // Aircraft type other field
    const typeSelect = document.getElementById('aircraft_type');
    const typeWrapper = document.getElementById('typeOtherWrapper');
    const typeOther = document.getElementById('aircraft_type_other');
    
    if (typeSelect) {
        typeSelect.addEventListener('change', function() {
            if (this.value === 'Other') {
                typeWrapper.style.display = 'block';
                typeOther.required = true;
            } else {
                typeWrapper.style.display = 'none';
                typeOther.required = false;
            }
        });
        
        <?php if (($aircraft['aircraft_type'] ?? '') === 'Other'): ?>
        typeSelect.value = 'Other';
        typeWrapper.style.display = 'block';
        <?php endif; ?>
    }
    
    // Insurance status toggle
    const insuranceStatus = document.getElementById('insurance_status');
    const insuranceExpiry = document.getElementById('insurance_expiry');
    
    if (insuranceStatus && insuranceExpiry) {
        insuranceStatus.addEventListener('change', function() {
            if (this.value === 'Valid') {
                insuranceExpiry.required = true;
            } else {
                insuranceExpiry.required = false;
                insuranceExpiry.value = '';
            }
        });
        
        // Initialize insurance expiry required state
        if (insuranceStatus.value === 'Valid') {
            insuranceExpiry.required = true;
        }
    }
    
    // State to LGA dropdown
    const stateSelect = document.getElementById('state_id');
    const lgaSelect = document.getElementById('lga_id');
    
    if (stateSelect) {
        stateSelect.addEventListener('change', function() {
            const stateId = this.value;
            
            if (!stateId) {
                lgaSelect.innerHTML = '<option value="">Select State First</option>';
                return;
            }
            
            const apiUrl = baseUrl.replace(/\/$/, '') + '/api/get_lgas.php?state_id=' + stateId;
            
            fetch(apiUrl)
                .then(response => response.json())
                .then(data => {
                    if (data.error) {
                        lgaSelect.innerHTML = '<option value="">Error loading LGAs</option>';
                        return;
                    }
                    
                    lgaSelect.innerHTML = '<option value="">Select LGA</option>';
                    if (data && data.length > 0) {
                        data.forEach(lga => {
                            const option = document.createElement('option');
                            option.value = lga.id;
                            option.textContent = lga.lga_name;
                            lgaSelect.appendChild(option);
                        });
                        
                        <?php if (!empty($aircraft['lga_id'])): ?>
                        lgaSelect.value = '<?php echo $aircraft['lga_id']; ?>';
                        <?php endif; ?>
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    lgaSelect.innerHTML = '<option value="">Error loading LGAs</option>';
                });
        });
        
        <?php if (!empty($aircraft['state_id'])): ?>
        stateSelect.value = '<?php echo $aircraft['state_id']; ?>';
        stateSelect.dispatchEvent(new Event('change'));
        <?php endif; ?>
    }
    
    // Zone to Command dropdown
    const zoneSelect = document.getElementById('zone_id');
    const commandSelect = document.getElementById('command_id');
    
    if (zoneSelect) {
        zoneSelect.addEventListener('change', function() {
            const zoneId = this.value;
            
            if (!zoneId) {
                commandSelect.innerHTML = '<option value="">Select Zone First</option>';
                return;
            }
            
            const apiUrl = baseUrl.replace(/\/$/, '') + '/api/get_commands.php?zone_id=' + zoneId;
            
            fetch(apiUrl)
                .then(response => response.json())
                .then(data => {
                    if (data.error) {
                        commandSelect.innerHTML = '<option value="">Error loading commands</option>';
                        return;
                    }
                    
                    commandSelect.innerHTML = '<option value="">Select Command</option>';
                    if (data && data.length > 0) {
                        data.forEach(cmd => {
                            const option = document.createElement('option');
                            option.value = cmd.id;
                            option.textContent = cmd.command_name;
                            commandSelect.appendChild(option);
                        });
                        
                        <?php if (!empty($aircraft['command_id'])): ?>
                        commandSelect.value = '<?php echo $aircraft['command_id']; ?>';
                        <?php endif; ?>
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    commandSelect.innerHTML = '<option value="">Error loading commands</option>';
                });
        });
        
        <?php if (!empty($aircraft['zone_id'])): ?>
        zoneSelect.value = '<?php echo $aircraft['zone_id']; ?>';
        zoneSelect.dispatchEvent(new Event('change'));
        <?php endif; ?>
    }
    
    // Add Document button click handler
    document.getElementById('addDocumentBtn').addEventListener('click', function() {
        addDocumentRow();
    });
});

function addDocumentRow() {
    const container = document.getElementById('documentTypesContainer');
    const rowId = 'doc_row_' + documentCount;
    
    const row = document.createElement('div');
    row.className = 'document-row';
    row.id = rowId;
    
    let options = '<option value="">Select Document Type</option>';
    for (const [value, label] of Object.entries(documentTypes)) {
        options += `<option value="${value}">${label}</option>`;
    }
    
    row.innerHTML = `
        <select name="new_document_types[]" class="document-type-select">
            ${options}
        </select>
        <div class="file-input-wrapper">
            <div class="file-input-custom" id="custom_${rowId}">
                <i class="fas fa-cloud-upload-alt"></i>
                <span>Choose file...</span>
            </div>
            <input type="file" name="new_documents[]" class="file-input-element" 
                   accept=".pdf,.jpg,.jpeg,.png,.doc,.docx" 
                   data-custom="custom_${rowId}"
                   onchange="updateFileLabel(this, '${rowId}')">
        </div>
        <button type="button" class="remove-document-btn" onclick="removeDocumentRow('${rowId}')" ${documentCount === 0 ? 'disabled' : ''}>
            <i class="fas fa-times"></i>
        </button>
    `;
    
    container.appendChild(row);
    documentCount++;
}

function updateFileLabel(input, rowId) {
    const customDiv = document.getElementById('custom_' + rowId);
    if (customDiv) {
        if (input.files && input.files[0]) {
            const file = input.files[0];
            const fileSizeMB = (file.size / 1024 / 1024).toFixed(2);
            
            if (file.size > 5 * 1024 * 1024) {
                alert('File ' + file.name + ' exceeds 5MB limit. Please choose a smaller file.');
                input.value = '';
                customDiv.innerHTML = '<i class="fas fa-cloud-upload-alt"></i><span>Choose file...</span>';
                return;
            }
            
            const allowedTypes = ['pdf', 'jpg', 'jpeg', 'png', 'doc', 'docx'];
            const fileExt = file.name.split('.').pop().toLowerCase();
            
            if (!allowedTypes.includes(fileExt)) {
                alert('File type not allowed. Allowed types: PDF, JPG, PNG, DOC, DOCX');
                input.value = '';
                customDiv.innerHTML = '<i class="fas fa-cloud-upload-alt"></i><span>Choose file...</span>';
                return;
            }
            
            customDiv.innerHTML = `<i class="fas fa-check-circle text-success"></i><span>${file.name} (${fileSizeMB} MB)</span>`;
            customDiv.style.borderColor = '#27ae60';
            customDiv.style.background = '#f0f9f4';
        } else {
            customDiv.innerHTML = '<i class="fas fa-cloud-upload-alt"></i><span>Choose file...</span>';
        }
    }
}

function removeDocumentRow(rowId) {
    const row = document.getElementById(rowId);
    if (row) {
        row.remove();
        documentCount--;
    }
}

function removeDocument(button, docId) {
    if (confirm('Are you sure you want to remove this document?')) {
        const input = document.createElement('input');
        input.type = 'hidden';
        input.name = 'remove_docs[]';
        input.value = docId;
        document.getElementById('aircraftForm').appendChild(input);
        
        button.closest('.existing-doc-item').remove();
        
        if (typeof showNotification === 'function') {
            showNotification('success', 'Document marked for removal');
        } else {
            alert('Document marked for removal');
        }
    }
}
</script>

<?php require_once __DIR__ . '/../../layouts/footer.php'; ?>