<?php
include '../koneksi.php';
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) { echo '<p class="text-danger">ID tidak valid.</p>'; return; }

$q = mysqli_query($koneksi, "SELECT * FROM laporan_kasir WHERE id = $id");
$d = mysqli_fetch_assoc($q);
if (!$d) { echo '<p class="text-danger">Data tidak ditemukan.</p>'; return; }

$tgl = $d['tanggal'];
$tgl_fmt = preg_match('/^\d{4}-\d{2}-\d{2}$/', $tgl) ? date('d/m/Y', strtotime($tgl)) : $tgl;
$nom = (float)($d['nominal'] ?? 0);
$nom_fmt = $nom > 0 ? 'Rp. ' . number_format($nom, 0, '', '.') : '';
?>
<div class="modal-header">
  <h4 class="modal-title">Edit Pembayaran Tunai</h4>
  <button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<form action="pembayaran_tunai_update.php" method="post">
  <div class="modal-body">
    <input type="hidden" name="id" value="<?php echo $id; ?>">
    <div class="form-group">
      <label>Tanggal Laporan</label>
      <input type="text" name="tanggal" class="form-control datepicker2" required value="<?php echo htmlspecialchars($tgl_fmt); ?>" placeholder="dd/mm/yyyy" autocomplete="off">
    </div>
    <div class="form-group">
      <label>Shift</label>
      <select name="shift" class="form-control" required>
        <option value="Pagi" <?php echo $d['shift']==='Pagi'?'selected':''; ?>>Pagi</option>
        <option value="Siang" <?php echo $d['shift']==='Siang'?'selected':''; ?>>Siang</option>
        <option value="Malam" <?php echo $d['shift']==='Malam'?'selected':''; ?>>Malam</option>
      </select>
    </div>
    <div class="form-group">
      <label>Jumlah Uang (Rp)</label>
      <input type="text" name="nominal" class="form-control input-rupiah" required value="<?php echo htmlspecialchars($nom_fmt); ?>" placeholder="0" autocomplete="off">
    </div>
    <div class="form-group">
      <label>Keterangan <small class="text-muted">(opsional)</small></label>
      <textarea name="keterangan" class="form-control" rows="2"><?php echo htmlspecialchars($d['keterangan'] ?? ''); ?></textarea>
    </div>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Batal</button>
    <button type="submit" class="btn btn-primary">Simpan</button>
  </div>
</form>
