@extends('layouts.admin')
@section('title',$article->title)
@section('content')
<div class="container">
  <div class="d-flex justify-content-between align-items-start mb-3">
    <div>
      <h4>{{ $article->title }}</h4>
      <div class="text-muted small">{{ $article->summary }}</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>
      <a href="{{ route('admin.knowledgebase.index') }}" class="btn btn-outline-secondary btn-sm">Kembali</a>
    </div>
  </div>

  @if($article->type === 'image' && $article->resource_path)
    <div class="mb-4"><img src="{{ asset('storage/'.$article->resource_path) }}" class="img-fluid rounded"></div>
  @endif

  @if($article->type === 'video' && $article->video_url)
    @php
      $videoUrl = $article->video_url;
      $embedUrl = null;
      if (\Illuminate\Support\Str::contains($videoUrl, 'youtu.be')) {
          $embedUrl = 'https://www.youtube.com/embed/' . \Illuminate\Support\Str::afterLast($videoUrl, '/');
      } elseif (\Illuminate\Support\Str::contains($videoUrl, 'youtube.com')) {
          parse_str(parse_url($videoUrl, PHP_URL_QUERY), $query);
          if (!empty($query['v'])) {
              $embedUrl = 'https://www.youtube.com/embed/' . $query['v'];
          }
      }
    @endphp
    @if($embedUrl)
      <div class="ratio ratio-16x9 mb-4"><iframe src="{{ $embedUrl }}" title="Video Knowledge Base" frameborder="0" allowfullscreen></iframe></div>
    @else
      <div class="alert alert-secondary">Video URL: <a href="{{ $videoUrl }}" target="_blank">{{ $videoUrl }}</a></div>
    @endif
  @endif

  @if($article->content)
    <div class="card card-body mb-4">{!! nl2br(e($article->content)) !!}</div>
  @endif

  @if(auth()->user()->isAdmin())
    <div class="d-flex gap-2">
      @php $kbKey = $article->getRouteKey() ?: $article->getKey(); @endphp
      <a href="{{ route('admin.knowledgebase.edit', $kbKey) }}" class="btn btn-primary btn-sm">Edit</a>
      <form action="{{ route('admin.knowledgebase.destroy', $kbKey) }}" method="POST" onsubmit="return confirm('Hapus artikel ini?');">
        @csrf
        @method('DELETE')
        <button class="btn btn-danger btn-sm">Hapus</button>
      </form>
    </div>
  @endif
</div>
@endsection
