<?php

namespace App\Http\Controllers\User;

use Alert;
use App\Http\Controllers\Controller;
use App\Repositories\User\FasilitasRepository;
use Crypt;
use Illuminate\Http\Request;

class FasilitasController extends Controller
{
    private $fasilitasRepo;

    public function __construct(FasilitasRepository $fasilitasRepo)
    {
        $this->fasilitasRepo = $fasilitasRepo;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $menu = 'fasilitas';
        $fasilitas = $this->fasilitasRepo->get();

        $data = [
            'fasilitas' => $fasilitas,
            'menu' => $menu
        ];

        return view('user.fasilitas.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('user.fasilitas.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $data = $request->except('_token');
        $data['user_id'] = auth()->user()->id;
        $fasilitas = $this->fasilitasRepo->store($data);

        $data = [
            'fasilitas' => $fasilitas->id,
        ];

        Alert::success('Berhasil', 'Fasilitas berhasil disimpan');

        return redirect()->route('fasilitas.index', $data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $menu = 'fasilitas';
        $fasilitas = $this->fasilitasRepo->findId(null, $id);

        $data = [

            'menu' => $menu,
            'fasilitas' => $fasilitas,
        ];

        return view('user.fasilitas.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->except('_token');
        $data['userid_updated'] = auth()->user()->id;
        $fasilitas = $this->fasilitasRepo->findId(null, $id);
        $this->fasilitasRepo->update($data, $fasilitas);

        $data = [
            'fasilitas' => $fasilitas->id,
        ];

        Alert::success('Berhasil', 'Fasilitas berhasil diupdated');

        return redirect()->route('fasilitas.index', $data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $model = $this->fasilitasRepo->findId(null, Crypt::decrypt($id));
        $this->fasilitasRepo->destroy($model);

        Alert::success('Data berhasil dihapus')->persistent('Ok');

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