<?php
include 'branch_connection.php';
session_start();

$userid = isset($_SESSION['userid']) ? $_SESSION['userid'] : 'Not set';
$username = isset($_SESSION['username']) ? $_SESSION['username'] : '';
$is_admin = ($username === 'admin');

$start_date = isset($_GET['start_date']) ? $_GET['start_date'] : '';
$end_date   = isset($_GET['end_date']) ? $_GET['end_date'] : '';

// Base query
$sql = "SELECT t.*, s.segment_name, u.name FROM tb_positiveleads t 
        LEFT JOIN segments s ON t.segment = s.id 
        LEFT JOIN users u ON t.staff = u.id WHERE 1=1";

// Staff filter (if not admin)
$params = [];
if (!$is_admin) {
    $sql .= " AND t.staff = ?";
    $params[] = $userid;
}

// Date filters
if ($start_date != '' && $end_date != '') {
    $sql .= " AND DATE(t.created_at) BETWEEN ? AND ?";
    $params[] = $start_date;
    $params[] = $end_date;
} elseif ($start_date != '') {
    $sql .= " AND DATE(t.created_at) >= ?";
    $params[] = $start_date;
} elseif ($end_date != '') {
    $sql .= " AND DATE(t.created_at) <= ?";
    $params[] = $end_date;
}

$sql .= " ORDER BY t.created_at DESC";

$stmt = $conn->prepare($sql);

// Bind parameters dynamically
if (count($params) > 0) {
    $types = '';
    foreach ($params as $p) $types .= is_int($p) ? 'i' : 's';
    $stmt->bind_param($types, ...$params);
}

$stmt->execute();
$result = $stmt->get_result();
$rows = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Datewise Report</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css">
    <style>
        /* Hide the print button when printing */
        @media print {
            #btnprint {
                display: none;
            }
            @page {
        size: A4 portrait;
        margin:0!important;
        
                
            }
            .container-fluid{
                margin-top:50px;
            }
        }

        /* Make table single line, remove padding/margin */
        table {
            border-collapse: collapse;
            width: 100%;
            font-size: 12px;
        }
        table th, table td {
            padding: 4px 6px !important;
            border: 1px solid #000 !important;
            white-space: nowrap;
        }
        .container {
            padding: 10px !important;
        }
        h3, p {
            margin: 2px 0;
        }
    </style>
</head>
<body>
<div class="container-fluid text-center">
    <h3>Positive Leads Report</h3>
    <p>From: <?= htmlspecialchars($start_date) ?> To: <?= htmlspecialchars($end_date) ?></p>
    <button onclick="window.print();" class="btn btn-success mb-2" id='btnprint'>Print</button>
<div class="table-responsive">
    <table class="table table-bordered table-striped">
        <thead>
            <tr>
                <th>#</th>
                <th style="width:50px !important;">Company</th>
                <th>Mobile</th>
                <th>Location</th>
                <th>Segment</th>
                <th>Status</th>
                <th>Remarks</th>
                <th>Staff</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <?php if(count($rows) > 0): 
                $count = 1;
                foreach($rows as $r): ?>
                    <tr>
                        <td><?= $count++ ?></td>
                        <td style="width:50px !important;text-align:left">
                        <?php 
                                $text = $r['company_name'];
                                if(strlen($text) > 20){
                                    echo htmlspecialchars(substr($text, 0, 20)) . '...';
                                } else {
                                    echo htmlspecialchars($text);
                                }
                            ?>
                        </td>
                        <td><?= htmlspecialchars($r['mobile']) ?></td>
                        <td><?= htmlspecialchars($r['location']) ?></td>
                        <td><?= htmlspecialchars($r['segment_name']) ?></td>
                        <td><?= htmlspecialchars($r['status']) ?></td>
                        <td>
                            <?php 
                                $text = $r['remarks'];
                                if(strlen($text) > 10){
                                    echo htmlspecialchars(substr($text, 0, 10)) . '...';
                                } else {
                                    echo htmlspecialchars($text);
                                }
                            ?>
                            </td>
                        <td><?= htmlspecialchars($r['name']) ?></td>
                        <td><?= date('d-m-Y', strtotime($r['created_at'])) ?></td>
                    </tr>
            <?php endforeach; else: ?>
                <tr>
                    <td colspan="9" class="text-center">No records found</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
</div>
</body>
</html>
<?php $conn->close(); ?>
