<?php

namespace App\Http\Controllers\User;

use SweetAlert;
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,
        ];

        SweetAlert::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)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 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);

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

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