<?php
require_once 'config/database.php';
require_once 'config/session.php';
require_once 'includes/header.php';

requireLogin();

$pageTitle = 'Dashboard';

// Get dashboard statistics
try {
    $pdo = getConnection();
    
    // Total customers
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM customers");
    $totalCustomers = $stmt->fetch()['total'];
    
    // Total quotations
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM quotations");
    $totalQuotations = $stmt->fetch()['total'];
    
    // Total invoices
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM invoices");
    $totalInvoices = $stmt->fetch()['total'];
    
    // Total materials
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM materials");
    $totalMaterials = $stmt->fetch()['total'];
    
    // Recent quotations
    $stmt = $pdo->query("
        SELECT q.*, c.name as customer_name 
        FROM quotations q 
        JOIN customers c ON q.customer_id = c.id 
        ORDER BY q.created_at DESC 
        LIMIT 5
    ");
    $recentQuotations = $stmt->fetchAll();
    
    // Recent invoices
    $stmt = $pdo->query("
        SELECT i.*, c.name as customer_name 
        FROM invoices i 
        JOIN customers c ON i.customer_id = c.id 
        ORDER BY i.created_at DESC 
        LIMIT 5
    ");
    $recentInvoices = $stmt->fetchAll();
    
} catch (Exception $e) {
    $totalCustomers = $totalQuotations = $totalInvoices = $totalMaterials = 0;
    $recentQuotations = $recentInvoices = [];
}
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">
            <i class="fas fa-tachometer-alt text-primary"></i> Dashboard
        </h1>
        <div class="text-muted">
            <i class="fas fa-calendar-alt"></i> <?php echo date('l, F j, Y'); ?>
        </div>
    </div>
    
    <!-- Welcome Card -->
    <!--<div class="row mb-4">-->
    <!--    <div class="col-12">-->
    <!--        <div class="card bg-gradient-primary text-white shadow">-->
    <!--            <div class="card-body">-->
    <!--                <div class="row no-gutters align-items-center">-->
    <!--                    <div class="col mr-2">-->
    <!--                        <div class="text-xs font-weight-bold text-uppercase mb-1">-->
    <!--                            Welcome Back-->
    <!--                        </div>-->
    <!--                        <div class="h5 mb-0 font-weight-bold">-->
    <!--                            Hello, <?php echo htmlspecialchars($_SESSION['user_name']); ?>!-->
    <!--                        </div>-->
    <!--                        <div class="text-xs mt-1">-->
    <!--                            <i class="fas fa-clock"></i> Last login: Today-->
    <!--                        </div>-->
    <!--                    </div>-->
    <!--                    <div class="col-auto">-->
    <!--                        <i class="fas fa-user-circle fa-4x text-white-50"></i>-->
    <!--                    </div>-->
    <!--                </div>-->
    <!--            </div>-->
    <!--        </div>-->
    <!--    </div>-->
    <!--</div>-->
    
    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-primary shadow h-100 py-2 hover-card">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                                Total Customers
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $totalCustomers; ?></div>
                            <div class="text-xs text-success">
                                <i class="fas fa-arrow-up"></i> Active customers
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-users fa-2x text-primary"></i>
                        </div>
                    </div>
                </div>
                <div class="card-footer bg-primary text-white">
                    <a href="customers.php" class="text-white text-decoration-none">
                        <small>View All <i class="fas fa-arrow-right"></i></small>
                    </a>
                </div>
            </div>
        </div>
        
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-success shadow h-100 py-2 hover-card">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                Total Quotations
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $totalQuotations; ?></div>
                            <div class="text-xs text-info">
                                <i class="fas fa-file-alt"></i> Business proposals
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-file-alt fa-2x text-success"></i>
                        </div>
                    </div>
                </div>
                <div class="card-footer bg-success text-white">
                    <a href="quotations.php" class="text-white text-decoration-none">
                        <small>View All <i class="fas fa-arrow-right"></i></small>
                    </a>
                </div>
            </div>
        </div>
        
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-info shadow h-100 py-2 hover-card">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
                                Total Invoices
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $totalInvoices; ?></div>
                            <div class="text-xs text-warning">
                                <i class="fas fa-money-bill-wave"></i> Revenue generated
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-file-invoice fa-2x text-info"></i>
                        </div>
                    </div>
                </div>
                <div class="card-footer bg-info text-white">
                    <a href="invoices.php" class="text-white text-decoration-none">
                        <small>View All <i class="fas fa-arrow-right"></i></small>
                    </a>
                </div>
            </div>
        </div>
        
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-warning shadow h-100 py-2 hover-card">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                                Total Materials
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $totalMaterials; ?></div>
                            <div class="text-xs text-secondary">
                                <i class="fas fa-warehouse"></i> Inventory items
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-boxes fa-2x text-warning"></i>
                        </div>
                    </div>
                </div>
                <div class="card-footer bg-warning text-dark">
                    <a href="materials.php" class="text-dark text-decoration-none">
                        <small>View All <i class="fas fa-arrow-right"></i></small>
                    </a>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Quick Actions -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card shadow">
                <div class="card-header bg-gradient-primary text-white py-3">
                    <h6 class="m-0 font-weight-bold">
                        <i class="fas fa-bolt"></i> Quick Actions
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3 mb-3">
                            <a href="customers.php?action=add" class="btn btn-primary btn-lg w-100 quick-action-btn">
                                <i class="fas fa-user-plus fa-2x mb-2"></i><br>
                                <strong>Add Customer</strong><br>
                                <small>Register new client</small>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="quotations.php?action=add" class="btn btn-success btn-lg w-100 quick-action-btn">
                                <i class="fas fa-file-plus fa-2x mb-2"></i><br>
                                <strong>Add Quotation</strong><br>
                                <small>Create proposal</small>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="invoices.php?action=add" class="btn btn-info btn-lg w-100 quick-action-btn">
                                <i class="fas fa-file-invoice-dollar fa-2x mb-2"></i><br>
                                <strong>Add Invoice</strong><br>
                                <small>Generate bill</small>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="materials.php?action=add" class="btn btn-warning btn-lg w-100 quick-action-btn">
                                <i class="fas fa-box fa-2x mb-2"></i><br>
                                <strong>Add Material</strong><br>
                                <small>Update inventory</small>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Recent Activity -->
    <div class="row">
        <div class="col-lg-6 mb-4">
            <div class="card shadow">
                <div class="card-header bg-gradient-success text-white py-3">
                    <h6 class="m-0 font-weight-bold">
                        <i class="fas fa-file-alt"></i> Recent Quotations
                    </h6>
                </div>
                <div class="card-body">
                    <?php if (empty($recentQuotations)): ?>
                        <p class="text-muted">No quotations found.</p>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>Quotation #</th>
                                        <th>Customer</th>
                                        <th>Total</th>
                                        <th>Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($recentQuotations as $quotation): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($quotation['quotation_number']); ?></td>
                                        <td><?php echo htmlspecialchars($quotation['customer_name']); ?></td>
                                        <td>₹<?php echo number_format($quotation['grand_total'], 2); ?></td>
                                        <td>
                                            <span class="badge bg-<?php echo $quotation['status'] == 'accepted' ? 'success' : ($quotation['status'] == 'rejected' ? 'danger' : 'warning'); ?>">
                                                <?php echo ucfirst($quotation['status']); ?>
                                            </span>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <div class="col-lg-6 mb-4">
            <div class="card shadow">
                <div class="card-header bg-gradient-info text-white py-3">
                    <h6 class="m-0 font-weight-bold">
                        <i class="fas fa-file-invoice"></i> Recent Invoices
                    </h6>
                </div>
                <div class="card-body">
                    <?php if (empty($recentInvoices)): ?>
                        <p class="text-muted">No invoices found.</p>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>Invoice #</th>
                                        <th>Customer</th>
                                        <th>Total</th>
                                        <th>Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($recentInvoices as $invoice): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($invoice['invoice_number']); ?></td>
                                        <td><?php echo htmlspecialchars($invoice['customer_name']); ?></td>
                                        <td>₹<?php echo number_format($invoice['grand_total'], 2); ?></td>
                                        <td>
                                            <span class="badge bg-<?php echo $invoice['payment_status'] == 'paid' ? 'success' : ($invoice['payment_status'] == 'partial' ? 'warning' : 'danger'); ?>">
                                                <?php echo ucfirst($invoice['payment_status']); ?>
                                            </span>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
/* Dashboard Styles */
.border-left-primary {
    border-left: 0.25rem solid #4e73df !important;
}
.border-left-success {
    border-left: 0.25rem solid #1cc88a !important;
}
.border-left-info {
    border-left: 0.25rem solid #36b9cc !important;
}
.border-left-warning {
    border-left: 0.25rem solid #f6c23e !important;
}

.text-xs {
    font-size: 0.7rem;
}
.font-weight-bold {
    font-weight: 700 !important;
}
.text-uppercase {
    text-transform: uppercase !important;
}
.text-gray-800 {
    color: #5a5c69 !important;
}
.text-gray-300 {
    color: #dddfeb !important;
}

/* Gradient Backgrounds */
.bg-gradient-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
}
.bg-gradient-success {
    background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%) !important;
}
.bg-gradient-info {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
}

/* Hover Effects */
.hover-card {
    transition: all 0.3s ease;
}
.hover-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15) !important;
}

.quick-action-btn {
    transition: all 0.3s ease;
    border: none;
    box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
}
.quick-action-btn:hover {
    transform: translateY(-3px);
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
}

/* Card Headers */
.card-header {
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

/* Table Styling */
.table th {
    border-top: none;
    font-weight: 600;
    font-size: 0.85rem;
    color: #5a5c69;
}
.table td {
    border-top: 1px solid #e3e6f0;
    font-size: 0.85rem;
}

/* Badge Styling */
.badge {
    font-size: 0.75rem;
    padding: 0.375rem 0.75rem;
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .quick-action-btn {
        margin-bottom: 1rem;
    }
    .h3 {
        font-size: 1.5rem;
    }
}
</style>

<?php require_once 'includes/footer.php'; ?>
