<?php $page_title = 'Detail Tagihan'; ?>
<?php $this->load->view('kasir/_nav', compact('page_title')); ?>

<div class="row">
  <div class="col-md-7">
    <div class="card">
      <div class="card-header">Informasi Tagihan</div>
      <div class="card-body">
        <div class="row">
          <div class="col-md-6">
            <p><strong>No. Tagihan:</strong> <?= html_escape($tagihan['no_tagihan']); ?></p>
            <p><strong>No. RM:</strong> <?= html_escape($tagihan['rme_no']); ?></p>
            <p><strong>Pasien:</strong> <?= html_escape($tagihan['nama_pasien']); ?></p>
            <p><strong>Poli:</strong> <?= html_escape($tagihan['nama_poli']); ?></p>
          </div>
          <div class="col-md-6">
            <p><strong>Dokter:</strong> <?= html_escape($tagihan['dokter']); ?></p>
            <p><strong>Tanggal:</strong> <?= html_escape($tagihan['tanggal_tagihan']); ?></p>
            <p><strong>Tipe Bayar:</strong> <?= html_escape($tagihan['tipe_pembayaran']); ?></p>
            <p><strong>Status:</strong>
              <span class="badge badge-<?= $tagihan['status_tagihan'] === 'LUNAS' ? 'success' : 'warning'; ?>">
                <?= html_escape($tagihan['status_tagihan']); ?>
              </span>
            </p>
          </div>
        </div>
      </div>
    </div>

    <div class="card">
      <div class="card-header">Rincian Billing</div>
      <div class="card-body table-responsive">
        <table class="table table-bordered">
          <thead>
            <tr>
              <th>Kategori</th>
              <th>Deskripsi</th>
              <th>Qty</th>
              <th>Harga</th>
              <th>Total</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($tagihan['items'] as $item): ?>
              <tr>
                <td><?= html_escape($item['kategori']); ?></td>
                <td><?= html_escape($item['deskripsi']); ?></td>
                <td><?= (float) $item['qty']; ?></td>
                <td>Rp <?= number_format((float) $item['harga'], 0, ',', '.'); ?></td>
                <td>Rp <?= number_format((float) $item['total'], 0, ',', '.'); ?></td>
              </tr>
            <?php endforeach; ?>
          </tbody>
          <tfoot>
            <tr>
              <th colspan="4" class="text-right">Subtotal</th>
              <th>Rp <?= number_format((float) $tagihan['subtotal'], 0, ',', '.'); ?></th>
            </tr>
            <tr>
              <th colspan="4" class="text-right">Diskon</th>
              <th>Rp <?= number_format((float) $tagihan['diskon'], 0, ',', '.'); ?></th>
            </tr>
            <tr class="table-success">
              <th colspan="4" class="text-right">Grand Total</th>
              <th>Rp <?= number_format((float) $tagihan['grand_total'], 0, ',', '.'); ?></th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>

  <div class="col-md-5">
    <?php if ($tagihan['status_tagihan'] === 'LUNAS'): ?>
      <div class="card card-success">
        <div class="card-header">Status Pembayaran</div>
        <div class="card-body">
          <p><strong>Pembayaran sudah lunas.</strong></p>
          <p><strong>Metode:</strong> <?= html_escape($tagihan['metode_bayar']); ?></p>
          <p><strong>Jumlah Bayar:</strong> Rp <?= number_format((float) $tagihan['jumlah_bayar'], 0, ',', '.'); ?></p>
          <p><strong>Kembalian:</strong> Rp <?= number_format((float) ($tagihan['kembalian'] ?? 0), 0, ',', '.'); ?></p>
          <p><strong>Kasir:</strong> <?= html_escape($tagihan['kasir_nama'] ?? '-'); ?></p>
          <p><strong>Waktu Bayar:</strong> <?= !empty($tagihan['waktu_bayar']) ? date('d/m/Y H:i', strtotime($tagihan['waktu_bayar'])) : '-'; ?></p>
          <a href="<?= site_url('kasir/selesai/' . $tagihan['tagihan_id']); ?>" class="btn btn-success btn-block">Lihat Bukti Dummy</a>
        </div>
      </div>
    <?php else: ?>
      <div class="card">
        <div class="card-header">Form Pembayaran Dummy</div>
        <div class="card-body">
          <form method="post" action="<?= site_url('kasir/bayar/' . $tagihan['tagihan_id']); ?>">
            <div class="form-group">
              <label>Total Tagihan</label>
              <input type="text" class="form-control" value="Rp <?= number_format((float) $tagihan['grand_total'], 0, ',', '.'); ?>" readonly>
            </div>
            <div class="form-group">
              <label>Metode Bayar</label>
              <select name="metode_bayar" class="form-control" required>
                <?php foreach ($metode_bayar as $value => $label): ?>
                  <option value="<?= $value; ?>"><?= $label; ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="form-group">
              <label>Jumlah Bayar</label>
              <input type="number" min="<?= (float) $tagihan['grand_total']; ?>" step="0.01" name="jumlah_bayar" class="form-control" value="<?= (float) $tagihan['grand_total']; ?>" required>
            </div>
            <div class="form-group">
              <label>No. Referensi</label>
              <input type="text" name="nomor_referensi" class="form-control" placeholder="Opsional untuk QRIS / transfer / debit">
            </div>
            <button class="btn btn-success btn-block">
              <i class="fas fa-check-circle"></i> Bayar dan Lunaskan
            </button>
          </form>
        </div>
      </div>
    <?php endif; ?>
  </div>
</div>
