<!DOCTYPE html>
<html lang="en">
<head>
    <title>Teledata Print</title>
    <meta charset="utf-8">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            padding: 20px;
            background-color: #f9f9f9;
        }
        h1, h2 {
            text-align: center;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #4CAF50;
            color: white;
        }
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        tr:hover {
            background-color: #ddd;
        }
        .no-print {
            text-align: center;
            margin-top: 20px;
        }
        .btn {
            padding: 10px 15px;
            margin: 5px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        .btn-info {
            background-color: #007BFF;
            color: white;
        }
        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }
        .btn:hover {
            opacity: 0.8;
        }
        @media print {
            .no-print {
                display: none;
            }
            table {
                border: none;
            }
            th, td {
                border: 1px solid black;
            }
            .print-title {
                display: block;
            }body {
        margin: 0 !important;
        padding: 10px !important;
    }
    th, td {
            border: 1px solid #ddd;
            padding: 2px;
            text-align: left;
            border:1px dashed black;
        }
        }
        .print-title {
            display: none;
            margin-bottom: 20px;
            font-size: 18px;
            font-weight: bold;
        }
        .date-range-display {
            margin-bottom: 20px;
            font-style: italic;
            color: #555;
        }
        
    </style>
</head>
<body>
    <div class="print-title">Teledata Report</div>
    
    <?php
    session_start();
    include '../config.php';

    ini_set('display_errors', 0);
    error_reporting(E_ALL);

    // Get the query parameters
    $start_date = isset($_GET['start_date']) ? $_GET['start_date'] : '';
    $end_date = isset($_GET['end_date']) ? $_GET['end_date'] : '';
    $segment_filter = (isset($_GET['segment']) && $_GET['segment'] !== '') ? (int)$_GET['segment'] : null;
    $company_name = (isset($_GET['company_name']) && $_GET['company_name'] !== '') ? $_GET['company_name'] : null;
    // Prepare date range text
    $date_range_display = "";
    if ($start_date || $end_date) {
        $date_range_display = ($start_date ? $start_date : 'Start Date') . 
                             " to " . 
                             ($end_date ? $end_date : 'End Date');
    } else {
        $date_range_display = "All Records (No date filter applied)";
    }

    echo "<h1>Teledata Report</h1>";
    echo "<div class='date-range-display'>Date Range: " . htmlspecialchars($date_range_display) . "</div>";

    // Build dynamic SQL
    $teledata_rows = [];
    $sql = "SELECT t.*, s.segment_name, u.name 
            FROM teledata t 
            LEFT JOIN segments s ON t.segment = s.id 
            LEFT JOIN users u ON t.staff = u.id 
            WHERE 1=1";

    $params = [];
    $types = "";

    if (!is_null($company_name)) {
        $sql .= " AND t.company_name LIKE ?";
        $params[] = '%' . $company_name . '%';
        $types .= "s";
    }


        if (!is_null($segment_filter)) {
        $sql .= " AND t.segment = ?";
        $params[] = $segment_filter;
        $types .= "i";
    }

    if (!empty($start_date)) {
        $sql .= " AND t.created_at >= ?";
        $params[] = $start_date;
        $types .= "s";
    }

    if (!empty($end_date)) {
        $sql .= " AND t.created_at <= ?";
        $params[] = $end_date;
        $types .= "s";
    }

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

    $stmt = $conn->prepare($sql);
    if (!empty($params)) {
        $stmt->bind_param($types, ...$params);
    }

    $stmt->execute();
    $result = $stmt->get_result();

    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $teledata_rows[] = $row;
        }
    }
    $stmt->close();
    ?>

    <table>
        <thead>
            <tr>
                <th>Company</th>
                <th>Mobile No.</th>
                <th>Location</th>
                <th>Segment</th>
                <th>Staff</th>
                <th>Status</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <?php if (count($teledata_rows) > 0): ?>
                <?php foreach ($teledata_rows as $row): ?>
                    <tr>
                        <td><?= htmlspecialchars($row['company_name']) ?></td>
                        <td><?= htmlspecialchars($row['mobile']) ?></td>
                        <td><?= htmlspecialchars($row['location']) ?></td>
                        <td><?= htmlspecialchars($row['segment_name']) ?></td>
                        <td><?= htmlspecialchars($row['name']) ?></td>
                        <td><?= htmlspecialchars($row['status']) ?></td>
                        <td><?= date('Y-m-d', strtotime($row['created_at'])) ?></td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="7" class="text-center">No records found.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>

    <div class="no-print">
        <button onclick="window.print();" class="btn btn-info">Print Report</button>
        <button onclick="window.location.href='teledata.php';" class="btn btn-secondary">Back</button>
    </div>

    <?php $conn->close(); ?>
</body>
</html>
