<?php
require_once __DIR__ . '/vendor/autoload.php';
$page_title = 'Report Scan Data';
require_once('includes/load.php');
$user = current_user();

// Check user level (uncomment if needed)
// page_require_level(1);

// Default Date Range
$startDate = isset($_GET['startDate']) ? $_GET['startDate'] : date('Y-m-d'); // Default to today's date
$endDate = isset($_GET['endDate']) ? $_GET['endDate'] : date('Y-m-d'); // Default to today's date
$shopId = isset($_GET['shop']) ? $_GET['shop'] : '';

// Build the SQL Query
$sql = "
    SELECT 
        nama_toko, 
        COUNT(DISTINCT invoice) AS total,
        COUNT(DISTINCT CASE WHEN scan_in IS NULL AND scan_status IS NULL THEN invoice END) AS scan_all,
        COUNT(DISTINCT CASE WHEN scan_in IS NULL THEN invoice END) AS scan_in,
        COUNT(DISTINCT CASE WHEN scan_status IS NULL THEN invoice END) AS scan_out
    FROM awb
    WHERE date_scan BETWEEN '$startDate' AND '$endDate'
    GROUP BY nama_toko";

// Optional filter for shop selection
if (!empty($shopId) && $shopId != 'all') {
  $sql .= " AND nama_toko like '%" . $db->escape($shopId) . "'";
}

// Execute the query
$data = $db->query($sql);

// Shop options
$shops = $db->query("SELECT DISTINCT nama_toko FROM awb WHERE date_scan BETWEEN '$startDate' AND '$endDate'");

?>
<?php include_once('layouts/header.php'); ?>

<div class="row">
  <div class="col-md-12">
    <?php echo display_msg($msg); ?>
  </div>
</div>
<div class="row">
  <div class="col-md-12">
    <div class="panel panel-default">
      <div class="panel-heading clearfix">
        <strong>
          <span class="glyphicon glyphicon-th"></span>
          <span>Scan Data per Shop</span>
        </strong>
      </div>
      <div class="panel-body">
        <div id="filter" style="margin-bottom: 10px;">
          <form method="get" action="" class="mb-4">
            <div class="form-row">
              <div class="range-date" style="border: 1px;">
                <div class="form-group col-md-4">
                  <label for="startDate">Start Date</label>
                  <input type="date" name="startDate" class="form-control" id="startDate"
                    value="<?= htmlspecialchars($startDate) ?>">
                </div>
                <div class="form-group col-md-4">
                  <label for="endDate">End Date</label>
                  <input type="date" name="endDate" class="form-control" id="endDate"
                    value="<?= htmlspecialchars($endDate) ?>">
                </div>
              </div>

              <div class="form-group col-md-4">
                <label for="shop">Shop</label>
                <select id="shop" name="shop" class="form-control">
                  <option value="">Choose...</option>
                  <option value="all" <?= $shopId === 'all' ? 'selected' : '' ?>>All</option>
                  <?php while ($row = $shops->fetch_assoc()): ?>
                    <option value="<?= htmlspecialchars($row['nama_toko']) ?>"
                      <?= $row['nama_toko'] === $shopId ? 'selected' : '' ?>>
                      <?= htmlspecialchars($row['nama_toko']) ?>
                    </option>
                  <?php endwhile; ?>
                </select>
              </div>
            </div>
            <div class="col-md-12">
              <button type="submit" class="btn btn-primary" style="margin-bottom: 15px;">Filter</button>
              <button type="button" class="btn btn-secondary" style="margin-bottom: 15px;"
                onclick="window.location.href = window.location.pathname;">Refresh</button>
            </div>
          </form>
        </div>

        <table id="datatable" class="table table-bordered">
          <thead>
            <tr>
              <th>#</th>
              <th>Nama Toko</th>
              <th>Not Scan In - Out</th>
              <th>Not Scan In</th>
              <th>Not Scan Out</th>
            </tr>
          </thead>
          <tbody>
            <?php if ($data->num_rows > 0): ?>
              <?php $no = 1; ?>
              <?php while ($row = $data->fetch_assoc()): ?>
                <tr>
                  <td><?= $no++ ?></td>
                  <td><?= htmlspecialchars($row['nama_toko']) ?></td>
                  <td><?= htmlspecialchars($row['scan_all']) ?></td>
                  <td><?= htmlspecialchars($row['scan_in']) ?></td>
                  <td><?= htmlspecialchars($row['scan_out']) ?></td>
                </tr>
              <?php endwhile; ?>
            <?php else: ?>
              <tr>
                <td colspan="5">No data found</td>
              </tr>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<script>
  $.noConflict();
  jQuery(document).ready(function($) {
    // Initialize DataTable
    $('#datatable').DataTable({
      "paging": true,
      "searching": true,
      "info": true
    });
  });
</script>

<?php include_once('layouts/footer.php'); ?>