<?php

namespace App\Http\Controllers\Admin;

use App\ResidentalComplexIcon;
use App\ResidentialComplex;
use App\Section;
use App\SectionType;
use App\StageOfConstruction;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreSectionsRequest;
use App\Http\Requests\Admin\UpdateSectionsRequest;

class SectionsController extends Controller
{
    /**
     * Display a listing of Section.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if (! Gate::allows('section_access')) {
            return abort(401);
        }

        $residental_complex_id = $request->query->get('residental_complex_id');

        if (request('show_deleted') == 1) {
            if (! Gate::allows('section_delete')) {
                return abort(401);
            }
            $sections = Section::onlyTrashed()->get();
        } else {
            if (!empty($residental_complex_id)) {
                $sections = $this->search($residental_complex_id);
            } else {
                $sections = Section::paginate(15);
            }
        }

        $residential_complexes = ResidentialComplex::get()->pluck('name', 'id')
            ->prepend(trans('quickadmin.qa_residential_complex_filter'), '');

        return view('admin.sections.index', compact('sections', 'residential_complexes', 'residental_complex_id'));
    }

    /**
     * @param $search_id
     * @return mixed
     */
    private function search($search_id){

        $sections = Section::whereResidentalComplexId($search_id)->get();

        return $sections;
    }

    /**
     * Show the form for creating new Section.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if (! Gate::allows('section_create')) {
            return abort(401);
        }
        
        $section_types = SectionType::get()->pluck('type', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
        $stage_of_constructions = StageOfConstruction::get()->pluck('name', 'id')
            ->prepend(trans('quickadmin.qa_please_select'), '');
        $residental_complexes = \App\ResidentialComplex::get()->pluck('name', 'id')
            ->prepend(trans('quickadmin.qa_please_select'), '');
        $quarterly_starts = \App\Quarterly::get()->pluck('name', 'id')
            ->prepend(trans('quickadmin.qa_please_select'), '');
        $quarterly_finishes = \App\Quarterly::get()->pluck('name', 'id')
            ->prepend(trans('quickadmin.qa_please_select'), '');

        return view('admin.sections.create', compact(
            'section_types',
            'stage_of_constructions',
            'residental_complexes',
            'quarterly_starts',
            'quarterly_finishes'
        ));
    }

    /**
     * Store a newly created Section in storage.
     *
     * @param StoreSectionsRequest  $request
     * @return Response
     */
    public function store(StoreSectionsRequest $request)
    {
        if (! Gate::allows('section_create')) {
            return abort(401);
        }

        $data = $request->all();

//        $residentialComplex = ResidentialComplex::findOrFail($data['residental_complex_id']);
//        $sectionType = mb_strtolower(SectionType::findOrFail($data['section_type_id'])->type);
//
//        $data['name'] = $residentialComplex->name . ", " . $sectionType . " #" . $data['number'];

        $section = Section::create($data);

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

    /**
     * Show the form for editing Section.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if (! Gate::allows('section_edit')) {
            return abort(401);
        }
        
        $section_types = \App\SectionType::get()->pluck('type', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
        $stage_of_constructions = \App\StageOfConstruction::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
        $residental_complexes = \App\ResidentialComplex::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
        $quarterly_starts = \App\Quarterly::get()->pluck('name', 'id')
            ->prepend(trans('quickadmin.qa_please_select'), '');
        $quarterly_finishes = \App\Quarterly::get()->pluck('name', 'id')
            ->prepend(trans('quickadmin.qa_please_select'), '');

        $section = Section::findOrFail($id);

        return view('admin.sections.edit', compact(
            'section',
            'section_types',
            'stage_of_constructions',
            'residental_complexes',
            'quarterly_starts',
            'quarterly_finishes'
        ));
    }

    /**
     * Update Section in storage.
     *
     * @param  UpdateSectionsRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateSectionsRequest $request, $id)
    {
        if (! Gate::allows('section_edit')) {
            return abort(401);
        }
        $section = Section::findOrFail($id);

        $data = $request->all();

//        $residentialComplex = ResidentialComplex::findOrFail($data['residental_complex_id']);
//        $sectionType = mb_strtolower(SectionType::findOrFail($data['section_type_id'])->type);
//
//        $data['name'] = $residentialComplex->name . ", " . $sectionType . " #" . $data['number'];

        $section->update($data);

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

    /**
     * Display Section.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        if (! Gate::allows('section_view')) {
            return abort(401);
        }
        
        $section_types = \App\SectionType::get()->pluck('type', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
        $stage_of_constructions = \App\StageOfConstruction::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
        $residental_complexes = \App\ResidentialComplex::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');$plannings = \App\Planning::where('section_id', $id)->get();

        $section = Section::findOrFail($id);

        return view('admin.sections.show', compact('section', 'plannings'));
    }

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

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

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

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

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

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

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

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