<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\Questionnaire;
use App\Models\QuestionnairePart;
use App\Models\QuestionnaireSection;
use App\Models\QuestionnaireTable;
use App\Models\Category;
use App\Models\SubCategory;
use App\Models\SubQuestionnaire;
use App\Models\FinancialYear;
use Illuminate\Support\Facades\Auth;

class QuestionnaireController extends Controller
{

    const QUESTIONNAIRE_BLADE = 'src/questionnaire';
    const SUB_QUESTIONNAIRE_BLADE = 'src/sub_questionnaire';
    const QUESTIONNAIRE_URL = 'questionnaire';
    const PREVIEW_BLADE = 'src/questionnaire_preview';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $questionnaires = Questionnaire::all();
        $questionnaire_parts = QuestionnairePart::all();
        $questionnaire_sections = QuestionnaireSection::all();
        $categories = Category::all();
        $sub_categories = SubCategory::all();
        
        return view(QuestionnaireController::QUESTIONNAIRE_BLADE,
        [
            'questionnaires' => $questionnaires, 
            'questionnaire_parts' => $questionnaire_parts,
            'questionnaire_sections' => $questionnaire_sections,
            'categories' => $categories,
            'sub_categories' => $sub_categories
        ]);
    }

    public function save(Request $request){

        $data = $request->validate([
            'question' => 'required',
            'question_no' => 'required',
            'questionnaire_part_id' => 'required',
            'questionnaire_section_id' => 'required'
        ]);

        $checkbox_options = null;
        $dropdown_options = null;

        $data['created_by'] = Auth::user()->user_id;
        $data['feedback_area'] = $request->feedback;

        $include_checkbox_options = $request->include_checkbox_options;
        
        if($include_checkbox_options == "1"){

            $checkbox_options = $request->checkbox_options;
        }


        $data['checkbox_options'] = $checkbox_options;
        $include_dropdown_options = $request->include_dropdown_options;
        
        if($include_dropdown_options == "1"){

            $dropdown_options = $request->dropdown_options;
        }

        $data['dropdown_options'] = $dropdown_options;
        $data['needs_upload'] = $request->needs_upload;

        $questionnaire = tap(new Questionnaire($data))->save();

        $include_answers_table  = $request->include_answers_table;

        if($include_answers_table == "1") {

            $questionnaire_id = $questionnaire->questionnaire_id;
            $table_no = $request->table_no;
            $table_title = $request->table_title;
            $table_html = $request->table_html;
            $table_js = $request->table_js;

            $tableData = ['questionnaire_id' => $questionnaire_id,
                          'table_no' => $table_no,
                          'table_title' => $table_title,
                          'created_by' => Auth::user()->user_id,
                          'table_html' => $table_html,
                          'table_js' => $table_js
                        ];
            tap(new QuestionnaireTable($tableData))->save();
        }

        return redirect(QuestionnaireController::QUESTIONNAIRE_URL);
    }

    public function search(Request $request){

        $keyword = $request->keyword;
        $questionnaires = DB::select(Db::raw("select * from questionnaire inner join   
            questionnaire_part on questionnaire.questionnaire_part_id = 
            questionnaire_part.questionnaire_part_id inner join   
            questionnaire_section on questionnaire.questionnaire_section_id = 
            questionnaire_section.questionnaire_section_id left join   
            category on questionnaire.category_id = category.category_id
            left join sub_category on questionnaire.sub_category_id = 
            sub_category.sub_category_id where question 
            like '%".$keyword."%' or part_name 
            like '%".$keyword."%' or section_name 
            like '%".$keyword."%' or sub_category_name 
            like '%".$keyword."%' or category_name 
            like '%".$keyword."%'"));

        return json_encode($questionnaires);
    }

    public function edit($id) {
        
        //TO DO: The Whole Thing

        return redirect(QuestionnaireController::QUESTIONNAIRE_URL);

    }

    public function del(Request $request) {

        $questionnaireId = $request->deletequestionnaire;

        //TO DO: Delete Associated Sub-Questionnaire
        $questionnaire = Questionnaire::find($questionnaireId);
        $questionnaire->delete();

        return redirect(QuestionnaireController::QUESTIONNAIRE_URL);
    }

    public function view($id) {

        $questionnaireId = $id;
        $selectedQuestion = Questionnaire::find($questionnaireId);

        $sub_questionnaires = SubQuestionnaire::where('questionnaire_id',
         '=', $questionnaireId)->get();

        return view(QuestionnaireController::SUB_QUESTIONNAIRE_BLADE,
            ['sub_questionnaires' => $sub_questionnaires,
            'selectedQuestion' => $selectedQuestion]);
    }

    public function preview() {

        $questionnaires = DB::select(Db::raw("select 
            questionnaire.* from questionnaire inner join   
            questionnaire_part on questionnaire.questionnaire_part_id = 
            questionnaire_part.questionnaire_part_id inner join   
            questionnaire_section on questionnaire.questionnaire_section_id = 
            questionnaire_section.questionnaire_section_id 
            left join questionnaire_table on questionnaire.questionnaire_id
             = questionnaire_table.questionnaire_id  
            left join category on questionnaire.category_id = category.category_id
            left join sub_category on questionnaire.sub_category_id = 
            sub_category.sub_category_id order by questionnaire.questionnaire_part_id asc,
            questionnaire.questionnaire_section_id asc, questionnaire.question_no asc"));

        return view(QuestionnaireController::PREVIEW_BLADE,
            ['questionnaires' => $questionnaires]);
    }

    public function financialYears() {

        $html = '<option value=""></option>';
        $financialYears = FinancialYear::where('status', '=', '1')->get();
        foreach($financialYears as $year) {
            $html = $html.'<option value="'.$year->financial_year_name.'">'.$year->financial_year_name.'</option>';
        }
        return $html;
    }
}