<div class="row g-3">
  <div class="col-lg-3">
    <div class="card mb-3"><div class="card-header"><i class="bi bi-gear me-1"></i>Kelompok Pengaturan</div>
      <div class="list-group list-group-flush">
        <?php foreach ($groups as $g => $grp): ?>
          <a class="list-group-item list-group-item-action d-flex justify-content-between align-items-center <?= $aktif===$g?'active':'' ?>"
             href="<?= url('/pengaturan',['grup'=>$g]) ?>">
            <span><i class="bi bi-<?= e($grp['icon']) ?> me-2"></i><?= e($grp['label']) ?></span>
            <span class="badge bg-<?= $aktif===$g?'light text-dark':'secondary' ?>"><?= count($grp['items']) ?></span></a>
        <?php endforeach; ?>
        <a class="list-group-item list-group-item-action" href="<?= url('/pengaturan/aging') ?>">
          <i class="bi bi-hourglass-split me-2"></i>Konfigurasi Bucket Aging</a>
        <a class="list-group-item list-group-item-action" href="<?= url('/acp/target') ?>">
          <i class="bi bi-bullseye me-2"></i>Target ACP</a>
      </div>
    </div>
    <div class="card"><div class="card-header"><i class="bi bi-server me-1"></i>Informasi Sistem</div>
      <div class="card-body small"><dl class="dl-grid" style="grid-template-columns:minmax(90px,120px) 1fr;font-size:12px">
        <dt>PHP</dt><dd><?= e($info['php']) ?></dd>
        <dt>Basis Data</dt><dd><?= e($info['db']) ?></dd>
        <dt>Ekstensi ZIP</dt><dd><?= e($info['zip']) ?></dd>
        <dt>Composer</dt><dd><?= e($info['composer']) ?></dd>
        <dt>PhpSpreadsheet</dt><dd><?= e($info['phpspreadsheet']) ?></dd>
        <dt>Dompdf</dt><dd><?= e($info['dompdf']) ?></dd>
        <dt>Maks Unggah</dt><dd><?= e($info['upload_max']) ?></dd>
        <dt>Batas Memori</dt><dd><?= e($info['memory']) ?></dd>
        <dt>Zona Waktu</dt><dd><?= e($info['timezone']) ?></dd>
        <dt>Waktu Server</dt><dd><?= e(tglWaktu($info['waktu_server'])) ?></dd>
      </dl></div></div>
  </div>

  <div class="col-lg-9">
    <?php $info2 = $groups[$aktif] ?? null; if ($info2): ?>
    <form method="post" action="<?= url('/pengaturan/simpan') ?>" data-loading>
      <?= csrf() ?>
      <input type="hidden" name="grup" value="<?= attr($aktif) ?>">
      <div class="card">
        <div class="card-header"><i class="bi bi-<?= e($info2['icon']) ?> me-1"></i><?= e($info2['label']) ?>
          <div class="card-actions"><?php if (can('pengaturan','edit')): ?>
            <button class="btn btn-sm btn-primary" type="submit"><i class="bi bi-save me-1"></i>Simpan Pengaturan</button><?php endif; ?></div></div>
        <div class="card-body">
          <?php foreach ($info2['items'] as $it):
            $key = $it['setting_key']; $name = 's_' . str_replace('.', '__', $key);
            $val = setting($key); $opts = arr($it['options_json']); ?>
            <div class="row g-2 align-items-start py-2 border-bottom">
              <div class="col-md-5">
                <label class="form-label mb-0" for="<?= attr($name) ?>"><?= e($it['label']) ?></label>
                <?php if ($it['description']): ?><div class="form-text mt-0"><?= e($it['description']) ?></div><?php endif; ?>
                <div class="text-body-tertiary" style="font-size:10.5px"><code><?= e($key) ?></code></div>
              </div>
              <div class="col-md-7">
                <?php $dis = (int)$it['is_editable'] === 0 || !can('pengaturan','edit') ? 'disabled' : ''; ?>
                <?php if ($it['value_type'] === 'bool'): ?>
                  <div class="form-check form-switch">
                    <input class="form-check-input" type="checkbox" id="<?= attr($name) ?>" name="<?= attr($name) ?>" value="1" <?= $val ? 'checked':'' ?> <?= $dis ?>>
                    <label class="form-check-label small" for="<?= attr($name) ?>"><?= $val ? 'Aktif' : 'Tidak aktif' ?></label></div>
                <?php elseif ($it['value_type'] === 'select' && $opts): ?>
                  <select class="form-select form-select-sm" id="<?= attr($name) ?>" name="<?= attr($name) ?>" <?= $dis ?>>
                    <?php foreach ($opts as $ok => $ov): ?>
                      <option value="<?= attr($ok) ?>" <?= (string)$val === (string)$ok ? 'selected':'' ?>><?= e($ov) ?></option>
                    <?php endforeach; ?></select>
                <?php elseif (in_array($it['value_type'], ['int','decimal'], true)): ?>
                  <input type="number" step="<?= $it['value_type']==='decimal'?'0.01':'1' ?>" class="form-control form-control-sm"
                         id="<?= attr($name) ?>" name="<?= attr($name) ?>" value="<?= attr($val) ?>" <?= $dis ?>>
                <?php elseif ($it['value_type'] === 'date'): ?>
                  <input type="date" class="form-control form-control-sm" id="<?= attr($name) ?>" name="<?= attr($name) ?>" value="<?= attr($val) ?>" <?= $dis ?>>
                <?php else: ?>
                  <input type="text" class="form-control form-control-sm" id="<?= attr($name) ?>" name="<?= attr($name) ?>" value="<?= attr(is_array($val) ? json_encode($val) : $val) ?>" <?= $dis ?>>
                <?php endif; ?>
              </div>
            </div>
          <?php endforeach; ?>
        </div>
      </div>
    </form>
    <?php endif; ?>

    <?php if (can('pengaturan','edit')): ?>
    <div class="card mt-3 border-warning">
      <div class="card-header text-warning-emphasis bg-warning-subtle"><i class="bi bi-arrow-clockwise me-1"></i>Hitung Ulang Seluruh Piutang</div>
      <form method="post" action="<?= url('/pengaturan/hitung-ulang') ?>" class="card-body" data-loading>
        <?= csrf() ?>
        <p class="small text-body-secondary">Menghitung ulang DPP, PPN, potongan, nilai bersih, pembayaran, saldo, status pembayaran,
          umur piutang, dan revenue untuk <strong>seluruh invoice</strong> memakai aturan bisnis terkini. Jalankan setelah mengubah
          pengaturan pajak, termin, atau basis revenue.</p>
        <div class="row g-2 align-items-end">
          <div class="col-md-4"><label class="form-label" for="tanggal_lapor">Tanggal Pelaporan</label>
            <input type="date" class="form-control form-control-sm" id="tanggal_lapor" name="tanggal_lapor" value="<?= attr(App\Core\Dates::today()) ?>"></div>
          <div class="col-md-5"><div class="form-check">
            <input class="form-check-input" type="checkbox" id="hitung_ulang_jatuh_tempo" name="hitung_ulang_jatuh_tempo" value="1">
            <label class="form-check-label small" for="hitung_ulang_jatuh_tempo">Hitung ulang juga tanggal jatuh tempo</label>
            <div class="form-text">Jatuh tempo manual tetap dipertahankan.</div></div></div>
          <div class="col-md-3"><button class="btn btn-warning w-100" type="submit"
            data-konfirmasi="Hitung ulang seluruh invoice sekarang? Proses ini dapat memakan waktu beberapa menit untuk data besar."
            data-konfirmasi-warna="warning">Jalankan</button></div>
        </div>
      </form>
    </div>
    <?php endif; ?>
  </div>
</div>
