<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\BeritaModel;
use App\Models\KategoriModel;
use Illuminate\Support\Str;

class BeritaController extends Controller
{

    public function index()
    {
        $data = BeritaModel::allBerita();
        return view('dashboard.berita.berita', ['data' => $data]);
    }


    public function edit(Request $request, $id)
    {
        $data = BeritaModel::find($id);
        if ($request->isMethod('get')) {
            $kategori = KategoriModel::allKategori();
            return view('dashboard.berita.edit', ['data' => $data, 'kategori' => $kategori]);
        } elseif ($request->isMethod('post')) {
            $judul = $request->input('judul');
            $tanggal = date('Y-m-d', strtotime($request->input('tanggal')));
            $gambar = $request->file('gambar');
            $kategori = $request->input('kategori');
            $status = $request->input('status');
            $isi = $request->input('isi');

            $data_raw = [
                'id_user' => $request->session()->get('userID'),
                'judul_berita' => $judul,
                'slug_berita' => Str::slug($judul),
                'tanggal_post' => $tanggal,
                'id_kategori' => $kategori,
                'status_berita' => $status,
                'isi_berita' => $isi,
            ];

            if ($gambar != null) {
                if ($data->gambar != null && file_exists(storage_path('app/public/berita/' . $data->gambar))) {
                    unlink(storage_path('app/public/berita/' . $data->gambar));
                }
                $destinationPath = storage_path('app/public/berita');
                $filename = time() . '.' . $request->foto->extension();
                $gambar->move($destinationPath, $filename);
                $data_raw['gambar'] = $filename;
            }

            $result = BeritaModel::where('id_berita', $id)->update($data_raw);

            if ($result) {
                return redirect()->route('berita')->with('success', 'Data berhasil di update');
            } else {
                return back()->with('error', 'Data gagal di update');
            }
        }
    }


    public function tambah(Request $request)
    {
        if ($request->isMethod('get')) {
            $kategori = KategoriModel::allKategori();
            return view('dashboard.berita.tambah', compact('kategori'));
        } elseif ($request->isMethod('post')) {
            $judul = $request->input('judul');
            $tanggal = date('Y-m-d', strtotime($request->input('tanggal')));
            $gambar = $request->file('gambar');
            $kategori = $request->input('kategori');
            $status = $request->input('status');
            $isi = $request->input('isi');

            $data_raw = [
                'id_user' => $request->session()->get('userID'),
                'judul_berita' => $judul,
                'slug_berita' => Str::slug($judul),
                'tanggal_post' => $tanggal,
                'id_kategori' => $kategori,
                'status_berita' => $status,
                'isi_berita' => $isi,
            ];

            if ($gambar != null) {
                $destinationPath = storage_path('app/public/berita');
                $filename = time() . '.' . $request->gambar->extension();
                $gambar->move($destinationPath, $filename);
                $data_raw['gambar'] = $filename;
            }

            $result = BeritaModel::insert($data_raw);

            if ($result) {
                return redirect()->route('berita')->with('success', 'Data berhasil di tambahkan');
            } else {
                return back()->with('error', 'Data gagal di tambahkan');
            }
        }
    }

    public function hapus(Request $request)
    {
        $id = $request->input('id_berita');
        $data = BeritaModel::find($id);

        if ($data->gambar != null && file_exists(storage_path('app/public/berita/' . $data->gambar))) {
            unlink(storage_path('app/public/berita/' . $data->gambar));
        }

        $result = BeritaModel::where('id_berita', $id)->delete();

        if ($result) {
            return redirect()->route('berita')->with('success', 'Data berhasil di hapus');
        } else {
            return back()->with('error', 'Data gagal di hapus');
        }
    }
}
