<?php

namespace App\Http\Controllers\Admin;

use App\OtherOption;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreOtherOptionsRequest;
use App\Http\Requests\Admin\UpdateOtherOptionsRequest;

class OtherOptionsController extends Controller
{
    /**
     * Display a listing of OtherOption.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (! Gate::allows('other_option_access')) {
            return abort(401);
        }


        if (request('show_deleted') == 1) {
            if (! Gate::allows('other_option_delete')) {
                return abort(401);
            }
            $other_options = OtherOption::onlyTrashed()->get();
        } else {
            $other_options = OtherOption::all();
        }

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

    /**
     * Show the form for creating new OtherOption.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if (! Gate::allows('other_option_create')) {
            return abort(401);
        }
        return view('admin.other_options.create');
    }

    /**
     * Store a newly created OtherOption in storage.
     *
     * @param  \App\Http\Requests\StoreOtherOptionsRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreOtherOptionsRequest $request)
    {
        if (! Gate::allows('other_option_create')) {
            return abort(401);
        }
        $other_option = OtherOption::create($request->all());



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


    /**
     * Show the form for editing OtherOption.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if (! Gate::allows('other_option_edit')) {
            return abort(401);
        }
        $other_option = OtherOption::findOrFail($id);

        return view('admin.other_options.edit', compact('other_option'));
    }

    /**
     * Update OtherOption in storage.
     *
     * @param  \App\Http\Requests\UpdateOtherOptionsRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateOtherOptionsRequest $request, $id)
    {
        if (! Gate::allows('other_option_edit')) {
            return abort(401);
        }
        $other_option = OtherOption::findOrFail($id);
        $other_option->update($request->all());



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


    /**
     * Display OtherOption.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        if (! Gate::allows('other_option_view')) {
            return abort(401);
        }
        $residential_complexes = \App\ResidentialComplex::whereHas('other_option',
                    function ($query) use ($id) {
                        $query->where('id', $id);
                    })->get();

        $other_option = OtherOption::findOrFail($id);

        return view('admin.other_options.show', compact('other_option', 'residential_complexes'));
    }


    /**
     * Remove OtherOption from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if (! Gate::allows('other_option_delete')) {
            return abort(401);
        }
        $other_option = OtherOption::findOrFail($id);
        $other_option->delete();

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

    /**
     * Delete all selected OtherOption at once.
     *
     * @param Request $request
     */
    public function massDestroy(Request $request)
    {
        if (! Gate::allows('other_option_delete')) {
            return abort(401);
        }
        if ($request->input('ids')) {
            $entries = OtherOption::whereIn('id', $request->input('ids'))->get();

            foreach ($entries as $entry) {
                $entry->delete();
            }
        }
    }


    /**
     * Restore OtherOption from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function restore($id)
    {
        if (! Gate::allows('other_option_delete')) {
            return abort(401);
        }
        $other_option = OtherOption::onlyTrashed()->findOrFail($id);
        $other_option->restore();

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

    /**
     * Permanently delete OtherOption from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function perma_del($id)
    {
        if (! Gate::allows('other_option_delete')) {
            return abort(401);
        }
        $other_option = OtherOption::onlyTrashed()->findOrFail($id);
        $other_option->forceDelete();

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