<?php
/**
 * Dashboard View
 * 
 * Variables expected:
 * $stats - Dashboard statistics array
 * $activities - Recent activities array
 * $user - Current user data
 * $isSuperAdmin - Boolean for super admin
 * $userPermissions - Array of user permissions
 */
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-tachometer-alt"></i>
                Dashboard
            </h1>
            <p>Welcome back, <?php echo htmlspecialchars($user['full_name'] ?? 'User'); ?>! Here's your system overview.</p>
        </div>
        <div class="header-actions">
            <button class="btn btn-outline" onclick="refreshDashboard()">
                <i class="fas fa-sync-alt"></i> Refresh
            </button>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="dashboard-grid">
        <!-- Weapons Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #e74c3c;">
                <i class="fas fa-gun"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_weapons'] ?? 0); ?></div>
                <div class="card-label">Total Weapons</div>
                <div class="card-trend">
                    <span class="text-muted"><?php echo $stats['weapons_issued'] ?? 0; ?> issued</span>
                </div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/weapons" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View Weapons
                </a>
            </div>
        </div>

        <!-- Ammunition Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #f39c12;">
                <i class="fas fa-bullseye"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_ammunition'] ?? 0); ?></div>
                <div class="card-label">Ammunition Types</div>
                <div class="card-trend">
                    <span class="text-muted"><?php echo number_format($stats['ammunition_balance'] ?? 0); ?> rounds total</span>
                </div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/ammunition" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View Ammo
                </a>
            </div>
        </div>

        <!-- Land Assets Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #27ae60;">
                <i class="fas fa-map-marked-alt"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_land'] ?? 0); ?></div>
                <div class="card-label">Land Assets</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/land" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View Land
                </a>
            </div>
        </div>

        <!-- Buildings Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #3498db;">
                <i class="fas fa-building"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_buildings'] ?? 0); ?></div>
                <div class="card-label">Buildings</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/buildings" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View Buildings
                </a>
            </div>
        </div>

        <!-- Rented Properties Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #e67e22;">
                <i class="fas fa-house-user"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_rented'] ?? 0); ?></div>
                <div class="card-label">Rented Properties</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/rented" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View Rented
                </a>
            </div>
        </div>

        <!-- Ongoing Projects Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #9b59b6;">
                <i class="fas fa-hard-hat"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_projects'] ?? 0); ?></div>
                <div class="card-label">Ongoing Projects</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/projects" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View Projects
                </a>
            </div>
        </div>

        <!-- Movable Assets Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #1abc9c;">
                <i class="fas fa-tools"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_movable'] ?? 0); ?></div>
                <div class="card-label">Movable Assets</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/movable" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View Movable
                </a>
            </div>
        </div>

        <!-- ICT Assets Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #34495e;">
                <i class="fas fa-server"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_ict'] ?? 0); ?></div>
                <div class="card-label">ICT Assets</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/ict" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View ICT
                </a>
            </div>
        </div>

        <!-- Vehicles Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #e67e22;">
                <i class="fas fa-car"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_vehicles'] ?? 0); ?></div>
                <div class="card-label">Vehicles</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/fleet/vehicles" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View Vehicles
                </a>
            </div>
        </div>

        <!-- Pending Requisitions Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #f39c12;">
                <i class="fas fa-file-signature"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo $stats['pending_requisitions'] ?? 0; ?></div>
                <div class="card-label">Pending Requisitions</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/requisition/pending" class="btn-link">
                    <i class="fas fa-arrow-right"></i> Review
                </a>
            </div>
        </div>

        <!-- Users Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #2c3e50;">
                <i class="fas fa-users"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_users'] ?? 0); ?></div>
                <div class="card-label">Active Users</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/users" class="btn-link">
                    <i class="fas fa-arrow-right"></i> Manage Users
                </a>
            </div>
        </div>

        <!-- Alerts Card -->
        <div class="stat-card <?php echo (($stats['expiring_ammunition'] ?? 0) > 0 || ($stats['unserviceable_weapons'] ?? 0) > 0) ? 'stat-danger' : ''; ?>">
            <div class="card-icon" style="background: #e74c3c;">
                <i class="fas fa-exclamation-triangle"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo ($stats['expiring_ammunition'] ?? 0) + ($stats['unserviceable_weapons'] ?? 0); ?></div>
                <div class="card-label">System Alerts</div>
                <div class="card-trend">
                    <span><?php echo $stats['expiring_ammunition'] ?? 0; ?> expiring | <?php echo $stats['unserviceable_weapons'] ?? 0; ?> unserviceable</span>
                </div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/weapons?filter=unserviceable" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View Alerts
                </a>
            </div>
        </div>
    </div>

    <!-- Super Admin Section - Full Access -->
    <?php if ($isSuperAdmin): ?>
    <div class="dashboard-details">
        <div class="detail-section">
            <div class="section-header">
                <h3><i class="fas fa-crown" style="color: #f1c40f;"></i> Super Admin Controls</h3>
            </div>
            <div class="action-grid">
                <a href="<?php echo BASE_URL; ?>/users" class="action-card">
                    <i class="fas fa-users-cog"></i>
                    <span>User Management</span>
                </a>
                <a href="<?php echo BASE_URL; ?>/roles" class="action-card">
                    <i class="fas fa-user-tag"></i>
                    <span>Roles & Permissions</span>
                </a>
                <a href="<?php echo BASE_URL; ?>/settings" class="action-card">
                    <i class="fas fa-cogs"></i>
                    <span>System Settings</span>
                </a>
                <a href="<?php echo BASE_URL; ?>/audit/history" class="action-card">
                    <i class="fas fa-history"></i>
                    <span>Audit Logs</span>
                </a>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <!-- Recent Activity -->
    <div class="detail-section full-width">
        <div class="section-header">
            <h3><i class="fas fa-history"></i> Recent Activity</h3>
            <a href="<?php echo BASE_URL; ?>/audit/history" class="view-all">View All</a>
        </div>
        <div class="activity-list">
            <?php if (empty($activities)): ?>
                <div class="empty-state">
                    <i class="fas fa-history"></i>
                    <p>No recent activity</p>
                </div>
            <?php else: ?>
                <?php foreach ($activities as $activity): ?>
                    <div class="activity-item">
                        <div class="activity-icon">
                            <?php
                            $icon = 'fa-info-circle';
                            $color = '';
                            if (strpos($activity['action'], 'CREATE') !== false) {
                                $icon = 'fa-plus-circle';
                                $color = 'text-success';
                            } elseif (strpos($activity['action'], 'UPDATE') !== false) {
                                $icon = 'fa-edit';
                                $color = 'text-warning';
                            } elseif (strpos($activity['action'], 'DELETE') !== false) {
                                $icon = 'fa-trash';
                                $color = 'text-danger';
                            } elseif (strpos($activity['action'], 'LOGIN') !== false) {
                                $icon = 'fa-sign-in-alt';
                                $color = 'text-info';
                            }
                            ?>
                            <i class="fas <?php echo $icon . ' ' . $color; ?>"></i>
                        </div>
                        <div class="activity-details">
                            <div class="activity-title">
                                <?php echo htmlspecialchars($activity['action']); ?>
                            </div>
                            <div class="activity-description">
                                <?php echo htmlspecialchars($activity['description'] ?? ''); ?>
                                <?php if (!empty($activity['full_name'])): ?>
                                    by <?php echo htmlspecialchars($activity['full_name']); ?>
                                <?php endif; ?>
                            </div>
                        </div>
                        <div class="activity-time">
                            <?php echo date('M j, g:i A', strtotime($activity['created_at'])); ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="quick-actions">
        <h3><i class="fas fa-bolt"></i> Quick Actions</h3>
        <div class="action-grid">
            <?php if ($isSuperAdmin || in_array('requisition.create', $userPermissions)): ?>
            <a href="<?php echo BASE_URL; ?>/requisition/create" class="action-card">
                <i class="fas fa-file-signature"></i>
                <span>New Requisition</span>
            </a>
            <?php endif; ?>
            
            <?php if ($isSuperAdmin || in_array('audit.create', $userPermissions)): ?>
            <a href="<?php echo BASE_URL; ?>/audit/quarterly/create" class="action-card">
                <i class="fas fa-clipboard-check"></i>
                <span>New Audit</span>
            </a>
            <?php endif; ?>
            
            <?php if ($isSuperAdmin || in_array('weapons.create', $userPermissions)): ?>
            <a href="<?php echo BASE_URL; ?>/weapons/create" class="action-card">
                <i class="fas fa-plus-circle"></i>
                <span>Add Weapon</span>
            </a>
            <?php endif; ?>
            
            <?php if ($isSuperAdmin || in_array('land.create', $userPermissions)): ?>
            <a href="<?php echo BASE_URL; ?>/land/create" class="action-card">
                <i class="fas fa-map-marked-alt"></i>
                <span>Add Land Asset</span>
            </a>
            <?php endif; ?>
            
            <?php if ($isSuperAdmin || in_array('reports.view', $userPermissions)): ?>
            <a href="<?php echo BASE_URL; ?>/reports" class="action-card">
                <i class="fas fa-chart-bar"></i>
                <span>Generate Report</span>
            </a>
            <?php endif; ?>
            
            <a href="#" onclick="window.location.reload()" class="action-card">
                <i class="fas fa-sync-alt"></i>
                <span>Refresh</span>
            </a>
        </div>
    </div>
</div>

<script>
function refreshDashboard() {
    const btn = event.target.closest('button');
    const originalHtml = btn.innerHTML;
    
    btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Refreshing...';
    btn.disabled = true;
    
    setTimeout(() => {
        location.reload();
    }, 1000);
}
</script>