<!-- Begin Page Content -->
<div class="container-fluid">
  <div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800"><?= $title; ?></h1>
    <ol class="breadcrumb">
      <li class="breadcrumb-item"><a href="./">Home</a></li>
      <li class="breadcrumb-item"><a href="<?= base_url('journal'); ?>">Journal</a></li>
      <li class="breadcrumb-item active" aria-current="page"><?= $title; ?></li>
    </ol>
  </div>

  <div class="card border-bottom-primary shadow mb-4">
    <div class="card-header py-3">
      <div class="row">
        <div class="col-sm">
          <h6 class="m-0 font-weight-bold text-primary mt-2"><?= $title; ?></h6>
        </div>
      </div>
    </div>

    <div class="card-body">

      <?php
      // =========================
      // SAFE GETTER
      // =========================
      if (!function_exists('_vj')) {
        function _vj($arr, $key, $default = '')
        {
          return (is_array($arr) && array_key_exists($key, $arr) && $arr[$key] !== null) ? $arr[$key] : $default;
        }
      }

      // =========================
      // SAFE HEADER ROW (ambil baris pertama yang ada)
      // =========================
      $head = [];
      if (isset($data) && is_array($data)) {
        if (isset($data[0]) && is_array($data[0])) $head = $data[0];
        else {
          $first = reset($data);
          $head = is_array($first) ? $first : [];
        }
      } elseif (is_object($data)) {
        $head = (array)$data;
      }

      $tgl_raw = (string)_vj($head, 'tgl', '');
      $tgl_val = $tgl_raw ? date('Y-m-d', strtotime($tgl_raw)) : '';
      $bukti   = trim((string)_vj($head, 'bukti', ''));
      ?>

      <!-- HEADER DETAIL -->
      <div class="row">
        <div class="col-lg-12">

          <input type="hidden" id="id" name="id" value="<?= htmlspecialchars((string)_vj($head, 'id_jurnal', '')); ?>">

          <div class="form-group row">
            <label for="tglTransaksi" class="col-sm-3 col-form-label">Tanggal Transaksi</label>
            <div class="col-sm-9">
              <input type="date" class="form-control" id="tglTransaksi" name="tglTransaksi"
                     value="<?= htmlspecialchars($tgl_val); ?>" required readonly>
            </div>
          </div>

          <div class="form-group row">
            <label for="noTransaksi" class="col-sm-3 col-form-label">Nomor Transaksi</label>
            <div class="col-sm-9">
              <input type="text" class="form-control" id="noTransaksi" name="noTransaksi"
                     value="<?= htmlspecialchars((string)_vj($head, 'no_trans', '')); ?>" readonly>
            </div>
          </div>

          <div class="form-group row">
            <label for="keterangan" class="col-sm-3 col-form-label">Keterangan</label>
            <div class="col-sm-9">
              <input type="text" class="form-control" id="keterangan" name="keterangan"
                     value="<?= htmlspecialchars((string)_vj($head, 'keterangan', '')); ?>" autocomplete="off" readonly>
            </div>
          </div>

          <div class="form-group row">
            <label class="col-sm-3 col-form-label">
              Upload Bukti (Opsional) <br>
              <small class="text-primary">*format : pdf/jpg/jpeg/png. maksimal: 5mb</small>
            </label>
            <div class="col-sm-9">
              <?php if ($bukti !== ''): ?>
                <label>File :
                  <a href="<?= base_url('uploads/lampiran/' . $bukti); ?>" target="__BLANK"><?= htmlspecialchars($bukti); ?></a>
                </label>
              <?php else: ?>
                <label class="text-muted">File : -</label>
              <?php endif; ?>
            </div>
          </div>

        </div>
      </div>

      <!-- DETAIL JURNAL -->
      <div class="row">
        <div class="col-12">
          <div class="table-responsive">
            <table class="table table-hover table-sm table-bordered">
              <thead>
                <tr>
                  <th scope="col">Nomor Akun</th>
                  <th scope="col">Nama Akun</th>
                  <th scope="col">Nominal</th>
                  <th scope="col">Perkiraan</th>
                </tr>
              </thead>
              <tbody id="insert-form">
                <?php if (!empty($data) && is_array($data)): ?>
                  <?php foreach ($data as $da): ?>
                    <?php
                      // safe detail keys (biar transaksi lain gak blank/error)
                      $da_id_akun     = _vj($da, 'id_akun', '');
                      $da_nama        = _vj($da, 'nama', '');
                      $da_nilai       = (float)_vj($da, 'nilai', 0);
                      $da_id_perkiraan= _vj($da, 'id_perkiraan', '');
                    ?>
                    <tr>
                      <td>
                        <select class="form-control bootstrap-select" name="idAkun[]" title="Nomor Akun"
                                data-container="body" data-live-search="true" disabled>
                          <?php foreach ($akun as $ak): ?>
                            <?php $selected = ((string)_vj($ak,'id_akun','') === (string)$da_id_akun) ? 'selected' : ''; ?>
                            <option value="<?= htmlspecialchars((string)_vj($ak,'id_akun','')); ?>" <?= $selected; ?>>
                              <?= htmlspecialchars((string)_vj($ak,'kel_akun','')); ?>-<?= htmlspecialchars((string)_vj($ak,'noakun','')); ?>
                            </option>
                          <?php endforeach; ?>
                        </select>
                      </td>

                      <td>
                        <select class="form-control bootstrap-select" name="namaAkun[]" title="Pilih Nama Akun"
                                data-container="body" data-live-search="true" disabled>
                          <?php foreach ($akun as $ak): ?>
                            <?php $selected = ((string)_vj($ak,'nama','') === (string)$da_nama) ? 'selected' : ''; ?>
                            <option value="<?= htmlspecialchars((string)_vj($ak,'nama','')); ?>" <?= $selected; ?>>
                              <?= htmlspecialchars((string)_vj($ak,'nama','')); ?>
                            </option>
                          <?php endforeach; ?>
                        </select>
                      </td>

                      <td>
                        <input type="text" class="form-control" name="nominal[]"
                               value="<?= 'Rp. ' . number_format($da_nilai, 0, ',', '.'); ?>" readonly>
                      </td>

                      <td>
                        <select class="form-control bootstrap-select" name="perkiraan[]" title="Select perkiraan"
                                data-container="body" data-width="100%" disabled>
                          <?php foreach ($perkiraan as $p): ?>
                            <?php $selected = ((string)_vj($p,'id_perkiraan','') === (string)$da_id_perkiraan) ? 'selected' : ''; ?>
                            <option value="<?= htmlspecialchars((string)_vj($p,'id_perkiraan','')); ?>" <?= $selected; ?>>
                              <?= htmlspecialchars((string)_vj($p,'nama','')); ?>
                            </option>
                          <?php endforeach; ?>
                        </select>
                      </td>
                    </tr>
                  <?php endforeach; ?>
                <?php endif; ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>

      <hr class="mb-3 mt-3">

      <div class="row">
        <div class="col-sm text-right">
          <a href="<?= base_url('journal'); ?>" class="btn btn-danger btn-sm">
            <i class="fa fa-fw fa-arrow-left"></i> Kembali
          </a>
        </div>
      </div>

    </div>
  </div>
</div>
<!-- /.container-fluid -->
