<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyPengakhiranRequest;
use App\Http\Requests\StorePengakhiranRequest;
use App\Http\Requests\UpdatePengakhiranRequest;
use App\IdentitasKlien;
use App\Pengakhiran;

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

        $pengakhirans = Pengakhiran::all();

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

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

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

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

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

        $pengakhiran = Pengakhiran::create($request->all());

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

    public function edit(Pengakhiran $pengakhiran)
    {
        abort_unless(\Gate::allows('pengakhiran_edit'), 403);

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

        $pengakhiran->load('nama');

        return view('admin.pengakhirans.edit', compact('namas', 'pengakhiran'));
    }

    public function update(UpdatePengakhiranRequest $request, Pengakhiran $pengakhiran)
    {
        abort_unless(\Gate::allows('pengakhiran_edit'), 403);

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

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

    public function show(Pengakhiran $pengakhiran)
    {
        abort_unless(\Gate::allows('pengakhiran_show'), 403);

        $pengakhiran->load('nama');

        return view('admin.pengakhirans.show', compact('pengakhiran'));
    }

    public function destroy(Pengakhiran $pengakhiran)
    {
        abort_unless(\Gate::allows('pengakhiran_delete'), 403);

        $pengakhiran->delete();

        return back();
    }

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

        return response(null, 204);
    }
}
