<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Institution;
use App\Workexperience;
use App\Score;
use App\User;
use App\Proposal;
use App\StudentProposalUpload;
use App\StudentProposal;
use App\AcademicLevel;
use App\Models\Department;
use App\Models\Publication;
use Auth;
use Carbon\Carbon;


class StudentHomeController extends Controller
{

   public function __construct()
   {
       $this->middleware('auth');
   }

   public function index(){
      $user = Auth::user();
      $student_proposal =StudentProposal::where(
         'student_id',$user->id
      )->where(
         'proposal_submit_status',1
      )->orderBy('id','DESC')->get();
      return view('application.home',compact('student_proposal'));
   }

   public function application(){
      $user = Auth::user(); 
      $proposal= Proposal::where('student_id',$user->id)->doesntExist();
      $publication =User::where('id',$user->id)->where('is_publication',0)->exists(); 
      $worK_exp =User::where('id',$user->id)->where('is_work_experience',0)->exists(); 
      $institution =User::where('id',$user->id)->where('is_education',0)->exists();
      $score =User::where('id',$user->id)->where('is_test_score',0)->exists();  
      $student_proposal =StudentProposal::where('student_id',$user->id)->orderBy('id','DESC')->get();
      $data =[
         'proposal'=>$proposal,
         'publication'=>$publication,
         'worK_exp'=>$worK_exp,
         'institution'=>$institution,
         'score'=>$score,
         'student_proposal'=>$student_proposal,
      ];
      return view('application.application',$data);
   }

   public function profile()
    {
         $user = Auth::user();
         $publications= Publication::where('student_id',$user->id)->get();
         $institution= Institution::where('student_id',Auth::user()->id)->get();
         $work_experience= Workexperience::where('student_id',Auth::user()->id)->get();
         $score= Score::where('student_id',$user->id)->get();
         $proposal= Proposal::where('student_id',$user->id)->orderBy('id','DESC')->get();
         $user_info= User::find($user->id);
         $academic_level= AcademicLevel::all();
         $proposal_files= StudentProposalUpload::where('student_id',$user->id)->get();
         $departments = Department::where('status',1)->orderBy('name','ASC')->get();  

         $is_freez_profile = User::where(
            'id',$user->id
         )->where(
            'freez_profile','!=',0 
         )->exists();   
    
        return view('application.profile',compact('publications','is_freez_profile','institution','academic_level','work_experience','score','user_info','proposal','proposal_files','departments'));
    }




    public function institution_save(Request $request)
    {
        $student =Auth::user(); 
        $institution= New  Institution;
        $institution->student_id= Auth::user()->id;
        $institution->academic_level= $request->academiclevel;
        $institution->subject= $request->subject;
        $institution->institution= $request->institution;
        $institution->passing_year= $request->passingyear;
        //$institution->during= $request->during;
        $institution->result= $request->result;
        //$institution->start_date= date($request->startdate.'-'.$request->startmonth.'-01');
        //$institution->end_date= date($request->enddate.'-'.$request->endmonth.'-30');
        $institution->save();

        $data = User::find($student->id); 
        $data->is_education =1;
        $data->update();

        return redirect()->back()->with('Added successfully.');
    }

    public function work_save(Request $request)
    {
        $work_experience= New  Workexperience;
        $work_experience->student_id= Auth::user()->id;
        $work_experience->company_name= $request->companyname;
        $work_experience->designation= $request->designation;
        $work_experience->currently_working= $request->currentlyworking ? 0 : 1;
        $work_experience->start_date= date($request->startdate.'-'.$request->startmonth.'-01');
        if($request->enddate && $request->endmonth){
        $work_experience->end_date= date($request->enddate.'-'.$request->endmonth.'-01');
        }
        $work_experience->save();
        return redirect()->back()->with('Added successfully.');
    }

     public function score_save(Request $request)
     {
         $score= New Score;
         $score->student_id= Auth::user()->id;
         $score->test_date= Carbon::parse($request->testdate)->format('Y-m-d');
         $score->expiration_date= Carbon::parse($request->expirationdate)->format('Y-m-d');
         $score->test_name= $request->testname;
         $score->overall_score= $request->overallscore;
         $score->listening= $request->listening;
         $score->writing= $request->speaking;
         $score->reading= $request->writing;
         $score->speaking= $request->reading;
         $score->save();
        return redirect()->back()->with('Added successfully.');
     }

     public function proposal_save(Request $request)
     {
         $student_id =Auth::user()->id;
         $proposal= New  Proposal;
         $proposal->student_id= $student_id;
         $proposal->proposal_ref= proposalRefNoGenerate($student_id);
         $proposal->title= $request->title;
         $proposal->methology= $request->methology;
         $proposal->department= $request->department;
         $proposal->status= $request->status;

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

            $proposal->document_file = $fileName;
            $proposal->file_type = $ext;
         }
         $proposal->save();
         return redirect()->back()->with('Added successfully.');
     }

     public function save_as_complete()
     {
         $user= User::find(Auth::user()->id);
         $user->save_as_complete= 1;
         $user->save();
        return redirect()->back()->with('Added successfully.');
     }

     public function education_remove($id)
     {
        $institution= Institution::find($id);
        $institution->delete();
     }

     public function work_remove($id)
     {
        $work_experience= Workexperience::find($id);
        $work_experience->delete();
     }

     public function score_remove($id)
     {
        $score=  Score::find($id);
        $score->delete();
     }

     public function proposal_remove($id)
     {
        $proposal=  Proposal::find($id);
        $proposal->delete();
     }

     public function institution_update($id,Request $request)
     {
      $institution= Institution::find($id);
      $institution->student_id= Auth::user()->id;
      $institution->academic_level= $request->academiclevel;
      $institution->subject= $request->subject;
      $institution->institution= $request->institution;
      $institution->passing_year= $request->passingyear;
      $institution->during= $request->during;
      $institution->result= $request->result;
      $institution->start_date= date($request->startdate.'-'.$request->startmonth.'-01');
      $institution->end_date= date($request->enddate.'-'.$request->endmonth.'-30');
      $institution->update();
      return redirect()->back()->with('Updated successfully.');
     }

     public function work_update($id,Request $request)
     {
      $work_experience= Workexperience::find($id);
      $work_experience->student_id= Auth::user()->id;
      $work_experience->company_name= $request->companyname;
      $work_experience->designation= $request->designation;
      $work_experience->currently_working= $request->checkbox;
      $work_experience->start_date= date($request->startdate.'-'.$request->startmonth.'-01');
      $work_experience->end_date= date($request->enddate.'-'.$request->endmonth.'-01');
      $work_experience->update();
      return redirect()->back()->with('Updated successfully.');
     }

     public function score_update($id,Request $request)
     {
        $score= Score::find($id);
        $score->student_id= Auth::user()->id;
        $score->test_date= Carbon::parse($request->testdate)->format('Y-m-d');
        $score->expiration_date= Carbon::parse($request->expirationdate)->format('Y-m-d');
        $score->test_name= $request->testname;
        $score->overall_score= $request->overallscore;
        $score->listening= $request->listening;
        $score->writing= $request->speaking;
        $score->reading= $request->writing;
        $score->speaking= $request->reading;
        $score->update();
        return redirect()->back()->with('Updated successfully.');
     }

     public function proposal_update($id,Request $request)
     {
        $proposal=Proposal::find($id);
        $proposal->student_id= Auth::user()->id;
        $proposal->title= $request->title;
        $proposal->methology= $request->methology;
        $proposal->department= $request->department;
        $proposal->status= $request->status;

        $proposal->update();
        return redirect()->back()->with('Added successfully.');
     }

   //   public function proposal_media_upload(Request $request)
   //   {
   //          $student_doc=new StudentProposalUpload;
   //          $student_doc->student_id=Auth::user()->id;
   //          $student_doc->created_by=Auth::user()->id;
   //      if($request->documentfile!=''){
   //          $destinationPath =   public_path('/uploads/proposal');
   //          $file = $request->documentfile;
   //          $ext = $file->getClientOriginalExtension();
   //          $fileName = time().'documentfile_'.'.'.$ext;
   //          if(file_exists($destinationPath.'/'.$fileName)){
   //              @unlink($destinationPath.'/'.$fileName);
   //          }
   //          $file->move($destinationPath, $fileName);

   //          $student_doc->document_file = $fileName;
   //          $student_doc->file_type = $ext;
   //          $student_doc->save();
   //          return redirect()->back()->with('Added successfully.');
   //      }
   //   }

     public function change_work_experience($status)
     {
        $change_work_experience= Auth::user();
        $change_work_experience->is_work_experience=$status;
        $change_work_experience->update();
     }
      public function change_test_score($status)
     {
        $change_test_score= Auth::user();
        $change_test_score->is_test_score=$status;
        $change_test_score->update();
     }


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

        return view('ref_search_load',$data);
     }

     public function logout(){
        \Auth::guard()->logout();
        request()->session()->invalidate();
        return redirect('/applicant/login');

    }

}