<?php
session_start();

require_once __DIR__ . "/db.php";
require_once __DIR__ . "/../header.php";

$salon_id  = (int)($_SESSION['salon_id']  ?? 1);
$branch_id = (int)($_SESSION['branch_id'] ?? 1);
$user_id   = (int)($_SESSION['user_id']   ?? 0);
$role      = $_SESSION['role'] ?? 'staff';

if(!$user_id){
    header("Location: auth_login.php");
    exit;
}

if($role !== 'admin'){
    echo "<div class='alert alert-danger m-3'>Admin only.</div>";
    require_once __DIR__ . "/../footer.php";
    exit;
}

/* use existing esc() from db.php if already defined */
if(!function_exists('esc')){
    function esc($v){
        return htmlspecialchars($v ?? '', ENT_QUOTES, 'UTF-8');
    }
}

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

$fromE = mysqli_real_escape_string($conn, $from);
$toE   = mysqli_real_escape_string($conn, $to);

$inv = mysqli_query($conn, "
    SELECT i.invoice_id, i.bill_date, i.subtotal, i.discount, i.grand_total, i.payment_method,
           c.customer_name, c.phone
    FROM invoices i
    LEFT JOIN customers c ON c.customer_id=i.customer_id
    WHERE i.salon_id=$salon_id AND i.branch_id=$branch_id
      AND DATE(i.bill_date) BETWEEN '$fromE' AND '$toE'
    ORDER BY i.invoice_id DESC
    LIMIT 300
");
?>
<div class="container-fluid p-3">
  <h5 class="mb-2">Sale History (Reprint Receipts)</h5>

  <form method="get" class="row g-2 mb-2">
    <div class="col-md-3">
      <label class="form-label">From</label>
      <input type="date" name="from" value="<?= esc($from) ?>" class="form-control form-control-sm">
    </div>
    <div class="col-md-3">
      <label class="form-label">To</label>
      <input type="date" name="to" value="<?= esc($to) ?>" class="form-control form-control-sm">
    </div>
    <div class="col-md-2 d-flex align-items-end">
      <button class="btn btn-sm btn-dark w-100">Filter</button>
    </div>
  </form>

  <table class="table table-bordered table-sm">
    <thead>
      <tr>
        <th>Invoice#</th>
        <th>Date</th>
        <th>Customer</th>
        <th>Phone</th>
        <th>Method</th>
        <th>Total</th>
        <th width="120">Action</th>
      </tr>
    </thead>
    <tbody>
      <?php if($inv && mysqli_num_rows($inv)>0): ?>
        <?php while($r=mysqli_fetch_assoc($inv)): ?>
          <tr>
            <td><?= (int)$r['invoice_id'] ?></td>
            <td><?= esc($r['bill_date']) ?></td>
            <td><?= esc($r['customer_name'] ?? '-') ?></td>
            <td><?= esc($r['phone'] ?? '-') ?></td>
            <td><?= esc($r['payment_method'] ?? '-') ?></td>
            <td><?= number_format((float)$r['grand_total'],2) ?></td>
            <td>
              <!-- bill_print.php is most likely in ROOT (one level up from /restyle/) -->
              <a class="btn btn-sm btn-outline-dark"
                 href="../bill_print.php?invoice_id=<?= (int)$r['invoice_id'] ?>">
                 Reprint
              </a>
            </td>
          </tr>
        <?php endwhile; ?>
      <?php else: ?>
        <tr><td colspan="7" class="text-muted">No sales found.</td></tr>
      <?php endif; ?>
    </tbody>
  </table>

  <a class="btn btn-outline-secondary btn-sm" href="admin_inprogress.php">Back</a>
</div>

<?php require_once __DIR__ . "/../footer.php"; ?>