<?php
$title = 'Ammunition Reports';
$active = 'reports';
$init_charts = true;
require_once __DIR__ . '/../layouts/header.php';
require_once __DIR__ . '/../layouts/sidebar.php';

$type = $_GET['type'] ?? 'inventory';

// Ensure $data is always defined and is an array
$data = $data ?? [];

// Check if data is empty
$hasData = !empty($data);
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-bullseye"></i>
                Ammunition Reports
            </h1>
            <p>Ammunition inventory, expiry tracking, and usage reports</p>
        </div>
        <div class="header-actions">
            <button class="btn btn-success" onclick="saveReport()">
                <i class="fas fa-bookmark"></i> Save Report
            </button>
            <div class="btn-group">
                <button class="btn btn-outline" onclick="exportReport('pdf')">
                    <i class="fas fa-file-pdf"></i> PDF
                </button>
                <button class="btn btn-outline" onclick="exportReport('csv')">
                    <i class="fas fa-file-csv"></i> CSV
                </button>
                <button class="btn btn-outline" onclick="exportReport('excel')">
                    <i class="fas fa-file-excel"></i> Excel
                </button>
            </div>
            <a href="<?php echo BASE_URL; ?>/reports" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back
            </a>
        </div>
    </div>

    <!-- Report Controls -->
    <div class="report-controls">
        <form method="GET" action="<?php echo BASE_URL; ?>/reports/ammunition" id="reportForm">
            <div class="controls-grid">
                <div class="control-group">
                    <label>Report Type</label>
                    <select name="type" id="reportType" onchange="this.form.submit()">
                        <option value="inventory" <?php echo $type == 'inventory' ? 'selected' : ''; ?>>Full Inventory</option>
                        <option value="expiring" <?php echo $type == 'expiring' ? 'selected' : ''; ?>>Expiring Ammunition</option>
                        <option value="low_stock" <?php echo $type == 'low_stock' ? 'selected' : ''; ?>>Low Stock Alert</option>
                        <option value="issued" <?php echo $type == 'issued' ? 'selected' : ''; ?>>Issued Ammunition</option>
                    </select>
                </div>
            </div>
        </form>
    </div>

    <!-- Report Content -->
    <div class="report-content" id="reportContent">
        <?php if (!$hasData): ?>
            <div class="empty-state">
                <i class="fas fa-bullseye"></i>
                <p>No ammunition data found for the selected report type.</p>
            </div>
        <?php elseif ($type == 'inventory'): ?>
            <div class="report-section">
                <h2>Ammunition Inventory Report</h2>
                <p class="report-date">Generated: <?php echo date('d/m/Y H:i'); ?></p>
                
                <!-- Summary Cards -->
                <div class="summary-cards">
                    <?php
                    $totalRounds = 0;
                    $totalValue = 0;
                    $expiringCount = 0;
                    $lowStockCount = 0;
                    
                    foreach ($data as $ammo) {
                        $balance = (int)($ammo['quantity_received'] ?? 0) - (int)($ammo['quantity_issued'] ?? 0);
                        $totalRounds += $balance;
                        
                        if (isset($ammo['expiry_date']) && $ammo['expiry_date']) {
                            $expiryDate = strtotime($ammo['expiry_date']);
                            $today = time();
                            $daysToExpiry = round(($expiryDate - $today) / (60 * 60 * 24));
                            if ($daysToExpiry <= 90) $expiringCount++;
                        }
                        
                        if ($balance < 100) $lowStockCount++;
                    }
                    ?>
                    
                    <div class="summary-card" style="background: linear-gradient(135deg, #0D47A1 0%, #1976D2 100%);">
                        <div class="card-icon"><i class="fas fa-bullseye"></i></div>
                        <div class="card-content">
                            <div class="card-value"><?php echo number_format($totalRounds); ?></div>
                            <div class="card-label">Total Rounds</div>
                        </div>
                    </div>
                    
                    <div class="summary-card" style="background: linear-gradient(135deg, #B71C1C 0%, #D32F2F 100%);">
                        <div class="card-icon"><i class="fas fa-exclamation-triangle"></i></div>
                        <div class="card-content">
                            <div class="card-value"><?php echo $expiringCount; ?></div>
                            <div class="card-label">Expiring Soon</div>
                        </div>
                    </div>
                    
                    <div class="summary-card" style="background: linear-gradient(135deg, #B26A00 0%, #FF8F00 100%);">
                        <div class="card-icon"><i class="fas fa-exclamation-circle"></i></div>
                        <div class="card-content">
                            <div class="card-value"><?php echo $lowStockCount; ?></div>
                            <div class="card-label">Low Stock</div>
                        </div>
                    </div>
                    
                    <div class="summary-card" style="background: linear-gradient(135deg, #2E7D32 0%, #4CAF50 100%);">
                        <div class="card-icon"><i class="fas fa-check-circle"></i></div>
                        <div class="card-content">
                            <div class="card-value"><?php echo count($data); ?></div>
                            <div class="card-label">Total Batches</div>
                        </div>
                    </div>
                </div>
                
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Ammo ID</th>
                                <th>Type</th>
                                <th>Calibre</th>
                                <th>Batch Number</th>
                                <th>Storage Location</th>
                                <th>Received</th>
                                <th>Issued</th>
                                <th>Balance</th>
                                <th>Expiry Date</th>
                                <th>Condition</th>
                                <th>Manufacturer</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data as $ammo): 
                                $received = (int)($ammo['quantity_received'] ?? 0);
                                $issued = (int)($ammo['quantity_issued'] ?? 0);
                                $balance = $received - $issued;
                                $expiryDate = isset($ammo['expiry_date']) ? strtotime($ammo['expiry_date']) : null;
                                $today = time();
                                $daysToExpiry = $expiryDate ? round(($expiryDate - $today) / (60 * 60 * 24)) : null;
                                
                                $rowClass = '';
                                if ($balance < 100) $rowClass = 'low-stock-row';
                                elseif ($daysToExpiry && $daysToExpiry <= 90) $rowClass = 'expiring-row';
                                if ($daysToExpiry && $daysToExpiry < 0) $rowClass = 'expired-row';
                            ?>
                            <tr class="<?php echo $rowClass; ?>">
                                <td><span class="asset-code"><?php echo isset($ammo['ammo_id']) ? htmlspecialchars($ammo['ammo_id']) : '-'; ?></span></td>
                                <td><?php echo isset($ammo['ammo_type']) ? htmlspecialchars($ammo['ammo_type']) : 'Other'; ?></td>
                                <td><?php echo isset($ammo['calibre']) ? htmlspecialchars($ammo['calibre']) : 'N/A'; ?></td>
                                <td><code><?php echo isset($ammo['batch_number']) ? htmlspecialchars($ammo['batch_number']) : '-'; ?></code></td>
                                <td><?php echo $ammo['storage_location'] ?? '-'; ?></td>
                                <td class="text-right"><?php echo number_format($received); ?></td>
                                <td class="text-right"><?php echo number_format($issued); ?></td>
                                <td class="text-right <?php echo $balance < 100 ? 'text-danger' : ''; ?>">
                                    <strong><?php echo number_format($balance); ?></strong>
                                </td>
                                <td>
                                    <?php if (!empty($ammo['expiry_date'])): ?>
                                        <?php echo date('d/m/Y', strtotime($ammo['expiry_date'])); ?>
                                        <?php if ($daysToExpiry && $daysToExpiry <= 90): ?>
                                            <br>
                                            <span class="days-badge <?php 
                                                echo $daysToExpiry < 0 ? 'expired' : 
                                                    ($daysToExpiry <= 30 ? 'critical' : 'warning'); 
                                            ?>">
                                                <?php echo $daysToExpiry < 0 ? 'Expired' : $daysToExpiry . ' days'; ?>
                                            </span>
                                        <?php endif; ?>
                                    <?php else: ?>
                                        -
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <?php 
                                    $condition = $ammo['condition'] ?? 'Unknown';
                                    $conditionClass = '';
                                    if ($condition == 'Serviceable') $conditionClass = 'badge-success';
                                    elseif ($condition == 'Unserviceable') $conditionClass = 'badge-warning';
                                    elseif ($condition == 'Condemned') $conditionClass = 'badge-danger';
                                    ?>
                                    <span class="badge <?php echo $conditionClass; ?>">
                                        <?php echo $condition; ?>
                                    </span>
                                </td>
                                <td><?php echo $ammo['manufacturer'] ?? '-'; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                        <?php if (!empty($data)): ?>
                        <tfoot>
                            <?php
                            $totalReceived = array_sum(array_column($data, 'quantity_received'));
                            $totalIssued = array_sum(array_column($data, 'quantity_issued'));
                            $totalBalance = $totalReceived - $totalIssued;
                            ?>
                            <tr>
                                <th colspan="5" class="text-right">Totals:</th>
                                <th class="text-right"><?php echo number_format($totalReceived); ?></th>
                                <th class="text-right"><?php echo number_format($totalIssued); ?></th>
                                <th class="text-right"><?php echo number_format($totalBalance); ?></th>
                                <th colspan="3"></th>
                            </tr>
                        </tfoot>
                        <?php endif; ?>
                    </table>
                </div>
            </div>

        <?php elseif ($type == 'expiring'): ?>
            <div class="report-section">
                <h2>Expiring Ammunition Report</h2>
                <p class="report-date">Generated: <?php echo date('d/m/Y H:i'); ?></p>
                <p class="report-note">Ammunition expiring within 90 days</p>
                
                <?php if (empty($data)): ?>
                    <div class="empty-state">
                        <i class="fas fa-calendar-check"></i>
                        <p>No expiring ammunition found</p>
                    </div>
                <?php else: ?>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Ammo ID</th>
                                <th>Type</th>
                                <th>Calibre</th>
                                <th>Batch Number</th>
                                <th>Expiry Date</th>
                                <th>Days Remaining</th>
                                <th>Balance</th>
                                <th>Storage Location</th>
                                <th>Condition</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data as $ammo): 
                                $daysRemaining = $ammo['days_remaining'] ?? 0;
                            ?>
                            <tr class="<?php 
                                echo $daysRemaining < 0 ? 'expired-row' : 
                                    ($daysRemaining <= 30 ? 'critical-row' : 'warning-row'); 
                            ?>">
                                <td><span class="asset-code"><?php echo isset($ammo['ammo_id']) ? htmlspecialchars($ammo['ammo_id']) : '-'; ?></span></td>
                                <td><?php echo isset($ammo['ammo_type']) ? htmlspecialchars($ammo['ammo_type']) : '-'; ?></td>
                                <td><?php echo isset($ammo['calibre']) ? htmlspecialchars($ammo['calibre']) : '-'; ?></td>
                                <td><code><?php echo isset($ammo['batch_number']) ? htmlspecialchars($ammo['batch_number']) : '-'; ?></code></td>
                                <td><?php echo isset($ammo['expiry_date']) ? date('d/m/Y', strtotime($ammo['expiry_date'])) : '-'; ?></td>
                                <td class="text-center">
                                    <span class="days-badge <?php 
                                        echo $daysRemaining < 0 ? 'expired' : 
                                            ($daysRemaining <= 30 ? 'critical' : 'warning'); 
                                    ?>">
                                        <?php echo $daysRemaining < 0 ? 'Expired' : $daysRemaining . ' days'; ?>
                                    </span>
                                </td>
                                <td class="text-right"><?php echo number_format($ammo['balance'] ?? 0); ?></td>
                                <td><?php echo $ammo['storage_location'] ?? '-'; ?></td>
                                <td><?php echo $ammo['condition'] ?? '-'; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                
                <div class="report-summary">
                    <h3>Summary</h3>
                    <?php
                    $expired = count(array_filter($data, function($a) { return ($a['days_remaining'] ?? 0) < 0; }));
                    $critical = count(array_filter($data, function($a) { 
                        $days = $a['days_remaining'] ?? 0;
                        return $days >= 0 && $days <= 30;
                    }));
                    $warning = count(array_filter($data, function($a) { 
                        $days = $a['days_remaining'] ?? 0;
                        return $days > 30 && $days <= 90;
                    }));
                    ?>
                    <p>Total Expiring Items: <?php echo count($data); ?></p>
                    <p><span class="badge badge-danger">Expired</span>: <?php echo $expired; ?></p>
                    <p><span class="badge badge-warning">Critical (≤30 days)</span>: <?php echo $critical; ?></p>
                    <p><span class="badge badge-info">Warning (31-90 days)</span>: <?php echo $warning; ?></p>
                </div>
                <?php endif; ?>
            </div>

        <?php elseif ($type == 'low_stock'): ?>
            <div class="report-section">
                <h2>Low Stock Ammunition Report</h2>
                <p class="report-date">Generated: <?php echo date('d/m/Y H:i'); ?></p>
                <p class="report-note">Ammunition with balance below 100 rounds</p>
                
                <?php if (empty($data)): ?>
                    <div class="empty-state">
                        <i class="fas fa-check-circle"></i>
                        <p>No low stock ammunition found</p>
                    </div>
                <?php else: ?>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Ammo ID</th>
                                <th>Type</th>
                                <th>Calibre</th>
                                <th>Batch Number</th>
                                <th>Current Balance</th>
                                <th>Reorder Level</th>
                                <th>Storage Location</th>
                                <th>Expiry Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data as $ammo): 
                                $balance = (int)($ammo['quantity_received'] ?? 0) - (int)($ammo['quantity_issued'] ?? 0);
                            ?>
                            <tr class="low-stock-row">
                                <td><span class="asset-code"><?php echo isset($ammo['ammo_id']) ? htmlspecialchars($ammo['ammo_id']) : '-'; ?></span></td>
                                <td><?php echo isset($ammo['ammo_type']) ? htmlspecialchars($ammo['ammo_type']) : '-'; ?></td>
                                <td><?php echo isset($ammo['calibre']) ? htmlspecialchars($ammo['calibre']) : '-'; ?></td>
                                <td><code><?php echo isset($ammo['batch_number']) ? htmlspecialchars($ammo['batch_number']) : '-'; ?></code></td>
                                <td class="text-right"><strong class="text-danger"><?php echo number_format($balance); ?></strong></td>
                                <td class="text-center"><span class="badge badge-warning">100</span></td>
                                <td><?php echo $ammo['storage_location'] ?? '-'; ?></td>
                                <td><?php echo isset($ammo['expiry_date']) ? date('d/m/Y', strtotime($ammo['expiry_date'])) : '-'; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>
            </div>

        <?php elseif ($type == 'issued'): ?>
            <div class="report-section">
                <h2>Issued Ammunition Report</h2>
                <p class="report-date">Generated: <?php echo date('d/m/Y H:i'); ?></p>
                
                <?php if (empty($data)): ?>
                    <div class="empty-state">
                        <i class="fas fa-clipboard-list"></i>
                        <p>No issued ammunition records found</p>
                    </div>
                <?php else: ?>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Issue Date</th>
                                <th>Ammunition</th>
                                <th>Type</th>
                                <th>Calibre</th>
                                <th>Officer</th>
                                <th>Rank</th>
                                <th>Unit</th>
                                <th>Purpose</th>
                                <th>Rounds Issued</th>
                                <th>Requisition #</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data as $issue): ?>
                            <tr>
                                <td><?php echo isset($issue['issue_date']) ? date('d/m/Y', strtotime($issue['issue_date'])) : '-'; ?></td>
                                <td><?php echo isset($issue['ammo_id']) ? htmlspecialchars($issue['ammo_id']) : '-'; ?></td>
                                <td><?php echo isset($issue['ammo_type']) ? htmlspecialchars($issue['ammo_type']) : '-'; ?></td>
                                <td><?php echo isset($issue['calibre']) ? htmlspecialchars($issue['calibre']) : '-'; ?></td>
                                <td><?php echo isset($issue['officer_name']) ? htmlspecialchars($issue['officer_name']) : '-'; ?></td>
                                <td><?php echo isset($issue['officer_rank']) ? htmlspecialchars($issue['officer_rank']) : '-'; ?></td>
                                <td><?php echo isset($issue['unit']) ? htmlspecialchars($issue['unit']) : '-'; ?></td>
                                <td><?php echo $issue['purpose'] ?? '-'; ?></td>
                                <td class="text-right"><strong><?php echo number_format($issue['rounds_issued'] ?? 0); ?></strong></td>
                                <td><code><?php echo $issue['requisition_number'] ?? '-'; ?></code></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                        <?php if (!empty($data)): ?>
                        <tfoot>
                            <tr>
                                <th colspan="8" class="text-right">Total Rounds Issued:</th>
                                <th class="text-right"><?php echo number_format(array_sum(array_column($data, 'rounds_issued'))); ?></th>
                                <th></th>
                            </tr>
                        </tfoot>
                        <?php endif; ?>
                    </table>
                </div>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<style>
:root {
    --primary-dark: #2E7D32;
    --primary: #4CAF50;
    --danger-dark: #B71C1C;
    --danger: #D32F2F;
    --warning-dark: #B26A00;
    --warning: #FF8F00;
    --info-dark: #0D47A1;
    --info: #1976D2;
    --text-dark: #2c3e50;
    --text-light: #7f8c8d;
    --border: #e2e8f0;
    --bg-light: #f8f9fa;
    --shadow-sm: 0 2px 4px rgba(0,0,0,0.02);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.05);
    --shadow-lg: 0 10px 15px rgba(0,0,0,0.05);
}

.report-controls {
    background: white;
    border-radius: 8px;
    padding: 20px 25px;
    margin-bottom: 25px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border);
}

.controls-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
}

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

.control-group label {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-light);
    margin-bottom: 6px;
    font-weight: 600;
}

.control-group select,
.control-group input {
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.9rem;
    transition: all 0.2s;
    background: white;
}

.control-group select:focus,
.control-group input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.1);
}

.report-content {
    background: white;
    border-radius: 8px;
    padding: 30px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border);
}

.report-section h2 {
    margin: 0 0 8px 0;
    color: var(--text-dark);
    font-size: 1.5rem;
    font-weight: 500;
    letter-spacing: -0.5px;
}

.report-date {
    color: var(--text-light);
    margin-bottom: 25px;
    font-size: 0.85rem;
    padding-bottom: 15px;
    border-bottom: 1px solid var(--border);
}

.report-note {
    color: var(--warning);
    font-style: italic;
    margin-bottom: 15px;
    font-size: 0.9rem;
}

/* Summary Cards */
.summary-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.summary-card {
    color: white;
    padding: 20px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 15px;
    box-shadow: var(--shadow-lg);
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
}

.summary-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(45deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 100%);
    pointer-events: none;
}

.summary-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 15px 30px rgba(0,0,0,0.1);
}

.summary-card .card-icon {
    font-size: 2.2rem;
    opacity: 0.9;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
    color: white;
}

.summary-card .card-content {
    flex: 1;
}

.summary-card .card-value {
    font-size: 2rem;
    font-weight: 600;
    line-height: 1.2;
    letter-spacing: -1px;
    color: white;
}

.summary-card .card-label {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    opacity: 0.9;
    color: white;
}

/* Table Styles */
.table-responsive {
    overflow-x: auto;
    margin-top: 20px;
    border-radius: 6px;
    border: 1px solid var(--border);
}

.report-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.report-table th {
    background: var(--bg-light);
    padding: 14px 12px;
    text-align: left;
    font-weight: 600;
    color: var(--text-dark);
    border-bottom: 2px solid var(--border);
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.5px;
}

.report-table td {
    padding: 12px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

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

.report-table tfoot th {
    background: var(--bg-light);
    padding: 12px;
    font-weight: 600;
    color: var(--text-dark);
    border-top: 2px solid var(--border);
}

/* Row highlighting */
.low-stock-row {
    background-color: #FFF3E0;
}

.low-stock-row:hover {
    background-color: #FFE0B2 !important;
}

.expiring-row {
    background-color: #FFF8E1;
}

.expiring-row:hover {
    background-color: #FFECB3 !important;
}

.expired-row {
    background-color: #FFEBEE;
}

.expired-row:hover {
    background-color: #FFCDD2 !important;
}

.critical-row {
    background-color: #FFEBEE;
}

.critical-row:hover {
    background-color: #FFCDD2 !important;
}

.warning-row {
    background-color: #FFF8E1;
}

.warning-row:hover {
    background-color: #FFECB3 !important;
}

/* Badges */
.badge {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.badge-success {
    background: #E8F5E9;
    color: #2E7D32;
}

.badge-danger {
    background: #FFEBEE;
    color: #B71C1C;
}

.badge-warning {
    background: #FFF3E0;
    color: #B26A00;
}

.badge-info {
    background: #E3F2FD;
    color: #0D47A1;
}

/* Days badge */
.days-badge {
    display: inline-block;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
    margin-top: 4px;
}

.days-badge.warning {
    background: #FFF3E0;
    color: #B26A00;
}

.days-badge.critical {
    background: #FFEBEE;
    color: #B71C1C;
}

.days-badge.expired {
    background: #FFEBEE;
    color: #B71C1C;
}

/* Asset code */
.asset-code {
    font-family: 'SF Mono', 'Fira Code', monospace;
    background: var(--bg-light);
    padding: 3px 6px;
    border-radius: 4px;
    font-size: 0.8rem;
    color: var(--info-dark);
}

code {
    font-family: 'SF Mono', 'Fira Code', monospace;
    background: var(--bg-light);
    padding: 2px 4px;
    border-radius: 3px;
    font-size: 0.8rem;
}

/* Text utilities */
.text-right {
    text-align: right;
}

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

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

/* Empty state */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-light);
}

.empty-state i {
    font-size: 3rem;
    margin-bottom: 15px;
    color: #ccc;
}

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

/* Report summary */
.report-summary {
    margin-top: 30px;
    padding: 20px;
    background: var(--bg-light);
    border-radius: 8px;
    border: 1px solid var(--border);
}

.report-summary h3 {
    margin: 0 0 15px 0;
    font-size: 1.1rem;
    color: var(--text-dark);
}

.report-summary p {
    margin: 8px 0;
    font-size: 0.95rem;
}

/* Button group */
.btn-group {
    display: flex;
    gap: 5px;
}

.btn {
    transition: all 0.2s;
}

.btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.05);
}

/* Responsive */
@media (max-width: 768px) {
    .report-content {
        padding: 20px;
    }
    
    .summary-cards {
        grid-template-columns: 1fr 1fr;
        gap: 12px;
    }
    
    .summary-card {
        padding: 15px;
    }
    
    .summary-card .card-icon {
        font-size: 1.8rem;
    }
    
    .summary-card .card-value {
        font-size: 1.5rem;
    }
    
    .btn-group {
        flex-direction: column;
        width: 100%;
    }
    
    .btn-group .btn {
        width: 100%;
    }
}

@media (max-width: 480px) {
    .summary-cards {
        grid-template-columns: 1fr;
    }
}

/* Print styles */
@media print {
    .header-actions,
    .sidebar,
    footer,
    .report-controls,
    .btn-group {
        display: none !important;
    }
    
    .main-content {
        margin-left: 0 !important;
        width: 100% !important;
        padding: 20px !important;
    }
    
    .report-content {
        box-shadow: none !important;
        border: none !important;
        padding: 0 !important;
    }
    
    .report-table th {
        background: var(--bg-light) !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}
</style>

<script>
function exportReport(format) {
    const type = document.getElementById('reportType').value;
    window.location.href = '<?php echo BASE_URL; ?>/reports/ammunition?type=' + type + '&format=' + format;
}

function saveReport() {
    const reportName = prompt('Enter a name for this report:');
    if (reportName) {
        const type = document.getElementById('reportType').value;
        
        const form = document.createElement('form');
        form.method = 'POST';
        form.action = '<?php echo BASE_URL; ?>/reports/save';
        
        const csrf = document.createElement('input');
        csrf.type = 'hidden';
        csrf.name = 'csrf_token';
        
        <?php if (class_exists('Security') && method_exists('Security', 'csrfToken')): ?>
        csrf.value = '<?php echo Security::csrfToken(); ?>';
        <?php else: ?>
        csrf.value = '<?php echo $_SESSION['csrf_token'] ?? ''; ?>';
        <?php endif; ?>
        
        form.appendChild(csrf);
        
        const nameInput = document.createElement('input');
        nameInput.type = 'hidden';
        nameInput.name = 'report_name';
        nameInput.value = reportName;
        form.appendChild(nameInput);
        
        const typeInput = document.createElement('input');
        typeInput.type = 'hidden';
        typeInput.name = 'report_type';
        typeInput.value = 'ammunition';
        form.appendChild(typeInput);
        
        const paramsInput = document.createElement('input');
        paramsInput.type = 'hidden';
        paramsInput.name = 'parameters';
        paramsInput.value = JSON.stringify({type});
        form.appendChild(paramsInput);
        
        document.body.appendChild(form);
        form.submit();
    }
}
</script>

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