<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<style>
:root {
  --teal-900: #0d3d38; --teal-800: #0b5a52; --teal-700: #0b7a6e;
  --teal-600: #0d9488; --teal-500: #14b8a6; --teal-400: #2dd4bf;
  --teal-100: #ccfbf1; --teal-50: #f0fdfa;
  --red-500: #ef4444; --blue-500: #3b82f6; --blue-600: #2563eb;
  --slate-50: #f8fafc; --slate-100: #f1f5f9; --slate-200: #e2e8f0;
  --slate-400: #94a3b8; --slate-500: #64748b; --slate-700: #334155; --slate-900: #0f172a;
  --radius: 14px; --radius-sm: 9px; --radius-xs: 6px;
  --shadow: 0 4px 24px rgba(13,148,136,0.10), 0 1px 4px rgba(0,0,0,0.05);
}
.ea-wrapper { max-width: 700px; margin: 0 auto; padding: 0 14px 56px; }
.ea-page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; flex-wrap: wrap; gap: 10px; }
.ea-page-header h4 { display: flex; align-items: center; gap: 11px; font-size: 1.2rem; font-weight: 800; color: var(--teal-800); margin: 0; }
.ea-page-header h4 .hi { width: 40px; height: 40px; border-radius: 11px; background: linear-gradient(135deg, var(--teal-800), var(--teal-500)); display: flex; align-items: center; justify-content: center; color: #fff; font-size: 1.05rem; }
.btn-back { display: inline-flex; align-items: center; gap: 6px; padding: 8px 18px; border-radius: 50px; border: 1.5px solid var(--slate-200); background: #fff; color: var(--slate-700); font-size: 0.83rem; font-weight: 600; text-decoration: none; transition: all .2s; }
.btn-back:hover { border-color: var(--teal-400); color: var(--teal-700); background: var(--teal-50); }
.ea-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; border: 1.5px solid var(--slate-100); }
.ea-card-header { background: linear-gradient(135deg, var(--teal-800), var(--teal-600) 60%, var(--teal-500)); padding: 22px 26px 20px; }
.ea-card-header h5 { color: #fff; font-weight: 700; font-size: 1.02rem; margin: 0; display: flex; align-items: center; gap: 8px; }
.ea-card-header .header-sub { color: rgba(255,255,255,0.72); font-size: 0.77rem; margin-top: 4px; }
.ea-card-body { padding: 26px; }
.val-error { background: #fef2f2; border-left: 4px solid var(--red-500); border-radius: var(--radius-sm); padding: 13px 16px; font-size: 0.84rem; color: #7f1d1d; margin-bottom: 22px; display: flex; gap: 10px; }
.sec-div { display: flex; align-items: center; gap: 10px; margin: 22px 0 18px; }
.sec-div span { font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: .7px; color: var(--slate-400); white-space: nowrap; }
.sec-div::before, .sec-div::after { content: ''; flex: 1; height: 1px; background: var(--slate-200); }
.form-group-c { margin-bottom: 18px; }
.form-label-c { display: block; font-size: 0.77rem; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; color: var(--slate-500); margin-bottom: 7px; }
.form-label-c .req { color: var(--red-500); margin-left: 2px; }
.form-label-c .opt { font-size: .7rem; font-weight: 400; color: var(--slate-400); margin-left: 4px; }
.input-wrap { display: flex; border: 1.5px solid var(--slate-200); border-radius: var(--radius-sm); overflow: hidden; transition: border-color .2s, box-shadow .2s; background: #fff; }
.input-wrap:focus-within { border-color: var(--teal-600); box-shadow: 0 0 0 3px rgba(13,148,136,0.12); }
.input-prefix { padding: 0 14px; background: var(--slate-50); border-right: 1.5px solid var(--slate-200); display: flex; align-items: center; font-size: 0.88rem; font-weight: 700; color: var(--slate-500); white-space: nowrap; }
.input-wrap input { flex: 1; border: none; outline: none; padding: 11px 14px; font-size: 0.9rem; color: var(--slate-900); background: transparent; min-width: 0; }
.custom-input, .custom-select, .custom-textarea {
  width: 100%; padding: 11px 14px; border: 1.5px solid var(--slate-200); border-radius: var(--radius-sm);
  font-size: 0.9rem; color: var(--slate-900); background: #fff; transition: border-color .2s, box-shadow .2s; box-sizing: border-box;
}
.custom-select { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 14px center; -webkit-appearance: none; appearance: none; }
.custom-textarea { resize: vertical; min-height: 90px; font-family: inherit; }
.custom-input:focus, .custom-select:focus, .custom-textarea:focus { border-color: var(--teal-600); box-shadow: 0 0 0 3px rgba(13,148,136,0.12); outline: none; }
.hint-text { font-size: 0.73rem; color: var(--slate-400); margin-top: 5px; display: flex; align-items: center; gap: 4px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* Status cards */
.status-cards { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.status-card { position: relative; border: 2px solid var(--slate-200); border-radius: var(--radius-sm); padding: 13px 12px 11px; cursor: pointer; transition: all .2s; background: #fff; text-align: center; }
.status-card input[type=radio] { position: absolute; opacity: 0; width: 0; height: 0; }
.status-card .sc-icon  { font-size: 1.3rem; margin-bottom: 4px; }
.status-card .sc-title { font-size: 0.83rem; font-weight: 700; color: var(--slate-700); }
.status-card .sc-check { position: absolute; top: 7px; right: 7px; width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--slate-200); display: flex; align-items: center; justify-content: center; font-size: 0.55rem; color: transparent; background: #fff; transition: all .2s; }
.status-card.aktif:has(input:checked)    { border-color: var(--teal-600); background: var(--teal-50); }
.status-card.aktif:has(input:checked) .sc-title { color: var(--teal-700); }
.status-card.aktif:has(input:checked) .sc-check { border-color: var(--teal-600); background: var(--teal-600); color: #fff; }
.status-card.selesai:has(input:checked)  { border-color: var(--blue-500); background: #eff6ff; }
.status-card.selesai:has(input:checked) .sc-title { color: var(--blue-600); }
.status-card.selesai:has(input:checked) .sc-check { border-color: var(--blue-500); background: var(--blue-500); color: #fff; }
.status-card.nonaktif:has(input:checked) { border-color: var(--slate-500); background: var(--slate-100); }
.status-card.nonaktif:has(input:checked) .sc-check { border-color: var(--slate-500); background: var(--slate-500); color: #fff; }

/* Mode undian cards */
.mode-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.mode-card { position: relative; border: 2px solid var(--slate-200); border-radius: var(--radius-sm); padding: 16px 14px 12px; cursor: pointer; transition: all .2s; background: #fff; text-align: center; }
.mode-card input[type=radio] { position: absolute; opacity: 0; width: 0; height: 0; }
.mode-card:has(input:checked) { border-color: var(--teal-600); background: var(--teal-50); }
.mode-card .mc-icon { font-size: 1.6rem; margin-bottom: 6px; }
.mode-card .mc-title { font-size: 0.88rem; font-weight: 700; color: var(--slate-700); margin-bottom: 3px; }
.mode-card .mc-desc { font-size: 0.72rem; color: var(--slate-400); line-height: 1.4; }
.mode-card:has(input:checked) .mc-title { color: var(--teal-700); }
.mode-card .mc-check { position: absolute; top: 8px; right: 8px; width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--slate-200); display: flex; align-items: center; justify-content: center; font-size: 0.6rem; color: transparent; background: #fff; transition: all .2s; }
.mode-card:has(input:checked) .mc-check { border-color: var(--teal-600); background: var(--teal-600); color: #fff; }

.form-footer { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 26px; padding-top: 22px; border-top: 1.5px solid var(--slate-100); flex-wrap: wrap; }
.btn-submit { display: inline-flex; align-items: center; gap: 7px; padding: 12px 28px; border-radius: var(--radius-sm); border: none; background: linear-gradient(135deg, var(--teal-800), var(--teal-600)); color: #fff; font-size: 0.92rem; font-weight: 700; cursor: pointer; box-shadow: 0 4px 14px rgba(13,148,136,0.36); transition: all .2s; }
.btn-submit:hover { background: linear-gradient(135deg, var(--teal-900), var(--teal-700)); transform: translateY(-1px); }
@media (max-width: 576px) {
  .ea-card-body { padding: 18px 16px 100px; }
  .two-col { grid-template-columns: 1fr; gap: 0; }
  .status-cards { grid-template-columns: 1fr; }
  .mode-cards { grid-template-columns: 1fr 1fr; }
  .ea-page-header { flex-direction: column; align-items: flex-start; }
  .form-footer { display: none; }
  .mobile-sticky { display: flex; flex-direction: column; gap: 8px; position: fixed; bottom: 0; left: 0; right: 0; background: #fff; padding: 12px 16px; box-shadow: 0 -4px 20px rgba(0,0,0,0.10); z-index: 100; border-top: 1px solid var(--slate-100); }
  .btn-submit-mob { width: 100%; padding: 14px; border-radius: var(--radius-sm); border: none; background: linear-gradient(135deg, var(--teal-800), var(--teal-600)); color: #fff; font-size: 0.92rem; font-weight: 700; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 7px; }
  .btn-cancel-mob { width: 100%; padding: 12px; border-radius: var(--radius-sm); border: 1.5px solid var(--slate-200); background: #fff; color: var(--slate-500); font-size: 0.88rem; font-weight: 600; text-align: center; text-decoration: none; display: flex; align-items: center; justify-content: center; gap: 6px; }
}
@media (min-width: 577px) { .mobile-sticky { display: none; } }
</style>

<div class="ea-wrapper">
  <div class="ea-page-header">
    <h4>
      <span class="hi"><i class="bi bi-pencil-square"></i></span>
      Edit Arisan
    </h4>
    <a href="<?php echo base_url('arisan'); ?>" class="btn-back">
      <i class="bi bi-arrow-left"></i> Kembali
    </a>
  </div>

  <div class="ea-card">
    <div class="ea-card-header">
      <h5><i class="bi bi-pencil"></i> Form Edit Arisan</h5>
      <div class="header-sub">Perbarui detail arisan yang sudah ada</div>
    </div>
    <div class="ea-card-body">

      <?php $errors = validation_errors(); if(!empty($errors)): ?>
      <div class="val-error">
        <i class="bi bi-exclamation-triangle-fill" style="color:var(--red-500);flex-shrink:0;margin-top:1px"></i>
        <div><?php echo $errors; ?></div>
      </div>
      <?php endif; ?>

      <form action="" method="post" id="formEditArisan">

        <div class="sec-div"><span>Informasi Arisan</span></div>

        <div class="form-group-c">
          <label class="form-label-c">Nama Arisan <span class="req">*</span></label>
          <input type="text" name="nama_arisan" class="custom-input" required
                 placeholder="Contoh: Arisan RT 05 Tahun 2025"
                 value="<?php echo set_value('nama_arisan', $arisan->nama_arisan); ?>">
        </div>

        <div class="form-group-c">
          <label class="form-label-c">Nominal Iuran <span class="req">*</span></label>
          <div class="input-wrap">
            <span class="input-prefix">Rp</span>
            <input type="number" name="nominal_iuran" min="10000" step="1000" required
                   placeholder="100000"
                   value="<?php echo set_value('nominal_iuran', $arisan->nominal_iuran); ?>">
          </div>
        </div>

        <div class="form-group-c">
          <label class="form-label-c">Status <span class="req">*</span></label>
          <div class="status-cards">
            <label class="status-card aktif">
              <input type="radio" name="status" value="aktif"
                     <?php echo set_select('status', 'aktif', $arisan->status == 'aktif'); ?>>
              <div class="sc-check">✓</div>
              <div class="sc-icon">🟢</div>
              <div class="sc-title">Aktif</div>
            </label>
            <label class="status-card selesai">
              <input type="radio" name="status" value="selesai"
                     <?php echo set_select('status', 'selesai', $arisan->status == 'selesai'); ?>>
              <div class="sc-check">✓</div>
              <div class="sc-icon">🏁</div>
              <div class="sc-title">Selesai</div>
            </label>
            <label class="status-card nonaktif">
              <input type="radio" name="status" value="nonaktif"
                     <?php echo set_select('status', 'nonaktif', $arisan->status == 'nonaktif'); ?>>
              <div class="sc-check">✓</div>
              <div class="sc-icon">⚫</div>
              <div class="sc-title">Nonaktif</div>
            </label>
          </div>
        </div>

        <div class="sec-div"><span>Setting Undian</span></div>

        <div class="form-group-c">
          <label class="form-label-c">Mode Undian <span class="req">*</span></label>
          <div class="mode-cards">
            <label class="mode-card">
              <input type="radio" name="mode_undian" value="manual"
                     <?php echo set_select('mode_undian', 'manual', ($arisan->mode_undian ?? 'manual') == 'manual'); ?>>
              <div class="mc-check">✓</div>
              <div class="mc-icon">🎲</div>
              <div class="mc-title">Manual (Kocokan)</div>
              <div class="mc-desc">Pemenang dipilih sendiri saat pertemuan berlangsung</div>
            </label>
            <label class="mode-card">
              <input type="radio" name="mode_undian" value="otomatis"
                     <?php echo set_select('mode_undian', 'otomatis', ($arisan->mode_undian ?? '') == 'otomatis'); ?>>
              <div class="mc-check">✓</div>
              <div class="mc-icon">🎰</div>
              <div class="mc-title">Otomatis (Sistem)</div>
              <div class="mc-desc">Pemenang dipilih secara acak oleh sistem</div>
            </label>
          </div>
        </div>

        <div class="form-group-c">
          <label class="form-label-c">Jumlah Pemenang per Pertemuan <span class="req">*</span></label>
          <div class="input-wrap">
            <span class="input-prefix"><i class="bi bi-trophy"></i></span>
            <input type="number" name="jumlah_pemenang_per_pertemuan" min="1" max="10" required
                   value="<?php echo set_value('jumlah_pemenang_per_pertemuan', $arisan->jumlah_pemenang_per_pertemuan ?? 1); ?>">
          </div>
          <div class="hint-text"><i class="bi bi-info-circle"></i> Berapa orang yang menang per pertemuan</div>
        </div>

        <div class="sec-div"><span>Pengaturan Kas</span></div>

        <div class="form-group-c">
          <label class="form-label-c">Kas untuk Arisan Ini <span class="opt">(opsional)</span></label>
          <select name="kas_id" class="custom-select">
            <option value="">-- Tidak dihubungkan ke kas tertentu --</option>
            <?php foreach($kas_list as $kas): ?>
            <option value="<?php echo $kas->id; ?>"
                    <?php echo set_select('kas_id', $kas->id, ($arisan->kas_id ?? '') == $kas->id); ?>>
              <?php echo $kas->nama; ?>
              (<?php echo ucfirst($kas->jenis); ?>
              <?php if(!empty($kas->nomor_rekening)): ?> - <?php echo $kas->nomor_rekening; ?><?php endif; ?>)
              — Saldo: Rp <?php echo number_format($kas->saldo_saat_ini, 0, ',', '.'); ?>
            </option>
            <?php endforeach; ?>
          </select>
          <div class="hint-text"><i class="bi bi-info-circle"></i> Kas ini akan menjadi default saat pembayaran iuran dan pemenang</div>
        </div>

        <div class="form-group-c">
          <label class="form-label-c">Keterangan <span class="opt">(opsional)</span></label>
          <textarea name="keterangan" class="custom-textarea"
                    placeholder="Keterangan tambahan..."><?php echo set_value('keterangan', $arisan->keterangan); ?></textarea>
        </div>

        <div class="form-footer">
          <a href="<?php echo base_url('arisan'); ?>" class="btn-back">
            <i class="bi bi-arrow-left"></i> Kembali
          </a>
          <button type="submit" class="btn-submit">
            <i class="bi bi-save"></i> Update Arisan
          </button>
        </div>

      </form>
    </div>
  </div>
</div>

<div class="mobile-sticky">
  <button type="submit" form="formEditArisan" class="btn-submit-mob">
    <i class="bi bi-save"></i> Update Arisan
  </button>
  <a href="<?php echo base_url('arisan'); ?>" class="btn-cancel-mob">Batal</a>
</div>