<?php

namespace App\Http\Controllers\CMS;


use App\Http\Controllers\Controller;
use App\Models\Feedback;
use Carbon\Carbon;
use Input;

class CMSFeedbackController extends Controller
{

/*
* Create a new controller instance.
*
* @return void
*/
    public function __construct()
    {
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = [
            'feedbacks' => Feedback::all(),//Feedback::where('lvl', 1)->get(),
        ];
        return view('cms.feedback.index', $data);

    }

    public function detail($id)
    {
        if($id == "new") {
            $feedback = new Feedback();
            $id = 'new';
            $feedback->status = 1;

        }else{
            $feedback = Feedback::findOrFail($id);
            $id = $feedback->id;
        }
        $data = [
            'feedback' => $feedback,
            'id' => $id,
        ];
        return view('cms.feedback.detail', $data);

    }


    public function store($id)
    {
        $data = Input::all();
        if($id == "new") {

            if(!isset($data['feedback_parent']) && !$data['feedback_parent']) {
                abort(404);
                die();
            }
            $feedback = new Feedback();
            $feedback->model = 'feedback';
            $feedback->owner_second = 0;
            $feedback->show_in_navi = 1;
            $feedback->catalog_id = 1;

        }else{
            $feedback = Feedback::findOrFail($id);
        }

        $feedback->title = $data['title'];
        $feedback->status = isset($data['status']) ? true : false;
        $feedback->slug = $data['slug'];
        $feedback->date = $data['date'];
        $feedback->content = $data['content'];
        $feedback->description = $data['description'];
        $feedback->pagetitle = $data['pagetitle'];
        $feedback->keywords = $data['keywords'];

        $feedback->order = (new Carbon($feedback->date))->format('Ymd')*-1;

        //parent -- lvl
       if(isset($data['feedback_parent']) && $data['feedback_parent']){
           $feedback->lvl = 2;

           if($feedback->parent_id != $data['feedback_parent']){
               $feedback->owner = $data['feedback_parent'];
               $feedback->parent_id = $data['feedback_parent'];
               $parent_feedback = Feedback::findOrFail($data['feedback_parent']);
               if($first_feedback = $parent_feedback->children->first()){
                   $feedback->lft = $first_feedback->lft;
                   $feedback->rgt = $first_feedback->rgt;
               }
               $feedback->tree_root = $parent_feedback->tree_root;

           }

           $feedback->parent_id = $data['feedback_parent'];

       }else{
           // $feedback->lvl = 1;
           //TODO need real parent from Navi tree
           // $feedback->parent_id = 0;

       }

        $feedback->save();
        \Session()->flash('alert-save', '1');
        return redirect(route('cms_feedback_detail', [$feedback->id]));

    }

    public function delete($id){
        $feedback = Feedback::findOrFail($id);

        //TODO
        //check for delete, only delete lvl 2 .,...?
        if ($feedback->lvl != 2){
            abort(404);
            die();
        }

        $feedback->delete();
        \Session()->flash('alert-success', __('Feedback gelöscht'));
        return redirect(route('cms_feedback'));
    }


}
