<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyKunjunganRequest;
use App\Http\Requests\StoreKunjunganRequest;
use App\Http\Requests\UpdateKunjunganRequest;
use App\IdentitasKlien;
use App\Kunjungan;

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

        $kunjungans = Kunjungan::all();

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

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

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

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

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

        $kunjungan = Kunjungan::create($request->all());

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

    public function edit(Kunjungan $kunjungan)
    {
        abort_unless(\Gate::allows('kunjungan_edit'), 403);

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

        $kunjungan->load('nama');

        return view('admin.kunjungans.edit', compact('namas', 'kunjungan'));
    }

    public function update(UpdateKunjunganRequest $request, Kunjungan $kunjungan)
    {
        abort_unless(\Gate::allows('kunjungan_edit'), 403);

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

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

    public function show(Kunjungan $kunjungan)
    {
        abort_unless(\Gate::allows('kunjungan_show'), 403);

        $kunjungan->load('nama');

        return view('admin.kunjungans.show', compact('kunjungan'));
    }

    public function destroy(Kunjungan $kunjungan)
    {
        abort_unless(\Gate::allows('kunjungan_delete'), 403);

        $kunjungan->delete();

        return back();
    }

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

        return response(null, 204);
    }
}
