<?php

namespace App\Http\Controllers\Admin;

use App\CourseOfConstruction;
use App\ResidentialComplex;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreCourseOfConstructionsRequest;
use App\Http\Requests\Admin\UpdateCourseOfConstructionsRequest;
use App\Http\Controllers\Traits\FileUploadTrait;

class CourseOfConstructionsController extends Controller
{
    use FileUploadTrait;

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

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

        if (request('show_deleted') == 1) {
            if (! Gate::allows('course_of_construction_delete')) {
                return abort(401);
            }

            $course_of_constructions = CourseOfConstruction::onlyTrashed()->get();
        } else {
            if (!empty($residental_complex_id)) {
                $course_of_constructions = $this->search($residental_complex_id);
            } else {
                $course_of_constructions = CourseOfConstruction::paginate(15);
            }
        }

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

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

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

        $course_of_constructions = CourseOfConstruction::whereResidentalComplexId($search_id)->get();

        return $course_of_constructions;
    }

    /**
     * Show the form for creating new CourseOfConstruction.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if (! Gate::allows('course_of_construction_create')) {
            return abort(401);
        }
        
        $residental_complexes = ResidentialComplex::get()
            ->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');

        return view('admin.course_of_constructions.create', compact('residental_complexes'));
    }

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


        foreach ($request->input('images_id', []) as $index => $id) {
            $model          = config('medialibrary.media_model');
            $file           = $model::find($id);
            $file->model_id = $course_of_construction->id;
            $file->save();
        }


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

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

        $course_of_construction = CourseOfConstruction::findOrFail($id);

        return view('admin.course_of_constructions.edit', compact('course_of_construction', 'residental_complexes'));
    }

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


        $media = [];
        foreach ($request->input('images_id', []) as $index => $id) {
            $model          = config('medialibrary.media_model');
            $file           = $model::find($id);
            $file->model_id = $course_of_construction->id;
            $file->save();
            $media[] = $file->toArray();
        }
        $course_of_construction->updateMedia($media, 'images');


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


    /**
     * Display CourseOfConstruction.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        if (! Gate::allows('course_of_construction_view')) {
            return abort(401);
        }
        $course_of_construction = CourseOfConstruction::findOrFail($id);

        return view('admin.course_of_constructions.show', compact('course_of_construction'));
    }


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

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

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

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


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

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

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

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

    public function removeFile(Request $request, $id)
    {
        if (! Gate::allows('course_of_construction_delete')) {
            return abort(401);
        }

        $course_of_construction = CourseOfConstruction::findOrFail($id);

        $course_of_construction->restore();

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