<?php

namespace App\Http\Controllers;

use App\Models\Jadwal;
use Illuminate\Http\Request;
use Carbon\Carbon;

class JadwalController extends Controller
{
    public function index()
{
    // Ambil semua jadwal dari database
    $jadwals = Jadwal::all();

    // Kirim data jadwals ke tampilan
    return view('jadwal.index', compact('jadwals'));
}

    public function create()
    {
        return view('jadwal.create');
    }

    public function store(Request $request)
{
    // Validasi data
    $request->validate([
        'jumat' => 'required|string',
        'khotib' => 'required|string',
        'bilal' => 'required|string',
        'muadzin' => 'required|string',
    ]);

    // Simpan data jadwal
    Jadwal::create([
        'jumat' => $request->jumat,
        'khotib' => $request->khotib,
        'bilal' => $request->bilal,
        'muadzin' => $request->muadzin,
    ]);

    // Redirect ke halaman jadwal dan menampilkan pesan sukses
    return redirect()->route('jadwal.index')->with('success', 'Jadwal berhasil ditambahkan!');
}

    public function edit($id)
    {
        $jadwal = Jadwal::findOrFail($id);
        return view('jadwal.edit', compact('jadwal'));
    }

    public function update(Request $request, $id)
{
    $request->validate([
        'jumat' => 'required|string',
        'khotib' => 'required|string',
        'bilal' => 'required|string',
        'muadzin' => 'required|string',
    ]);

    $jadwal = Jadwal::findOrFail($id);
    $jadwal->update($request->all());

    return redirect()->route('jadwal.index')->with('success', 'Jadwal berhasil diperbarui!');
}

    public function destroy($id)
    {
        $jadwal = Jadwal::findOrFail($id);
        $jadwal->delete();

        return redirect()->route('jadwal.index');
    }
}
