
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
$servername = "localhost";
$username = "balubaid_autozoneoffers";
$password = "Vision@2050";
$dbname = "balubaid_autozoneoffers";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get current month's start and end dates
$current_month_start = date('Y-m-01');
$current_month_end = date('Y-m-t');

// Get filter values from request or set defaults
$start_date = isset($_GET['start_date']) ? $_GET['start_date'] : $current_month_start;
$end_date = isset($_GET['end_date']) ? $_GET['end_date'] : $current_month_end;
$source_filter = isset($_GET['sourcee']) ? $_GET['sourcee'] : '';

// Build query with filters
$sql = "SELECT DATE_FORMAT(created_at,'%Y-%m-%d') as lead_date, sourcee, COUNT(id) as lead_count 
        FROM leads2 
        WHERE message = 'Added Successfully' 
        AND DATE(created_at) BETWEEN '$start_date' AND '$end_date'";

if (!empty($source_filter)) {
    $sql .= " AND sourcee = '$source_filter'";
}

$sql .= " GROUP BY DATE_FORMAT(created_at,'%Y-%m-%d'), sourcee WITH ROLLUP";

$result = $conn->query($sql);

if (!$result) {
    die("Query failed: " . $conn->error);
}

$leads_data = [];

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $leads_data[] = $row;
    }
}

$conn->close();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Leads Dashboard</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        table, th, td {
            border: 1px solid black;
            padding: 10px;
            text-align: left;
        }
    </style>
</head>
<body>
    <h2>Leads Dashboard</h2>
    
    <form method="GET">
        <label for="start_date">Start Date:</label>
        <input type="date" id="start_date" name="start_date" value="<?php echo $start_date; ?>">
        
        <label for="end_date">End Date:</label>
        <input type="date" id="end_date" name="end_date" value="<?php echo $end_date; ?>">
        
        <label for="sourcee">Source:</label>
        <input type="text" id="sourcee" name="sourcee" value="<?php echo $source_filter; ?>">
        
        <button type="submit">Filter</button>
    </form>

    <table>
        <tr>
            <th>Date</th>
            <th>Source</th>
            <th>Lead Count</th>
        </tr>
        <?php foreach ($leads_data as $row): ?>
            <tr>
                <td><?php echo $row['lead_date'] ? $row['lead_date'] : 'Total'; ?></td>
                <td><?php echo $row['sourcee'] ? $row['sourcee'] : 'Total'; ?></td>
                <td><?php echo $row['lead_count']; ?></td>
            </tr>
        <?php endforeach; ?>
    </table>

    <canvas id="leadsChart" width="400" height="200"></canvas>
    <script>
        var ctx = document.getElementById('leadsChart').getContext('2d');
        var chartData = <?php echo json_encode($leads_data); ?>;
        
        var labels = [];
        var data = [];
        
        chartData.forEach(function(row) {
            if (row.lead_date !== null && row.sourcee !== null) {
                labels.push(row.lead_date + ' (' + row.sourcee + ')');
                data.push(row.lead_count);
            }
        });
        
        var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: labels,
                datasets: [{
                    label: 'Leads Count',
                    data: data,
                    backgroundColor: 'rgba(75, 192, 192, 0.2)',
                    borderColor: 'rgba(75, 192, 192, 1)',
                    borderWidth: 1
                }]
            },
            options: {
                responsive: true,
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });
    </script>
</body>
</html>
