<div class="d-flex gap-2 mb-3">
  <a class="btn btn-sm btn-light" href="<?= url('/persetujuan') ?>"><i class="bi bi-arrow-left me-1"></i>Kembali</a>
</div>
<div class="row g-3">
  <div class="col-lg-5">
    <div class="card mb-3"><div class="card-header"><i class="bi bi-clipboard-check me-1"></i>Permintaan</div>
      <div class="card-body"><dl class="dl-grid">
        <dt>Nomor</dt><dd class="fw-bold"><?= e($r['request_no']) ?></dd>
        <dt>Jenis</dt><dd><?= e($r['flow_name'] ?: $r['module'].' / '.$r['action']) ?></dd>
        <dt>Judul</dt><dd><?= e($r['title']) ?></dd>
        <dt>Nilai</dt><dd class="fw-bold"><?= rph($r['amount']) ?></dd>
        <dt>Entitas</dt><dd><?= e($r['entity_type']) ?> #<?= (int)$r['entity_id'] ?></dd>
        <dt>Diajukan Oleh</dt><dd><?= e($r['requested_by_name'] ?: '-') ?> · <?= tglWaktu($r['created_at']) ?></dd>
        <dt>Tingkat</dt><dd><?= (int)$r['current_level'] ?> dari <?= (int)$r['total_level'] ?></dd>
        <dt>Status</dt><dd><?= badge(['pending'=>'Menunggu','approved'=>'Disetujui','rejected'=>'Ditolak','cancelled'=>'Dibatalkan'][$r['status']] ?? $r['status'],
            ['pending'=>'warning','approved'=>'success','rejected'=>'danger'][$r['status']] ?? 'secondary') ?></dd>
        <?php if ($r['decided_by_name']): ?><dt>Diputuskan Oleh</dt><dd><?= e($r['decided_by_name']) ?> · <?= tglWaktu($r['decided_at']) ?></dd><?php endif; ?>
      </dl>
      <?php if ($r['notes']): ?><hr><div class="small"><strong>Catatan keputusan:</strong><br><?= nlbr($r['notes']) ?></div><?php endif; ?>
      </div></div>

    <?php if ($payload): ?>
    <div class="card mb-3"><div class="card-header"><i class="bi bi-braces me-1"></i>Data Permintaan</div>
      <div class="table-wrap"><table class="table table-sm mb-0"><tbody>
        <?php foreach ($payload as $k => $v): ?>
          <tr><td class="text-body-secondary" style="width:40%"><?= e(str_replace('_',' ',$k)) ?></td>
            <td><?= e(is_array($v) ? json_encode($v, JSON_UNESCAPED_UNICODE) : (string)$v) ?></td></tr>
        <?php endforeach; ?>
      </tbody></table></div></div>
    <?php endif; ?>

    <?php if ($r['status'] === 'pending' && can('persetujuan','approve')): ?>
    <div class="card"><div class="card-header"><i class="bi bi-pencil-square me-1"></i>Keputusan</div>
      <form method="post" action="<?= url('/persetujuan/'.(int)$r['id'].'/putuskan') ?>" class="card-body">
        <?= csrf() ?>
        <div class="mb-3"><label class="form-label" for="catatan">Catatan Keputusan</label>
          <textarea class="form-control" id="catatan" name="catatan" rows="3" maxlength="1000"
                    placeholder="Wajib diisi bila menolak permintaan"></textarea></div>
        <div class="d-flex gap-2">
          <button class="btn btn-success flex-fill" name="keputusan" value="approved" type="submit"
                  data-konfirmasi="Setujui permintaan ini? Tindakan terkait akan langsung dijalankan sistem." data-konfirmasi-warna="success">
            <i class="bi bi-check-lg me-1"></i>Setujui</button>
          <button class="btn btn-danger flex-fill" name="keputusan" value="rejected" type="submit"
                  data-konfirmasi="Tolak permintaan ini?"><i class="bi bi-x-lg me-1"></i>Tolak</button>
        </div>
      </form>
    </div>
    <?php endif; ?>
  </div>

  <div class="col-lg-7">
    <?php if ($entity): ?>
    <div class="card mb-3"><div class="card-header"><i class="bi bi-file-earmark me-1"></i>Objek Terkait</div>
      <div class="table-wrap"><table class="table table-sm mb-0"><tbody>
        <?php
        $skip = ['payload_json','mapping_json','options_json','error_summary','dq_flags','created_at','updated_at','deleted_at'];
        foreach ($entity as $k => $v):
          if (in_array($k, $skip, true) || $v === null || $v === '') continue;
          $isMoney = preg_match('/(amount|limit)$/', $k); ?>
          <tr><td class="text-body-secondary" style="width:38%"><?= e(str_replace('_',' ',$k)) ?></td>
            <td class="<?= $isMoney?'num':'' ?>"><?= $isMoney ? rph($v) : e(truncate((string)$v,70)) ?></td></tr>
        <?php endforeach; ?>
      </tbody></table></div></div>
    <?php endif; ?>

    <div class="card"><div class="card-header"><i class="bi bi-clock-history me-1"></i>Riwayat Persetujuan</div>
      <div class="card-body"><div class="timeline">
        <?php foreach ($history as $h): ?>
          <div class="timeline-item <?= $h['decision']==='approved'?'success':($h['decision']==='rejected'?'danger':'') ?>">
            <div class="timeline-time"><?= tglWaktu($h['created_at']) ?> · Tingkat <?= (int)$h['level'] ?></div>
            <div class="small"><strong><?= e(['submitted'=>'Diajukan','approved'=>'Disetujui','rejected'=>'Ditolak','cancelled'=>'Dibatalkan','commented'=>'Komentar'][$h['decision']] ?? $h['decision']) ?></strong>
              — <?= e($h['actor_name'] ?: 'Sistem') ?></div>
            <?php if ($h['notes']): ?><div class="small text-body-secondary"><?= nlbr($h['notes']) ?></div><?php endif; ?>
          </div>
        <?php endforeach; ?>
      </div></div></div>
  </div>
</div>
