<?php
$title = 'Motorcycle Details';
$active = 'fleet';
require_once __DIR__ . '/../../layouts/header.php';
require_once __DIR__ . '/../../layouts/sidebar.php';
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-motorcycle"></i>
                Motorcycle Details: <?php echo Security::escape($motorcycle['asset_code']); ?>
            </h1>
            <p>Complete information for motorcycle asset</p>
        </div>
        <div class="header-actions">
            <a href="<?php echo BASE_URL; ?>/fleet/motorcycles" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back to List
            </a>
            <?php if (Auth::can('fleet.edit')): ?>
            <a href="<?php echo BASE_URL; ?>/fleet/motorcycles/edit/<?php echo $motorcycle['id']; ?>" class="btn btn-success">
                <i class="fas fa-edit"></i> Edit Motorcycle
            </a>
            <?php endif; ?>
            <button class="btn btn-outline" onclick="window.print()">
                <i class="fas fa-print"></i> Print
            </button>
        </div>
    </div>

    <!-- Insurance Status Banner -->
    <?php if (!empty($motorcycle['insurance_expiry'])): ?>
    <?php
    $today = time();
    $expiry = strtotime($motorcycle['insurance_expiry']);
    $daysRemaining = round(($expiry - $today) / (60 * 60 * 24));
    
    if ($motorcycle['insurance_status'] == 'Valid' && $daysRemaining <= 30):
    ?>
    <div class="status-banner warning">
        <div class="status-icon">
            <i class="fas fa-exclamation-triangle"></i>
        </div>
        <div class="status-content">
            <h3>Insurance Alert</h3>
            <p>Insurance will expire in <?php echo $daysRemaining; ?> days (<?php echo date('d/m/Y', strtotime($motorcycle['insurance_expiry'])); ?>). Please renew.</p>
        </div>
    </div>
    <?php elseif ($motorcycle['insurance_status'] == 'Expired'): ?>
    <div class="status-banner danger">
        <div class="status-icon">
            <i class="fas fa-exclamation-circle"></i>
        </div>
        <div class="status-content">
            <h3>Insurance Alert</h3>
            <p>Insurance has expired on <?php echo date('d/m/Y', strtotime($motorcycle['insurance_expiry'])); ?>. Immediate action required.</p>
        </div>
    </div>
    <?php endif; ?>
    <?php endif; ?>

    <!-- Motorcycle Details Grid -->
    <div class="details-grid">
        <!-- Basic Information Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-info-circle"></i> Basic Information</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Asset Code:</div>
                    <div class="detail-value">
                        <span class="asset-code"><?php echo Security::escape($motorcycle['asset_code']); ?></span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Use Purpose:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['use_purpose']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Ownership Type:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['ownership_type']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Motorcycle Type:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['motorcycle_type']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Make/Model:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['make_model']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Engine Capacity:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['engine_capacity']); ?></div>
                </div>
            </div>
        </div>

        <!-- Identification Numbers Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-hashtag"></i> Identification Numbers</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Chassis Number:</div>
                    <div class="detail-value">
                        <span class="serial-number"><?php echo Security::escape($motorcycle['chassis_number']); ?></span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Engine Number:</div>
                    <div class="detail-value">
                        <span class="serial-number"><?php echo Security::escape($motorcycle['engine_number']); ?></span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Registration Number:</div>
                    <div class="detail-value">
                        <span class="serial-number"><?php echo Security::escape($motorcycle['registration_number']); ?></span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Fuel Type:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['fuel_type']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Current Mileage:</div>
                    <div class="detail-value"><?php echo number_format($motorcycle['current_mileage']); ?> km</div>
                </div>
            </div>
        </div>

        <!-- Location Information Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-map-marker-alt"></i> Location Information</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">State:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['state_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">LGA:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['lga_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Zone:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['zone_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Command:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['command_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Current Location:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['current_location'] ?? 'Not specified'); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Assigned To:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['assigned_officer_unit'] ?? 'Not assigned'); ?></div>
                </div>
            </div>
        </div>

        <!-- Acquisition Details Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-file-invoice-dollar"></i> Acquisition Details</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Acquisition Type:</div>
                    <div class="detail-value"><?php echo Security::escape($motorcycle['acquisition_type']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Acquisition Date:</div>
                    <div class="detail-value"><?php echo date('d/m/Y', strtotime($motorcycle['acquisition_date'])); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Purchase Value:</div>
                    <div class="detail-value highlight">₦<?php echo number_format($motorcycle['purchase_value'], 2); ?></div>
                </div>
                <?php if (!empty($motorcycle['current_value'])): ?>
                <div class="detail-row">
                    <div class="detail-label">Current Value:</div>
                    <div class="detail-value">₦<?php echo number_format($motorcycle['current_value'], 2); ?></div>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Condition & Insurance Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-clipboard-check"></i> Condition & Insurance</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Condition:</div>
                    <div class="detail-value">
                        <?php 
                        $conditionClass = '';
                        if ($motorcycle['condition'] == 'Excellent') $conditionClass = 'status-active';
                        elseif ($motorcycle['condition'] == 'Good') $conditionClass = 'status-good';
                        elseif ($motorcycle['condition'] == 'Fair') $conditionClass = 'status-warning';
                        elseif ($motorcycle['condition'] == 'Poor') $conditionClass = 'status-rejected';
                        ?>
                        <span class="status-badge <?php echo $conditionClass; ?>">
                            <?php echo Security::escape($motorcycle['condition']); ?>
                        </span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Insurance Status:</div>
                    <div class="detail-value">
                        <?php 
                        $insClass = '';
                        if ($motorcycle['insurance_status'] == 'Valid') $insClass = 'status-active';
                        elseif ($motorcycle['insurance_status'] == 'Expired') $insClass = 'status-rejected';
                        else $insClass = 'status-warning';
                        ?>
                        <span class="status-badge <?php echo $insClass; ?>">
                            <?php echo Security::escape($motorcycle['insurance_status']); ?>
                        </span>
                    </div>
                </div>
                <?php if (!empty($motorcycle['insurance_expiry'])): ?>
                <div class="detail-row">
                    <div class="detail-label">Insurance Expiry:</div>
                    <div class="detail-value">
                        <?php echo date('d/m/Y', strtotime($motorcycle['insurance_expiry'])); ?>
                        <?php if (isset($daysRemaining) && $motorcycle['insurance_status'] == 'Valid'): ?>
                            <span class="days-remaining <?php echo $daysRemaining <= 30 ? 'warning' : ''; ?>">
                                (<?php echo $daysRemaining; ?> days left)
                            </span>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Maintenance Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-tools"></i> Maintenance</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Last Serviced:</div>
                    <div class="detail-value">
                        <?php echo $motorcycle['last_serviced_date'] ? date('d/m/Y', strtotime($motorcycle['last_serviced_date'])) : 'Never'; ?>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Next Service Due:</div>
                    <div class="detail-value">
                        <?php if ($motorcycle['next_service_due']): ?>
                            <?php echo date('d/m/Y', strtotime($motorcycle['next_service_due'])); ?>
                            <?php 
                            $serviceDue = strtotime($motorcycle['next_service_due']);
                            $daysToService = round(($serviceDue - $today) / (60 * 60 * 24));
                            if ($daysToService <= 30 && $daysToService > 0):
                            ?>
                            <span class="days-remaining warning">(<?php echo $daysToService; ?> days)</span>
                            <?php elseif ($daysToService <= 0): ?>
                            <span class="days-remaining expired">(Overdue)</span>
                            <?php endif; ?>
                        <?php else: ?>
                            Not scheduled
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Remarks Card -->
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-sticky-note"></i> Remarks</h3>
            </div>
            <div class="card-body">
                <p class="remarks-text"><?php echo nl2br(Security::escape($motorcycle['remarks'] ?? 'No remarks')); ?></p>
            </div>
        </div>

        <!-- Documents Card -->
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-file-alt"></i> Attached Documents</h3>
            </div>
            <div class="card-body">
                <?php if (empty($documents)): ?>
                    <div class="empty-state">
                        <i class="fas fa-file"></i>
                        <p>No documents attached</p>
                    </div>
                <?php else: ?>
                    <div class="documents-grid">
                        <?php foreach ($documents as $doc): ?>
                        <div class="document-item">
                            <div class="document-icon">
                                <?php
                                $ext = pathinfo($doc['file_name'], PATHINFO_EXTENSION);
                                $icon = 'fa-file';
                                if (in_array($ext, ['pdf'])) $icon = 'fa-file-pdf';
                                elseif (in_array($ext, ['jpg', 'jpeg', 'png'])) $icon = 'fa-file-image';
                                elseif (in_array($ext, ['doc', 'docx'])) $icon = 'fa-file-word';
                                ?>
                                <i class="fas <?php echo $icon; ?>"></i>
                            </div>
                            <div class="document-info">
                                <h4><?php echo Security::escape($doc['file_name']); ?></h4>
                                <p>
                                    <span class="file-size"><?php echo round($doc['file_size'] / 1024, 2); ?> KB</span>
                                    <span class="upload-date">Uploaded: <?php echo date('d/m/Y', strtotime($doc['uploaded_at'])); ?></span>
                                </p>
                            </div>
                            <div class="document-actions">
                                <a href="<?php echo BASE_URL . '/' . $doc['file_path']; ?>" target="_blank" class="btn-icon" title="View">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <a href="<?php echo BASE_URL . '/' . $doc['file_path']; ?>" download class="btn-icon" title="Download">
                                    <i class="fas fa-download"></i>
                                </a>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Metadata Card -->
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-history"></i> Record Information</h3>
            </div>
            <div class="card-body">
                <div class="metadata-grid">
                    <div class="metadata-item">
                        <span class="metadata-label">Created By:</span>
                        <span class="metadata-value"><?php echo Security::escape($motorcycle['created_by_name'] ?? 'System'); ?></span>
                    </div>
                    <div class="metadata-item">
                        <span class="metadata-label">Created At:</span>
                        <span class="metadata-value"><?php echo date('d/m/Y H:i:s', strtotime($motorcycle['created_at'])); ?></span>
                    </div>
                    <div class="metadata-item">
                        <span class="metadata-label">Last Updated:</span>
                        <span class="metadata-value"><?php echo date('d/m/Y H:i:s', strtotime($motorcycle['updated_at'])); ?></span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Action Buttons -->
    <div class="action-buttons-group">
        <?php if (Auth::can('fleet.edit')): ?>
        <a href="<?php echo BASE_URL; ?>/fleet/motorcycles/edit/<?php echo $motorcycle['id']; ?>" class="btn btn-success">
            <i class="fas fa-edit"></i> Edit Motorcycle
        </a>
        <?php endif; ?>
        
        <?php if (Auth::can('fleet.delete')): ?>
        <a href="<?php echo BASE_URL; ?>/fleet/motorcycles/delete/<?php echo $motorcycle['id']; ?>" class="btn btn-danger" 
           onclick="return confirm('Are you sure you want to delete this motorcycle? This action cannot be undone.')">
            <i class="fas fa-trash"></i> Delete Motorcycle
        </a>
        <?php endif; ?>
        
        <a href="<?php echo BASE_URL; ?>/fleet/motorcycles" class="btn btn-secondary">
            <i class="fas fa-arrow-left"></i> Back to List
        </a>
    </div>
</div>

<style>
.details-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    margin-bottom: 30px;
}

.detail-card {
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    overflow: hidden;
}

.detail-card.full-width {
    grid-column: 1 / -1;
}

.card-header {
    padding: 15px 20px;
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-light) 100%);
    color: white;
}

.card-header h3 {
    margin: 0;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    gap: 8px;
}

.card-body {
    padding: 20px;
}

.detail-row {
    display: flex;
    margin-bottom: 12px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 8px;
}

.detail-row:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.detail-label {
    flex: 0 0 120px;
    font-weight: 600;
    color: var(--text-secondary);
}

.detail-value {
    flex: 1;
    color: var(--text-primary);
    word-break: break-word;
}

.highlight {
    font-weight: 700;
    color: var(--success-color);
    font-size: 1.2rem;
}

.asset-code {
    font-family: 'Courier New', monospace;
    font-weight: 600;
    color: var(--primary-color);
    background: var(--light-bg);
    padding: 4px 8px;
    border-radius: 4px;
}

.serial-number {
    font-family: 'Courier New', monospace;
    background: var(--light-bg);
    padding: 2px 6px;
    border-radius: 4px;
}

/* Status Banner */
.status-banner {
    background: white;
    border-radius: 10px;
    padding: 20px 25px;
    margin-bottom: 25px;
    display: flex;
    align-items: center;
    gap: 20px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    border-left: 6px solid;
}

.status-banner.warning {
    border-left-color: var(--warning-color);
}

.status-banner.danger {
    border-left-color: var(--danger-color);
}

.status-icon i {
    font-size: 2.5rem;
}

.status-banner.warning .status-icon i {
    color: var(--warning-color);
}

.status-banner.danger .status-icon i {
    color: var(--danger-color);
}

.status-content h3 {
    margin: 0 0 5px 0;
}

.status-content p {
    margin: 0;
}

/* Status Badges */
.status-badge {
    display: inline-block;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: 600;
}

.status-active {
    background: #d4edda;
    color: #155724;
}

.status-good {
    background: #d1ecf1;
    color: #0c5460;
}

.status-warning {
    background: #fff3cd;
    color: #856404;
}

.status-rejected {
    background: #f8d7da;
    color: #721c24;
}

/* Days remaining */
.days-remaining {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.8rem;
    font-weight: 600;
    margin-left: 8px;
}

.days-remaining.warning {
    background: #fff3cd;
    color: #856404;
}

.days-remaining.expired {
    background: #f8d7da;
    color: #721c24;
}

/* Documents Grid */
.documents-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 15px;
}

.document-item {
    display: flex;
    align-items: center;
    padding: 15px;
    background: var(--light-bg);
    border-radius: 8px;
    border: 1px solid var(--border-color);
    transition: all 0.3s;
}

.document-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}

.document-icon {
    margin-right: 15px;
    font-size: 2rem;
    color: var(--success-color);
}

.document-info {
    flex: 1;
}

.document-info h4 {
    margin: 0 0 5px 0;
    font-size: 0.95rem;
    color: var(--text-primary);
    word-break: break-word;
}

.document-info p {
    margin: 0;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.file-size {
    margin-right: 10px;
}

.upload-date {
    opacity: 0.8;
}

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

/* Metadata Grid */
.metadata-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 15px;
}

.metadata-item {
    padding: 10px;
    background: var(--light-bg);
    border-radius: 6px;
}

.metadata-label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 5px;
}

.metadata-value {
    display: block;
    font-weight: 600;
    color: var(--text-primary);
}

/* Empty State */
.empty-state {
    text-align: center;
    padding: 40px;
    color: var(--text-secondary);
}

.empty-state i {
    font-size: 3rem;
    margin-bottom: 15px;
    opacity: 0.5;
}

/* Action Buttons */
.action-buttons-group {
    display: flex;
    gap: 15px;
    margin-top: 20px;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
}

@media (max-width: 768px) {
    .details-grid {
        grid-template-columns: 1fr;
    }
    
    .detail-row {
        flex-direction: column;
    }
    
    .detail-label {
        margin-bottom: 5px;
    }
    
    .documents-grid {
        grid-template-columns: 1fr;
    }
    
    .metadata-grid {
        grid-template-columns: 1fr;
    }
    
    .action-buttons-group {
        flex-direction: column;
    }
}

@media print {
    .header-actions,
    .action-buttons-group,
    .sidebar,
    footer {
        display: none !important;
    }
    
    .main-content {
        margin-left: 0 !important;
        width: 100% !important;
        padding: 20px !important;
    }
    
    .detail-card {
        break-inside: avoid;
        box-shadow: none !important;
        border: 1px solid #ddd !important;
    }
}
</style>

