<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyIdentitasKlienRequest;
use App\Http\Requests\StoreIdentitasKlienRequest;
use App\Http\Requests\UpdateIdentitasKlienRequest;
use App\IdentitasKlien;
use App\Referensi;

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

        $identitasKliens = IdentitasKlien::all();

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

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

        $agamas = Referensi::all()->pluck('agama', 'id')->prepend(trans('global.pleaseSelect'), '');

        return view('admin.identitasKliens.create', compact('agamas'));
    }

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

        $identitasKlien = IdentitasKlien::create($request->all());

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

    public function edit(IdentitasKlien $identitasKlien)
    {
        abort_unless(\Gate::allows('identitas_klien_edit'), 403);

        $agamas = Referensi::all()->pluck('agama', 'id')->prepend(trans('global.pleaseSelect'), '');

        $identitasKlien->load('agama');

        return view('admin.identitasKliens.edit', compact('agamas', 'identitasKlien'));
    }

    public function update(UpdateIdentitasKlienRequest $request, IdentitasKlien $identitasKlien)
    {
        abort_unless(\Gate::allows('identitas_klien_edit'), 403);

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

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

    public function show(IdentitasKlien $identitasKlien)
    {
        abort_unless(\Gate::allows('identitas_klien_show'), 403);

        $identitasKlien->load('agama');

        return view('admin.identitasKliens.show', compact('identitasKlien'));
    }

    public function destroy(IdentitasKlien $identitasKlien)
    {
        abort_unless(\Gate::allows('identitas_klien_delete'), 403);

        $identitasKlien->delete();

        return back();
    }

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

        return response(null, 204);
    }
}
