<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Faq;
use App\Http\Controllers\AdminController;
use App\Aws\FileSystem;
use App\FaqQuestion;
use App\Media;
class FAQController extends AdminController {

    public function __construct() {
        $this->middleware('auth');

        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['link'] = "admin/faqs";
        $this->breadcrumbs[1]['name'] = "FAQ's";
    }

    public function index() {

        $model = Faq::get();

        $data["pageHeader"] = false;
        $data["model"] = $model;
        $data["title"] = "FAQ";
        $data["SubTitle"] = "List";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/faqs/index', $data);
    }

    public function create(Request $request) {

        $data["pageHeader"] = false;
        $data["model"] = new Faq();
        $data["title"] = "FAQ";
        $data["subTitle"] = "Create";
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Create";


        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/faqs/create', $data);
    }

    public function save(Request $request) {

        $rules = [
            'title' => 'required|max:255',
        ];

        $input = $request->all();
        $message = [];
        $validator = Validator::make($input, $rules, $message);


        $input["created_at"] = date("Y-m-d H:i:s");
        $response = array();
        $response['error'] = false;

        if ($validator->fails()) {
            $errors = $validator->errors();
            $response['errors'] = $errors;
            $response['error'] = true;
            $header = 402;
//            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {
            $questions = empty($input["questions"]) ? array() : $input["questions"];

            unset($input["_token"]);
        
            unset($input["questions"]);
            $model = new Faq();
            
            $id = $model->insertGetId($input);
            

            $this->insertquestions($id, $questions);
            $response["message"] = "FAQ's added successfully!";
            $header = 200;
        }
        return response()->json($response, $header);
    }

    public function edit($id) {

        $data["model"] = Faq::find($id);

        $data["pageHeader"] = false;
        $data["title"] = "FAQ";
        $data["subTitle"] = "Edit";
        $data["id"] = $id;

        $plans = array();

            
             if (count($data["model"]->qusetions) > 0) {
            $i = 0;
            foreach ($data["model"]->qusetions as $p) {
                $plans[$i]['id'] = $p->id;
                $plans[$i]['title'] = $p->title;
                $plans[$i]['description'] = $p->description;
                $plans[$i]['position'] = $p->position;
                $plans[$i]['status'] = $p->status;

                $i++;
            }
        }

        


        $data['plans'] = $plans;

        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Edit";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/faqs/edit', $data);
        
    }

    public function update($id, Request $request) {
        $input = $request->all();

        $rules = [
            'title' => 'required|max:255',
        ];

        $messages = [];

        $validator = Validator::make($input, $rules, $messages);
        $response = array();

        if ($validator->fails()) {
            $errors = $validator->errors();
            $response['errors'] = $errors;
            $response['error'] = true;
            $header = 402;
        } else {

            $questions = empty($input["questions"]) ? array() : $input["questions"];

            unset($input["questions"]);
            unset($input["_token"]);

            $input['updated_at'] = date("Y-m-d H:i:s");


            Faq::where('id', $id)->update($input);

            $this->insertquestions($id, $questions);
            $response["message"] = "Tour Updated successfully!";
            $header = 200;
            $error = false;
            return response()->json($response, $header);
        }
    }

    public function delete($id) {
        Faq::where('id', $id)->delete();
        \Session::flash('error', "FAQ's  deleted successfully!");
        return redirect('admin/faqs');
    }

    public function show($id) {

        $q = new Faq();
        $data['model'] = $q->find($id);
        $data["title"] = "FAQ";
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = $data['model']->title;

        $data['breadcrumbs'] = $this->breadcrumbs;

        return view('admin/faqs/show', $data);
    }
    
    public function savequestion(Request $request) {
        $input = $request->all();

        $rules = [
            'title' => 'required|max:255'
        ];

        $messages = [];

        $validator = Validator::make($input, $rules, $messages);
        $response = array();

        if ($validator->fails()) {
            $errors = $validator->errors();
            $response['errors'] = $errors;
            $response['error'] = true;
            $header = 402;
        } else {
            unset($input["_token"]);

            $response['error'] = false;
            $header = 200;
            $response['input'] = $input;
        }
        return response()->json($response, $header);
    }
    
    public function insertquestions($id, $input) {

        if (count($input) > 0) {

            foreach ($input as $question) {
               
                $status = isset($question["status"]) ? 0 : 1;
                $remove = isset($question["remove"]) ? 1 : 0;

                $data = json_decode($question["data"], true);

               
                $question_id = empty($data["id"]) ? "" : $data["id"];


                if ($remove == 0) {

                    unset($data["details"]);
                    unset($data["id"]);

                    $data["status"] = $status;
                    $data["faq_page_id"] = $id;
                    $data["position"]=$question["position"];
                    if ($question_id == "") {
                        $question_id = FaqQuestion::insertGetId($data);
                    } else {
                        FaqQuestion::where("id", $question_id)->update($data);
                    }

                   

                  
                } else {

                    if ($question_id != "") {
                        FaqQuestion::where("id", $question_id)->delete();
                       
                    }
                }
            }
        }
        //d($input, 1);
    }

}
