<?php
$page_title = 'Reject Transfer History';
require_once('includes/load.php');
page_require_level(10);

// Set default to current month
$current_month = date('m');
$current_year = date('Y');
$selected_month = isset($_GET['month']) ? (int)$_GET['month'] : $current_month;
$selected_year = isset($_GET['year']) ? (int)$_GET['year'] : $current_year;

// Validate month/year
if($selected_month < 1 || $selected_month > 12) $selected_month = $current_month;
if($selected_year < 2000 || $selected_year > date('Y')) $selected_year = $current_year;

// Build query
$query = "SELECT * FROM reject_transfer_area 
          WHERE MONTH(date) = {$selected_month} 
          AND YEAR(date) = {$selected_year}
          ORDER BY date DESC";

$transfers = $db->query($query)->fetch_all(MYSQLI_ASSOC);
?>

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

<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>Reject Transfer History - <?php echo date('F Y', mktime(0, 0, 0, $selected_month, 1, $selected_year)); ?></span>
        </strong>
        <div class="pull-right">
          <form class="form-inline" method="get" action="">
            <div class="form-group">
              <select class="form-control input-sm" name="month">
                <?php for($m=1; $m<=12; $m++): ?>
                  <option value="<?php echo $m; ?>" <?php echo $m == $selected_month ? 'selected' : ''; ?>>
                    <?php echo date('F', mktime(0, 0, 0, $m, 1)); ?>
                  </option>
                <?php endfor; ?>
              </select>
            </div>
            <div class="form-group">
              <select class="form-control input-sm" name="year">
                <?php for($y=date('Y'); $y>=2020; $y--): ?>
                  <option value="<?php echo $y; ?>" <?php echo $y == $selected_year ? 'selected' : ''; ?>>
                    <?php echo $y; ?>
                  </option>
                <?php endfor; ?>
              </select>
            </div>
            <button type="submit" class="btn btn-primary btn-sm">Filter</button>
            <a href="transfer_reject.php" class="btn btn-success btn-sm">New Transfer</a>
          </form>
        </div>
      </div>
      <div class="panel-body">
        <table class="table table-bordered table-striped table-hover">
          <thead>
            <tr>
              <th class="text-center" style="width: 50px;">#</th>
              <th class="text-center">Part Number</th>
              <th class="text-center">Quantity</th>
              <th>Notes</th>
              <th class="text-center">Date & Time</th>
            </tr>
          </thead>
          <tbody>
            <?php if(!empty($transfers)): ?>
              <?php foreach ($transfers as $transfer): ?>
              <tr>
                <td class="text-center"><?php echo $transfer['id']; ?></td>
                <td class="text-center"><?php echo remove_junk($transfer['part_number']); ?></td>
                <td class="text-center"><?php echo (int)$transfer['quantity']; ?></td>
                <td><?php echo !empty($transfer['notes']) ? remove_junk($transfer['notes']) : '<span class="text-muted">No notes</span>'; ?></td>
                <td class="text-center"><?php echo date('d-m-Y H:i:s', strtotime($transfer['date'])); ?></td>
              </tr>
              <?php endforeach; ?>
            <?php else: ?>
              <tr>
                <td colspan="5" class="text-center">No records found for selected month</td>
              </tr>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
      <div class="panel-footer">
        <div class="row">
          <div class="col-md-6">
            <span class="panel-title">Total Records: <?php echo count($transfers); ?></span>
          </div>
          <div class="col-md-6 text-right">
            <button onclick="window.print()" class="btn btn-primary btn-sm">Print</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

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