<?php

namespace App\Http\Controllers\Admin;

use App\SectionType;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreSectionTypesRequest;
use App\Http\Requests\Admin\UpdateSectionTypesRequest;

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


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

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

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

        return view('admin.section_types.create');
    }

    /**
     * Store a newly created SectionType in storage.
     *
     * @param StoreSectionTypesRequest  $request
     * @return Response
     */
    public function store(StoreSectionTypesRequest $request)
    {
        if (! Gate::allows('section_type_create')) {
            return abort(401);
        }
        $section_type = SectionType::create($request->all());



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

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

        return view('admin.section_types.edit', compact('section_type'));
    }

    /**
     * Update SectionType in storage.
     *
     * @param UpdateSectionTypesRequest  $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateSectionTypesRequest $request, $id)
    {
        if (! Gate::allows('section_type_edit')) {
            return abort(401);
        }
        $section_type = SectionType::findOrFail($id);
        $section_type->update($request->all());



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


    /**
     * Display SectionType.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        if (! Gate::allows('section_type_view')) {
            return abort(401);
        }
        $sections = \App\Section::where('section_type_id', $id)->get();

        $section_type = SectionType::findOrFail($id);

        return view('admin.section_types.show', compact('section_type', 'sections'));
    }


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

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

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

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


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

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

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

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