<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Models\Note;
use Auth;
use DB;
use App\Models\Attendance;
use Excel;
class UserController extends Controller
{
    public function index(){
        $user_id    =   Auth::id();
        $user_info  =   User::find($user_id);
        return view('user.profile',compact('user_info'));
    }

    public function update_profile(Request $request)
    {

        $this->validate($request, [
            'address' => 'required',
            'per_address' => 'required',
            'mob' => 'required',
            'email' => 'required',
            'joining_date' => 'required',
            'fb' => 'required',
            'skype' => 'required',
            'linkedIn' => 'required',
            'emrgncy_contact' => 'required',
            'emrgncy_contact_phone' => 'required',
        ]);
        $user_id    =   Auth::id();
        $user_info  =   User::find($user_id);
        $destinationPath = public_path('user/');
        $older_user_img= $user_info->image;
        
        if($request->file('user_image') !=''){
            if($older_user_img !=''){
                @unlink($destinationPath.'/'.$older_user_img);
            }
        } else {
            $user_info->image=$older_user_img;
        }
        $user_info->address                 =  $request->input('address');
        $user_info->per_address             =  $request->input('per_address');
        $user_info->mob                     =  $request->input('mob');
        $user_info->mob2                    =  $request->input('mob2');
        $user_info->email                   =  $request->input('email');
        $user_info->email2                  =  $request->input('email2');
        $user_info->joining_date            =  $request->input('joining_date');
        $user_info->fb                      =  $request->input('fb');
        $user_info->skype                   =  $request->input('skype');
        $user_info->linkedIn                =  $request->input('linkedIn');
        $user_info->emrgncy_contact         =  $request->input('emrgncy_contact');
        $user_info->emrgncy_contact_phone   =  $request->input('emrgncy_contact_phone');
        $user_info->save();
        if($request->file('user_image') !=''){
            $file=$request->file('user_image');
            $originalName =$file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension(); 
            $imageName = 'user_'.$user_id.'.'.$extension;
            $file->move($destinationPath, $imageName);
            $image_name=$imageName;
            DB::table('users')->where('id','=',$user_id)->update(array('image'=>$image_name));
        }
        return redirect()->action('User\UserController@index')->with('success','Profile Updated successfully');
    }
    public function group(){

        return "group";
    }

    public function add_note(Request $req)
    {
        $note = $req->note;
        $date_time = $req->date_time;

        $sav = new Note;
        $sav->note = $note;
        $sav->user_id = Auth::user()->id;
        $sav->time = $date_time;
        $sav->save();

        return redirect()->back();
    }

    public function change_note_status($id){

        $ch = Note::find($id);
            if($ch->status == 1){
                $ch->status = 0;
            }
            else{
                $ch->status = 1;
            }
        $ch->save();

        return redirect()->back();
    }

    public function remove_note($id){

        $del = Note::find($id)->delete();

        return redirect()->back();
    }
    public function attend_entry()
    {
        $title="Attendance Entry";
        return view('user.list',compact('title'));
    }
    public function attendace_sheet_entry(Request $request)
    {
        date_default_timezone_set("Asia/Dhaka");
        //
        //
        if($request->file('import_file') !=''){
            $path = $request->file('import_file')->getRealPath();
            $data = Excel::load($path, function($reader) {})->get();
            if(!empty($data) && $data->count()){

                foreach ($data->toArray() as $key => $value) {
                    if( (int)$value['user_id'] == 1)
                    {
                        $user_name  =   'Reza';
                    } 
                    else if((int)$value['user_id'] == 2)
                    {
                        $user_name  =   'Mamun';
                    }
                    else if((int)$value['user_id'] == 3)
                    {
                        $user_name  =   'Golam Sayed';
                    }
                    else if((int)$value['user_id'] == 4)
                    {
                        $user_name  =   'Akib';
                    }
                    else if((int)$value['user_id'] == 5)
                    {
                        $user_name  =   'Masum';
                    }
                    else if((int)$value['user_id'] == 6)
                    {
                        $user_name  =   'Helal';
                    }
                    else if((int)$value['user_id'] == 7)
                    {
                        $user_name  =   'Zeba';
                    }
                    else if((int)$value['user_id'] == 8)
                    {
                        $user_name  =   'Shabuj';
                    }
                    else if((int)$value['user_id'] == 9)
                    {
                        $user_name  =   'Hasib';
                    }
                    else if((int)$value['user_id'] == 10)
                    {
                        $user_name  =   'Tipu';
                    }
                    else if((int)$value['user_id'] == 11)
                    {
                        $user_name  =   'Niloy';
                    }
                    else if((int)$value['user_id'] == 12)
                    {
                        $user_name  =   'Nishat';
                    }
                    else if((int)$value['user_id'] == 13)
                    {
                        $user_name  =   'Jamil';
                    }
                    else if((int)$value['user_id'] == 14)
                    {
                        $user_name  =   'Shamim';
                    }
                    else if((int)$value['user_id'] == 15)
                    {
                        $user_name  =   'Payer';
                    }  
                    $att_time   =   date('Y-m-d H:i:s',strtotime($value['attendance_time']));
                    $absent_count=(int)$value['absent_count'];
                    if($user_name  !=   'Hasib'){
                        $insert[] = ['user_id'=>(int)$value['user_id'],'user_name' => $user_name, 'attendance_time' =>$att_time,'absent_count'=>$absent_count,'status'=>(int)$value['status']];
                    }
                    
                }
                
                if(!empty($insert)){
                    Attendance::insert($insert);
                    return redirect()->action('User\UserController@attend_entry')->with('success', 'Data Added!');
                    
                }

            }

        }

        return back()->with('success','Please Check your file, Something is wrong there.');
    }
    public function leave_apply()
    {
        $title          =   "Apply For Leave";
        $leave_type     =   DB::table('leave_type_head')->where('is_published','=',1)->get();
        $users          =   User::where('utype',1)->where('active',1)->orderBy('name','ASC')->get();
        return view('user.leave',compact('title','leave_type','users'));
    }
    public function leave_req(Request $request)
    {
        $purpose= $request->purpose;
        $apply_to= 2;
        $from= $request->get('from',date('Y-m-d H:i:s'));
        $to= $request->get('to',date('Y-m-d H:i:s'));
        $from_day = strtotime($from);
        $to_day = strtotime($to);
        $days_between = (ceil(abs( $to_day - $from_day) / 86400))+1;
        $description    =   $request->description;
        $approval    =   0;
        $created_at  =  date('Y-m-d H:i:s');
        $user_id    =   Auth::id();
        DB::table('leave_req')->insert(array(
                    'purpose'=>$purpose,
                    'apply_to'=>$apply_to,
                    'from'=>$from,
                    'to'=>$to,
                    'days'=>$days_between,
                    'description'=>$description,
                    'approval'=>$approval,
                    'created_at'=>$created_at,
                    'user_id'=>$user_id,
        ));
        return redirect()->action('User\UserController@leave_apply')->with('success','Sccessfully Sent !');
    }

}
