<?php

namespace App\Http\Controllers\Instansi;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\JamKerja;
use Illuminate\Support\Facades\Auth;

class JamKerjaController extends Controller
{
    public function index()
    {
        $jamKerjas = JamKerja::where('instansi_id', Auth::user()->instansi_id)->get();
        return view('instansi.jamkerja.index', compact('jamKerjas'));
    }

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

    public function store(Request $request)
    {
        $request->validate([
            'jam_masuk' => 'required',
            'jam_pulang' => 'required',
            'toleransi_menit' => 'required|integer'
        ]);

        JamKerja::create([
            'instansi_id' => Auth::user()->instansi_id,
            'jam_masuk' => $request->jam_masuk,
            'jam_pulang' => $request->jam_pulang,
            'toleransi_menit' => $request->toleransi_menit,
        ]);

        return redirect()->route('instansi.jam-kerja.index')
            ->with('success','Jam kerja berhasil ditambahkan');
    }

    public function edit(JamKerja $jam_kerja)
    {
        return view('instansi.jamkerja.edit', compact('jam_kerja'));
    }

    public function update(Request $request, JamKerja $jam_kerja)
    {
        $request->validate([
            'jam_masuk' => 'required',
            'jam_pulang' => 'required',
            'toleransi_menit' => 'required|integer'
        ]);

        $jam_kerja->update($request->all());

        return redirect()->route('instansi.jam-kerja.index')
            ->with('success','Jam kerja berhasil diupdate');
    }

    public function destroy(JamKerja $jam_kerja)
    {
        $jam_kerja->delete();
        return redirect()->route('instansi.jam-kerja.index')
            ->with('success','Jam kerja berhasil dihapus');
    }
}
