<?php

namespace App\Http\Controllers\Admin;

use App\Promotion;
use App\ResidentialComplex;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StorePromotionsRequest;
use App\Http\Requests\Admin\UpdatePromotionsRequest;
use App\Http\Controllers\Traits\FileUploadTrait;

class PromotionsController extends Controller
{
    use FileUploadTrait;

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

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

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

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

            }
        }

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

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

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

        $promotions = Promotion::whereResidentalComplexId($search_id)->get();

        return  $promotions;
    }

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

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

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

        $request = $this->saveFiles($request, ['promotion' => true]);
        $promotion = Promotion::create($request->all());

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

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

        $promotion = Promotion::findOrFail($id);

        return view('admin.promotions.edit', compact('promotion', 'residental_complexes'));
    }

    /**
     * Update Promotion in storage.
     *
     * @param  UpdatePromotionsRequest  $request
     * @param  int  $id
     * @return Response
     */
    public function update(UpdatePromotionsRequest $request, $id)
    {
        if (! Gate::allows('promotion_edit')) {
            return abort(401);
        }

        $request = $this->saveFiles($request, ['promotion' => true]);
        $promotion = Promotion::findOrFail($id);
        $promotion->update($request->all());

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

    /**
     * Display Promotion.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        if (! Gate::allows('promotion_view')) {
            return abort(401);
        }
        $promotion = Promotion::findOrFail($id);

        return view('admin.promotions.show', compact('promotion'));
    }

    /**
     * Remove Promotion from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        if (! Gate::allows('promotion_delete')) {
            return abort(401);
        }
        $promotion = Promotion::findOrFail($id);
        $promotion->delete();

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

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

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

    /**
     * Restore Promotion from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function restore($id)
    {
        if (! Gate::allows('promotion_delete')) {
            return abort(401);
        }
        $promotion = Promotion::onlyTrashed()->findOrFail($id);
        $promotion->restore();

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

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

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