<?php
$title = 'Weapon Details';
$active = 'weapons';
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>
                Weapon Details
            </h1>
            <p><?php echo Security::escape($weapon['make_model']); ?> - <?php echo Security::escape($weapon['weapon_id']); ?></p>
        </div>
        <div class="header-actions">
            <a href="<?php echo BASE_URL; ?>/weapons" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back to Weapons
            </a>
            <?php if (Auth::can('weapons.edit')): ?>
            <a href="<?php echo BASE_URL; ?>/weapons/edit/<?php echo $weapon['id']; ?>" class="btn btn-success">
                <i class="fas fa-edit"></i> Edit Weapon
            </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 ($weapon['condition'] == 'Unserviceable'):
    ?>
    <div class="status-banner status-danger">
        <div class="status-icon">
            <i class="fas fa-exclamation-circle"></i>
        </div>
        <div class="status-content">
            <h3>Weapon Unserviceable</h3>
            <p>This weapon has been marked as unserviceable. Please take appropriate action.</p>
        </div>
    </div>
    <?php 
    elseif ($weapon['condition'] == 'Under Repair'):
    ?>
    <div class="status-banner status-warning">
        <div class="status-icon">
            <i class="fas fa-tools"></i>
        </div>
        <div class="status-content">
            <h3>Weapon Under Repair</h3>
            <p>This weapon is currently under repair.</p>
        </div>
    </div>
    <?php 
    endif;
    
    if ($weapon['current_location'] == 'Issued' && !empty($weapon['custodian'])):
    ?>
    <div class="status-banner status-info">
        <div class="status-icon">
            <i class="fas fa-hand-holding"></i>
        </div>
        <div class="status-content">
            <h3>Weapon Issued</h3>
            <p>This weapon is currently issued to <?php echo Security::escape($weapon['custodian']); ?>.</p>
        </div>
    </div>
    <?php 
    endif;
    
    if (!empty($weapon['next_inspection_date'])):
        $today = time();
        $inspectionDate = strtotime($weapon['next_inspection_date']);
        $daysToInspection = round(($inspectionDate - $today) / (60 * 60 * 24));
        
        if ($daysToInspection <= 30 && $daysToInspection >= 0):
    ?>
    <div class="status-banner status-warning">
        <div class="status-icon">
            <i class="fas fa-calendar-alt"></i>
        </div>
        <div class="status-content">
            <h3>Inspection Due Soon</h3>
            <p>Weapon inspection is due in <?php echo $daysToInspection; ?> days (<?php echo date('d/m/Y', strtotime($weapon['next_inspection_date'])); ?>).</p>
        </div>
    </div>
    <?php 
        elseif ($daysToInspection < 0):
    ?>
    <div class="status-banner status-danger">
        <div class="status-icon">
            <i class="fas fa-exclamation-circle"></i>
        </div>
        <div class="status-content">
            <h3>Inspection Overdue</h3>
            <p>Weapon inspection was due on <?php echo date('d/m/Y', strtotime($weapon['next_inspection_date'])); ?>. Please conduct inspection immediately.</p>
        </div>
    </div>
    <?php 
        endif;
    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">Weapon ID:</div>
                    <div class="detail-value">
                        <span class="asset-code"><?php echo Security::escape($weapon['weapon_id']); ?></span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Serial Number:</div>
                    <div class="detail-value highlight"><?php echo Security::escape($weapon['serial_no']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Type:</div>
                    <div class="detail-value">
                        <span class="badge badge-primary"><?php echo Security::escape($weapon['type_name'] ?? 'Other'); ?></span>
                        <?php if (!empty($weapon['weapon_type_other'])): ?>
                            (<?php echo Security::escape($weapon['weapon_type_other']); ?>)
                        <?php endif; ?>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Make/Model:</div>
                    <div class="detail-value"><?php echo Security::escape($weapon['make_model']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Calibre:</div>
                    <div class="detail-value">
                        <?php echo Security::escape($weapon['calibre_name'] ?? 'N/A'); ?>
                        <?php if (!empty($weapon['calibre_other'])): ?>
                            (<?php echo Security::escape($weapon['calibre_other']); ?>)
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Source & Acquisition Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-truck"></i> Source & Acquisition</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Source:</div>
                    <div class="detail-value"><?php echo Security::escape($weapon['source']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Date Acquired:</div>
                    <div class="detail-value"><?php echo $weapon['date_acquired'] ? date('d/m/Y', strtotime($weapon['date_acquired'])) : 'Not specified'; ?></div>
                </div>
            </div>
        </div>

        <!-- Current Status Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-clipboard-check"></i> Current Status</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Condition:</div>
                    <div class="detail-value">
                        <?php 
                        $conditionClass = '';
                        if ($weapon['condition'] == 'Serviceable') $conditionClass = 'status-active';
                        elseif ($weapon['condition'] == 'Unserviceable') $conditionClass = 'status-rejected';
                        elseif ($weapon['condition'] == 'Under Repair') $conditionClass = 'status-warning';
                        ?>
                        <span class="status-badge <?php echo $conditionClass; ?>">
                            <?php echo Security::escape($weapon['condition']); ?>
                        </span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Location:</div>
                    <div class="detail-value">
                        <?php 
                        $locationClass = '';
                        if ($weapon['current_location'] == 'Armoury') $locationClass = 'status-active';
                        elseif ($weapon['current_location'] == 'Issued') $locationClass = 'status-warning';
                        elseif ($weapon['current_location'] == 'In Repair') $locationClass = 'status-maintenance';
                        elseif ($weapon['current_location'] == 'Lost') $locationClass = 'status-rejected';
                        ?>
                        <span class="status-badge <?php echo $locationClass; ?>">
                            <?php echo Security::escape($weapon['current_location']); ?>
                        </span>
                        <?php if (!empty($weapon['current_location_other'])): ?>
                            (<?php echo Security::escape($weapon['current_location_other']); ?>)
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Custodian Card (if issued) -->
        <?php if ($weapon['current_location'] == 'Issued' && !empty($weapon['custodian'])): ?>
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-user-shield"></i> Custodian Information</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Name:</div>
                    <div class="detail-value"><?php echo Security::escape($weapon['custodian']); ?></div>
                </div>
                <?php if (!empty($weapon['custodian_rank'])): ?>
                <div class="detail-row">
                    <div class="detail-label">Rank:</div>
                    <div class="detail-value"><?php echo Security::escape($weapon['custodian_rank']); ?></div>
                </div>
                <?php endif; ?>
                <?php if (!empty($weapon['custodian_nis'])): ?>
                <div class="detail-row">
                    <div class="detail-label">NIS Number:</div>
                    <div class="detail-value"><?php echo Security::escape($weapon['custodian_nis']); ?></div>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Inspection Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-clipboard-list"></i> Inspection</h3>
            </div>
            <div class="card-body">
                <?php if ($weapon['last_inspection_date']): ?>
                <div class="detail-row">
                    <div class="detail-label">Last Inspection:</div>
                    <div class="detail-value"><?php echo date('d/m/Y', strtotime($weapon['last_inspection_date'])); ?></div>
                </div>
                <?php endif; ?>
                <?php if ($weapon['next_inspection_date']): ?>
                <div class="detail-row">
                    <div class="detail-label">Next Inspection:</div>
                    <div class="detail-value">
                        <?php echo date('d/m/Y', strtotime($weapon['next_inspection_date'])); ?>
                        <?php
                        $daysToInspection = round((strtotime($weapon['next_inspection_date']) - time()) / (60 * 60 * 24));
                        if ($daysToInspection <= 30 && $daysToInspection >= 0) {
                            echo '<span class="days-remaining warning">(' . $daysToInspection . ' days left)</span>';
                        } elseif ($daysToInspection < 0) {
                            echo '<span class="days-remaining expired">(Overdue)</span>';
                        }
                        ?>
                    </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($weapon['remarks'] ?? 'No remarks')); ?></p>
            </div>
        </div>

        <!-- Issue History Card -->
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-history"></i> Issue History</h3>
            </div>
            <div class="card-body">
                <?php if (empty($issueHistory)): ?>
                    <div class="empty-state">
                        <i class="fas fa-history"></i>
                        <p>No issue history found</p>
                    </div>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="history-table">
                            <thead>
                                <tr>
                                    <th>Issue Date</th>
                                    <th>Officer</th>
                                    <th>Rank</th>
                                    <th>Purpose</th>
                                    <th>Expected Return</th>
                                    <th>Actual Return</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($issueHistory as $issue): ?>
                                <tr>
                                    <td><?php echo date('d/m/Y', strtotime($issue['issue_date'])); ?></td>
                                    <td><?php echo Security::escape($issue['officer_name']); ?></td>
                                    <td><?php echo Security::escape($issue['officer_rank']); ?></td>
                                    <td><?php echo Security::escape($issue['purpose']); ?></td>
                                    <td><?php echo $issue['expected_return_date'] ? date('d/m/Y', strtotime($issue['expected_return_date'])) : '-'; ?></td>
                                    <td><?php echo $issue['actual_return_date'] ? date('d/m/Y', strtotime($issue['actual_return_date'])) : '-'; ?></td>
                                    <td>
                                        <?php 
                                        $historyStatusClass = '';
                                        if ($issue['status'] == 'Returned') $historyStatusClass = 'status-active';
                                        elseif ($issue['status'] == 'Issued') $historyStatusClass = 'status-warning';
                                        elseif ($issue['status'] == 'Overdue') $historyStatusClass = 'status-rejected';
                                        ?>
                                        <span class="status-badge <?php echo $historyStatusClass; ?>">
                                            <?php echo Security::escape($issue['status']); ?>
                                        </span>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </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($weapon['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($weapon['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($weapon['updated_at'])); ?></span>
                    </div>
                </div>
            </div>
        </div>
    </div>

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

<style>
.asset-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.1rem;
}

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

.remarks-text {
    line-height: 1.6;
    color: var(--text-primary);
    white-space: pre-wrap;
}

/* 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.status-success {
    border-left-color: var(--success-color);
}

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

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

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

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

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

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

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

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

.status-content h3 {
    margin: 0 0 5px 0;
    color: var(--text-primary);
}

.status-content p {
    margin: 0;
    color: var(--text-secondary);
}

/* 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;
}

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

.badge-primary {
    background: #e3f2fd;
    color: #1976d2;
}

/* 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-warning {
    background: #fff3cd;
    color: #856404;
}

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

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

/* History Table */
.history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.history-table th {
    background: var(--light-bg);
    padding: 12px 10px;
    text-align: left;
    font-weight: 600;
    color: var(--text-secondary);
    border-bottom: 2px solid var(--border-color);
}

.history-table td {
    padding: 10px;
    border-bottom: 1px solid var(--border-color);
}

.history-table tr:hover {
    background: var(--light-bg);
}

/* 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);
}

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

/* 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;
}

.empty-state p {
    font-size: 1rem;
    margin: 0;
}

/* Responsive */
@media (max-width: 768px) {
    .asset-details-grid {
        grid-template-columns: 1fr;
    }
    
    .detail-row {
        flex-direction: column;
    }
    
    .detail-label {
        margin-bottom: 5px;
    }
    
    .status-banner {
        flex-direction: column;
        text-align: center;
    }
    
    .history-table {
        font-size: 0.8rem;
    }
    
    .metadata-grid {
        grid-template-columns: 1fr;
    }
    
    .action-buttons-group {
        flex-direction: column;
    }
    
    .action-buttons-group .btn {
        width: 100%;
    }
}

@media print {
    .header-actions,
    .action-buttons-group,
    .sidebar,
    footer,
    .status-banner {
        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;
    }
    
    .card-header {
        background: #f8f9fa !important;
        color: #000 !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}
</style>

