<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyPengawasanRequest;
use App\Http\Requests\StorePengawasanRequest;
use App\Http\Requests\UpdatePengawasanRequest;
use App\IdentitasKlien;
use App\Pengawasan;

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

        $pengawasans = Pengawasan::all();

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

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

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

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

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

        $pengawasan = Pengawasan::create($request->all());

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

    public function edit(Pengawasan $pengawasan)
    {
        abort_unless(\Gate::allows('pengawasan_edit'), 403);

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

        $pengawasan->load('nama');

        return view('admin.pengawasans.edit', compact('namas', 'pengawasan'));
    }

    public function update(UpdatePengawasanRequest $request, Pengawasan $pengawasan)
    {
        abort_unless(\Gate::allows('pengawasan_edit'), 403);

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

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

    public function show(Pengawasan $pengawasan)
    {
        abort_unless(\Gate::allows('pengawasan_show'), 403);

        $pengawasan->load('nama');

        return view('admin.pengawasans.show', compact('pengawasan'));
    }

    public function destroy(Pengawasan $pengawasan)
    {
        abort_unless(\Gate::allows('pengawasan_delete'), 403);

        $pengawasan->delete();

        return back();
    }

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

        return response(null, 204);
    }
}
