@extends('layouts.admin')
@section('title', $post->exists ? 'Edit Artikel' : 'Tambah Artikel')
@section('page-title', $post->exists ? 'Edit Artikel' : 'Tambah Berita / Pengumuman')

@section('content')
<div class="max-w-3xl">
    <form method="POST" action="{{ $post->exists ? route('admin.posts.update', $post->id) : route('admin.posts.store') }}" enctype="multipart/form-data" class="space-y-5">
        @csrf
        @if($post->exists) @method('PUT') @endif

        <div class="card-admin grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
                <label class="form-label">Tipe *</label>
                <select name="tipe" class="form-select" required>
                    <option value="berita" {{ old('tipe', $post->tipe ?? 'berita') === 'berita' ? 'selected' : '' }}>Berita</option>
                    <option value="pengumuman" {{ old('tipe', $post->tipe) === 'pengumuman' ? 'selected' : '' }}>Pengumuman</option>
                </select>
            </div>
            <div>
                <label class="form-label">Tanggal *</label>
                <input type="date" name="tanggal" value="{{ old('tanggal', $post->exists ? $post->tanggal->format('Y-m-d') : date('Y-m-d')) }}" class="form-input" required>
            </div>
            <div class="md:col-span-2">
                <label class="form-label">Judul *</label>
                <input name="judul" value="{{ old('judul', $post->judul) }}" class="form-input" required placeholder="Judul berita atau pengumuman">
            </div>
            <div>
                <label class="form-label">Penulis</label>
                <input name="penulis" value="{{ old('penulis', $post->penulis) }}" class="form-input" placeholder="Admin Desa Suka Baru">
            </div>
            <div>
                <label class="form-label">Foto Sampul</label>
                @if($post->exists && $post->foto_url)
                    <img src="{{ $post->foto_url }}" class="w-full h-24 object-cover rounded-lg mb-2">
                @endif
                <input type="file" name="foto" accept="image/*" class="form-input">
            </div>
        </div>

        <div class="card-admin">
            <label class="form-label">Isi Artikel *</label>
            <textarea name="isi" rows="14" class="form-textarea" required>{{ old('isi', $post->isi) }}</textarea>
        </div>

        @if($errors->any())
            <div class="bg-red-50 border border-red-200 rounded-lg p-3">
                @foreach($errors->all() as $e)<p class="text-red-600 text-sm">{{ $e }}</p>@endforeach
            </div>
        @endif

        <div class="flex gap-3">
            <button type="submit" class="btn-primary">💾 {{ $post->exists ? 'Perbarui' : 'Publikasikan' }}</button>
            <a href="{{ route('admin.posts.index') }}" class="btn-secondary">Batal</a>
        </div>
    </form>
</div>
@endsection
