<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Auth;
use DB;
use App\Models\Vocabulary;
use App\Models\Vocabulary_questions;
class VocabularyController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        date_default_timezone_set("Asia/Dhaka");
        $all_vocabularies=Vocabulary::orderBy('id','DESC')->paginate(15);
        return view('admin.vocabulary.index',compact('all_vocabularies'))->with('i', ($request->input('page', 1) - 1) * 15);
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $item           =   Vocabulary::find($id);
        $meaning_ques   =   Vocabulary_questions::where('word_id','=',$id)->where('level','=',1)->pluck('question')->first();
        
        $meaning_opt    =   Vocabulary_questions::where('word_id','=',$id)->where('level','=',1)->pluck('options')->first();

        $syn_ques       =   Vocabulary_questions::where('word_id','=',$id)->where('level','=',2)->pluck('question')->first();
        $syn_opt        =   Vocabulary_questions::where('word_id','=',$id)->where('level','=',2)->pluck('options')->first();
        $anto_ques      =   Vocabulary_questions::where('word_id','=',$id)->where('level','=',3)->pluck('question')->first();
        $anto_opt       =   Vocabulary_questions::where('word_id','=',$id)->where('level','=',3)->pluck('options')->first();
        return view('admin.vocabulary.edit',compact('item','meaning_ques','syn_ques','syn_opt','meaning_opt','anto_ques','anto_opt'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        
        
        $id=$request->hdn_id;
        $vocabulary=Vocabulary::find($id);
        $vocabulary->word=$request->word;
        $vocabulary->meaning=$request->meaning;
        $vocabulary->synonyms=$request->syn;
        $vocabulary->antonyms=$request->antonym;
        $vocabulary->example=$request->example;
        $vocabulary->save();
        
        $v_ques1= new Vocabulary_questions();
        $v_ques1->question="What is the meaning of the word {$vocabulary->word}?";
        $v_ques1->options=strip_tags($request->meaning_opt);
        $v_ques1->level=1;
        $v_ques1->word_id=$id;
        $v_ques1->display=1;
        $v_ques1->updated_at=date('Y-m-d H:i:s');
        $v_ques1->save();

        $v_ques2= new Vocabulary_questions();
        $v_ques2->question="What is the synonym of the word {$vocabulary->word}?";
        $v_ques2->options=strip_tags($request->syn_opt);
        $v_ques2->level=2;
        $v_ques2->word_id=$id;
        $v_ques2->display=1;
        $v_ques2->updated_at=date('Y-m-d H:i:s');
        $v_ques2->save();

        $v_ques3= new Vocabulary_questions();
        $v_ques3->question="What is the antonym of the word {$vocabulary->word}?";
        $v_ques3->options=strip_tags($request->anto_opt);
        $v_ques3->level=3;      
        $v_ques3->word_id=$id;
        $v_ques3->display=1;
        $v_ques3->updated_at=date('Y-m-d H:i:s');
        $v_ques3->save();
        return redirect()->action('Admin\VocabularyController@index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
