<?php
/**
 * Admin Dashboard
 * Patel E-Mitra & Aadhaar Seva Kendra CMS
 */

require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/includes/auth.php';

requireLogin();

$page_title = 'Dashboard';

$db = getDB();

// Get statistics
$stats = [];

// Total Services
$stmt = $db->query("SELECT COUNT(*) as count FROM services WHERE status = 'active'");
$stats['services'] = $stmt->fetch()['count'];

// Total Gallery Images
$stmt = $db->query("SELECT COUNT(*) as count FROM gallery WHERE status = 'active'");
$stats['gallery'] = $stmt->fetch()['count'];

// Total Appointments
$stmt = $db->query("SELECT COUNT(*) as count FROM appointments");
$stats['total_appointments'] = $stmt->fetch()['count'];

// Pending Appointments
$stmt = $db->query("SELECT COUNT(*) as count FROM appointments WHERE status = 'pending'");
$stats['pending_appointments'] = $stmt->fetch()['count'];

// Today's Appointments
$stmt = $db->query("SELECT COUNT(*) as count FROM appointments WHERE appointment_date = CURDATE()");
$stats['today_appointments'] = $stmt->fetch()['count'];

// Total Enquiries
$stmt = $db->query("SELECT COUNT(*) as count FROM enquiries");
$stats['total_enquiries'] = $stmt->fetch()['count'];

// New Enquiries
$stmt = $db->query("SELECT COUNT(*) as count FROM enquiries WHERE status = 'new'");
$stats['new_enquiries'] = $stmt->fetch()['count'];

// Active Notices
$stmt = $db->query("SELECT COUNT(*) as count FROM notices WHERE status = 'active'");
$stats['active_notices'] = $stmt->fetch()['count'];

// Total Downloads
$stmt = $db->query("SELECT COUNT(*) as count FROM downloads WHERE status = 'active'");
$stats['downloads'] = $stmt->fetch()['count'];

// Total Users
$stmt = $db->query("SELECT COUNT(*) as count FROM users WHERE status = 'active'");
$stats['users'] = $stmt->fetch()['count'];

// Recent Appointments
$stmt = $db->query("SELECT * FROM appointments ORDER BY created_at DESC LIMIT 5");
$recent_appointments = $stmt->fetchAll();

// Recent Enquiries
$stmt = $db->query("SELECT * FROM enquiries ORDER BY created_at DESC LIMIT 5");
$recent_enquiries = $stmt->fetchAll();

include dirname(__DIR__) . '/includes/header.php';
?>

<!-- Statistics Cards -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
    <!-- Total Services -->
    <div class="bg-gradient-to-r from-blue-500 to-blue-600 rounded-xl shadow-lg p-6 text-white">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-blue-100 text-sm">Total Services</p>
                <p class="text-3xl font-bold mt-2"><?php echo $stats['services']; ?></p>
            </div>
            <div class="bg-white bg-opacity-20 p-4 rounded-lg">
                <i class="fa-solid fa-list-check text-3xl"></i>
            </div>
        </div>
    </div>
    
    <!-- Pending Appointments -->
    <div class="bg-gradient-to-r from-orange-500 to-orange-600 rounded-xl shadow-lg p-6 text-white">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-orange-100 text-sm">Pending Appointments</p>
                <p class="text-3xl font-bold mt-2"><?php echo $stats['pending_appointments']; ?></p>
            </div>
            <div class="bg-white bg-opacity-20 p-4 rounded-lg">
                <i class="fa-solid fa-clock text-3xl"></i>
            </div>
        </div>
    </div>
    
    <!-- New Enquiries -->
    <div class="bg-gradient-to-r from-green-500 to-green-600 rounded-xl shadow-lg p-6 text-white">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-green-100 text-sm">New Enquiries</p>
                <p class="text-3xl font-bold mt-2"><?php echo $stats['new_enquiries']; ?></p>
            </div>
            <div class="bg-white bg-opacity-20 p-4 rounded-lg">
                <i class="fa-solid fa-envelope text-3xl"></i>
            </div>
        </div>
    </div>
    
    <!-- Gallery Images -->
    <div class="bg-gradient-to-r from-purple-500 to-purple-600 rounded-xl shadow-lg p-6 text-white">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-purple-100 text-sm">Gallery Images</p>
                <p class="text-3xl font-bold mt-2"><?php echo $stats['gallery']; ?></p>
            </div>
            <div class="bg-white bg-opacity-20 p-4 rounded-lg">
                <i class="fa-solid fa-images text-3xl"></i>
            </div>
        </div>
    </div>
</div>

<!-- Quick Stats -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
    <!-- Today's Appointments -->
    <div class="bg-white rounded-xl shadow-lg p-6">
        <div class="flex items-center gap-4">
            <div class="bg-blue-100 p-3 rounded-lg">
                <i class="fa-solid fa-calendar-day text-blue-600 text-2xl"></i>
            </div>
            <div>
                <p class="text-gray-600 text-sm">Today's Appointments</p>
                <p class="text-2xl font-bold text-gray-800"><?php echo $stats['today_appointments']; ?></p>
            </div>
        </div>
    </div>
    
    <!-- Total Appointments -->
    <div class="bg-white rounded-xl shadow-lg p-6">
        <div class="flex items-center gap-4">
            <div class="bg-green-100 p-3 rounded-lg">
                <i class="fa-solid fa-calendar-check text-green-600 text-2xl"></i>
            </div>
            <div>
                <p class="text-gray-600 text-sm">Total Appointments</p>
                <p class="text-2xl font-bold text-gray-800"><?php echo $stats['total_appointments']; ?></p>
            </div>
        </div>
    </div>
    
    <!-- Active Notices -->
    <div class="bg-white rounded-xl shadow-lg p-6">
        <div class="flex items-center gap-4">
            <div class="bg-yellow-100 p-3 rounded-lg">
                <i class="fa-solid fa-bullhorn text-yellow-600 text-2xl"></i>
            </div>
            <div>
                <p class="text-gray-600 text-sm">Active Notices</p>
                <p class="text-2xl font-bold text-gray-800"><?php echo $stats['active_notices']; ?></p>
            </div>
        </div>
    </div>
</div>

<!-- Recent Activity -->
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
    <!-- Recent Appointments -->
    <div class="bg-white rounded-xl shadow-lg p-6">
        <div class="flex items-center justify-between mb-4">
            <h2 class="text-xl font-bold text-gray-800">
                <i class="fa-solid fa-calendar-check text-blue-600 mr-2"></i>
                Recent Appointments
            </h2>
            <a href="<?php echo SITE_URL; ?>/admin/appointments/" class="text-blue-600 hover:text-blue-800 text-sm">
                View All <i class="fa-solid fa-arrow-right ml-1"></i>
            </a>
        </div>
        
        <?php if (empty($recent_appointments)): ?>
            <p class="text-gray-500 text-center py-8">No appointments yet</p>
        <?php else: ?>
            <div class="space-y-3">
                <?php foreach ($recent_appointments as $appointment): ?>
                    <div class="flex items-center gap-3 p-3 bg-gray-50 rounded-lg hover:bg-gray-100 transition">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center">
                            <i class="fa-solid fa-user text-blue-600"></i>
                        </div>
                        <div class="flex-1">
                            <p class="font-semibold text-gray-800"><?php echo sanitize($appointment['customer_name']); ?></p>
                            <p class="text-sm text-gray-600"><?php echo sanitize($appointment['service_name']); ?></p>
                        </div>
                        <div class="text-right">
                            <span class="inline-block px-2 py-1 text-xs rounded-full 
                                <?php echo $appointment['status'] === 'pending' ? 'bg-yellow-100 text-yellow-800' : 
                                   ($appointment['status'] === 'confirmed' ? 'bg-green-100 text-green-800' : 
                                   ($appointment['status'] === 'completed' ? 'bg-blue-100 text-blue-800' : 'bg-red-100 text-red-800')); ?>">
                                <?php echo ucfirst($appointment['status']); ?>
                            </span>
                            <p class="text-xs text-gray-500 mt-1"><?php echo formatDate($appointment['appointment_date']); ?></p>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
    
    <!-- Recent Enquiries -->
    <div class="bg-white rounded-xl shadow-lg p-6">
        <div class="flex items-center justify-between mb-4">
            <h2 class="text-xl font-bold text-gray-800">
                <i class="fa-solid fa-envelope text-green-600 mr-2"></i>
                Recent Enquiries
            </h2>
            <a href="<?php echo SITE_URL; ?>/admin/enquiries/" class="text-blue-600 hover:text-blue-800 text-sm">
                View All <i class="fa-solid fa-arrow-right ml-1"></i>
            </a>
        </div>
        
        <?php if (empty($recent_enquiries)): ?>
            <p class="text-gray-500 text-center py-8">No enquiries yet</p>
        <?php else: ?>
            <div class="space-y-3">
                <?php foreach ($recent_enquiries as $enquiry): ?>
                    <div class="flex items-center gap-3 p-3 bg-gray-50 rounded-lg hover:bg-gray-100 transition">
                        <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center">
                            <i class="fa-solid fa-user text-green-600"></i>
                        </div>
                        <div class="flex-1">
                            <p class="font-semibold text-gray-800"><?php echo sanitize($enquiry['name']); ?></p>
                            <p class="text-sm text-gray-600"><?php echo sanitize($enquiry['phone']); ?></p>
                        </div>
                        <div class="text-right">
                            <span class="inline-block px-2 py-1 text-xs rounded-full 
                                <?php echo $enquiry['status'] === 'new' ? 'bg-red-100 text-red-800' : 
                                   ($enquiry['status'] === 'read' ? 'bg-yellow-100 text-yellow-800' : 
                                   ($enquiry['status'] === 'replied' ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800')); ?>">
                                <?php echo ucfirst($enquiry['status']); ?>
                            </span>
                            <p class="text-xs text-gray-500 mt-1"><?php echo formatDate($enquiry['created_at']); ?></p>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php include dirname(__DIR__) . '/includes/footer.php'; ?>
