<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

$username = $_SESSION['username'] ?? 'Guest';
$current_user = $_SESSION['username'] ?? '';
$current_role = strtolower($_SESSION['role'] ?? 'guest');

// ================= DATE FILTER =================
$date_from = $_GET['date_from'] ?? date('Y-m-d');
$date_to   = $_GET['date_to'] ?? date('Y-m-d');

$date_from = $_GET['date_from'] ?? date('Y-m-d');

/* CHECK SHIFT STATUS */
$shift_check = $conn->query("
    SELECT * FROM shifts 
    WHERE DATE(date_from) = '$date_from'
      AND status = 'CLOSED'
    LIMIT 1
")->fetch_assoc();

$shift_closed = $shift_check ? true : false;

/* ================= PAYMENTS ================= */
$payments = $conn->query("
    SELECT 
        p.id,
        p.payment_date,
        p.amount,
        p.method,
          r.room_number,
        p.guest_id,
        g.name AS paid_by,
        i.invoice_number,
        i.created_by
    FROM payments p
    LEFT JOIN guests g ON g.id = p.guest_id
    LEFT JOIN invoices i ON i.guest_id = p.guest_id
       LEFT JOIN stays s ON i.guest_id = s.guest_id
       LEFT JOIN rooms r 
        ON s.room_id = r.id
    WHERE DATE(p.payment_date) BETWEEN '$date_from' AND '$date_to'
      AND p.state = 4
    ORDER BY p.payment_date ASC
");

/* ================= TOTALS ================= */
$total_cash = 0;
$total_bank = 0;
$total_all  = 0;

$rows = [];

while($row = $payments->fetch_assoc()) {
    $rows[] = $row;

    $total_all += $row['amount'];

    if(strtolower($row['method']) == 'cash'){
        $total_cash += $row['amount'];
    } else {
        $total_bank += $row['amount'];
    }
}

$potential_cash = $total_cash;
$actual_cash    = $total_cash;
$over_short     = $actual_cash - $potential_cash;

$cashier_name = $rows[0]['created_by'] ?? $current_user;
?>

<!-- ================= STYLES (UNCHANGED) ================= -->
<style>
body{
    background:#f4f6f9;
    font-family: "Segoe UI", Arial, sans-serif;
}

#printArea{
    background:#fff;
    padding:20px;
    border-radius:10px;
    box-shadow:0 2px 10px rgba(0,0,0,0.08);
}

h2{
    font-weight:700;
    letter-spacing:1px;
}

h4{
    color:#444;
}

table{
    font-size:13px;
}

table th{
    background:#222 !important;
    color:#fff !important;
    font-size:12px;
    text-transform:uppercase;
}

table td{
    padding:6px !important;
}

table tr td{
    font-weight:500;
}

.btn-dark{
    border-radius:8px;
    padding:8px 15px;
}

.filter-box{
    background:#fff;
    padding:15px;
    border-radius:10px;
    margin-bottom:15px;
    box-shadow:0 1px 6px rgba(0,0,0,0.05);
}
</style>

<div class="container mt-4">
   <!-- ================= CLOSE SHIFT BUTTON ================= -->
<?php if(!$shift_closed): ?>
<form method="POST" action="shift_close.php">
    <button class="btn btn-danger mb-3">
        <i class="fa fa-lock"></i> Close Shift
    </button>
</form>
<?php else: ?>
<button class="btn btn-secondary mb-3" disabled>
    <i class="fa fa-lock"></i> Shift Already Closed
</button>

<div class="alert alert-warning">
    This shift has already been closed. No further changes are allowed.
</div>
<?php endif; ?>

<!-- ================= FILTER ================= -->
<form method="GET" class="filter-box row">

    <div class="col-md-5">
        <label>From Date</label>
        <input type="date" name="date_from" value="<?= $date_from ?>" class="form-control">
    </div>

    <div class="col-md-5">
        <label>To Date</label>
        <input type="date" name="date_to" value="<?= $date_to ?>" class="form-control">
    </div>

    <div class="col-md-2 d-flex align-items-end">
        <button class="btn btn-success w-100">Filter</button>
    </div>

</form>

<div id="printArea">

<!-- ================= HEADER ================= -->
<div class="text-center" style="margin-bottom:10px;">
    
    <img src="assets/logo.png" alt="Logo" style="width:80px; height:auto; margin-bottom:10px;">

    <h2 style="margin:0;">RAMS EXECUTIVE LODGE</h2>
    <h4 style="margin:5px 0;">Front Office Cashier Summary Report</h4>
    <p style="margin:0;">Phone: +265 993 899 382 / +265 883 899 382</p>
</div>

<!-- ================= SHIFT INFO ================= -->
<table class="table table-borderless">
  <tr>
    <td><b>Shift Date:</b> <?= date('d/m/Y', strtotime($date_from)) ?> - <?= date('d/m/Y', strtotime($date_to)) ?></td>
    <td><b>Time Ended:</b> <?= $shift['closed_at'] ?? date('h:i:s A') ?></td>
</tr>
<tr>
    <td><b>Shift No:</b> <?= $shift['shift_no'] ?? 'NOT CLOSED' ?></td>
    <td><b>Cashier:</b> <?= strtoupper($cashier_name) ?></td>
</tr>
</table>

<!-- ================= MAIN TABLE ================= -->
<table class="table table-bordered">
<thead>
<tr>
    <th>Time Paid</th>
    <th>Date</th>
    <th>Paid By</th>
    <th>Receipt No</th>
       <th>RoomNo</th>
    <th>Amount</th>
    <th>Pay Mode</th>
    <th>Cashier</th>
   
</tr>
</thead>

<tbody>
<?php foreach($rows as $r): ?>
<tr>
    <td><?= date('h:i:s A', strtotime($r['payment_date'])) ?></td>
    <td><?= date('d/m/Y', strtotime($r['payment_date'])) ?></td>
    <td><?= $r['paid_by'] ?></td>
   <td><?= $r['invoice_number'] ?? '-' ?></td>
     <td><?= $r['room_number'] ?></td>
    <td><?= number_format($r['amount'],2) ?></td>
    <td><?= strtoupper($r['method']) ?></td>
    <td><?= strtoupper($r['created_by']) ?></td>
   
</tr>
<?php endforeach; ?>
</tbody>
</table>

<!-- ================= SUMMARY ================= -->
<table class="table table-bordered">
<tr>
    <td><b>BANK TRANSFER</b></td>
    <td><?= number_format($total_bank,2) ?></td>
</tr>
<tr>
    <td><b>CASH</b></td>
    <td><?= number_format($total_cash,2) ?></td>
</tr>
<tr>
    <td><b>TOTAL PAYMENTS</b></td>
    <td><b><?= number_format($total_all,2) ?></b></td>
</tr>
<tr>
    <td><b>POTENTIAL CASH TURN IN</b></td>
    <td><?= number_format($potential_cash,2) ?> MK</td>
</tr>
<tr>
    <td><b>ACTUAL CASH TURN IN</b></td>
    <td><?= number_format($actual_cash,2) ?> MK</td>
</tr>
<tr>
    <td><b>OVER / SHORT</b></td>
    <td><?= number_format($over_short,2) ?></td>
</tr>
</table>

<!-- ================= SIGNATURE ================= -->
<h5>FOR OFFICIAL USE ONLY</h5>

<table class="table table-bordered">
<tr>
    <td><b>Cashier Name:</b> <?= strtoupper($cashier_name) ?></td>
    <td>Accounts:<br><br>____________________</td>
    <td>Controller:<br><br>____________________</td>
</tr>
</table>

</div>

<!-- ================= PRINT ================= -->
<button onclick="printReport()" class="btn btn-dark mt-3">
    <i class="fa fa-print"></i> Print Report
</button>

<script>
function printReport(){
    var content = document.getElementById("printArea").innerHTML;
    var w = window.open("", "", "width=1200,height=800");

    w.document.write(`
        <html>
        <head>
        <title>Shift Report</title>
       <style>
    body{
        font-family: Arial;
        padding: 20px;
        font-size: 10px;
    }

    table{
        width: 100%;
        border-collapse: collapse;
    }

    table, th, td{
        border: 1px solid #000;
    }

    th, td{
        padding: 4px;
        text-align: center;
        font-size: 10px;
    }

    h2, h4{
        text-align: center;
        font-size: 12px;
    }
</style>
        </head>
        <body>${content}</body>
        </html>
    `);

    w.document.close();
    w.print();
}
</script>

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