<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Project;
use App\Models\AssignProject;
use App\Models\Group;
use App\Models\Task;
use App\Models\ActivityLog;
use App\Models\Notification;
use App\Models\ProjAttachment;
use App\Models\Note;
use App\User;
use Auth;
use Illuminate\Support\Facades\File;

use Illuminate\Support\Facades\DB;

class UProjectController extends Controller
{
    public function index(){    

        $data['title'] = "User panel";

        $data['login_user_id'] =  Auth::user()->id;
        $user_id = Auth::user()->id; 
        
        $project_id = Project::where('is_active',1)->where('client_id',$user_id)->pluck('id')->toArray();
        //dd($project_id);
        
        //  $data['projj'] = AssignProject::with('proj_name.tasks','one_user','assigned')->whereHas('proj_name', function($a){return $a->where('is_active',1);})->where('user_id',$user_id)->orderBy('id','desc')->get();
        //dd($data['projj']);
        $data['tasks'] = Task::with('company_name')->where('user_id',$user_id)->where('status','<',6)->orderBy('assign_time','desc')->get();
        $data['notes'] = Note::where('user_id',$user_id)->orderBy('time')->get();



        $chk = AssignProject::where('pm_status',1)->where('user_id',$user_id)->get();

        $proj_id_ary = array();

        if(count($chk) > 0)
        {
            foreach($chk as $info)
            {
                array_push($proj_id_ary, $info->proj_id);
            }

            $data['pm_created_task'] = Task::whereIn('proj_id',$proj_id_ary)->where('status',1)->get();
            $data['pm_inprocess_task'] = Task::whereIn('proj_id',$proj_id_ary)->where('status',2)->get();
        }

        
        return view('user.uprojects',$data);
    }

    public function add_file(Request $req){

        $file = $req->file('file');
        $proj_id = $req->proj_id;

        date_default_timezone_set("Asia/Dhaka");
        
        if ($req->hasFile('file')) {
            $extension = $file->getClientOriginalExtension();
            $os = array("jpg", "jpeg", "png", "pdf", "doc", "docx", "txt", "psd");
            if (in_array($extension, $os)){
                $name = $file->getClientOriginalName();
                $newFileName = $newFileName = "proj_".date('d_m_y_h_m_s').".".$extension;;
                $fileee = $file->move('assets/document/project/', $newFileName);

                //echo $newFileName;
                $cat = new ProjAttachment; 
                $cat->url = $newFileName;
                $cat->uploader_id = Auth::user()->id;
                $cat->proj_id = $proj_id;
                $cat->name = $name;
                $cat->save();

                $msg = "Add document: $name";
                $this->project_log($msg,$proj_id);  //call function
               
                //notification
                $projj_name = Project::find($proj_id)->name;
                $adminss = User::where('utype',1)->get();
                
                foreach($adminss as $info){
                    $msg = "File:$name added in Proj:$projj_name by ".Auth::user()->name;
                    $this->notification($info->id, $msg);
                }
                
                $ass_usr = AssignProject::where('proj_id',$proj_id)->get();

                if(count($ass_usr) > 0){
                    foreach($ass_usr as $info){
                        $msg = "File:$name added in Proj:$projj_name by ".Auth::user()->name;
                        $this->notification($info->user_id, $msg);
                    }    
                }
            }
        }
    }

    public function uproj_inprocess_pending_task($id){
        
        $data['title'] = "Project details";
        $data['users'] = User::where('active',1)->get();
        $data['groups'] = Group::where('user_id',0)->get();
        $data['self'] = Project::find($id);
        $data['project_id'] = $id;
        $data['documents'] = ProjAttachment::where('proj_id',$id)->orderBy('id','desc')->get();
        $data['proj_assign_user'] = AssignProject::with('one_user')->where('active',1)->where('proj_id',$id)->get();
        
        $user_id = Auth::User()->id;

        $chk_pm = AssignProject::where('proj_id',$id)->where('user_id',$user_id)->where('pm_status',1)->get();

        if(count($chk_pm) > 0)
        {
            $data['pending'] = Task::where('proj_id',$id)->where('status',1)->get();
            $data['in_process'] = Task::where('proj_id',$id)->whereIn('status',[2,3,4,5])->get();
        }
        else
        {
            $data['pending'] = Task::whereIn('user_id', [$user_id,0])->where('proj_id',$id)->where('status',1)->get();
            $data['in_process'] = Task::where('user_id',$user_id)->where('proj_id',$id)->whereIn('status',[2,3,4,5])->get();
        }

        $data['logss'] = ActivityLog::with('one_user')->where('proj_id',$id)->orderBy('created_at','DESC')->take(20)->get();

        return view('user.uproj_inprocess_pending_task',$data);
    }

    public function uproject_details($id){
        
        $data['title'] = "Project details";
        $data['users'] = User::where('active',1)->get();
        $data['groups'] = Group::where('user_id',0)->get();
        $data['self'] = Project::find($id);
        $data['project_id'] = $id;
        $data['documents'] = ProjAttachment::where('proj_id',$id)->orderBy('id','desc')->get();
        $data['proj_assign_user'] = AssignProject::with('one_user')->where('active',1)->where('proj_id',$id)->get();
        
        $user_id = Auth::User()->id;

        $chk_pm = AssignProject::where('proj_id',$id)->where('user_id',$user_id)->where('pm_status',1)->get();

        if(count($chk_pm) > 0)
        {
            $data['tasks'] = Task::where('proj_id',$id)->where('status',1)->get();
            $data['in_process'] = Task::where('proj_id',$id)->where('status',2)->get();
            $data['complete'] = Task::where('proj_id',$id)->where('status',3)->get();
            $data['check'] = Task::where('proj_id',$id)->where('status',4)->get();
            $data['bug'] = Task::where('proj_id',$id)->where('status',5)->get();
            $data['done'] = Task::where('proj_id',$id)->where('status',6)->get();
        }
        else
        {
            $data['tasks'] = Task::whereIn('user_id', [$user_id,0])->where('proj_id',$id)->where('status',1)->get();
            $data['in_process'] = Task::where('user_id',$user_id)->where('proj_id',$id)->where('status',2)->get();
            $data['complete'] = Task::where('user_id',$user_id)->where('proj_id',$id)->where('status',3)->get();
            $data['check'] = Task::where('user_id',$user_id)->where('proj_id',$id)->where('status',4)->get();
            $data['bug'] = Task::where('user_id',$user_id)->where('proj_id',$id)->where('status',5)->get();
            $data['done'] = Task::where('user_id',$user_id)->where('proj_id',$id)->where('status',6)->get();
        }

        $data['logss'] = ActivityLog::with('one_user')->where('proj_id',$id)->orderBy('created_at','DESC')->take(20)->get();

        return view('user.uproject_details',$data);
    }

    public function assign_project(Request $req)
    {
        $proj_id = $req->proj_id;
        $type = $req->types;
        $group_id = $req->group_id;
        $user_id = $req->user_id;
        date_default_timezone_set("Asia/Dhaka"); 
        $assign_time = date('Y-m-d H:i:s');
        /*
            1=Created;
            2=in process;
            3=Completed;
            4=checking;
            5=bugfixing;
            5=done;
        */
        
        if($type == 1){
            $all = Group::where('depend_on',$group_id)->where('active','>',0)->get();

            foreach($all as $info)
            {
                $sav = new AssignProject;
                $sav->proj_id = $proj_id;
                $sav->user_id = $info->user_id;
                $sav->assign_time = $assign_time;
                $sav->assign_by = Auth::user()->id;
                $sav->active = 1;
                $sav->save();

                $u_name = $this->get_user_name($info->user_id); //call function
                
                $msg = $u_name." assigned.";
                $this->project_log($msg,$proj_id);  //call function
            }
        }
        elseif($type == 2){
            $sav = new AssignProject;
            $sav->proj_id = $proj_id;
            $sav->user_id = $user_id;
            $sav->assign_time = $assign_time;
            $sav->assign_by = Auth::user()->id;
            $sav->active = 1;
            $sav->save();

            $u_name = $this->get_user_name($user_id); //call function
                
            $msg = $u_name." assigned.";
            $this->project_log($msg,$proj_id);  //call function
        }
        else{
            return redirect()->back()->with('msg','Select type first..');
        }

        return redirect()->back()->with('msg','Add successfully!');
    }

    public function remove_proj_user($id)
    {   
        $del = AssignProject::find($id);

        $u_name = $this->get_user_name($del->user_id); //call function
        $msg = "$u_name removed.";
        $this->project_log($msg,$del->proj_id);  //call function

        $del->delete();
        //echo $msg;
        return redirect()->back();
    }

    public function project_log($msg,$proj_id)
    {
        date_default_timezone_set("Asia/Dhaka"); 
        $assign_time = date('Y-m-d H:i:s');

        $sav = new ActivityLog;
        $sav->msg = $msg;
        $sav->proj_id = $proj_id;
        $sav->logged_user_id = Auth::user()->id;
        $sav->created_at = $assign_time;
        $sav->save();
    }

    public function get_user_name($id){
        $fetch = User::find($id);
        return $fetch->name;
    }

    public function remove_proj_document($id)
    {
        $del = ProjAttachment::find($id);
            //for notification
            $proj_id = $del->proj_id;
            $name = $del->name;
       
            if (File::exists('assets/document/project/'.$del->url) && !empty($del->url))
            {
                File::delete('assets/document/project/'.$del->url);
                $msg = "Document: $del->name removed.";
                $this->project_log($msg,$del->proj_id);  //call function
                $del->delete();

                //notification
                $projj_name = Project::find($proj_id)->name;
                $adminss = User::where('utype',1)->get();
                
                foreach($adminss as $info){
                    $msg = "File:$name removed from Proj:$projj_name by ".Auth::user()->name;
                    $this->notification($info->id, $msg);
                }
                
                $ass_usr = AssignProject::where('proj_id',$proj_id)->get();

                if(count($ass_usr) > 0){
                    foreach($ass_usr as $info){
                        $msg = "File:$name removed from Proj:$projj_name by ".Auth::user()->name;
                        $this->notification($info->user_id, $msg);
                    }    
                }
            }

        return redirect()->back()->with('data','Delete successfully !!');
    }

    public function notification($user_id,$msg){

        $sav = new Notification;
        $sav->user_id = $user_id;
        $sav->msg = $msg;
        $sav->status = 1;
        $sav->save();
    }
    public function change_task_status($id)
    {
        $new_value;
        $msg;
        $start_time;
        $end_time;
        $prv_value=DB::table('task')->where('id','=',$id)->first();
        if($prv_value->status==1)
        {
            $new_value= 2;
            date_default_timezone_set("Asia/Dhaka"); 
            $start_time = date('Y-m-d H:i:s');
        }
        else if($prv_value->status > 1 && $prv_value->status < 6)
        {
            $new_value= 6;
            $end_time = date('Y-m-d H:i:s');
        } 
        if($new_value==2){
            DB::table('task')->where('id','=',$id)->update(array('status'=>$new_value,'start_time'=>$start_time));
        } else if($new_value ==6){
           DB::table('task')->where('id','=',$id)->update(array('status'=>$new_value,'closing_time'=>$end_time)); 
        }
        return redirect()->back();
    }
}
