<?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;
  --green-600: #059669;
  --amber-400: #fbbf24; --amber-500: #f59e0b; --amber-600: #d97706;
  --amber-50: #fffbeb; --amber-100: #fef3c7;
  --red-500: #ef4444;
  --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);
}
.fa-wrapper { max-width: 700px; margin: 0 auto; padding: 0 14px 56px; }
.fa-page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; flex-wrap: wrap; gap: 10px; }
.fa-page-header h4 { display: flex; align-items: center; gap: 11px; font-size: 1.2rem; font-weight: 800; color: var(--teal-800); margin: 0; }
.fa-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; box-shadow: 0 4px 12px rgba(13,148,136,0.32); }
.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); }
.fa-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; border: 1.5px solid var(--slate-100); }
.fa-card-header { background: linear-gradient(135deg, var(--teal-800), var(--teal-600) 60%, var(--teal-500)); padding: 22px 26px 20px; }
.fa-card-header h5 { color: #fff; font-weight: 700; font-size: 1.02rem; margin: 0; display: flex; align-items: center; gap: 8px; }
.fa-card-header .header-sub { color: rgba(255,255,255,0.72); font-size: 0.77rem; margin-top: 4px; }
.fa-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, .input-wrap select { 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; }
.three-col { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; }

/* 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:hover { border-color: var(--teal-400); background: var(--teal-50); }
.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; }

/* Periode cards */
.periode-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.periode-card { position: relative; border: 2px solid var(--slate-200); border-radius: var(--radius-sm); padding: 14px 14px 12px; cursor: pointer; transition: all .2s; background: #fff; text-align: center; }
.periode-card input[type=radio] { position: absolute; opacity: 0; width: 0; height: 0; }
.periode-card:hover { border-color: var(--teal-400); background: var(--teal-50); }
.periode-card:has(input:checked) { border-color: var(--teal-600); background: var(--teal-50); }
.periode-card .pc-icon { font-size: 1.5rem; margin-bottom: 5px; }
.periode-card .pc-title { font-size: 0.88rem; font-weight: 700; color: var(--slate-700); margin-bottom: 2px; }
.periode-card .pc-desc { font-size: 0.72rem; color: var(--slate-400); }
.periode-card:has(input:checked) .pc-title { color: var(--teal-700); }
.periode-card .pc-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; }
.periode-card:has(input:checked) .pc-check { border-color: var(--teal-600); background: var(--teal-600); color: #fff; }

/* Kas badge */
.kas-option { display: flex; align-items: center; gap: 8px; }
.kas-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }

.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); }
.info-box { display: flex; gap: 12px; background: var(--teal-50); border: 1px solid var(--teal-100); border-left: 4px solid var(--teal-500); border-radius: var(--radius-sm); padding: 14px 16px; margin-bottom: 0; }
.info-box i { color: var(--teal-600); flex-shrink: 0; margin-top: 1px; }
.info-box .ib-title { font-size: 0.8rem; font-weight: 700; color: var(--teal-700); margin-bottom: 5px; }
.info-box ul { margin: 0; padding-left: 16px; }
.info-box ul li { font-size: 0.77rem; color: var(--slate-500); line-height: 1.6; }
@media (max-width: 576px) {
  .fa-card-body { padding: 18px 16px 100px; }
  .two-col, .three-col { grid-template-columns: 1fr; gap: 0; }
  .fa-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; }
  .mode-cards { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 577px) { .mobile-sticky { display: none; } }
</style>

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

  <div class="fa-card">
    <div class="fa-card-header">
      <h5><i class="bi bi-plus-circle"></i> Form Tambah Arisan</h5>
      <div class="header-sub">Isi detail arisan baru yang akan dikelola</div>
    </div>
    <div class="fa-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="formTambahArisan">

        <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'); ?>">
        </div>

        <div class="two-col">
          <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'); ?>">
            </div>
            <div class="hint-text"><i class="bi bi-info-circle"></i> Iuran per pertemuan per peserta</div>
          </div>
          <div class="form-group-c">
            <label class="form-label-c">Tanggal Mulai <span class="req">*</span></label>
            <input type="date" name="tanggal_mulai" class="custom-input" required
                   value="<?php echo set_value('tanggal_mulai', date('Y-m-d')); ?>">
          </div>
        </div>

        <div class="form-group-c">
          <label class="form-label-c">Periode Arisan <span class="req">*</span></label>
          <div class="periode-cards">
            <label class="periode-card">
              <input type="radio" name="periode" value="mingguan" <?php echo set_radio('periode', 'mingguan'); ?>>
              <div class="pc-check">✓</div>
              <div class="pc-icon">📅</div>
              <div class="pc-title">Mingguan</div>
              <div class="pc-desc">Undian setiap minggu</div>
            </label>
            <label class="periode-card">
              <input type="radio" name="periode" value="bulanan" <?php echo set_radio('periode', 'bulanan', TRUE); ?>>
              <div class="pc-check">✓</div>
              <div class="pc-icon">🗓️</div>
              <div class="pc-title">Bulanan</div>
              <div class="pc-desc">Undian setiap bulan</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_radio('mode_undian', 'manual', TRUE); ?>>
              <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_radio('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 class="hint-text"><i class="bi bi-info-circle"></i> Mode otomatis menggunakan tombol Undi di halaman tambah pertemuan</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
                   placeholder="1" value="<?php echo set_value('jumlah_pemenang_per_pertemuan', 1); ?>">
          </div>
          <div class="hint-text"><i class="bi bi-info-circle"></i> Berapa orang yang menang undian setiap pertemuan (umumnya 1)</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="">-- Pilih Kas (akan jadi default saat bayar iuran) --</option>
            <?php foreach($kas_list as $kas): ?>
            <option value="<?php echo $kas->id; ?>" <?php echo set_select('kas_id', $kas->id); ?>
                    style="color: <?php echo $kas->warna ?? '#333'; ?>">
              <?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> Pilih kas yang akan menampung iuran dan membayar pemenang arisan ini</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 tentang arisan ini..."><?php echo set_value('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-gift"></i> Simpan Arisan
          </button>
        </div>

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

  <div class="info-box" style="margin-top:16px">
    <i class="bi bi-info-circle-fill"></i>
    <div>
      <div class="ib-title">Catatan Penting</div>
      <ul>
        <li>Setelah arisan dibuat, tambahkan peserta terlebih dahulu sebelum membuat pertemuan</li>
        <li>Mode <strong>Kocokan</strong>: pemenang diinput manual saat pertemuan</li>
        <li>Mode <strong>Sistem</strong>: gunakan tombol 🎰 Undi untuk mengacak pemenang otomatis</li>
        <li>Kas default bisa diubah saat proses pembayaran iuran berlangsung</li>
      </ul>
    </div>
  </div>
</div>

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