<?php

namespace App\Http\Controllers;

use App\Models\Psikiater;
use Illuminate\Http\Request;

class PsikiaterController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['psikiater'] = Psikiater::all();
        return view('psikiater.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validate = $request->validate([
            'tanggal' => 'required|date',
            'id_siswa' => 'required|exists:siswas,id_siswa',
            'diagnosis_awal' => 'required|max:255',
            'penanganan' => 'required|max:255',
            'resep_obat' => 'required|max:255',
        ]);

        // dd($validate);

        Psikiater::create([
            'tanggal' => $validate['tanggal'],
            'id_siswa' => $validate['id_siswa'],
            'diagnosis_awal' => $validate['diagnosis_awal'],
            'penanganan' => $validate['penanganan'],
            'resep_obat' => $validate['resep_obat'],
            'status' => 0,
        ]);

        $notification = [
            'message' => 'Data penanganan berhasil ditambah!',
            'alert-type' => 'success'
        ];

        return redirect()->route('psikiater.index')->with($notification);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id_psikiater)
    {
        $psikiater = Psikiater::findOrFail($id_psikiater);

        $validate = $request->validate([
            'tanggal' => 'required|date',
            'id_siswa' => 'required|exists:siswas,id_siswa',
            'diagnosis_awal' => 'required|max:255',
            'penanganan' => 'required|max:255',
            'resep_obat' => 'required|max:255',
        ]);

        $psikiater->update([
            'tanggal' => $validate['tanggal'],
            'id_siswa' => $validate['id_siswa'],
            'diagnosis_awal' => $validate['diagnosis_awal'],
            'penanganan' => $validate['penanganan'],
            'resep_obat' => $validate['resep_obat'],
            'status' => 0,
        ]);

        $notification = array(
            'message' => 'Data penanganan berhasil diubah!',
            'alert-type' => 'success'
        );

        return redirect()->route('psikiater.index')->with($notification);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id_psikiater)
    {
        $psikiater = Psikiater::find($id_psikiater);

        $psikiater->delete();

        $notification = array(
            'message' => 'Data psikiater berhasil dihapus!',
            'alert-type' => 'success'
        );

        return redirect()->route('psikiater.index')->with($notification);
    }

    public function verify($id_psikiater)
    {
        $psikiater = Psikiater::find($id_psikiater);

        $psikiater->status = 1;
        $psikiater->save();

        $notification = [
            'message' => "Data psikiater berhasil diverifikasi!",
            'alert-type' => 'success'
        ];

        return redirect()->route('psikiater.index')->with($notification);
    }

    public function unverify($id_psikiater)
    {
        $psikiater = Psikiater::find($id_psikiater);

        $psikiater->status = 0;
        $psikiater->save();

        $notification = [
            'message' => "Data psikiater berhasil diunverifikasi!",
            'alert-type' => 'success'
        ];

        return redirect()->route('psikiater.index')->with($notification);
    }
}
