<?php

namespace App\Http\Controllers\Admin;

use App\Bimbingan;
use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyBimbinganRequest;
use App\Http\Requests\StoreBimbinganRequest;
use App\Http\Requests\UpdateBimbinganRequest;
use App\IdentitasKlien;

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

        $bimbingans = Bimbingan::all();

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

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

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

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

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

        $bimbingan = Bimbingan::create($request->all());

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

    public function edit(Bimbingan $bimbingan)
    {
        abort_unless(\Gate::allows('bimbingan_edit'), 403);

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

        $bimbingan->load('nama');

        return view('admin.bimbingans.edit', compact('namas', 'bimbingan'));
    }

    public function update(UpdateBimbinganRequest $request, Bimbingan $bimbingan)
    {
        abort_unless(\Gate::allows('bimbingan_edit'), 403);

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

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

    public function show(Bimbingan $bimbingan)
    {
        abort_unless(\Gate::allows('bimbingan_show'), 403);

        $bimbingan->load('nama');

        return view('admin.bimbingans.show', compact('bimbingan'));
    }

    public function destroy(Bimbingan $bimbingan)
    {
        abort_unless(\Gate::allows('bimbingan_delete'), 403);

        $bimbingan->delete();

        return back();
    }

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

        return response(null, 204);
    }
}
