<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyPenilaianakRequest;
use App\Http\Requests\StorePenilaianakRequest;
use App\Http\Requests\UpdatePenilaianakRequest;
use App\IdentitasKlien;
use App\Penilaianak;

class PenilaianakController extends Controller
{
    public function index()
    {
        abort_unless(\Gate::allows('penilaianak_access'), 403);

        $penilaianaks = Penilaianak::all();

        return view('admin.penilaianaks.index', compact('penilaianaks'));
    }

    public function create()
    {
        abort_unless(\Gate::allows('penilaianak_create'), 403);

        $namas = IdentitasKlien::all()->pluck('nama', 'id')->prepend(trans('global.pleaseSelect'), '');

        return view('admin.penilaianaks.create', compact('namas'));
    }

    public function store(StorePenilaianakRequest $request)
    {
        abort_unless(\Gate::allows('penilaianak_create'), 403);

        $penilaianak = Penilaianak::create($request->all());

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

    public function edit(Penilaianak $penilaianak)
    {
        abort_unless(\Gate::allows('penilaianak_edit'), 403);

        $namas = IdentitasKlien::all()->pluck('nama', 'id')->prepend(trans('global.pleaseSelect'), '');

        $penilaianak->load('nama');

        return view('admin.penilaianaks.edit', compact('namas', 'penilaianak'));
    }

    public function update(UpdatePenilaianakRequest $request, Penilaianak $penilaianak)
    {
        abort_unless(\Gate::allows('penilaianak_edit'), 403);

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

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

    public function show(Penilaianak $penilaianak)
    {
        abort_unless(\Gate::allows('penilaianak_show'), 403);

        $penilaianak->load('nama');

        return view('admin.penilaianaks.show', compact('penilaianak'));
    }

    public function destroy(Penilaianak $penilaianak)
    {
        abort_unless(\Gate::allows('penilaianak_delete'), 403);

        $penilaianak->delete();

        return back();
    }

    public function massDestroy(MassDestroyPenilaianakRequest $request)
    {
        Penilaianak::whereIn('id', request('ids'))->delete();

        return response(null, 204);
    }
}
