@extends('admin.layout.main')

@section('title', 'PeGas - Admin Management Artikel')

@section('content')
    <h1 class="h3 mb-4 text-gray-800">Management Berita</h1>

    @if(session('success'))
        <div id="swal-success" data-message="{{ session('success') }}"></div>
    @endif
    @if($errors->any())
        <div id="swal-error" data-message="{{ $errors->first() }}"></div>
    @endif

    <p>Ini adalah halaman untuk mengelola Berita</p>

    <button class="btn btn-primary mb-3" data-toggle="modal" data-target="#modalTambah">
        <i class="fas fa-plus fa-sm"></i> Tambah Berita
    </button>

    <div class="card shadow mb-4">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered" id="tableArtikel" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th width="5%">No</th>
                            <th width="20%">Judul</th>
                            <th>Deskripsi</th>
                            <th width="15%">Gambar</th>
                            <th width="15%">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($artikels as $index => $item)
                            <tr>
                                <td>{{ $index + 1 }}</td>
                                <td>{{ $item->judul }}</td>
                                <td>{{ Str::limit($item->deskripsi, 100) }}</td>
                                <td>
                                    <!-- PERBAIKAN: Menghapus string 'storage/' dan membaca langsung dari public path database -->
                                    @if($item->gambar && file_exists(public_path($item->gambar)))
                                        <img src="{{ asset($item->gambar) }}" width="80" class="img-thumbnail" alt="Gambar Artikel">
                                    @else
                                        <span class="badge badge-secondary">No Image</span>
                                    @endif
                                </td>
                                <td>
                                    <button class="btn btn-warning btn-sm" data-toggle="modal" data-target="#edit{{ $item->id }}">
                                        <i class="fas fa-edit"></i> Edit
                                    </button>

                                    <form action="{{ route('admin.management-artikel.destroy', $item->id) }}" method="POST" class="d-inline delete-artikel-form">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-danger btn-sm">
                                            <i class="fas fa-trash"></i> Hapus
                                        </button>
                                    </form>
                                </td>
                            </tr>

                            <!-- Modal Edit Artikel -->
                            <div class="modal fade" id="edit{{ $item->id }}" tabindex="-1" role="dialog" aria-hidden="true">
                                <div class="modal-dialog" role="document">
                                    <form action="{{ route('admin.management-artikel.update', $item->id) }}" method="POST" enctype="multipart/form-data" class="form-edit-artikel">
                                        @csrf
                                        @method('PUT')
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title">Edit Artikel</h5>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <div class="modal-body">
                                                <div class="form-group mb-2">
                                                    <label>Judul</label>
                                                    <input type="text" name="judul" value="{{ $item->judul }}" class="form-control" required>
                                                </div>
                                                <div class="form-group mb-2">
                                                    <label>Gambar (Kosongkan jika tidak diganti)</label>
                                                    <input type="file" name="gambar" class="form-control">
                                                </div>
                                                <div class="form-group mb-2">
                                                    <label>Deskripsi</label>
                                                    <textarea name="deskripsi" class="form-control" rows="4" required>{{ $item->deskripsi }}</textarea>
                                                </div>
                                                <div class="form-group mb-2">
                                                    <label>Sumber</label>
                                                    <input type="text" name="sumber" value="{{ $item->sumber }}" class="form-control" required>
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                                                <button type="submit" class="btn btn-success">Update</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Modal Tambah Artikel -->
    <div class="modal fade" id="modalTambah" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <form id="form-tambah-artikel" action="{{ route('admin.management-artikel.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Tambah Artikel Baru</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group mb-2">
                            <label>Judul</label>
                            <input type="text" name="judul" class="form-control" required>
                        </div>
                        <div class="form-group mb-2">
                            <label>Gambar</label>
                            <input type="file" name="gambar" class="form-control" required>
                            <small class="text-muted d-block mt-1">Format: JPG, PNG, GIF | Ukuran Maks: 5MB</small>
                        </div>
                        <div class="form-group mb-2">
                            <label>Deskripsi</label>
                            <textarea name="deskripsi" class="form-control" rows="4" required></textarea>
                        </div>
                        <div class="form-group mb-2">
                            <label>Sumber</label>
                            <input type="text" name="sumber" class="form-control" required>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-primary">Simpan</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
@endsection