<?php

namespace App\Http\Controllers\MasterData;

use App\Http\Controllers\Controller;
use App\Models\Indicator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class IndicatorController extends Controller
{
    public function index()
    {
        $indicators = Indicator::all();
        return view('master-data.indikator.index', compact('indicators'));
    }

    public function store(Request $request)
    {
        $customMessage = [
            "name.required" => "Nama wajib diisi",
            "name.max" => "Nama maksimal 25 karakter",
            "name.string" => "Nama harus berupa string",

            "description.required" => "Deskripsi wajib diisi",
            "description.max" => "Deskripsi maksimal 50 karakter",
            "description.string" => "Deskripsi harus berupa string",
        ];

        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:25',
            'description' => 'required|string|max:50',
        ], $customMessage);

        if ($validator->fails()) {
            toast($validator->messages()->all()[0], 'error')->position('top-right')->autoclose(3000);
            return redirect()->back()->withInput();
        }

        $indicator = new Indicator();
        $indicator->name = $request->name;
        $indicator->description = $request->description;

        try {
            $indicator->save();
            toast('Data berhasil disimpan', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }

    public function update(Request $request, $id)
    {
        $customMessage = [
            "name.required" => "Nama wajib diisi",
            "name.max" => "Nama maksimal 25 karakter",
            "name.string" => "Nama harus berupa string",

            "description.required" => "Deskripsi wajib diisi",
            "description.max" => "Deskripsi maksimal 50 karakter",
            "description.string" => "Deskripsi harus berupa string",
        ];

        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:25',
            'description' => 'required|string|max:50',
        ], $customMessage);

        if ($validator->fails()) {
            toast($validator->messages()->all()[0], 'error')->position('top')->autoclose(3000);
            return redirect()->back()->withInput();
        }

        $indicator = Indicator::find($id);
        $indicator->name = $request->name;
        $indicator->description = $request->description;

        try {
            $indicator->save();
            toast('Data berhasil diubah', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }

    public function destroy($id)
    {
        $indicator = Indicator::find($id);
        try {
            $indicator->delete();
            toast('Data berhasil dihapus', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }
}
