<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyPendampinganRequest;
use App\Http\Requests\StorePendampinganRequest;
use App\Http\Requests\UpdatePendampinganRequest;
use App\IdentitasKlien;
use App\Pendampingan;

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

        $pendampingans = Pendampingan::all();

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

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

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

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

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

        $pendampingan = Pendampingan::create($request->all());

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

    public function edit(Pendampingan $pendampingan)
    {
        abort_unless(\Gate::allows('pendampingan_edit'), 403);

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

        $pendampingan->load('nama');

        return view('admin.pendampingans.edit', compact('namas', 'pendampingan'));
    }

    public function update(UpdatePendampinganRequest $request, Pendampingan $pendampingan)
    {
        abort_unless(\Gate::allows('pendampingan_edit'), 403);

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

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

    public function show(Pendampingan $pendampingan)
    {
        abort_unless(\Gate::allows('pendampingan_show'), 403);

        $pendampingan->load('nama');

        return view('admin.pendampingans.show', compact('pendampingan'));
    }

    public function destroy(Pendampingan $pendampingan)
    {
        abort_unless(\Gate::allows('pendampingan_delete'), 403);

        $pendampingan->delete();

        return back();
    }

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

        return response(null, 204);
    }
}
