<?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;--green-50:#f0fdf4;--green-100:#bbf7d0;
  --red-500:#ef4444;--red-50:#fef2f2;
  --amber-600:#d97706;--amber-50:#fffbeb;--amber-100:#fef3c7;
  --blue-600:#2563eb;--blue-50:#eff6ff;--blue-100:#dbeafe;
  --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,.10),0 1px 4px rgba(0,0,0,.05);
}

/* ── Wrapper ── */
.surat-wrapper { width:100%; }

/* ── Page Header ── */
.surat-page-header {
  display:flex;justify-content:space-between;align-items:flex-start;
  margin-bottom:28px;flex-wrap:wrap;gap:14px;
}
.surat-header-left h4 {
  font-size:1.2rem;font-weight:800;color:var(--teal-800);
  margin:0 0 5px;display:flex;align-items:center;gap:11px;letter-spacing:-.3px;
}
.surat-header-left h4 .hi {
  width:40px;height:40px;border-radius:11px;
  background:linear-gradient(135deg,var(--teal-600),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,.32);flex-shrink:0;
}
.surat-breadcrumb {
  display:flex;align-items:center;gap:6px;
  font-size:.77rem;color:var(--slate-400);padding-left:51px;
}
.surat-breadcrumb a { color:var(--teal-600);text-decoration:none;font-weight:600; }
.surat-breadcrumb a:hover { text-decoration:underline; }

/* ── Flash Alerts ── */
.flash-alert {
  display:flex;align-items:flex-start;gap:10px;
  border-radius:var(--radius-sm);padding:13px 16px;
  margin-bottom:20px;font-size:.85rem;position:relative;
}
.flash-alert.error { background:#fef2f2;border-left:4px solid var(--red-500);color:#7f1d1d; }
.flash-alert .fa-close {
  margin-left:auto;cursor:pointer;font-size:1rem;opacity:.55;
  background:none;border:none;color:inherit;padding:0;line-height:1;
}

/* ── Layout ── */
.surat-layout { display:grid;grid-template-columns:1fr 320px;gap:22px;align-items:start; }
@media(max-width:900px) { .surat-layout { grid-template-columns:1fr; } }

/* ── Main Form Card ── */
.surat-card {
  background:#fff;border-radius:var(--radius);
  box-shadow:var(--shadow);border:1.5px solid var(--slate-100);overflow:hidden;
}
.surat-card-header {
  display:flex;align-items:center;gap:12px;
  padding:18px 24px;background:var(--teal-50);
  border-bottom:1.5px solid var(--teal-100);
}
.surat-card-header .sch-icon {
  width:38px;height:38px;border-radius:10px;
  background:linear-gradient(135deg,var(--teal-600),var(--teal-500));
  display:flex;align-items:center;justify-content:center;
  color:#fff;font-size:.95rem;box-shadow:0 3px 10px rgba(13,148,136,.28);flex-shrink:0;
}
.surat-card-header h5 { margin:0;font-weight:800;color:var(--teal-800);font-size:.95rem; }
.surat-card-header p  { margin:0;font-size:.76rem;color:var(--slate-400); }
.surat-card-body { padding:26px; }

/* ── Form Sections ── */
.form-section {
  background:var(--slate-50);border-radius:var(--radius-sm);
  border:1px solid var(--slate-200);padding:18px 18px 14px;margin-bottom:18px;
}
.form-section-title {
  font-size:.72rem;font-weight:700;text-transform:uppercase;
  letter-spacing:.6px;color:var(--teal-600);margin-bottom:14px;
  display:flex;align-items:center;gap:7px;
}
.form-label { font-size:.83rem;font-weight:600;color:var(--slate-700);margin-bottom:5px;display:block; }
.form-label .req { color:var(--red-500); }
.form-control,.form-select {
  font-size:.85rem;border:1.5px solid var(--slate-200);border-radius:8px;
  padding:9px 13px;color:var(--slate-700);width:100%;
  transition:border-color .18s,box-shadow .18s;background:#fff;
}
.form-control:focus,.form-select:focus {
  border-color:var(--teal-500);box-shadow:0 0 0 3px rgba(20,184,166,.12);outline:none;
}
textarea.form-control { resize:vertical;min-height:100px; }
.form-text { font-size:.74rem;color:var(--slate-400);margin-top:4px; }

/* ── Info Pemohon (warga) ── */
.pemohon-box {
  background:linear-gradient(135deg,var(--teal-50),#fff);
  border:1.5px solid var(--teal-100);border-radius:var(--radius-sm);
  padding:16px 18px;margin-bottom:0;
}
.pemohon-box .pb-head {
  display:flex;align-items:center;gap:8px;
  font-size:.75rem;font-weight:700;text-transform:uppercase;letter-spacing:.5px;
  color:var(--teal-600);margin-bottom:12px;
}
.pemohon-row { display:flex;gap:8px;margin-bottom:7px;font-size:.84rem; }
.pemohon-row:last-child { margin-bottom:0; }
.pemohon-row .pr-label { color:var(--slate-400);font-weight:600;min-width:60px;flex-shrink:0; }
.pemohon-row .pr-value { color:var(--slate-700);font-weight:600; }
.pemohon-row .pr-value strong { color:var(--teal-700); }

/* ── File upload ── */
.file-upload-box {
  border:2px dashed var(--slate-200);border-radius:var(--radius-sm);
  padding:18px;text-align:center;cursor:pointer;transition:all .2s;
  background:var(--slate-50);position:relative;
}
.file-upload-box:hover { border-color:var(--teal-400);background:var(--teal-50); }
.file-upload-box.has-file { border-color:var(--teal-400);background:var(--teal-50);border-style:solid; }
.file-upload-box input[type=file] { position:absolute;inset:0;opacity:0;cursor:pointer;width:100%;height:100%; }
.file-upload-box .fub-icon { font-size:1.6rem;color:var(--slate-400);margin-bottom:6px; }
.file-upload-box .fub-text { font-size:.82rem;color:var(--slate-500); }
.file-upload-box .fub-hint { font-size:.73rem;color:var(--slate-400);margin-top:3px; }
.file-upload-box.has-file .fub-icon { color:var(--teal-600); }
.file-upload-box.has-file .fub-text { color:var(--teal-700);font-weight:600; }

/* ── Action Buttons ── */
.surat-actions { display:flex;align-items:center;gap:12px;margin-top:6px; }
.btn-submit {
  display:inline-flex;align-items:center;gap:8px;
  padding:11px 28px;border-radius:50px;border:none;
  background:linear-gradient(135deg,var(--teal-700),var(--teal-500));
  color:#fff;font-size:.88rem;font-weight:700;cursor:pointer;
  box-shadow:0 4px 16px rgba(13,148,136,.32);transition:all .2s;
}
.btn-submit:hover { transform:translateY(-1px);box-shadow:0 6px 20px rgba(13,148,136,.42); }
.btn-back {
  display:inline-flex;align-items:center;gap:7px;
  padding:11px 22px;border-radius:50px;
  border:1.5px solid var(--slate-200);background:#fff;
  color:var(--slate-500);font-size:.86rem;font-weight:600;
  text-decoration:none;transition:all .18s;
}
.btn-back:hover { border-color:var(--slate-400);color:var(--slate-700); }

/* ── Sidebar Info Card ── */
.info-card {
  background:#fff;border-radius:var(--radius);
  box-shadow:var(--shadow);border:1.5px solid var(--slate-100);overflow:hidden;
}
.info-card-header {
  padding:14px 18px;background:var(--teal-50);border-bottom:1.5px solid var(--teal-100);
  display:flex;align-items:center;gap:8px;
  font-size:.8rem;font-weight:700;color:var(--teal-700);
}
.info-card-body { padding:16px 18px; }
.info-item {
  display:flex;gap:10px;padding:9px 0;
  border-bottom:1px solid var(--slate-100);font-size:.82rem;
}
.info-item:last-child { border-bottom:none;padding-bottom:0; }
.info-item .ii-icon {
  width:28px;height:28px;border-radius:7px;flex-shrink:0;
  display:flex;align-items:center;justify-content:center;font-size:.78rem;
}
.info-item.blue .ii-icon  { background:var(--blue-50); color:var(--blue-600); }
.info-item.amber .ii-icon { background:var(--amber-50);color:var(--amber-600); }
.info-item.green .ii-icon { background:var(--green-50);color:var(--green-600); }
.info-item.teal .ii-icon  { background:var(--teal-50); color:var(--teal-600); }
.info-item .ii-text { color:var(--slate-500);line-height:1.45; }
.info-item .ii-text strong { color:var(--slate-700);display:block;margin-bottom:1px; }

/* ── Jenis Surat Preview ── */
.jenis-preview {
  display:none;background:var(--blue-50);border:1.5px solid var(--blue-100);
  border-radius:8px;padding:11px 14px;margin-top:8px;font-size:.81rem;color:var(--blue-600);
}
.jenis-preview.show { display:flex;align-items:center;gap:7px; }
</style>

<div class="surat-wrapper">

  <!-- ── Page Header ── -->
  <div class="surat-page-header">
    <div class="surat-header-left">
      <h4>
        <span class="hi"><i class="bi bi-file-earmark-plus"></i></span>
        <?php echo is_warga() ? 'Ajukan Surat Baru' : 'Ajukan Permohonan Surat'; ?>
      </h4>
      <div class="surat-breadcrumb">
        <a href="<?php echo base_url('dashboard'); ?>">Dashboard</a>
        <span>/</span>
        <a href="<?php echo base_url('surat'); ?>">Surat</a>
        <span>/</span>
        <span>Ajukan</span>
      </div>
    </div>
  </div>

  <!-- ── Flash Error ── -->
  <?php if(validation_errors()): ?>
  <div class="flash-alert error" id="flashError">
    <i class="bi bi-exclamation-triangle-fill"></i>
    <span><?php echo validation_errors(); ?></span>
    <button class="fa-close" onclick="this.parentElement.remove()">✕</button>
  </div>
  <?php endif; ?>

  <!-- ── Layout ── -->
  <div class="surat-layout">

    <!-- ── Form Card ── -->
    <div class="surat-card">
      <div class="surat-card-header">
        <div class="sch-icon"><i class="bi bi-file-earmark-text"></i></div>
        <div>
          <h5><?php echo is_warga() ? 'Form Permohonan Surat' : 'Buat Permohonan Atas Nama Warga'; ?></h5>
          <p>Isi formulir berikut dengan lengkap dan benar</p>
        </div>
      </div>

      <div class="surat-card-body">
        <form action="<?php echo base_url('surat/ajukan'); ?>" method="post" enctype="multipart/form-data">

          <!-- Bagian 1: Data Pemohon -->
          <div class="form-section">
            <div class="form-section-title"><i class="bi bi-person-fill"></i> Data Pemohon</div>

            <?php if(is_warga()): ?>
            <!-- Warga: tampilkan info saja -->
            <div class="pemohon-box">
              <div class="pb-head"><i class="bi bi-shield-check"></i> Data Anda (Terverifikasi)</div>
              <div class="pemohon-row">
                <span class="pr-label">Nama</span>
                <span class="pr-value">: <strong><?php echo $warga_saya->nama_lengkap ?? '-'; ?></strong></span>
              </div>
              <div class="pemohon-row">
                <span class="pr-label">NIK</span>
                <span class="pr-value">: <?php echo $warga_saya->nik ?? '-'; ?></span>
              </div>
              <div class="pemohon-row">
                <span class="pr-label">Alamat</span>
                <span class="pr-value">: <?php echo $warga_saya->alamat ?? '-'; ?></span>
              </div>
            </div>

            <?php else: ?>
            <!-- Admin: pilih warga -->
            <div>
              <label class="form-label">Pilih Warga <span class="req">*</span></label>
              <select name="warga_id" class="form-select" required>
                <option value="">-- Pilih Warga --</option>
                <?php foreach($warga as $w): ?>
                <option value="<?php echo $w->id; ?>"
                  <?php echo set_select('warga_id', $w->id); ?>>
                  <?php echo $w->nama_lengkap; ?> &mdash; NIK: <?php echo $w->nik; ?>
                </option>
                <?php endforeach; ?>
              </select>
              <div class="form-text">Pilih warga yang mengajukan surat</div>
            </div>
            <?php endif; ?>
          </div>

          <!-- Bagian 2: Jenis & Keperluan -->
          <div class="form-section">
            <div class="form-section-title"><i class="bi bi-card-list"></i> Jenis & Keperluan</div>

            <div class="mb-3">
              <label class="form-label">Jenis Surat <span class="req">*</span></label>
              <select name="jenis_surat_id" class="form-select" required id="jenisSurat">
                <option value="">-- Pilih Jenis Surat --</option>
                <?php foreach($jenis_surat as $js): ?>
                <option value="<?php echo $js->id; ?>"
                        data-kode="<?php echo $js->kode_surat; ?>"
                        data-nama="<?php echo $js->nama_surat; ?>"
                        <?php echo set_select('jenis_surat_id', $js->id); ?>>
                  <?php echo $js->nama_surat; ?>
                </option>
                <?php endforeach; ?>
              </select>
              <div id="jenisPrev" class="jenis-preview">
                <i class="bi bi-tag-fill"></i>
                <span id="jenisPrevText"></span>
              </div>
            </div>

            <div>
              <label class="form-label">Keperluan <span class="req">*</span></label>
              <textarea name="keperluan" class="form-control" rows="4" required
                placeholder="Jelaskan keperluan pengajuan surat secara lengkap..."><?php echo set_value('keperluan'); ?></textarea>
              <div class="form-text">Contoh: Untuk keperluan melamar pekerjaan di PT. ABC Tbk.</div>
            </div>
          </div>

          <!-- Bagian 3: Dokumen -->
          <div class="form-section" style="margin-bottom:22px;">
            <div class="form-section-title"><i class="bi bi-paperclip"></i> Dokumen Pendukung</div>
            <div class="file-upload-box" id="fileBox">
              <input type="file" name="dokumen" accept=".jpg,.jpeg,.png,.pdf" id="fileInput">
              <div class="fub-icon"><i class="bi bi-cloud-upload"></i></div>
              <div class="fub-text" id="fileText">Klik atau seret file ke sini</div>
              <div class="fub-hint">Format: JPG, PNG, PDF &bull; Maks. 2MB &bull; Opsional</div>
            </div>
          </div>

          <!-- Tombol -->
          <div class="surat-actions">
            <button type="submit" class="btn-submit">
              <i class="bi bi-send-fill"></i> Ajukan Permohonan
            </button>
            <a href="<?php echo base_url('surat'); ?>" class="btn-back">
              <i class="bi bi-arrow-left"></i> Batal
            </a>
          </div>

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

    <!-- ── Sidebar Info ── -->
    <div>
      <div class="info-card">
        <div class="info-card-header">
          <i class="bi bi-info-circle-fill"></i> Informasi Penting
        </div>
        <div class="info-card-body">
          <div class="info-item blue">
            <div class="ii-icon"><i class="bi bi-pencil-fill"></i></div>
            <div class="ii-text">
              <strong>Isi Data Lengkap</strong>
              Pastikan semua data yang diisi sudah benar dan sesuai identitas.
            </div>
          </div>
          <div class="info-item amber">
            <div class="ii-icon"><i class="bi bi-clock-fill"></i></div>
            <div class="ii-text">
              <strong>Waktu Proses</strong>
              Permohonan diproses dalam <strong style="color:var(--amber-600)">1–3 hari kerja</strong> oleh admin.
            </div>
          </div>
          <div class="info-item teal">
            <div class="ii-icon"><i class="bi bi-eye-fill"></i></div>
            <div class="ii-text">
              <strong>Pantau Status</strong>
              Status permohonan dapat dipantau di halaman Daftar Surat.
            </div>
          </div>
          <div class="info-item green">
            <div class="ii-icon"><i class="bi bi-download"></i></div>
            <div class="ii-text">
              <strong>Unduh Surat</strong>
              Surat yang selesai diproses dapat langsung diunduh dari sistem.
            </div>
          </div>
        </div>
      </div>

      <?php if(!is_warga()): ?>
      <!-- Tips admin -->
      <div class="info-card" style="margin-top:14px;">
        <div class="info-card-header">
          <i class="bi bi-shield-fill"></i> Mode Admin
        </div>
        <div class="info-card-body" style="font-size:.82rem;color:var(--slate-500);line-height:1.6;">
          Anda membuat permohonan <strong style="color:var(--slate-700)">atas nama warga</strong>. Pastikan data pemohon yang dipilih sudah tepat sebelum mengajukan.
        </div>
      </div>
      <?php endif; ?>
    </div>

  </div>
</div>

<script>
// Jenis surat preview kode
document.getElementById('jenisSurat').addEventListener('change', function() {
  var opt = this.options[this.selectedIndex];
  var prev = document.getElementById('jenisPrev');
  var txt  = document.getElementById('jenisPrevText');
  if(this.value) {
    txt.textContent = 'Kode: ' + (opt.getAttribute('data-kode') || '-') + ' — ' + opt.getAttribute('data-nama');
    prev.classList.add('show');
  } else {
    prev.classList.remove('show');
  }
});

// File upload label
document.getElementById('fileInput').addEventListener('change', function() {
  var box  = document.getElementById('fileBox');
  var txt  = document.getElementById('fileText');
  if(this.files && this.files[0]) {
    txt.textContent = '✓ ' + this.files[0].name;
    box.classList.add('has-file');
  } else {
    txt.textContent = 'Klik atau seret file ke sini';
    box.classList.remove('has-file');
  }
});

// Auto dismiss error
setTimeout(function() {
  var el = document.getElementById('flashError');
  if(el) { el.style.transition='opacity .4s'; el.style.opacity='0'; setTimeout(function(){ el.remove(); },420); }
}, 5000);
</script>