<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyPelimpahanRequest;
use App\Http\Requests\StorePelimpahanRequest;
use App\Http\Requests\UpdatePelimpahanRequest;
use App\IdentitasKlien;
use App\Pelimpahan;

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

        $pelimpahans = Pelimpahan::all();

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

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

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

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

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

        $pelimpahan = Pelimpahan::create($request->all());

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

    public function edit(Pelimpahan $pelimpahan)
    {
        abort_unless(\Gate::allows('pelimpahan_edit'), 403);

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

        $pelimpahan->load('nama');

        return view('admin.pelimpahans.edit', compact('namas', 'pelimpahan'));
    }

    public function update(UpdatePelimpahanRequest $request, Pelimpahan $pelimpahan)
    {
        abort_unless(\Gate::allows('pelimpahan_edit'), 403);

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

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

    public function show(Pelimpahan $pelimpahan)
    {
        abort_unless(\Gate::allows('pelimpahan_show'), 403);

        $pelimpahan->load('nama');

        return view('admin.pelimpahans.show', compact('pelimpahan'));
    }

    public function destroy(Pelimpahan $pelimpahan)
    {
        abort_unless(\Gate::allows('pelimpahan_delete'), 403);

        $pelimpahan->delete();

        return back();
    }

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

        return response(null, 204);
    }
}
