<?php
session_start();
if(!isset($_SESSION['username'])) {
    header("Location: ../login.php");
    exit();
}

include('../includes/db.php');

$from_date = isset($_GET['from_date']) ? $_GET['from_date'] : date('Y-m-01');
$to_date = isset($_GET['to_date']) ? $_GET['to_date'] : date('Y-m-d');

$sql = "SELECT * FROM manual_entries 
        WHERE entry_date BETWEEN '$from_date' AND '$to_date'
        ORDER BY entry_date ASC, id ASC";
$result = mysqli_query($conn, $sql);

$entries = [];
$total_hsd = 0;
$total_super = 0;
$total_other = 0;
$total_amount = 0;

if($result) {
    while($row = mysqli_fetch_assoc($result)) {
        $entries[] = $row;
        $total_hsd += $row['hsd_liters'];
        $total_super += $row['super_liters'];
        $total_other += $row['other_liters'];
        $total_amount += $row['amount'];
    }
}
?>

<?php include('../includes/log.php'); ?>
<?php include('../includes/sd.php'); ?>

<!-- ========== PRINT-ONLY HEADER ========== -->
<div class="print-only print-header-container">
    <div class="print-company-name">ALFALAH PETROLEUM SERVICES</div>
    <div class="print-report-title">Tando Muhammad Khan</div>
    <div class="print-report-title">Ashok: 03008378775</div>
    
</div>
<!-- ====================================== -->

<div class="entries-list-container" id="main-report-container">
    <div class="page-header no-print">
        <div>
            <h2><i class="fas fa-table"></i> Manual Entries Report</h2>
            <p>Period: <?php echo date('d-m-Y', strtotime($from_date)); ?> to <?php echo date('d-m-Y', strtotime($to_date)); ?></p>
        </div>
        <div class="header-buttons">
            <a href="manual_entry.php" class="btn-add"><i class="fas fa-plus"></i> New Entry</a>
            <button onclick="window.print()" class="btn-print"><i class="fas fa-print"></i> Print</button>
        </div>
    </div>

    <!-- Filter -->
    <div class="filter-card no-print">
        <form method="GET" class="filter-form">
            <div class="filter-row">
                <div class="filter-group">
                    <label>From Date</label>
                    <input type="date" name="from_date" value="<?php echo $from_date; ?>">
                </div>
                <div class="filter-group">
                    <label>To Date</label>
                    <input type="date" name="to_date" value="<?php echo $to_date; ?>">
                </div>
                <div class="filter-actions">
                    <button type="submit" class="btn-search">Filter</button>
                    <a href="manual_entries_list.php" class="btn-reset">Reset</a>
                </div>
            </div>
        </form>
    </div>

    <!-- Summary Stats (visible on screen, hidden when printing) -->
    <div class="summary-stats">
        <div class="stat-item"><span class="stat-label">Total HSD</span><span class="stat-value"><?php echo number_format($total_hsd, 2); ?> L</span></div>
        <div class="stat-item"><span class="stat-label">Total Super</span><span class="stat-value"><?php echo number_format($total_super, 2); ?> L</span></div>
        <div class="stat-item"><span class="stat-label">Total Others</span><span class="stat-value"><?php echo number_format($total_other, 2); ?> L</span></div>
        <div class="stat-item"><span class="stat-label">Total Amount</span><span class="stat-value">Rs <?php echo number_format($total_amount, 2); ?></span></div>
    </div>

    <!-- Data Table -->
    <div class="table-card" id="report-content">
        <div class="table-card-header no-print">
            <div class="header-info"><i class="fas fa-list"></i><h4>Entries List</h4></div>
            <div class="entry-count">Total: <?php echo count($entries); ?> entries</div>
        </div>
        <div class="table-responsive">
            <table class="report-table" id="print-table">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>S/No</th>
                        <th>Veh No</th>
                        <th>HSD (L)</th>
                        <th>Super (L)</th>
                        <th>Others (L)</th>
                        <th>Amount (Rs)</th>
                        <th class="no-print">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($entries as $entry): ?>
                        <tr>
                            <td data-label="Date"><?php echo date('d-m-Y', strtotime($entry['entry_date'])); ?></td>
                            <td data-label="S/No"><?php echo htmlspecialchars($entry['serial_no'] ?: '—'); ?></td>
                            <td data-label="Veh No"><?php echo htmlspecialchars($entry['vehicle_no'] ?: '—'); ?></td>
                            <td data-label="HSD (L)" class="text-right"><?php echo number_format($entry['hsd_liters'], 2); ?></td>
                            <td data-label="Super (L)" class="text-right"><?php echo number_format($entry['super_liters'], 2); ?></td>
                            <td data-label="Others (L)" class="text-right"><?php echo number_format($entry['other_liters'], 2); ?></td>
                            <td data-label="Amount (Rs)" class="text-right"><?php echo number_format($entry['amount'], 2); ?></td>
                            <td data-label="Action" class="no-print"><a href="manual_entry_delete.php?id=<?php echo $entry['id']; ?>" class="btn-delete" onclick="return confirm('Delete?')">Delete</a></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if(count($entries) == 0): ?>
                        <tr><td colspan="8" class="empty-state">No entries found</td></tr>
                    <?php endif; ?>
                </tbody>
                <tfoot>
                    <tr class="total-row">
                        <td colspan="3"><strong>GRAND TOTAL</strong></td>
                        <td class="text-right"><strong><?php echo number_format($total_hsd, 2); ?> L</strong></td>
                        <td class="text-right"><strong><?php echo number_format($total_super, 2); ?> L</strong></td>
                        <td class="text-right"><strong><?php echo number_format($total_other, 2); ?> L</strong></td>
                        <td class="text-right"><strong>Rs <?php echo number_format($total_amount, 2); ?></strong></td>
                        <td class="no-print"></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>

<style>
/* ============ GENERAL STYLES ============ */
.entries-list-container { padding: 20px 30px; }
.page-header { background: linear-gradient(135deg, #A04657, #6b2a36); border-radius: 16px; padding: 20px 25px; margin-bottom: 20px; display: flex; justify-content: space-between; align-items: center; color: #fff; }
.btn-add, .btn-print { background: rgba(255,255,255,0.2); color: #fff; padding: 8px 18px; border-radius: 8px; text-decoration: none; border: none; cursor: pointer; margin-left: 10px; font-size: 14px; transition: all 0.3s ease; }
.btn-add:hover, .btn-print:hover { background: rgba(255,255,255,0.35); transform: translateY(-1px); }
.filter-card { background: #fff; border-radius: 14px; padding: 20px; margin-bottom: 20px; border: 1px solid #eee; box-shadow: 0 2px 8px rgba(0,0,0,0.04); }
.filter-row { display: flex; gap: 15px; align-items: flex-end; flex-wrap: wrap; }
.filter-group label { font-weight: 600; font-size: 13px; color: #555; display: block; margin-bottom: 4px; }
.filter-group input { padding: 8px 12px; border: 2px solid #e0e0e0; border-radius: 8px; font-size: 14px; }
.btn-search { background: #A04657; color: #fff; border: none; padding: 8px 20px; border-radius: 8px; cursor: pointer; font-weight: 600; }
.btn-reset { background: #6c757d; color: #fff; padding: 8px 20px; border-radius: 8px; text-decoration: none; font-weight: 600; display: inline-block; }
.summary-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 20px; }
.stat-item { background: #fff; border-radius: 14px; padding: 18px 15px; text-align: center; border-left: 4px solid #A04657; box-shadow: 0 2px 8px rgba(0,0,0,0.04); }
.stat-label { font-size: 13px; color: #777; font-weight: 500; }
.stat-value { display: block; font-size: 20px; font-weight: 800; margin-top: 5px; color: #2d2d2d; }
.table-card { background: #fff; border-radius: 14px; overflow: hidden; border: 1px solid #eee; box-shadow: 0 2px 8px rgba(0,0,0,0.04); }
.table-card-header { padding: 14px 20px; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; align-items: center; }
.header-info { display: flex; align-items: center; gap: 8px; }
.header-info h4 { margin: 0; font-size: 15px; }
.entry-count { font-size: 13px; color: #888; background: #f5f5f5; padding: 4px 12px; border-radius: 20px; }
.report-table { width: 100%; border-collapse: collapse; }
.report-table th, .report-table td { padding: 12px 14px; border-bottom: 1px solid #eee; text-align: left; font-size: 14px; }
.report-table th { background: #f8f9fc; font-weight: 700; color: #444; white-space: nowrap; }
.report-table tbody tr:hover { background: #fafbfc; }
.text-right { text-align: right; }
.total-row { background: #f8f9fa; font-weight: bold; }
.total-row td { padding: 14px; font-size: 15px; border-top: 2px solid #ddd; }
.btn-delete { background: #dc3545; color: #fff; padding: 5px 12px; border-radius: 6px; text-decoration: none; font-size: 12px; font-weight: 500; transition: all 0.2s ease; }
.btn-delete:hover { background: #b52a37; }
.empty-state { text-align: center; padding: 40px; color: #999; font-style: italic; }

/* ============ PRINT-ONLY HEADER (hidden on screen) ============ */
.print-only { display: none; }

/* ============ PRINT MEDIA STYLES ============ */
@media print {
    /* Hide EVERYTHING by default */
    body * {
        visibility: hidden;
    }

    /* Hide sidebar, top nav, footer completely */
    nav, .sidebar, .main-sidebar, .navbar, .topbar, 
    header:not(.print-header-container), 
    footer, .footer, .main-footer,
    [class*="sidebar"], [class*="Sidebar"], [class*="side-nav"],
    [id*="sidebar"], [id*="Sidebar"],
    .no-print, .header-buttons, .filter-card, .table-card-header,
    .btn-delete, .btn-add, .btn-print, .btn-search, .btn-reset {
        display: none !important;
        visibility: hidden !important;
    }

    /* Hide summary stats completely in print */
    .summary-stats,
    .summary-stats *,
    .stat-item,
    .stat-item * {
        display: none !important;
        visibility: hidden !important;
    }

    /* Show ONLY the report container, print header, and table */
    #main-report-container,
    #main-report-container *,
    .print-only,
    .print-only *,
    #print-table,
    #print-table *,
    .table-card,
    .table-card *,
    .table-responsive,
    .table-responsive *,
    .report-table,
    .report-table * {
        visibility: visible;
    }

    /* Position the report container */
    #main-report-container {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        padding: 10px 15px !important;
        margin: 0 !important;
    }

    /* Show the print header */
    .print-only {
        display: block !important;
        visibility: visible !important;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        text-align: center;
        padding: 15px 20px 5px 20px;
        z-index: 9999;
        background: #fff;
    }

    .print-header-container {
        border-bottom: 2px solid #A04657;
        margin-bottom: 10px;
        padding-bottom: 10px;
    }

    .print-company-name {
        font-size: 22px;
        font-weight: 900;
        color: #A04657;
        letter-spacing: 1px;
        text-transform: uppercase;
        margin-bottom: 4px;
    }

    .print-report-title {
        font-size: 16px;
        font-weight: 700;
        color: #333;
        margin-bottom: 3px;
    }

    .print-period {
        font-size: 13px;
        color: #666;
        font-weight: 500;
    }

    /* Adjust report container to make room for print header */
    #main-report-container {
        margin-top: 100px !important;
    }

    /* Table print styles */
    .table-card {
        box-shadow: none !important;
        border: 1px solid #ddd !important;
        border-radius: 0 !important;
        visibility: visible !important;
    }

    #print-table {
        width: 100% !important;
        border-collapse: collapse !important;
        visibility: visible !important;
    }

    #print-table thead {
        display: table-header-group !important;
    }

    #print-table th {
        background: #f0f0f0 !important;
        color: #000 !important;
        font-weight: 700 !important;
        border: 1px solid #ccc !important;
        padding: 8px 10px !important;
        font-size: 12px !important;
        visibility: visible !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    #print-table td {
        border: 1px solid #ddd !important;
        padding: 7px 10px !important;
        font-size: 12px !important;
        visibility: visible !important;
        color: #000 !important;
    }

    #print-table .text-right {
        text-align: right !important;
    }

    #print-table .total-row td {
        background: #f5f5f5 !important;
        font-weight: bold !important;
        border-top: 2px solid #000 !important;
        font-size: 13px !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    .empty-state {
        text-align: center !important;
        padding: 30px !important;
        color: #555 !important;
    }

    /* Hide action column in print */
    #print-table th:last-child,
    #print-table td:last-child {
        display: none !important;
    }

    /* Hide the no-print class elements */
    .no-print {
        display: none !important;
        visibility: hidden !important;
    }

    /* Page setup */
    @page {
        size: A4 portrait;
        margin: 12mm 10mm 12mm 10mm;
    }

    /* Ensure white background */
    body {
        background: #fff !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    /* Remove hover effects in print */
    .report-table tbody tr:hover {
        background: transparent !important;
    }
}

/* ============ RESPONSIVE (Screen only) ============ */
@media screen and (max-width: 768px) { 
    .entries-list-container { padding: 12px; } 
    .summary-stats { grid-template-columns: 1fr 1fr; } 
    .page-header { flex-direction: column; text-align: center; gap: 15px; } 
    .header-buttons { display: flex; gap: 8px; }
    .report-table th { display: none; } 
    .report-table td { display: block; text-align: right; padding-left: 50%; position: relative; } 
    .report-table td::before { content: attr(data-label); position: absolute; left: 12px; font-weight: bold; color: #555; } 
    .report-table .total-row td { display: table-cell; text-align: right; padding: 12px; }
    .report-table .total-row td:first-child { text-align: left; }
}
</style>

<?php include('../includes/ft.php'); ?>