<?php

namespace App\Http\Controllers\Admin;

use App\StageOfConstruction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreStageOfConstructionsRequest;
use App\Http\Requests\Admin\UpdateStageOfConstructionsRequest;

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


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

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

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

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



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


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

        return view('admin.stage_of_constructions.edit', compact('stage_of_construction'));
    }

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



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


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

        $stage_of_construction = StageOfConstruction::findOrFail($id);

        return view('admin.stage_of_constructions.show', compact('stage_of_construction', 'sections', 'residential_complexes'));
    }


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

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

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

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


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

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

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

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