<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\StudentProposal;
use App\Models\Department;
use App\Models\ReferUser;
use App\Models\University;
use App\Models\Requirement;
use App\Institution;
use App\Workexperience;
use Auth;
use Input;

class TeacherHomeController extends Controller
{
    public function __construct()
   {
       $this->middleware('teacher');
   }

    public function dashboard(Request $request)
    {   $user = Auth::guard('teacher')->user();
       $students = StudentProposal::where(
           'teacher_id',$user->id
       )->where(
           'proposal_submit_status',1
       )->get();
       $departments =Department::where('status',1)->orderBy('name','ASC')->get();
      // $refer_users =ReferUser::where('teacher_id',$user->id)->orderBy('id','DESC')->get();
       $refer_users =User::where(
           'wishlist_teacher_id',$user->id
       )->whereIn(
           'register_type',[3,4] // 3=q_form; 4= wishlist
       )->orderBy('id','DESC')->get();

       $universities =University::orderBy('name','ASC')->get();
       return view('supervisor.dashboard',compact('students','user','departments','refer_users','universities'));
    }

    public function applications(){
        $user = Auth::guard('teacher')->user();
        $students = StudentProposal::where(
            'teacher_id',$user->id
        )->where(
            'proposal_submit_status',1
        )->get();
        return view('supervisor.applications',compact('user','students'));
    }

    public function search(Request $request)
    {
        $input = Input::all();
        $request = (object) $input;
        $carbon=new \Carbon\Carbon;
        $teacher = Auth::guard('teacher')->user();

        $students = StudentProposal::where(
            'proposal_submit_status',1
        )->where(
            'teacher_id',$teacher->id
        )->orderBy('id','desc');

        if ($request->apply_ref) {
            $students->where('student_apply_ref',$request->apply_ref);
        }

        if (isset($input['proposal_status'])) {
            $students->where('proposal_status',$input['proposal_status']);
        }

        if($request->submitted_date){
            $date_array = explode("to",$request->submitted_date);

            $from_date = date('d-m-Y', strtotime($date_array[0]));
            $to_date = date('d-m-Y', strtotime($date_array[1]));

            $start_date=$carbon->parse($from_date)->toDateString();
            $end_date=$carbon->parse($to_date)->toDateString();


            $students->whereRaw("DATE(submitted_date) >= ? AND DATE(submitted_date) <= ?", [$start_date, $end_date]);
        }

        if($request->preffered_start_date){
            $date_array = explode("to",$request->preffered_start_date);

            $from_month_year = date('m-Y', strtotime($date_array[0])); //01-06-2023
            $to_month_year = date('m-Y', strtotime($date_array[1]));//31-07-2023

            $start_date=$carbon->parse('01-'.$from_month_year)->toDateString();
            $end_date=$carbon->parse('01-'.$to_month_year)->toDateString();

            $students->whereRaw("DATE(preferred_start_date) >= ? AND DATE(preferred_start_date) <= ?", [$start_date, $end_date]);
        }

        $data=[
            'students' => $students->get(),
            'user'=> Auth::guard('teacher')->user(),
        ];

        return view('supervisor.load_application',$data);
    }

    public function wishlist(){
        $user = Auth::guard('teacher')->user();
        $refer_users =User::where(
            'wishlist_teacher_id',$user->id
        )->whereIn(
            'register_type',[3,4] // 3=q_form; 4= wishlist
        )->orderBy('id','DESC')->get();
        return view('supervisor.wishlist',compact('user','refer_users'));
    }

    public function explore(){
        $user = Auth::guard('teacher')->user();
        return view('supervisor.explore');
    }

    public function my_account(){
        $user = Auth::guard('teacher')->user();
        $universities =University::orderBy('name','ASC')->get();
        $departments =Department::where('status',1)->orderBy('name','ASC')->get();
        return view('supervisor.my_account',compact('user','departments','universities'));
    }



    public function student_proposal_details($hash){

        $user = Auth::guard('teacher')->user();
        $hashids=new \Hashids\Hashids('application', 25);
        $hash_ids=$hashids->decode($hash);
        if(empty($hash_ids[0])) abort(404);

        $proposal_details = StudentProposal::find($hash_ids[0]);
        $education_history =Institution::where(
            'student_id',$proposal_details->student_id
        )->orderBy('id','desc')->get();

        $experience =Workexperience::where(
            'student_id',$proposal_details->student_id
        )->get();

        if(isset($experience)){
            $work_experince =[];
            foreach($experience as $key=>$row){
                array_push($work_experince,$row->start_date);
                array_push($work_experince,$row->end_date);
            }

            $start_date = current($work_experince);
            $end_date = end($work_experince);
        }

        $doc_info =Requirement::where(
            'std_proposal_id',$proposal_details->id
        )->get();

        return view('supervisor.student_proposal_details',compact('hash_ids','proposal_details','education_history','start_date','end_date','doc_info'));
    }

}
