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

    <!-- Status Alerts -->
    <?php
    $balance = $ammo['quantity_received'] - $ammo['quantity_issued'];
    
    if ($balance < 100):
    ?>
    <div class="status-banner status-warning">
        <div class="status-icon">
            <i class="fas fa-exclamation-triangle"></i>
        </div>
        <div class="status-content">
            <h3>Low Stock Alert</h3>
            <p>Current balance is <?php echo number_format($balance); ?> rounds. Please reorder soon.</p>
        </div>
    </div>
    <?php 
    endif;
    
    if (!empty($ammo['expiry_date'])):
        $expiryDate = strtotime($ammo['expiry_date']);
        $today = time();
        $daysToExpiry = round(($expiryDate - $today) / (60 * 60 * 24));
        
        if ($daysToExpiry <= 90 && $daysToExpiry >= 0):
    ?>
    <div class="status-banner status-warning">
        <div class="status-icon">
            <i class="fas fa-clock"></i>
        </div>
        <div class="status-content">
            <h3>Expiry Warning</h3>
            <p>Ammunition expires in <?php echo $daysToExpiry; ?> days (<?php echo date('d/m/Y', strtotime($ammo['expiry_date'])); ?>). Plan usage or disposal.</p>
        </div>
    </div>
    <?php 
        elseif ($daysToExpiry < 0):
    ?>
    <div class="status-banner status-danger">
        <div class="status-icon">
            <i class="fas fa-exclamation-circle"></i>
        </div>
        <div class="status-content">
            <h3>Expired Ammunition</h3>
            <p>This ammunition expired on <?php echo date('d/m/Y', strtotime($ammo['expiry_date'])); ?>. Please dispose immediately.</p>
        </div>
    </div>
    <?php 
        endif;
    endif;
    
    if ($ammo['condition'] == 'Unserviceable'):
    ?>
    <div class="status-banner status-danger">
        <div class="status-icon">
            <i class="fas fa-times-circle"></i>
        </div>
        <div class="status-content">
            <h3>Unserviceable</h3>
            <p>This ammunition has been marked as unserviceable.</p>
        </div>
    </div>
    <?php 
    elseif ($ammo['condition'] == 'Condemned'):
    ?>
    <div class="status-banner status-danger">
        <div class="status-icon">
            <i class="fas fa-skull-crossbones"></i>
        </div>
        <div class="status-content">
            <h3>Condemned</h3>
            <p>This ammunition has been condemned and must be disposed.</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">Ammunition ID:</div>
                    <div class="detail-value">
                        <span class="asset-code"><?php echo Security::escape($ammo['ammo_id']); ?></span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Batch Number:</div>
                    <div class="detail-value highlight"><?php echo Security::escape($ammo['batch_number'] ?? 'N/A'); ?></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($ammo['ammo_type'] ?? 'Other'); ?></span>
                        <?php if (!empty($ammo['ammo_type_other'])): ?>
                            (<?php echo Security::escape($ammo['ammo_type_other']); ?>)
                        <?php endif; ?>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Calibre:</div>
                    <div class="detail-value">
                        <?php echo Security::escape($ammo['calibre'] ?? 'N/A'); ?>
                        <?php if (!empty($ammo['calibre_other'])): ?>
                            (<?php echo Security::escape($ammo['calibre_other']); ?>)
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Quantity Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-calculator"></i> Quantity Information</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Storage Form:</div>
                    <div class="detail-value"><?php echo Security::escape($ammo['storage_form']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Quantity Received:</div>
                    <div class="detail-value"><?php echo number_format($ammo['quantity_received']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Quantity Issued:</div>
                    <div class="detail-value"><?php echo number_format($ammo['quantity_issued']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Current Balance:</div>
                    <div class="detail-value highlight <?php echo $balance < 100 ? 'text-danger' : ''; ?>">
                        <?php echo number_format($balance); ?> rounds
                    </div>
                </div>
            </div>
        </div>

        <!-- Storage Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-map-marker-alt"></i> Storage</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Location:</div>
                    <div class="detail-value">
                        <?php echo Security::escape($ammo['storage_location']); ?>
                        <?php if (!empty($ammo['storage_location_other'])): ?>
                            (<?php echo Security::escape($ammo['storage_location_other']); ?>)
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Manufacturer Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-industry"></i> Manufacturer</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Manufacturer:</div>
                    <div class="detail-value"><?php echo Security::escape($ammo['manufacturer'] ?? 'Not specified'); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Date Manufactured:</div>
                    <div class="detail-value"><?php echo $ammo['date_manufactured'] ? date('d/m/Y', strtotime($ammo['date_manufactured'])) : 'Not specified'; ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Expiry Date:</div>
                    <div class="detail-value">
                        <?php if ($ammo['expiry_date']): ?>
                            <?php echo date('d/m/Y', strtotime($ammo['expiry_date'])); ?>
                            <?php
                            $expiryDate = strtotime($ammo['expiry_date']);
                            $daysToExpiry = round(($expiryDate - time()) / (60 * 60 * 24));
                            if ($daysToExpiry <= 90 && $daysToExpiry >= 0) {
                                echo '<span class="days-remaining warning">(' . $daysToExpiry . ' days left)</span>';
                            } elseif ($daysToExpiry < 0) {
                                echo '<span class="days-remaining expired">(Expired)</span>';
                            }
                            ?>
                        <?php else: ?>
                            Not specified
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Condition Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-clipboard-check"></i> Condition</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Condition:</div>
                    <div class="detail-value">
                        <?php 
                        $conditionClass = '';
                        if ($ammo['condition'] == 'Serviceable') $conditionClass = 'status-active';
                        elseif ($ammo['condition'] == 'Unserviceable') $conditionClass = 'status-warning';
                        elseif ($ammo['condition'] == 'Condemned') $conditionClass = 'status-rejected';
                        ?>
                        <span class="status-badge <?php echo $conditionClass; ?>">
                            <?php echo Security::escape($ammo['condition']); ?>
                        </span>
                    </div>
                </div>
            </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>Unit</th>
                                    <th>Purpose</th>
                                    <th>Rounds Issued</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['unit']); ?></td>
                                    <td><?php echo Security::escape($issue['purpose']); ?></td>
                                    <td class="text-right"><?php echo number_format($issue['rounds_issued']); ?></td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </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($ammo['remarks'] ?? 'No remarks')); ?></p>
            </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($ammo['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($ammo['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($ammo['updated_at'])); ?></span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Action Buttons -->
    <div class="action-buttons-group">
        <?php if (Auth::can('ammunition.edit')): ?>
        <a href="<?php echo BASE_URL; ?>/ammunition/edit/<?php echo $ammo['id']; ?>" class="btn btn-success">
            <i class="fas fa-edit"></i> Edit Ammunition
        </a>
        <?php endif; ?>
        
        <?php if (Auth::can('ammunition.delete')): ?>
        <a href="<?php echo BASE_URL; ?>/ammunition/delete/<?php echo $ammo['id']; ?>" class="btn btn-danger" 
           onclick="return confirm('Are you sure you want to delete this ammunition record? This action cannot be undone.')">
            <i class="fas fa-trash"></i> Delete Record
        </a>
        <?php endif; ?>
        
        <a href="<?php echo BASE_URL; ?>/ammunition" class="btn btn-secondary">
            <i class="fas fa-arrow-left"></i> Back to Ammunition
        </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-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-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-rejected {
    background: #f8d7da;
    color: #721c24;
}

.text-right {
    text-align: right;
}

.text-danger {
    color: var(--danger-color);
}

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

