<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\Helpers\PageSL;
use App\User;
use App\Models\MarketCountry;
use App\Models\University;
use App\Models\Department;
use App\Models\ReferUser;
use App\Models\ResearchInterest;
use App\Models\Requirement;
use App\Models\Publication;
use App\StudentProposal;
use App\Proposal;
use App\Institution;
use App\Score;
use App\Workexperience;
use Carbon\Carbon;
use Session;
use Mail;
use Auth;
use Hash;
use Illuminate\Support\Str;


class AjaxController extends Controller
{
    public function get($name, Request $req)
	{
		if($name=='available_ref_no_search') {

		}elseif(empty($name)){
            return response([
            'msg'=>'Invalid request please insert correct value!.'
            ], 403);
        }
	}

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

		if($name =='teacher_change_password') {

			$validator=\Validator::make($req->all(), [
                'old_password'=>'required',
                'new_password_confirmation'=>'required',
                'new_password'=>[
                    'required',
                    'confirmed',
                    'min:8',
                    //'regex:/[a-z]/',
                    //'regex:/[A-Z]/',
                    //'regex:/[0-9]/',
                    //'regex:/[@$!%*#?&]/',
                ]
            ]);
			//  [
            //     'regex' => 'The :attribute must contain at least one lowercase, uppercase, digit and special character.'
            // ]);

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

            $user=auth_user('teacher');

            if(\Illuminate\Support\Facades\Hash::check($req->old_password, $user->password)) {

                $user->password=bcrypt($req->new_password);
                $user->update();

                return ['msg'=>'Your login password updated successfully!. Please use your new password for next time login.'];

            }else{

                return response(
                    ['msg'=>'Sorry!, old password does not match with login credential.'],
                    403
                );

            }

		}elseif($name=="teacher_user_status_change"){
            $user=auth_user('teacher');
            $teacher =User::find($req->id);
            $teacher->apply_status =$req->status;
            $teacher->update();

            $data['name'] = $teacher->first_name.' '.$teacher->last_name;
            $data['receive_email']=$teacher->email;
            $user_mail = $teacher->email;
            $from = 'asraful@revinr.com';
            if($teacher->apply_status==1){
                Mail::send('email.approve_teacher_email',$data, function ($message) use ($user_mail,$from) {
                    $message->from($from);
                    $message->to($user_mail)->subject('Account Activation Confirmation');
                });
            }
            return response(
                ['msg'=>'Successfully saved.']
            );

        }elseif($name=="set_country"){
            $user =Auth::guard('admin')->user();
            if($req->checked == 'true'){
                $mc = new MarketCountry;
                $mc->country_id = $req->id;
                $mc->status = 1;
                $mc->added_by = $user->id;
                $mc->save();
                return response(
                    ['msg'=>'Added Successfully.']
                );
            }else{
                MarketCountry::where('country_id', $req->id)->delete();

                return response(
                    ['msg'=>'Removed Successfully.']
                );
            }
        }elseif($name=="add_country_wise_uni"){
            $user =Auth::guard('admin')->user();
            $validator=\Validator::make($req->all(), [
                'country_id'=>'required',
                'university_name'=>'required',
                'university_website'=>'required',
            ]);

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

            $data =new University;
            $data->country_id =$req->country_id;
            $data->name =$req->university_name;
            $data->web_address =$req->university_website;
            $data->save();

            return response(
                ['msg'=>'Saved Successfully.']
            );


        }elseif($name=="add_department"){
            $user =Auth::guard('admin')->user();

            $validator=\Validator::make($req->all(), [
                'dapartment'=>'required',
            ]);

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

            $data =new Department;
            $data->name =$req->dapartment;
            $data->status =1;
            $data->save();

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

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

            $validator=\Validator::make($req->all(), [
                'first_name'=>'required',
                'last_name'=>'required',
                'university_id'=>'required',
                'department'=>'required',
                'designation'=>'required',
            ]);

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

            $data = User::find($req->id);
            $data->first_name =$req->first_name;
            $data->last_name =$req->last_name;
            $data->university_id =$req->university_id;
            $data->university_name =NULL;
            $data->department =$req->department;
            $data->others_department =NULL;
            $data->designation =$req->designation;
            $data->update();

            return response(
                ['msg'=>'Saved successfully.']
            );
        }elseif($name=="save_student_refer_friend"){
            $user =Auth::user();
            $validator=\Validator::make($req->all(), [
                'name'=>'required',
                'email'=>'required|email|max:255'
            ]);

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

            $std_proposal =StudentProposal::where(
                'student_id',$user->id
            )->where(
                'proposal_submit_status',1
            )->first();

            if(empty($std_proposal)){
                return response(
                    ['msg'=>'Sorry! you have to submit a proposal first.'],403
                );
            }

            $user_data = new User;
            $user_data->first_name = $req->name;
            $user_data->email =$req->email;
            $user_data->invitation_date =now();
            $user_data->user_type =1;
            $user_data->register_type =6; // 6= student refer a friend  for a application proposal
            $user_data->created_by =$user->id;
            $user_data->save();

            $refer =new ReferUser;
            $refer->name =$req->name;
            $refer->email =$req->email;
            $refer->refer_by =$user->id;
            $refer->teacher_id =$std_proposal->teacher_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;
                $data['receive_email']=$refer->email;
                $user_mail = $refer->email;
                $from = 'asraful@revinr.com';

                Mail::send('email.refer_a_friend_email',$data, function ($message) use ($user_mail,$from) {
                    $message->from($from);
                    $message->to($user_mail)->subject('Recommendation - Research Admission Platform for Finding a Research Superviso.');
                });
            }

            return response(
                ['msg'=>'Saved successfully.']
            );
        }elseif($name=="research_interest"){

            $validator=\Validator::make($req->all(), [
                'research'=>'required',
            ]);

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

            $data = new ResearchInterest;
            $data->teacher_id =$req->id;
            $data->research =$req->research;
            $data->save();

            return response(
                ['msg'=>'Saved successfully.']
            );
        }elseif($name=="edit_university_save"){
            $user=Auth::guard('admin')->user();
            $validator=\Validator::make($req->all(), [
                'country_id'=>'required',
                'university_name'=>'required',
                'university_website'=>'required',
            ]);

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

			$data =University::find($req->edit_id);
            $data->country_id =$req->country_id;
            $data->name =$req->university_name;
            $data->web_address =$req->university_website;
            $data->update();

            return response([
                'msg'=>'Update Successfully.'
            ]);

        }elseif($name=="delete_university_info"){
            $user=Auth::guard('admin')->user();
            $data =University::find($req->delete_id);
            $data->delete();
            return response([
                'msg'=>'Successfully Deleted'
            ]);

        }elseif($name=="delete_department_info"){
            $user=Auth::guard('admin')->user();
            $data =Department::find($req->delete_id);
            $data->delete();
            return response([
                'msg'=>'Successfully Deleted'
            ]);
        }elseif($name=="edit_department_save"){
            $user=Auth::guard('admin')->user();
            $validator=\Validator::make($req->all(), [
                'department'=>'required',
            ]);

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

            $data =Department::find($req->edit_id);
            $data->name =$req->department;
            $data->update();
            return response([
                'msg'=>'Successfully updated.'
            ]);
        }elseif($name=="concept_status_update"){

            $data =StudentProposal::find($req->id);
            $data->concept =$req->value;
            $data->update();

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

        }elseif($name=="proposal_status_update"){
            $data =StudentProposal::find($req->id);
            $data->proposal_status =$req->value;
            $data->proposal_status_date =now();
            $data->feedback_msg =NULL;
            $data->update();

            if($req->value){
                $user_data = User::find($data->student_id);
                $user_data->freez_profile =2;  // 2 = [In Review,Pause,Interested]
                $user_data->update();
            }

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

        }elseif($name=="proposal_feedback_update"){
            $validator=\Validator::make($req->all(), [
                'message'=>'required',
            ]);

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

            $data =StudentProposal::find($req->id);
            $data->proposal_status =$req->proposal_status;
            $data->feedback_msg =$req->message;
            $data->proposal_status_date =now();
            $data->update();

            if($req->proposal_status ==3){ // 3= rejected
                $user_rejected = User::find($data->student_id);
                $user_rejected->freez_profile =0;
                $user_rejected->update();
            }

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

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

            $data =StudentProposal::find($req->id);
            $data->accepted_status =1;
            $data->accepted_date =now();
            $data->update();

            $user_accepted = User::find($data->student_id);
            $user_accepted->freez_profile =0;
            $user_accepted->update();


            return response([
                'msg'=>'Updated successfully.'
            ]);
        }elseif($name=="proposal_quality_rate_star_update"){
            $data =StudentProposal::find($req->id);
            $data->proposal_quality =$req->value;
            $data->update();

            return response([
                'msg'=>'Updated successfully.'
            ]);
        }elseif($name=="teacher_is_available"){
            $user =Auth::guard('teacher')->user();

            $data =User::find($user->id);
            if($req->checked =="true"){
                $data->is_available =1;
            }else{
                $data->is_available =0;
            }

            $data->update();
            return response(
                ['msg'=>"Updated Successfully."]
            );
        }elseif($name=="student_q_form"){

            $validator=\Validator::make($req->all(), [
                'name'=>'required',
                'email'=>'required|email|max:255'
            ]);

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


            $user =User::where('email',$req->email)->where('user_type',1)->first();

            if($user){
                $is_student_proposal_exists =StudentProposal::where(
                    'teacher_id',$req->id
                )->where(
                    'student_id',$user->id
                )->whereIn(
                    'proposal_status',[1,2,3,4] // 1 =New; 2 =In Review; 3=Push; 4=Hold;
                )->exists();

                if($is_student_proposal_exists){
                    return response(
                        ['msg'=>'Already pending a proposal'],403
                    );
                }else{

                    $student_proposal =new StudentProposal;
                    $student_proposal->student_id =$user->id;
                    $student_proposal->teacher_id =$req->id;
                    $student_proposal->draft_status =1;
                    $student_proposal->save();

                    $refer =new ReferUser;
                    $refer->name =$req->name;
                    $refer->email =$req->email;
                    $refer->refer_by =$user->id;
                    $refer->teacher_id =$req->id;
                    $refer->q_form_status =1;
                    $refer->save();
                }
            }else{
                $user_data =new User;
                $user_data->first_name =$req->name;
                $user_data->email =$req->email;
                $user_data->user_type =1;
                $user_data->status =0;
                $user_data->register_type =3; // 1=invitation ; 2= landing/wishlish; 3 =q_form ; 4=student_wishlist
                $user_data->invitation_date =now();
                $user_data->wishlist_teacher_id =$req->id;
                $user_data->save();

                $student_proposal =new StudentProposal;
                $student_proposal->student_id =$user->id;
                $student_proposal->teacher_id =$req->id;
                $student_proposal->draft_status =1;
                $student_proposal->save();

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

                if($refer->save()){
                    $teacher = User::find($refer->teacher_id);

                    if($teacher->others_department){
                        $department =$teacher->others_department;
                    }else{
                        $department = cn($teacher,'department_name.name','');
                    }

                    if($teacher->university_name){
                        $university =$teacher->university_name;
                    }else{
                        $university = cn($teacher,'university.name','');
                    }


                    $data['name'] = $refer->name;
                    $data['student_id'] = $user->id;
                    $data['teacher'] = $teacher->first_name.' '.$teacher->last_name;
                    $data['arn'] = $teacher->ref_no;
                    $data['designation'] = $teacher->designation;
                    $data['department'] = $department;
                    $data['university'] = $university;
                    $data['email'] =$refer->email;
                    $data['receive_email']=$refer->email;
                    $user_mail = $refer->email;
                    $from = 'asraful@revinr.com';

                    Mail::send('email.q_form_email',$data, function ($message) use ($user_mail,$from) {
                        $message->from($from);
                        $message->to($user_mail)->subject('Invitation to submit your doctoral application');
                    });
                }
            }
            return response(
                ['msg'=>'Saved successfully.']
            );
        }elseif($name=="q_apply_wishlist_registration_save"){

            $validator=\Validator::make($req->all(), [
                'first_name'=>'required',
                'last_name'=>'required',
                'dob'=>'required',
                'gender'=>'required',
                'nationality'=>'required',
                'email'=>'required',
                'password_confirmation'=>'required',
                'password'=>[
                    'required',
                    'confirmed',
                    'min:8',
                ]
            ]);

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

            $token =Str::random(64);

            $user_check = User::where(
                'id',$req->id
            )->where(
                'sign_up',1
            )->first();


            if($user_check){
               // return redirect()->to("/login")->with('success','You are already registered. Please login here.');
               return response(
                ['msg'=>'You are already registered.','action'=>'redirect']

                );
            }

            $user_data = User::find($req->id);
            $user_data->first_name =$req->first_name;
            $user_data->last_name =$req->last_name;
            $user_data->first_name =$req->first_name;
            $user_data->nationality =$req->nationality;
            $user_data->dob =\Carbon\Carbon::parse($req->dob)->format('Y-m-d');
            $user_data->gender =$req->gender;
            $user_data->password =Hash::make($req->password);
            $user_data->status =0;
            $user_data->sign_up =1;
            $user_data->registered_date =now();
            //$user_data->email_verification=1;
            $user_data->remember_token=$token;
            //$user_data->email_verified_at=now();
            $user_data->update();


            $data['name'] = $user_data->first_name .' '.$user_data->last_name;
            $data['token']=$token;
            $data['receive_email']=$user_data->email;
            $user_mail = $user_data->email;
            $from = 'asraful@revinr.com';

            Mail::send('email.q_apply_wishlist_registration_email',$data, function ($message) use ($user_mail,$from) {
                $message->from($from);
                $message->to($user_mail)->subject('Thank You for joining us!');
            });

            return response(
                ['msg'=>'Successfully saved. Please check your email for verify.']
            );

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

            $validator=\Validator::make($req->all(), [
                'first_name'=>'required',
                'last_name'=>'required',
                'email'=>'required',
                'university'=>'required',
                'others_department'=>'required',
                'designation'=>'required',
                'password_confirmation'=>'required',
                'password'=>[
                    'required',
                    'confirmed',
                    'min:8',
                ]
            ]);

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

            $isExists = User::where(
                'id',$req->id
            )->where(
                'user_type',2
            )->where(
                'sign_up',1
            )->first();

            if($isExists){
                return response(
                    ['msg'=>'You are already registered.','action'=>'redirect']
                );
            }else{

                $user_data = User::find($req->id);
                $user_data->first_name =$req->first_name;
                $user_data->last_name =$req->last_name;
                $user_data->university_name =$req->university;
                $user_data->ref_no = name_initials($req->first_name,$req->last_name).''.teacherRefNoGenerate();
                //$user_data->department =$req->department;
                $user_data->others_department =$req->others_department;
                $user_data->designation =$req->designation;
                $user_data->uni_website =$req->website;
                $user_data->password =Hash::make($req->password);
                $user_data->status =1;
                $user_data->sign_up = 1; // 1=signup
                $user_data->user_type = 2;
                $user_data->registered_date = now();
                $user_data->update();

                if($user_data->update()){
                    $data['name'] = $user_data->first_name .' '.$user_data->last_name;
                    $data['receive_email']=$user_data->email;
                    $user_mail = $user_data->email;
                    $from = 'asraful@revinr.com';

                    Mail::send('email.register_teacher_email',$data, function ($message) use ($user_mail,$from) {
                        $message->from($from);
                        $message->to($user_mail)->subject('We’re excited to have you as a member of our community');
                    });
                }

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

        }elseif($name=="delete_doc_required"){
            $requirements=Requirement::find($req->delete_id);
            $requirements->delete();
            return response([
                'msg'=>'Deleted Successfully.'
            ]);
        }elseif($name=="document_required_upload_save"){

            $rules=[
                'id'=>'required',
                'document_file' => 'required',
            ];

            $validator=\Validator::make($req->all(), $rules);

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

            $data=Requirement::find($req->id);
            $std_ref = StudentProposal::find($data->std_proposal_id);

            if($req->document_file){
                $destinationPath =   public_path('/uploads/required_doc');
                $file = $req->document_file;
                $ext = $file->getClientOriginalExtension();
                $fileName = 'document_file_'.$std_ref->student_apply_ref.'.'.$ext;
                if(file_exists($destinationPath.'/'.$fileName)){
                    @unlink($destinationPath.'/'.$fileName);
                }
                $file->move($destinationPath, $fileName);

                $data->document_file = $fileName;

            }

            $data->update();

            return response([
                'msg'=>'Successfully uploaded.'
            ]);

        }elseif($name=="update_proposal"){
            $user=auth_user();
            $validator=\Validator::make($req->all(), [
                'title'=>'required',
                'methology'=>'required',
                'department'=>'required',
            ]);

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

            $data =Proposal::find($req->edit_id);
            $data->title =$req->title;
            $data->methology= $req->methology;
            $data->department= $req->department;
            $data->status= $req->status;

            if($req->documentfile){
                $destinationPath =   public_path('/uploads/proposal');
                $file = $req->documentfile;
                $ext = $file->getClientOriginalExtension();
                $fileName = time().'documentfile_'.'.'.$ext;
                if(file_exists($destinationPath.'/'.$fileName)){
                    @unlink($destinationPath.'/'.$fileName);
                }
                $file->move($destinationPath, $fileName);

                $data->document_file = $fileName;

            }
            $data->update();
            return response([
                'msg'=>'Successfully updated.'
            ]);
        }elseif($name=="edit_register_teacher_save"){
            $validator=\Validator::make($req->all(), [
                'first_name'=>'required',
                'last_name'=>'required',
                'university'=>'required',
                'department'=>'required',
                'designation'=>'required',
            ]);

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

            $user_data = User::find($req->edit_id);
            $user_data->first_name =$req->first_name;
            $user_data->last_name =$req->last_name;
            $user_data->university_id =$req->university;
            $user_data->university_name = NULL;
            $user_data->department =$req->department;
            $user_data->others_department = NULL;
            $user_data->designation =$req->designation;
            $user_data->uni_website =$req->uni_website;
            $user_data->update();
            return response(
                ['msg'=>'Successfully updated.']
            );
        }elseif($name=="landing_invitation_email_send"){
            $user_data =User::find($req->id);
            $user_data->invitation_date =now();
            $user_data->update();

            if($user_data->update()){
                $data['name'] = $user_data->first_name .' '.$user_data->last_name;
                $data['receive_email']=$user_data->email;
                $user_mail = $user_data->email;
                $data['teacher_id'] = $user_data->id;
                $data['email'] = $user_data->email;
                $from = 'asraful@revinr.com';

                Mail::send('email.invitation_to_supervisor_email',$data, function ($message) use ($user_mail,$from) {
                    $message->from($from);
                    $message->to($user_mail)->subject('Manage Doctoral Applicants with Ease - You’re invited!');
                });
            }

            return response(
                ['msg'=>'Invitation sent successfully.']
            );


        }elseif($name=="delete_research_info"){
            $user=Auth::guard('teacher')->user();
            $data =ResearchInterest::find($req->delete_id);
            $data->delete();
            return response([
                'msg'=>'Successfully Deleted'
            ]);
        }elseif($name=="edit_research_save"){
            $user=Auth::guard('teacher')->user();
            $validator=\Validator::make($req->all(), [
                'research'=>'required',
            ]);

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

            $data =ResearchInterest::find($req->edit_id);
            $data->research =$req->research;
            $data->update();
            return response([
                'msg'=>'Successfully updated.'
            ]);
        }elseif($name=="add_proposal_publication"){
            $student=Auth::user();

            $validator=\Validator::make($req->all(), [
                'publication'=>'required',
                'status'=>'required',
            ]);

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

            $data =new Publication;
            $data->student_id =$student->id;
            $data->publication =$req->publication;
            $data->status =$req->status;
            $data->save();
            return response([
                'msg'=>'Successfully saved.'
            ]);
        }elseif($name=="update_publication"){
            $student=Auth::user();

            $validator=\Validator::make($req->all(), [
                'publication'=>'required',
                'status'=>'required',
            ]);

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

            $data =Publication::find($req->edit_id);
            $data->student_id  = $student->id;
            $data->publication = $req->publication;
            $data->status = $req->status;
            $data->update();
            return response([
                'msg'=>'Successfully updated.'
            ]);
        }elseif($name=="delete_publication_info"){
            $data =Publication::find($req->delete_id);
            $data->delete();
            return response([
                'msg'=>'Successfully deleted.'
            ]);
        }elseif($name=="is_attended_conference_status"){

            $data =User::find($req->uid);
            if($req->checked =='yes'){
                $data->is_attended_conference =1; // 1=yes
            }else{
                $data->is_attended_conference =2; // 2=no
            }

            $data->update();

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

        }elseif($name=="work_experience_status_update"){
            $data =User::find($req->uid);
            if($req->value =='yes'){
                $data->is_work_experience =1; // 1=yes
            }else{
                $data->is_work_experience =2; // 2=no
            }

            $data->update();

            return response([
                'msg'=>'Successfully saved.'
            ]);
        }elseif($name=="publication_status_update"){
            $data =User::find($req->uid);
            if($req->value =='yes'){
                $data->is_publication =1; // 1=yes
            }else{
                $data->is_publication =2; // 2=no
            }

            $data->update();

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

        }elseif($name=="education_status_update"){
            $data =User::find($req->uid);
            if($req->value =='yes'){
                $data->is_education =1; // 1=yes
            }else{
                $data->is_education =2; // 2=no
            }

            $data->update();

            return response([
                'msg'=>'Successfully saved.'
            ]);
        }elseif($name=="update_education_history"){
            $student=Auth::user();

            $validator=\Validator::make($req->all(), [
                'academiclevel'=>'required',
                'subject'=>'required',
                'institution'=>'required',
                'passingyear'=>'required',
                'result'=>'required',
            ]);

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

            $institution= Institution::find($req->edit_id);
            $institution->student_id= $student->id;
            $institution->academic_level= $req->academiclevel;
            $institution->subject= $req->subject;
            $institution->institution= $req->institution;
            $institution->passing_year= $req->passingyear;
            $institution->result= $req->result;
            $institution->update();
            return response([
                'msg'=>'Successfully updated.'
            ]);
        }elseif($name=="delete_education_info"){
            $data =Institution::find($req->delete_id);
            $data->delete();
            return response([
                'msg'=>'Successfully deleted.'
            ]);
        }elseif($name=="update_english_languge_proficiency"){
            $student=Auth::user();

            $validator=\Validator::make($req->all(), [
                'testdate'=>'required',
                'expirationdate'=>'required',
                'testname'=>'required',
                'overallscore'=>'required',
                'listening'=>'required',
                'speaking'=>'required',
                'writing'=>'required',
                'reading'=>'required',
            ]);

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

            $score= Score::find($req->edit_id);
            $score->student_id= $student->id;
            $score->test_date= Carbon::parse($req->testdate)->format('Y-m-d');
            $score->expiration_date= Carbon::parse($req->expirationdate)->format('Y-m-d');
            $score->test_name= $req->testname;
            $score->overall_score= $req->overallscore;
            $score->listening= $req->listening;
            $score->writing= $req->speaking;
            $score->reading= $req->writing;
            $score->speaking= $req->reading;
            $score->update();
            return response([
                'msg'=>'Successfully updated.'
            ]);

        }elseif($name=="delete_score_info"){
            $data =Score::find($req->delete_id);
            $data->delete();
            return response([
                'msg'=>'Successfully deleted.'
            ]);
        }elseif($name=="update_work_experience"){
            $student=Auth::user();

            $validator=\Validator::make($req->all(), [
                'companyname'=>'required',
                'designation'=>'required',
                'startdate'=>'required',
                'enddate'=>'required',
            ]);

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

            $work_experience= Workexperience::find($req->edit_id);
            $work_experience->student_id= $student->id;
            $work_experience->company_name= $req->companyname;
            $work_experience->designation= $req->designation;
            if($req->currently_working=='on'){
                $work_experience->currently_working=1;
            }else{
                $work_experience->currently_working=0;
            }

            $work_experience->start_date= date($req->startdate.'-'.$req->startmonth.'-01');
            $work_experience->end_date= date($req->enddate.'-'.$req->endmonth.'-01');
            $work_experience->update();

            return response([
                'msg'=>'Successfully updated.'
            ]);
        }elseif($name=="delete_work_experience_info"){
            $data =Workexperience::find($req->delete_id);
            $data->delete();
            return response([
                'msg'=>'Successfully deleted.'
            ]);
        }elseif($name=="test_score_status_update"){
            $data =User::find($req->uid);
            if($req->value =='yes'){
                $data->is_test_score =1; // 1=yes
            }else{
                $data->is_test_score =2; // 2=no
            }

            $data->update();

            return response([
                'msg'=>'Successfully saved.'
            ]);
        }elseif($name=="all_publication_delete"){
            Publication::where('student_id',$req->uid)->delete();
            return response([
                'msg'=>'Successfully delete.'
            ]);

        }elseif($name=="all_education_history_delete"){
            Institution::where('student_id',$req->uid)->delete();
            return response([
                'msg'=>'Successfully delete.'
            ]);

        }elseif($name=="all_work_experience_delete"){
            Workexperience::where('student_id',$req->uid)->delete();
            return response([
                'msg'=>'Successfully delete.'
            ]);

        }elseif($name=="all_test_score_delete"){
            Score::where('student_id',$req->uid)->delete();
            return response([
                'msg'=>'Successfully delete.'
            ]);

        }elseif($name=="update_feedback_msg"){
            $data =StudentProposal::find($req->edit_id);
            $data->feedback_msg =$req->feedback_msg;
            $data->update();

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

        }elseif($name=="delete_student_feedback_msg"){
            $data =StudentProposal::find($req->delete_id);
            $data->feedback_msg =NULL;
            $data->update();

            return response(['msg'=>'Deleted successfully.']);
        }


	}
}
