<?php

namespace App\Http\Controllers\Admin;

use App\Dokuman;
use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyDokumanRequest;
use App\Http\Requests\StoreDokumanRequest;
use App\Http\Requests\UpdateDokumanRequest;
use App\IdentitasKlien;

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

        $dokumen = Dokuman::all();

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

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

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

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

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

        $dokuman = Dokuman::create($request->all());

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

    public function edit(Dokuman $dokuman)
    {
        abort_unless(\Gate::allows('dokuman_edit'), 403);

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

        $dokuman->load('nama');

        return view('admin.dokumen.edit', compact('namas', 'dokuman'));
    }

    public function update(UpdateDokumanRequest $request, Dokuman $dokuman)
    {
        abort_unless(\Gate::allows('dokuman_edit'), 403);

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

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

    public function show(Dokuman $dokuman)
    {
        abort_unless(\Gate::allows('dokuman_show'), 403);

        $dokuman->load('nama');

        return view('admin.dokumen.show', compact('dokuman'));
    }

    public function destroy(Dokuman $dokuman)
    {
        abort_unless(\Gate::allows('dokuman_delete'), 403);

        $dokuman->delete();

        return back();
    }

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

        return response(null, 204);
    }
}
