<?php
$title = 'Marine Asset Details';
$active = 'marine';
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-eye"></i>
                Marine Asset Details
            </h1>
            <p><?php echo Security::escape($marine['boat_type']); ?> - <?php echo Security::escape($marine['registration_number']); ?></p>
        </div>
        <div class="header-actions">
            <a href="<?php echo BASE_URL; ?>/fleet/marine" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back to Marine Assets
            </a>
            <?php if (Auth::can('fleet.edit')): ?>
            <a href="<?php echo BASE_URL; ?>/fleet/marine/edit/<?php echo $marine['id']; ?>" class="btn btn-success">
                <i class="fas fa-edit"></i> Edit Asset
            </a>
            <?php endif; ?>
            <button class="btn btn-outline" onclick="window.print()">
                <i class="fas fa-print"></i> Print
            </button>
        </div>
    </div>

    <!-- Status Alerts -->
    <?php
    if (!empty($marine['next_dry_docking'])) {
        $today = time();
        $dockingDate = strtotime($marine['next_dry_docking']);
        $daysToDocking = round(($dockingDate - $today) / (60 * 60 * 24));
        
        if ($daysToDocking <= 30 && $daysToDocking >= 0):
    ?>
    <div class="status-banner status-warning">
        <div class="status-icon">
            <i class="fas fa-ship"></i>
        </div>
        <div class="status-content">
            <h3>Dry Docking Due Soon</h3>
            <p>Vessel dry docking is due in <?php echo $daysToDocking; ?> days (<?php echo date('d/m/Y', strtotime($marine['next_dry_docking'])); ?>).</p>
        </div>
    </div>
    <?php 
        elseif ($daysToDocking < 0):
    ?>
    <div class="status-banner status-danger">
        <div class="status-icon">
            <i class="fas fa-exclamation-circle"></i>
        </div>
        <div class="status-content">
            <h3>Dry Docking Overdue</h3>
            <p>Vessel dry docking was due on <?php echo date('d/m/Y', strtotime($marine['next_dry_docking'])); ?>. Please schedule maintenance immediately.</p>
        </div>
    </div>
    <?php 
        endif;
    }
    ?>

    <!-- Asset Information Grid -->
    <div class="asset-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($marine['asset_code']); ?></span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Use/Purpose:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['use_purpose']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Ownership:</div>
                    <div class="detail-value">
                        <span class="badge badge-info"><?php echo Security::escape($marine['ownership_type']); ?></span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Boat Type:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['boat_type']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Hull ID:</div>
                    <div class="detail-value highlight"><?php echo Security::escape($marine['hull_identification']); ?></div>
                </div>
            </div>
        </div>

        <!-- Engine Details Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-engine"></i> Engine Details</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Engine Type:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['engine_type']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Engine Capacity:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['engine_capacity']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Number of Engines:</div>
                    <div class="detail-value"><?php echo $marine['number_engines']; ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Registration:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['registration_number']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Fuel Type:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['fuel_type']); ?></div>
                </div>
            </div>
        </div>

        <!-- Acquisition 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">Type:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['acquisition_type']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Date:</div>
                    <div class="detail-value"><?php echo date('d/m/Y', strtotime($marine['acquisition_date'])); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Capital Value:</div>
                    <div class="detail-value highlight">₦<?php echo number_format($marine['capital_value'], 2); ?></div>
                </div>
            </div>
        </div>

        <!-- Location Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-map-marker-alt"></i> Location & Assignment</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">State:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['state_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">LGA:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['lga_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Zone:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['zone_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Command:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['command_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Docking Location:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['docking_location']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Assigned Crew/Unit:</div>
                    <div class="detail-value"><?php echo Security::escape($marine['assigned_crew_unit']); ?></div>
                </div>
            </div>
        </div>

        <!-- Status Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-clipboard-check"></i> Status</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Operational Status:</div>
                    <div class="detail-value">
                        <?php 
                        $statusClass = '';
                        if ($marine['operational_status'] == 'Operational') $statusClass = 'status-active';
                        elseif ($marine['operational_status'] == 'Maintenance') $statusClass = 'status-warning';
                        elseif ($marine['operational_status'] == 'Docked') $statusClass = 'status-info';
                        elseif ($marine['operational_status'] == 'Decommissioned') $statusClass = 'status-rejected';
                        ?>
                        <span class="status-badge <?php echo $statusClass; ?>">
                            <?php echo Security::escape($marine['operational_status']); ?>
                        </span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Condition:</div>
                    <div class="detail-value">
                        <?php 
                        $conditionClass = '';
                        if ($marine['condition'] == 'Excellent') $conditionClass = 'status-active';
                        elseif ($marine['condition'] == 'Good') $conditionClass = 'status-good';
                        elseif ($marine['condition'] == 'Fair') $conditionClass = 'status-warning';
                        elseif ($marine['condition'] == 'Poor') $conditionClass = 'status-rejected';
                        ?>
                        <span class="status-badge <?php echo $conditionClass; ?>">
                            <?php echo Security::escape($marine['condition']); ?>
                        </span>
                    </div>
                </div>
            </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">
                <?php if ($marine['last_dry_docking']): ?>
                <div class="detail-row">
                    <div class="detail-label">Last Dry Docking:</div>
                    <div class="detail-value"><?php echo date('d/m/Y', strtotime($marine['last_dry_docking'])); ?></div>
                </div>
                <?php endif; ?>
                <?php if ($marine['next_dry_docking']): ?>
                <div class="detail-row">
                    <div class="detail-label">Next Dry Docking:</div>
                    <div class="detail-value">
                        <?php echo date('d/m/Y', strtotime($marine['next_dry_docking'])); ?>
                        <?php
                        $daysToDocking = round((strtotime($marine['next_dry_docking']) - time()) / (60 * 60 * 24));
                        if ($daysToDocking <= 30 && $daysToDocking >= 0) {
                            echo '<span class="days-remaining warning">(' . $daysToDocking . ' days left)</span>';
                        } elseif ($daysToDocking < 0) {
                            echo '<span class="days-remaining expired">(Overdue)</span>';
                        }
                        ?>
                    </div>
                </div>
                <?php endif; ?>
                <?php if (!empty($marine['onboard_equipment'])): ?>
                <div class="detail-row full-width">
                    <div class="detail-label">Onboard Equipment:</div>
                    <div class="detail-value"><?php echo nl2br(Security::escape($marine['onboard_equipment'])); ?></div>
                </div>
                <?php endif; ?>
            </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($marine['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($marine['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($marine['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($marine['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/marine/edit/<?php echo $marine['id']; ?>" class="btn btn-success">
            <i class="fas fa-edit"></i> Edit Asset
        </a>
        <?php endif; ?>
        
        <?php if (Auth::can('fleet.delete')): ?>
        <a href="<?php echo BASE_URL; ?>/fleet/marine/delete/<?php echo $marine['id']; ?>" class="btn btn-danger" 
           onclick="return confirm('Are you sure you want to delete this marine asset? This action cannot be undone.')">
            <i class="fas fa-trash"></i> Delete Asset
        </a>
        <?php endif; ?>
        
        <a href="<?php echo BASE_URL; ?>/fleet/marine" class="btn btn-secondary">
            <i class="fas fa-arrow-left"></i> Back to Marine Assets
        </a>
    </div>
</div>

<style>
/* Status Badge for Info */
.status-info {
    background: #d1ecf1;
    color: #0c5460;
}

/* Days remaining badge */
.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;
}

/* All other styles match the aircraft and vehicle detail pages */
</style>

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