<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Proposal;
use App\User;
use App\StudentProposal;
use App\Models\ReferUser;
use Hash;
use DB;
use Session;
use Auth;
use Mail;

class ModalController extends Controller{

    public function get(Request $req, $name){
        $user = Auth::user();
        $carbon=new \Carbon\Carbon;

        $data=[
            'name'=>$name,
            'carbon'=>$carbon
        ];

        if($name=="apply_student_proposal"){
            $data['teacher'] = User::find($req->id);
            $data['student'] = User::find($user->id);
            $data['proposal']= Proposal::where('student_id',$user->id)->get();
        }elseif($name=="view_apply_proposal"){
            $data['std_proposal'] =StudentProposal::find($req->id);

        }elseif($name=="view_student_proposal"){
            $data['proposal']= Proposal::where(
                    'student_id',$user->id
                )->where(
                    'id',$req->id
                )->first();

        }elseif($name=="add_destination"){

        }elseif($name=="add_wishlist"){
            $user =Auth::guard('teacher')->user();
            $data['refer'] = new ReferUser;

        }else $name='default';

        return view("modal_pages.{$name}", $data);
    }

    public function post(Request $req, $name){

        $user=\Auth::user();
        $carbon=new \Carbon\Carbon;

        if($name=='apply_student_proposal'){

            $validator=\Validator::make($req->all(), [
                'proposal_id'=>'required',
                'startdate'=>'required',
                'startmonth'=>'required',

            ]);

            if($validator->fails()){

                $errors=$validator->errors()->all();
                return response(['msg'=>$errors[0]], 422);
            }

            $already_exists = StudentProposal::where('student_id',$req->student_id)->exists();
            if($already_exists){
                return response(['msg'=>'Sorry! You already submitted. You can not submit proposal.'],403);
            }

            $student_proposal =new StudentProposal;
            $student_proposal->student_id =$req->student_id;
            $student_proposal->teacher_id =$req->teacher_id;
            $student_proposal->proposal_id =$req->proposal_id;
            $student_proposal->start_year =$req->startdate;
            $student_proposal->start_month =$req->startmonth;
            $student_proposal->created_by =$user->id;
            $student_proposal->student_apply_ref =studentRefNoGenerate();
            $student_proposal->save();

            $user =User::find($student_proposal->student_id);
            $proposal_user =User::find($student_proposal->teacher_id);

			 if($user){
				$data['name'] = $user->first_name.' '.$user->last_name;
				$data['teacher'] = $proposal_user->first_name.' '.$proposal_user->last_name;
				$data['arn'] = $proposal_user->ref_no;
				$user_mail =$user->email;
				$from = 'test@mail.com';
				Mail::send('email.student_proposal_email',$data, function ($message) use ($user_mail,$from) {
					$message->from($from);
					$message->to($user_mail)->subject('New Email Send to you');
				});
			 }

             $proposal =Proposal::where(
                    'student_id',$student_proposal->student_id
                )->where(
                    'id',$student_proposal->proposal_id
                )->first();

             if($proposal_user){
                $data['name'] = $proposal_user->first_name.' '.$proposal_user->last_name;
                $data['std_name'] = $user->first_name.' '.$user->last_name;
                $data['proposal_title'] =$proposal->title;
                $data['date_time']=$student_proposal->created_at;
				$user_mail =$proposal_user->email;
				$from = 'test@mail.com';
				Mail::send('email.teacher_proposal_email',$data, function ($message) use ($user_mail,$from) {
					$message->from($from);
					$message->to($user_mail)->subject('New Email Send to you');
				});
             }


            return response(['msg'=>'Proposal send successfully.']);

        }elseif($name=="add_wishlist"){
            $user =Auth::guard('teacher')->user();
            $validator=\Validator::make($req->all(), [
                'name'=>'required',
                'email'=>'required|email|max:255|unique:refer_users'
            ]);

            if($validator->fails()){
                $errors=$validator->errors()->all();
                return response(['msg'=>$errors[0]], 422);
            }

            $user_data =new User;
            $user_data->first_name =$req->name;
            $user_data->email =$req->email;
            $user_data->wishlist_teacher_id =$user->id;
            $user_data->user_type =1;
            $user_data->save();

            $refer =new ReferUser;
            $refer->name =$req->name;
            $refer->email =$req->email;
            $refer->refer_by =$user->id;
            $refer->teacher_id =$user->id;
            $refer->save();

            if($refer->save()){
                $data['name'] = $refer->name;
                $data['teacher_id'] = $refer->teacher_id;
                $data['student_id'] = $user_data->id;
                $data['std_email'] = $refer->email;
                $data['applicant_name'] = $user->first_name.' '.$user->last_name;
                $user_mail = $refer->email;
                $from = 'test@mail.com';

                Mail::send('email.wishlist_email',$data, function ($message) use ($user_mail,$from) {
                    $message->from($from);
                    $message->to($user_mail)->subject('Wishlist Email Send to you');
                });
            }

            return response(
                ['msg'=>'Refer saved successfully.']
            );

        }

        return response(['msg'=>'No name argument found.'], 403);

    }

}
