<?php

namespace App\Http\Controllers;

use App;
use App\ExternalLink;
use App\MenuPlan;
use App\News;
use App\User;
use Barryvdh\DomPDF\Facade as PDF;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;


class MenuPlanController extends Controller
{
    public function __construct()
    {
        $this->middleware([ "auth" ]);
        $this->middleware([ "lang" ]);

        $this->middleware([ "check.auth:menuplan.list" ])->only("index");
        $this->middleware([ "check.auth:menuplan.show" ])->only("show");
        $this->middleware([ "check.auth:menuplan.create" ])->only("create", "store");
        $this->middleware([ "check.auth:menuplan.edit" ])->only("edit", "update");
        $this->middleware([ "check.auth:menuplan.delete" ])->only("delete");
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $menuPlans = MenuPlan::query()->paginate($request->input("limit", 20));

        return Response::detect("menuplans.index", [ "menuPlans" => $menuPlans]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return Response::detect("menuplans.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $requestMenuPlanCreate = $request->validate([
            "week" => "required|max:255",
            "monday" => "required|max:255",
            "tuesday" => "required|max:255",
            "wednesday" => "required|max:255",
            "thursday" => "required|max:255",
        ]);

        $menuPlan = new MenuPlan($requestMenuPlanCreate);

        $allMenuPlans = MenuPlan::query()->where('week', '=', $request->week)->get();

        if(count($allMenuPlans) > 0)
            return redirect()->route("menu-plans.index");
        else {
            $menuPlan->save();
            $menuPlans = MenuPlan::query()->paginate($request->input("limit", 20));

            $menuArray = [];

            if($request->newsoption == true){
                $news = new News();

                $news->name = "Ny menuplan";
                $news->subname = "Uge " . $menuPlan->week;
                $news->content =
                    '<h2 class="ql-align-center"><strong>Mandag:</strong></h2>'.
                    '<p class="ql-align-center">'.$menuPlan->monday.'</p><p class="ql-align-center"><br></p>'.

                    '<h2 class="ql-align-center"><strong>Tirsdag:</strong></h2>'.
                    '<p class="ql-align-center">'.$menuPlan->tuesday.'</p><p class="ql-align-center"><br></p>'.

                    '<h2 class="ql-align-center"><strong>Onsdag:</strong></h2>'.
                    '<p class="ql-align-center">'.$menuPlan->wednesday.'</p><p class="ql-align-center"><br></p>'.

                    '<h2 class="ql-align-center"><strong>Torsdag:</strong></h2>'.
                    '<p class="ql-align-center">'.$menuPlan->thursday.'</p>';
                $news->type_id = '2';
                $news->arrangement_id = $menuPlan->id;

                if($request->file("resource")) {
                    $news->resource_id = ResourceController::store($request)->id;
                }

                NewsController::storeAndGet($news);
            }

            return redirect()->route("menu-plans.index", ['menuPlans' => $menuPlans]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(MenuPlan $id)
    {
        return Response::detect("menuplans.show", [ "menuplan" => $id]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $menuplan = MenuPlan::find($id);
        return Response::detect("menuplans.edit", ["menuplan" => $menuplan]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->all();
        //FORCED UPDATE

        $menuplan = MenuPlan::find($id);

        $allMenuPlans = MenuPlan::query()->where('week', '=', $request->week)->where('id', '!=', $id)->get();

        if(count($allMenuPlans) > 0){
            return redirect()->route("menu-plans.index");
        }else{
            $menuplan->update($data);
            $menuplan->save();

            $menuPlans = MenuPlan::query()->paginate($request->input("limit", 20));

            if($request->newsoption == true){
                $news = new News();

                $news->name = "Opdateret menuplan";
                $news->subname = "Uge " . $menuplan->week;
                $news->arrangement_id = $menuplan->id;
                $news->type_id = '2';
                $news->content =
                    '<h2 class="ql-align-center"><strong>Mandag:</strong></h2>'.
                    '<p class="ql-align-center">'.$menuplan->monday.'</p><p class="ql-align-center"><br></p>'.

                    '<h2 class="ql-align-center"><strong>Tirsdag:</strong></h2>'.
                    '<p class="ql-align-center">'.$menuplan->tuesday.'</p><p class="ql-align-center"><br></p>'.

                    '<h2 class="ql-align-center"><strong>Onsdag:</strong></h2>'.
                    '<p class="ql-align-center">'.$menuplan->wednesday.'</p><p class="ql-align-center"><br></p>'.

                    '<h2 class="ql-align-center"><strong>Torsdag:</strong></h2>'.
                    '<p class="ql-align-center">'.$menuplan->thursday.'</p>';


                NewsController::storeAndGet($news);
            }

            return redirect()->route("menu-plans.index", ['menuPlans' => $menuPlans]);
        }

    }

    /**
     * Remove the specified resource from storage.
     * Test delete return index function.
     * @param  int  $id
     * @return RedirectResponse
     */
    public function destroy($id)
    {
        $menuplan = MenuPlan::find($id);
        $menuplan->delete();
        return redirect()->route("menu-plans.index");
    }

    /**
     * Remove the specified resource from storage.
     * Test delete return index function.
     * @param $Id
     * @return RedirectResponse
     */
    public function genPDF($Id){
        $menuPlan = MenuPlan::query()->where("id", "=", $Id)->first();

        $var = 149;

        $menuPlan->monday = substr($menuPlan->monday, 0, $var);
        $menuPlan->tuesday = substr($menuPlan->tuesday, 0, $var);
        $menuPlan->wednesday = substr($menuPlan->wednesday, 0, $var);
        $menuPlan->thursday = substr($menuPlan->thursday, 0, $var);

        $pdf = PDF::loadView('pdf.menuplan', ["menuPlan" => $menuPlan]);

        $pdf->setPaper('A4', 'portrait');

        return $pdf->stream("menuplan_uge_".$menuPlan->week.".pdf");

    }

    public function nameCheck(Request $request){
        $menuplan = MenuPlan::query()->where('week', 'LIKE',$request->nameCheck)->get();
        if(count($menuplan) > 0 && $request->nameCheck !== NULL){
            return 1;
        }
    }

    public function nameCheckUpdate(Request $request){
        $menuplan = MenuPlan::query()->where('week', 'LIKE',$request->nameCheck)->where('id', '!=', $request->id)->get();
        if(count($menuplan) > 0 && $request->nameCheck !== NULL){
            return 1;
        }
    }

}
