<?= $this->extend('layouts/admin') ?>
<?= $this->section('content') ?>

<div class="mb-4">
  <a href="<?= base_url('admin/documents') ?>" class="btn btn-sm btn-outline-secondary">
    <i class="bi bi-arrow-left me-1"></i>Kembali
  </a>
  <h1 class="section-title mt-2 mb-0"><?= esc($title) ?></h1>
</div>

<?php if (session()->has('errors')): ?>
  <div class="alert alert-danger small py-2 mb-3">
    <?php foreach (session('errors') as $err): ?>
      <div><i class="bi bi-dot"></i><?= esc($err) ?></div>
    <?php endforeach; ?>
  </div>
<?php endif; ?>

<?php $action = $doc ? base_url('admin/documents/' . $doc['id'] . '/update') : base_url('admin/documents'); ?>
<form method="post" action="<?= $action ?>" enctype="multipart/form-data">
  <?= csrf_field() ?>

  <div class="row g-4">
    <div class="col-lg-8">
      <div class="card-stt p-4 mb-4">
        <h6 class="fw-semibold mb-3 border-bottom pb-2">Informasi Dokumen</h6>

        <div class="mb-3">
          <label class="form-label small fw-semibold">Judul <span class="text-danger">*</span></label>
          <input type="text" name="title" class="form-control"
                 value="<?= old('title', $doc['title'] ?? '') ?>" required maxlength="255">
        </div>

        <div class="row g-3 mb-3">
          <div class="col-md-6">
            <label class="form-label small fw-semibold">Nama Penulis <span class="text-danger">*</span></label>
            <input type="text" name="author_name" class="form-control" required maxlength="255"
                   value="<?= old('author_name', $doc['author_name'] ?? '') ?>">
          </div>
          <div class="col-md-6">
            <label class="form-label small fw-semibold">NIM Penulis</label>
            <input type="text" name="nim" class="form-control" maxlength="30"
                   value="<?= old('nim', $doc['nim'] ?? '') ?>">
          </div>
        </div>

        <div class="row g-3 mb-3">
          <div class="col-md-5">
            <label class="form-label small fw-semibold">Jenis Dokumen <span class="text-danger">*</span></label>
            <select name="document_type_id" class="form-select" required>
              <option value="">-- Pilih --</option>
              <?php foreach ($types as $t): ?>
                <option value="<?= $t['id'] ?>"
                  <?= old('document_type_id', $doc['document_type_id'] ?? '') == $t['id'] ? 'selected' : '' ?>>
                  <?= esc($t['name']) ?>
                </option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-4">
            <label class="form-label small fw-semibold">Program Studi</label>
            <select name="study_program_id" class="form-select">
              <option value="">-- Pilih --</option>
              <?php foreach ($programs as $p): ?>
                <option value="<?= $p['id'] ?>"
                  <?= old('study_program_id', $doc['study_program_id'] ?? '') == $p['id'] ? 'selected' : '' ?>>
                  <?= esc($p['name']) ?>
                </option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-3">
            <label class="form-label small fw-semibold">Tahun <span class="text-danger">*</span></label>
            <input type="number" name="year" class="form-control" min="1900" max="<?= date('Y') ?>" required
                   value="<?= old('year', $doc['year'] ?? date('Y')) ?>">
          </div>
        </div>

        <div class="row g-3 mb-3">
          <div class="col-md-6">
            <label class="form-label small fw-semibold">Nama Pembimbing</label>
            <input type="text" name="supervisor" class="form-control" maxlength="255"
                   value="<?= old('supervisor', $doc['supervisor'] ?? '') ?>">
          </div>
          <div class="col-md-6">
            <label class="form-label small fw-semibold">Penerbit</label>
            <input type="text" name="publisher" class="form-control" maxlength="255"
                   value="<?= old('publisher', $doc['publisher'] ?? '') ?>">
          </div>
        </div>

        <div class="mb-3">
          <label class="form-label small fw-semibold">Kata Kunci</label>
          <input type="text" name="keywords" class="form-control" maxlength="500"
                 value="<?= old('keywords', $doc['keywords'] ?? '') ?>"
                 placeholder="Pisahkan dengan koma">
        </div>

        <div class="mb-3">
          <label class="form-label small fw-semibold">Abstrak</label>
          <textarea name="abstract" class="form-control" rows="6"><?= old('abstract', $doc['abstract'] ?? '') ?></textarea>
        </div>

        <div class="mb-0">
          <label class="form-label small fw-semibold">Status</label>
          <select name="status" class="form-select form-select-sm w-auto">
            <option value="draft"      <?= old('status', $doc['status'] ?? 'draft') === 'draft'     ? 'selected' : '' ?>>Draft</option>
            <option value="published"  <?= old('status', $doc['status'] ?? '') === 'published'       ? 'selected' : '' ?>>Terbitkan Langsung</option>
          </select>
        </div>
      </div>
    </div>

    <div class="col-lg-4">
      <div class="card-stt p-4 mb-3">
        <h6 class="fw-semibold mb-3 border-bottom pb-2">File PDF <?= $doc ? '' : '<span class="text-danger">*</span>' ?></h6>
        <?php if ($doc): ?>
          <p class="small text-muted mb-2"><i class="bi bi-file-earmark-pdf me-1 text-danger"></i>File sudah ada. Unggah ulang untuk mengganti.</p>
        <?php endif; ?>
        <input type="file" name="file" class="form-control form-control-sm" accept=".pdf"
               <?= $doc ? '' : 'required' ?>>
        <div class="form-text">Maks. 50 MB · Hanya PDF</div>
      </div>

      <div class="card-stt p-4 mb-3">
        <h6 class="fw-semibold mb-3 border-bottom pb-2">Gambar Sampul</h6>
        <?php if (!empty($doc['cover_img'])): ?>
          <img src="<?= base_url('covers/' . esc($doc['cover_img'])) ?>"
               class="img-fluid rounded mb-2" style="max-height:120px;">
        <?php endif; ?>
        <input type="file" name="cover_img" class="form-control form-control-sm"
               accept=".jpg,.jpeg,.png,.gif,.webp">
        <div class="form-text">Maks. 2 MB · JPG, PNG, WebP</div>
      </div>

      <div class="d-grid">
        <button type="submit" class="btn btn-primary">
          <i class="bi bi-save me-2"></i><?= $doc ? 'Simpan Perubahan' : 'Tambah Dokumen' ?>
        </button>
      </div>
    </div>
  </div>
</form>

<?= $this->endSection() ?>
