<?= $this->extend('admin/layout/default') ?>

<?= $this->section('css') ?>
<link rel="stylesheet" href="<?= assets_url('admin') ?>/plugins/datatables-buttons/css/buttons.bootstrap4.min.css">
<?= $this->endSection() ?>

<?= $this->section('content') ?>
<section class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-6">
        <h1>Payments Report</h1>
      </div>
      <div class="col-sm-6">
        <ol class="breadcrumb float-sm-right">
          <li class="breadcrumb-item"><a href="<?= url('reports') ?>">Reports</a></li>
          <li class="breadcrumb-item active">Payments</li>
        </ol>
      </div>
    </div>
  </div>
</section>

<section class="content">
  <div class="container-fluid">
    <div class="card">
      <div class="card-body">
        <?= form_open('reports/payments', ['method' => 'GET', 'autocomplete' => 'off']) ?>
          <div class="row">
            <div class="col-sm-2">
              <div class="form-group">
                <label>Date From</label>
                <input type="date" name="date_from" class="form-control" value="<?= esc($dateFrom) ?>">
              </div>
            </div>

            <div class="col-sm-2">
              <div class="form-group">
                <label>Date To</label>
                <input type="date" name="date_to" class="form-control" value="<?= esc($dateTo) ?>">
              </div>
            </div>

            <div class="col-sm-3">
              <div class="form-group">
                <label>Shop</label>
                <select name="shop_id" class="form-control select2">
                  <option value="">All</option>
                  <?php foreach ($shops as $s): ?>
                    <option value="<?= (int) $s->id ?>" <?= ((int) $shopId === (int) $s->id) ? 'selected' : '' ?>><?= esc($s->name) ?></option>
                  <?php endforeach ?>
                </select>
              </div>
            </div>

            <div class="col-sm-2">
              <div class="form-group">
                <label>Status</label>
                <select name="status" class="form-control">
                  <option value="" <?= ($status === '') ? 'selected' : '' ?>>All</option>
                  <option value="SUCCESS" <?= ($status === 'SUCCESS') ? 'selected' : '' ?>>SUCCESS</option>
                  <option value="PAID" <?= ($status === 'PAID') ? 'selected' : '' ?>>PAID</option>
                  <option value="PENDING" <?= ($status === 'PENDING') ? 'selected' : '' ?>>PENDING</option>
                  <option value="FAILED" <?= ($status === 'FAILED') ? 'selected' : '' ?>>FAILED</option>
                </select>
              </div>
            </div>

            <div class="col-sm-3">
              <div class="form-group">
                <label>Mode</label>
                <select name="gateway" class="form-control">
                  <option value="" <?= ($gateway === '') ? 'selected' : '' ?>>All</option>
                  <option value="offline" <?= ($gateway === 'offline') ? 'selected' : '' ?>>Offline</option>
                  <option value="razorpay" <?= ($gateway === 'razorpay') ? 'selected' : '' ?>>Razorpay</option>
                  <option value="phonepe" <?= ($gateway === 'phonepe') ? 'selected' : '' ?>>PhonePe</option>
                </select>
              </div>
            </div>

            <div class="col-sm-9">
              <div class="form-group">
                <label>Search</label>
                <input type="text" name="q" class="form-control" value="<?= esc($q) ?>" placeholder="ID / shop / customer / order id / payment id">
              </div>
            </div>

            <div class="col-sm-3 text-right" style="margin-top: 32px;">
              <a href="<?= url('reports/payments') ?>" class="btn btn-danger">Reset</a>
              <button type="submit" class="btn btn-primary">Filter</button>
            </div>
          </div>
        <?= form_close() ?>
      </div>
    </div>

    <div class="card">
      <div class="card-body table-responsive">
        <table id="reportTable" class="table table-bordered table-striped">
          <thead>
            <tr>
              <th>ID</th>
              <th>Date</th>
              <th>Shop</th>
              <th>Customer</th>
              <th>Amount</th>
              <th>Discount</th>
              <th>Payable</th>
              <th>Status</th>
              <th>Mode</th>
              <th>Order ID</th>
              <th>Payment ID</th>
              <th>Reward Points</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($rows as $r): ?>
              <tr>
                <td><?= (int) $r->id ?></td>
                <td><?= esc($r->created_at) ?></td>
                <td><?= esc($r->shop_name) ?></td>
                <td><?= esc($r->customer_name) ?></td>
                <td>₹<?= number_format((float) $r->amount, 2) ?></td>
                <td>₹<?= number_format((float) $r->discount_amount, 2) ?></td>
                <td>₹<?= number_format((float) $r->payable, 2) ?></td>
                <td><?= esc($r->status) ?></td>
                <td><?= $r->gateway ? ucfirst(esc($r->gateway)) : 'Offline' ?></td>
                <td><?= esc($r->gateway_order_id) ?></td>
                <td><?= esc($r->gateway_payment_id) ?></td>
                <td><?= (int) ($r->reward_points ?? 0) ?></td>
              </tr>
            <?php endforeach ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</section>
<?= $this->endSection() ?>

<?= $this->section('js') ?>
<script src="<?= assets_url('admin') ?>/plugins/datatables-buttons/js/dataTables.buttons.min.js"></script>
<script src="<?= assets_url('admin') ?>/plugins/datatables-buttons/js/buttons.bootstrap4.min.js"></script>
<script src="<?= assets_url('admin') ?>/plugins/jszip/jszip.min.js"></script>
<script src="<?= assets_url('admin') ?>/plugins/pdfmake/pdfmake.min.js"></script>
<script src="<?= assets_url('admin') ?>/plugins/pdfmake/vfs_fonts.js"></script>
<script src="<?= assets_url('admin') ?>/plugins/datatables-buttons/js/buttons.html5.min.js"></script>
<script src="<?= assets_url('admin') ?>/plugins/datatables-buttons/js/buttons.print.min.js"></script>

<script>
  $(function () {
    $('#reportTable').DataTable({
      responsive: true,
      autoWidth: false,
      order: [[0, 'desc']],
      dom: 'Bfrtip',
      buttons: [
        { extend: 'csvHtml5', title: 'Payments Report' },
        { extend: 'excelHtml5', title: 'Payments Report' },
        { extend: 'pdfHtml5', title: 'Payments Report' }
      ]
    });

    $('.select2').select2();
  });
</script>
<?= $this->endSection() ?>

