<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use \App\Helpers\PageSL;
use App\Models\University;
use App\Models\MarketCountry;
use App\Models\Department;
use App\Models\ResearchInterest;
use App\Models\Requirement;
use App\Models\Publication;
use App\StudentProposal;
use App\AcademicLevel;
use App\Institution;
use App\Proposal;
use App\Score;
use App\Workexperience;
use App\User;
use Auth;

class SubContentController extends Controller
{
    public function get(Request $req, $name){


		$carbon=new \Carbon\Carbon;

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

		if($name=="load_market_countries"){
			$user=Auth::guard('admin')->user();
			$data['market_countries'] = MarketCountry::where(
                'status', 1
            )->select(
                'market_countries.*', \DB::raw('(SELECT name FROM countries WHERE market_countries.country_id = countries.id ) as sort')
            )->orderBy('sort')->get();

		}elseif($name=='country_wise_university'){
			$user=Auth::guard('admin')->user();
			$data['universities'] =University::orderBy('name','ASC')->get();

		}elseif($name=="load_profile_info"){
			$tuser=Auth::guard('teacher')->user();
			$data['tuser'] =$tuser;
			$data['students'] = StudentProposal::where('teacher_id',$tuser->id)->get();

		}elseif($name=="load_department"){
			$data['departments'] =Department::where('status',1)->orderBy('name','ASC')->get();

		}elseif($name=="load_research_interest"){
			$tuser=Auth::guard('teacher')->user();
			$data['resarch'] =ResearchInterest::where('teacher_id',$tuser->id)->orderBy('id','DESC')->get();
		}elseif($name=="edit_university_load"){
			$user=Auth::guard('admin')->user();
			$data['edit_data'] =University::find($req->edit_id);
			$data['market_countries'] = MarketCountry::where(
                'status', 1
            )->select(
                'market_countries.*', \DB::raw('(SELECT name FROM countries WHERE market_countries.country_id = countries.id ) as sort')
            )->orderBy('sort')->get();

		}elseif($name=="edit_department_form"){
			$data['edit_dept'] =Department::find($req->dep_id);

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

            if($req->teacher_ref_no){
                $data['teacher'] =User::where(
                    'ref_no',$req->teacher_ref_no
                )->where(
                    'is_available',1
                )->first();
            }

        }elseif($name=="document_required_load"){
			$proposal_details = StudentProposal::find($req->id);
			$data['proposal_details'] =$proposal_details;
			$data['requirements'] = Requirement::where('std_proposal_id',$proposal_details->id)->get();
		}elseif($name=="upload_document_required"){
			$data['requirements'] = Requirement::find($req->req_id);
			$std_pro_id =$data['requirements'];
			$data['proposal_details'] = StudentProposal::where('id',$std_pro_id->std_proposal_id)->first();
		}elseif($name=="edit_proposal"){
			$data['proposal'] =Proposal::find($req->p_id);
			$data['departments'] =Department::where('status',1)->orderBy('name','ASC')->get();

		}elseif($name=="register_pending_teacher_load"){
			$data['register_teachers_list'] =User::whereIn(
                'apply_status', [0,2,3] // 0 = Pending; 2= In active ; 3= Suspend
            )->where(
                'user_type',2
            )->where(
				'sign_up',1
			)->orderBy(
                'id','DESC'
            )->get();
		}elseif($name=="edit_teacher_load"){
			$user=Auth::guard('admin')->user();
			$data['departments'] =Department::where('status',1)->orderBy('name','ASC')->get();
            $data['universities'] =University::orderBy('name','ASC')->get();
			$data['edit_data'] =User::find($req->edit_id);
		}elseif($name=="edit_research_form"){
			$data['edit_research'] =ResearchInterest::find($req->res_id);

		}elseif($name=="publication_list_load"){
			$student=Auth::user();
            $data['publications'] = Publication::where(
				'student_id',$student->id
			)->orderBy(
				'id','DESC'
			)->get();

			$data['is_freez_profile'] = User::where(
				'id',Auth::user()->id
			 )->where(
				'freez_profile','!=',0
			 )->exists();

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

			$data['edit_data']=Publication::find($req->edit_id);

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

			$data['institution']= Institution::where(
				'student_id',Auth::user()->id
			)->orderBy(
				'id','DESC'
			)->get();

			$data['is_freez_profile'] = User::where(
				'id',Auth::user()->id
			 )->where(
				'freez_profile','!=',0
			 )->exists();

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

			$data['edit_data']=Institution::find($req->edit_id);
			$data['academic_level']= AcademicLevel::orderBy('name','ASC')->get();
			$data['passingyear']= range((int)date('Y')-20,(int)date('Y'));

		}elseif($name=="english_languge_proficiency_load"){
			$student=Auth::user();
			$data['score']= Score::where(
				'student_id',$student->id
			)->orderBy(
				'id','ASC'
			)->get();
			$data['is_freez_profile'] = User::where(
				'id',Auth::user()->id
			 )->where(
				'freez_profile','!=',0
			 )->exists();

		}elseif($name=="edit_english_languge_proficiency_form"){
			$student=Auth::user();
			$data['edit_data']= Score::find($req->edit_id);

		}elseif($name=="work_experience_load"){
			$data['work_experience']= Workexperience::where(
				'student_id',Auth::user()->id
				)->orderBy('id','DESC')->get();
			$data['is_freez_profile'] = User::where(
				'id',Auth::user()->id
				)->where(
				'freez_profile','!=',0
				)->exists();
		}elseif($name=="edit_work_experience_form"){
			$student=Auth::user();
			$data['edit_data']= Workexperience::find($req->edit_id);
			$data['years']= range((int)date('Y')-20,(int)date('Y'));
		}elseif($name=="edit_feedback_msg_form"){
			$data['edit'] =StudentProposal::find($req->std_propsal_id);

		}else $name='default';
		//$data['page_sl']=new PageSL($data['chapter_details']);
		return view("admin.sub_contents.{$name}", $data);

	}
}
