<?php
$title = 'Weapons Dashboard';
$active = 'weapons';
$init_charts = true;
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-chart-pie"></i>
                Weapons Dashboard
            </h1>
            <p>Analytics and statistics for weapons inventory</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>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon total">
                <i class="fas fa-gun"></i>
            </div>
            <div class="stat-details">
                <h4>Total Weapons</h4>
                <p class="stat-number"><?php echo number_format($stats['total']); ?></p>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon issued">
                <i class="fas fa-hand-holding"></i>
            </div>
            <div class="stat-details">
                <h4>Issued</h4>
                <p class="stat-number"><?php echo number_format($stats['issued']); ?></p>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon serviceable">
                <i class="fas fa-check-circle"></i>
            </div>
            <div class="stat-details">
                <h4>Serviceable</h4>
                <p class="stat-number"><?php echo number_format($stats['serviceable']); ?></p>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon unserviceable">
                <i class="fas fa-exclamation-triangle"></i>
            </div>
            <div class="stat-details">
                <h4>Unserviceable</h4>
                <p class="stat-number"><?php echo number_format($stats['unserviceable']); ?></p>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon repair">
                <i class="fas fa-tools"></i>
            </div>
            <div class="stat-details">
                <h4>In Repair</h4>
                <p class="stat-number"><?php echo number_format($stats['in_repair']); ?></p>
            </div>
        </div>
    </div>

    <!-- Charts Section -->
    <div class="dashboard-details">
        <div class="detail-section">
            <div class="section-header">
                <h3><i class="fas fa-chart-pie"></i> Weapons by Type</h3>
            </div>
            <div style="height: 300px;">
                <canvas id="weaponTypeChart"></canvas>
            </div>
        </div>

        <div class="detail-section">
            <div class="section-header">
                <h3><i class="fas fa-chart-bar"></i> Weapons by Calibre</h3>
            </div>
            <div style="height: 300px;">
                <canvas id="weaponCalibreChart"></canvas>
            </div>
        </div>
    </div>

    <!-- Recent Issues -->
    <div class="detail-section full-width">
        <div class="section-header">
            <h3><i class="fas fa-history"></i> Recent Issues</h3>
        </div>
        <div class="table-responsive">
            <?php if (empty($recentIssues)): ?>
                <div class="empty-state">
                    <i class="fas fa-history"></i>
                    <p>No recent issues</p>
                </div>
            <?php else: ?>
            <table class="asset-table">
                <thead>
                    <tr>
                        <th>Weapon</th>
                        <th>Issue Date</th>
                        <th>Officer</th>
                        <th>Unit</th>
                        <th>Purpose</th>
                        <th>Expected Return</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recentIssues as $issue): ?>
                    <tr>
                        <td><?php echo Security::escape($issue['make_model']); ?></td>
                        <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['unit']); ?></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>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Weapons by Type Chart
    const typeCtx = document.getElementById('weaponTypeChart');
    if (typeCtx) {
        new Chart(typeCtx.getContext('2d'), {
            type: 'pie',
            data: {
                labels: <?php echo json_encode(array_keys($stats['by_type'])); ?>,
                datasets: [{
                    data: <?php echo json_encode(array_values($stats['by_type'])); ?>,
                    backgroundColor: [
                        '#27ae60',
                        '#3498db',
                        '#e74c3c',
                        '#f39c12',
                        '#9b59b6',
                        '#1abc9c',
                        '#e67e22',
                        '#2c3e50'
                    ]
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        position: 'bottom'
                    }
                }
            }
        });
    }
    
    // Weapons by Calibre Chart
    const calibreCtx = document.getElementById('weaponCalibreChart');
    if (calibreCtx) {
        new Chart(calibreCtx.getContext('2d'), {
            type: 'bar',
            data: {
                labels: <?php echo json_encode(array_keys($stats['by_calibre'])); ?>,
                datasets: [{
                    label: 'Number of Weapons',
                    data: <?php echo json_encode(array_values($stats['by_calibre'])); ?>,
                    backgroundColor: '#27ae60'
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                scales: {
                    y: {
                        beginAtZero: true,
                        ticks: {
                            stepSize: 1
                        }
                    }
                }
            }
        });
    }
});
</script>

