@extends('layouts.admin')
@section('title', $article->exists ? 'Edit Artikel KB' : 'Buat Artikel KB')
@section('content')
<div class="container">
  <h4>{{ $article->exists ? 'Edit Knowledge Base' : 'Buat Knowledge Base Baru' }}</h4>
  @php $kbKey = $article->exists ? ($article->getRouteKey() ?: $article->getKey()) : null; @endphp
  <form method="POST" action="{{ $article->exists ? route('admin.knowledgebase.update', $kbKey) : route('admin.knowledgebase.store') }}" enctype="multipart/form-data">
    @csrf
    @if($article->exists)
      @method('PUT')
    @endif

    <div class="mb-3">
      <label class="form-label">Judul</label>
      <input type="text" name="title" class="form-control" value="{{ old('title', $article->title) }}" required>
    </div>

    <div class="mb-3">
      <label class="form-label">Ringkasan</label>
      <textarea name="summary" class="form-control" rows="2">{{ old('summary', $article->summary) }}</textarea>
    </div>

    <div class="mb-3">
      <label class="form-label">Tipe Artikel</label>
      <select name="type" class="form-select" required onchange="document.getElementById('kbTypeHint').innerText = this.value;">
        @foreach(['text'=>'Teks','image'=>'Gambar','video'=>'Video'] as $key => $label)
          <option value="{{ $key }}" {{ old('type', $article->type) === $key ? 'selected' : '' }}>{{ $label }}</option>
        @endforeach
      </select>
      <div id="kbTypeHint" class="form-text">{{ ucfirst(old('type', $article->type ?? 'text')) }} content.</div>
    </div>

    <div class="mb-3">
      <label class="form-label">Konten Teks</label>
      <textarea name="content" class="form-control" rows="6">{{ old('content', $article->content) }}</textarea>
      <div class="form-text">Hanya diperlukan untuk tipe <strong>text</strong> atau untuk deskripsi tambahan.</div>
    </div>

    <div class="mb-3">
      <label class="form-label">Unggah Gambar</label>
      <input type="file" name="image" class="form-control">
      @if($article->type === 'image' && $article->resource_path)
        <div class="mt-2"><img src="{{ asset('storage/'.$article->resource_path) }}" class="img-fluid rounded" style="max-height:180px"></div>
      @endif
      <div class="form-text">Untuk tipe <strong>image</strong>, unggah gambar referensi.</div>
    </div>

    <div class="mb-3">
      <label class="form-label">URL Video</label>
      <input type="url" name="video_url" class="form-control" value="{{ old('video_url', $article->video_url) }}">
      <div class="form-text">Untuk tipe <strong>video</strong>, masukkan URL YouTube atau video eksternal.</div>
    </div>

    <div class="form-check mb-4">
      <input type="checkbox" name="published" value="1" class="form-check-input" id="published" {{ old('published', $article->published) ? 'checked' : '' }}>
      <label class="form-check-label" for="published">Published</label>
    </div>

    <button class="btn btn-primary">Simpan</button>
    <a href="{{ route('admin.knowledgebase.index') }}" class="btn btn-secondary">Batal</a>
  </form>
</div>
@endsection
