@extends('layouts.admin')
@section('title','Knowledge Base')
@section('content')
<div class="container">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <div>
      <h4 class="mb-0">Knowledge Base</h4>
      <p class="text-muted small mb-0">Dokumentasi teknis untuk teknisi: text, gambar, dan video.</p>
    </div>
    @if(auth()->user()->isAdmin())
      <a href="{{ route('admin.knowledgebase.create') }}" class="btn btn-primary">Buat Artikel Baru</a>
    @endif
  </div>

  <form class="row g-2 mb-4" method="GET">
    <div class="col-md-6">
      <input type="text" name="q" class="form-control" placeholder="Cari judul atau ringkasan..." value="{{ $search ?? '' }}">
    </div>
    <div class="col-auto">
      <button class="btn btn-outline-primary">Cari</button>
    </div>
  </form>

  @if($articles->isEmpty())
    <div class="alert alert-info">Tidak ada artikel Knowledge Base.</div>
  @else
    <div class="row gy-3">
      @foreach($articles as $article)
        <div class="col-12">
          <div class="card p-3">
            <div class="d-flex flex-column flex-md-row justify-content-between gap-3">
              <div>
                @php $kbKey = $article->getRouteKey() ?: $article->getKey(); @endphp
                <h5 class="mb-1"><a href="{{ route('admin.knowledgebase.show', $kbKey) }}">{{ $article->title }}</a></h5>
                <div class="text-muted small">{{ $article->summary ?: \Illuminate\Support\Str::limit(strip_tags($article->content), 120) }}</div>
                <div class="mt-2">
                  <span class="badge bg-secondary">{{ ucfirst($article->type) }}</span>
                  @if($article->published)
                    <span class="badge bg-success">Published</span>
                  @else
                    <span class="badge bg-warning text-dark">Draft</span>
                  @endif
                </div>
              </div>
              <div class="text-end">
                @if(auth()->user()->isAdmin())
                  @php $kbKey = $article->getRouteKey() ?: $article->getKey(); @endphp
                  <a href="{{ route('admin.knowledgebase.edit', $kbKey) }}" class="btn btn-sm btn-outline-primary">Edit</a>
                  <form action="{{ route('admin.knowledgebase.destroy', $kbKey) }}" method="POST" class="d-inline-block" onsubmit="return confirm('Hapus artikel ini?');">
                    @csrf
                    @method('DELETE')
                    <button class="btn btn-sm btn-outline-danger">Hapus</button>
                  </form>
                @else
                  <a href="{{ route('admin.knowledgebase.show', $article) }}" class="btn btn-sm btn-outline-secondary">Lihat</a>
                @endif
              </div>
            </div>
          </div>
        </div>
      @endforeach
    </div>
    <div class="mt-4">{{ $articles->links() }}</div>
  @endif
</div>
@endsection
