<?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\Group;
use App\Models\Task;
use App\Models\ActivityLog;
use App\Models\Notification;
use App\Models\AssignProject;
use App\Models\Comment;
use App\User;
use Auth;

class UTaskController extends Controller
{

    public function task_details(Request $req){

        $proj_id = $req->proj_id;
        $task_id = $req->task_id;

        $all = AssignProject::with('one_user')->where('proj_id',$proj_id)->get();

        $str = "";
        $str .="<select>";
        foreach($all as $info){
            $str .="<option value={$info->user_id}>".$info->one_user->name."</option>";
        }
        $str .="</select>";

        //getting status of this task
            $task_sta = Task::find($task_id);
            $task_status = $task_sta->status;

        //tast already assigned user
            $str1 = $this->fetch_task_assign_user($task_id);  //call function

        //activity log fetch
            $logss = ActivityLog::with('one_user')->where('task_id',$task_id)->orderBy('id','DESC')->get();
            $str_log = "";

            foreach($logss as $log){
                $str_log .="* $log->msg -".$log->one_user->name."<br>";
            }

        //comments fetch
            $str_comment = $this->fetch_comments($task_id);

        $data[0] = $str;
        $data[1] = $str1;
        $data[2] = $str_log;
        $data[3] = $str_comment;
        $data[4] = $task_status;

        return $data;
    }   

    public function add_task(Request $request){
        
        $name = $request->name;
        $proj_id = $request->proj_id;
        $p=Project::find($proj_id);
        $office_id=$p->office_id;
        $description = $request->description;
        $dead_line = $request->dead_line;
        $task_type = empty($request->task_type)?1:2;
        date_default_timezone_set("Asia/Dhaka"); 
        $cdate = date('Y-m-d H:i:s');

        $sav = new Task;
        $sav->name = $name;
        $sav->type = $task_type;
        $sav->proj_id = $proj_id;
        $sav->user_id = Auth::user()->id;
        $sav->assign_by = Auth::user()->id;
        $sav->assign_time = $cdate;
        $sav->dead_line = $dead_line;
        $sav->description = $description;
        $sav->created_time = $cdate;
        $sav->created_by = Auth::user()->id;
        $sav->status = 1;
        $sav->office_id =  $office_id;
        $sav->save();

        $task_id = $sav->id;

        //assing task 
            // $sav_assign_task = new AssignTask;
            // $sav_assign_task->task_id = $task_id;
            // $sav_assign_task->user_id = Auth::user()->id;
            // $sav_assign_task->assign_by = Auth::user()->id;
            // $sav_assign_task->assign_time = $cdate;
            // $sav_assign_task->estimated_time = $estimated_time;
            // $sav_assign_task->status = 1;
            // $sav_assign_task->save();

        $msg = "Task: $name created.";
        $this->project_log($msg,$proj_id);  //call function
        $this->task_log($msg,$task_id,$proj_id);  //call function

        //notification
        $fet_pro = Project::find($proj_id);
        $projj_name = $fet_pro->name;
        $client_id = $fet_pro->client_id;
        $adminss = User::where('utype',1)->get();
        
        $msg = "Task:$name created in Proj:$projj_name by ".Auth::user()->name;

        foreach($adminss as $info){
            $this->notification($info->id, $msg);
        }

        $this->notification($client_id, $msg);
        //--end notification

        return redirect()->back()->with('message','Success');
    }

    public function remove_task($id)
    {
        $del = Task::find($id);
            //activity log
            $msg = "Task:".$del->name." removed.";
            $this->project_log($msg,$del->proj_id);  //call function

            //notification
            $projj_name = Project::find($del->proj_id)->name;
            $name = $del->name;
            $adminss = User::where('utype',1)->get();
            
            $post_by_name = Auth::user()->name;
            $post_by_id = Auth::user()->id;

            $msg = "Task:$name removed from Proj:$projj_name by ".$post_by_name;

            foreach($adminss as $info){
                $this->notification($info->id, $msg);
            }
            
            $ass_usr = AssignProject::where('proj_id',$del->proj_id)->get();

            if(count($ass_usr) > 0){
                foreach($ass_usr as $info){
                    if($info->user_id != $post_by_id){
                        $this->notification($info->user_id, $msg);
                    }
                }    
            }
            //end notification

        $del->delete();

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

    public function change_tast_by_proj(Request $req)
    {
        $proj_id = $req->proj_id;
        $all = Task::where('proj_id',$proj_id)->get();
        $str = "";

        $str .="<select>";
            $str .="<option value='all'>All Tast</option>";
        foreach($all as $info){
            $str .="<option value=".$info->id.">".$info->name."</option>";
        }
        $str .="</select>";

        return $str;
    }

    public function user_list_for_task_assign(Request $req){

        $proj_id = $req->proj_id;
        $task_id = $req->task_id;

        $all = AssignProject::with('one_user')->where('proj_id',$proj_id)->get();

        $str = "";
        $str .="<select>";
        foreach($all as $info){
            $str .="<option value={$info->user_id}>".$info->one_user->name."</option>";
        }
        $str .="</select>";

        //getting status of this task
            $task_sta = Task::find($task_id);
            $task_status = $task_sta->status;

            if(!empty($task_sta->description))
            {
                $details = $task_sta->description;
            }
            else{
                $details = "No details...";
            }

        //tast already assigned user
            $str1 = $this->fetch_task_assign_user($task_id);  //call function

        //activity log fetch
            $logss = ActivityLog::with('one_user')->where('task_id',$task_id)->orderBy('id','DESC')->get();
            $str_log = "";

            foreach($logss as $log){
                $str_log .="* $log->msg -".$log->one_user->name."<br>";
            }

        //comments fetch
            $str_comment = $this->fetch_comments($task_id);

        $data[0] = $str;
        $data[1] = $str1;
        $data[2] = $str_log;
        $data[3] = $str_comment;
        $data[4] = $task_status;
        $data[5] = $details;

        return $data;
    }


    public function task_assign_to_user(Request $req){

        $task_id = $req->id_task_id;
        $estimated_time = $req->id_estimated_time;
        $user_id = $req->id_user_id;
        date_default_timezone_set("Asia/Dhaka"); 
        $cdate = date('Y-m-d H:i:s');

        $sav = Task::find($task_id);
            //fetch proj_id for activity log
            $proj_id = $sav->proj_id;
            $task_name = $sav->name;

        $sav->user_id = $user_id;
        $sav->assign_by = Auth::user()->id;
        $sav->assign_time = $cdate;
        $sav->estimated_time = $estimated_time;
        $sav->save();

        //activity log
        $nam = User::find($user_id);
        $msg = "User: $nam->name added";
        $this->task_log($msg,$task_id,$proj_id);  //call function

        //notification
        $projj_name = Project::find($proj_id)->name;
        $adminss = User::where('utype',1)->get();

        $post_by_name = Auth::user()->name;
        $post_by_id = Auth::user()->id;

        $msg = "$nam->name assigned in Task:$task_name of Proj:$projj_name by ".$post_by_name;
        
        foreach($adminss as $info){
            $this->notification($info->id, $msg);
        }
        
        if($user_id != $post_by_id){
            $this->notification($user_id, $msg);   
        }
        
        //--------end notification
        

        $str = $this->fetch_task_assign_user($task_id);

        return $str;
    }

    // public function remove_assign_task_user(Request $req){

    //     $id = $req->id;
    //     $task_id = $req->task_id;

    //     $del = AssignTask::find($id);
    //         //activity log
    //         $nam = User::find($del->user_id);
    //         $msg = "User: $nam->name removed";
    //         $this->task_log($msg,$task_id);  //call function
    //     $del->delete();

    //     $str = $this->fetch_task_assign_user($task_id); //call function
    //     return $str;

    // }

    public function add_task_comment(Request $req){

        $comment = $req->comment;
        $task_id = $req->task_id;

        date_default_timezone_set("Asia/Dhaka"); 
        $assign_time = date('Y-m-d H:i:s');

        if(!empty($comment) && $task_id > 0){
            $sav = new Comment;
            $sav->comment = $comment;
            $sav->task_id = $task_id;
            $sav->date_time = $assign_time;
            $sav->post_by = Auth::user()->id;
            $sav->save();
        }

            //activity log
            $task_info = Task::find($task_id);
            $task_name = $task_info->name;
            $proj_id  = $task_info->proj_id;
            $user_id = $task_info->user_id;
            $msg = "$comment -added to $task_name.";
            $this->task_log($msg,$task_id,$proj_id);  //call function

            //notification
            $fetch_proj = Project::find($proj_id);
            $projj_name = $fetch_proj->name;
            $client_id = $fetch_proj->client_id;
            $adminss = User::where('utype',1)->get();

            $notification_post_by = Auth::user()->name;
            $noti_post_by_id = Auth::user()->id;
            $msg = "A comment added in Task:$task_name of Proj:$projj_name by ".$notification_post_by;
            
            foreach($adminss as $info){
                $this->notification($info->id, $msg);
            }
            
            $this->notification($client_id, $msg);

        $str = $this->fetch_comments($task_id);

        return $str;
    }

    

    public function fetch_task_assign_user($task_id)
    {
        $fetch = Task::with('user')->find($task_id);

        $str = "";
        if($fetch->user_id > 0){
            $str .="<input type='hidden' value=".$fetch->id.">";
            $str .= $fetch->user->name."<br>Estimated time: ".$fetch->estimated_time;
        }
        else{
            $str .= "No user assigned";
        }

        return $str;
    } 

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

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

    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 fetch_comments($task_id){

        $fetch = Comment::with('user')->where('task_id',$task_id)->orderBy('id','DESC')->get();
        $str = "";

        $login_user = Auth::user()->id; 

        if(count($fetch) > 0){
            
            foreach($fetch as $fet)
            {
                $immmgg = "assets/document/task/$fet->url";
                $str .="<input type='hidden' value=".$fet->id.">";

                $str .="* $fet->comment -by ".$fet->user->name;
                
                if(!empty($fet->url)){
                $str .="<br/><a target='_blank' href=".url("assets/document/task/$fet->url").">
                <img height='150' width='120' src='".asset($immmgg)."'></a>";
                }
                
                $str .="<span style='color:#FD4B39;cursor:pointer;margin-left:10px' data-id='".$fet->id."' class='edit_comment pull-right'><i class='fa fa-pencil-square-o' aria-hidden='true'></i></span>
                <span style='color:#FD4B39;cursor:pointer;' data-id='".$fet->id."' class='remove_comment'><i class='fa fa-times pull-right' aria-hidden='true'></i></span><br/>";
            }
        }

        return $str;
    }

    public function change_to_process(Request $req)
    {
        $task_id = $req->task_id;
        $user_id = Auth::user()->id;
        $status = $req->status;

        date_default_timezone_set("Asia/Dhaka"); 
        $t_time = date('Y-m-d H:i:s');

        $sav = Task::find($task_id);
            $task_name = $sav->name;

        if($user_id = $sav->user_id){
            $sav->status = $status;
            if($status == 2){
                $sav->start_time = $t_time;   
            }
            elseif($status == 6){
                $sav->closing_time = $t_time;
            }

            //fetch proj_id for activity log
            $proj_id = $sav->proj_id;

            $sav->save();

            $text = "";
            if($status == 2){ $text = "in-process"; }
            elseif($status == 3){ $text = "completed"; }
            elseif($status == 4){ $text = "checking"; }
            elseif($status == 5){ $text = "bug-fixing"; }
            elseif($status == 6){ $text = "done"; }

            $msg = "$task_name -Change status to: $text";
            $this->task_log($msg,$task_id,$proj_id);  //call function

            return "Yes";            
        }

        return "No";
    }

    public function change_to_inprocess(Request $req){

        $task_id = $req->task_id;
        $status = $req->status;

        $user_id = Auth::user()->id;
        
        date_default_timezone_set("Asia/Dhaka"); 
        $t_time = date('Y-m-d H:i:s');

        $sav = Task::find($task_id);
            $task_name = $sav->name;

        if($user_id = $sav->user_id){
            $sav->status = $status;
            if($status == 2){
                $sav->start_time = $t_time;   
            }
            elseif($status == 6){
                $sav->closing_time = $t_time;
            }

            //fetch proj_id for activity log
            $proj_id = $sav->proj_id;

            $sav->save();

            $text = "";
            if($status == 2){ $text = "in-process"; }
            elseif($status == 6){ $text = "done"; }

            $msg = "$task_name -Change status to: $text";
            $this->task_log($msg,$task_id,$proj_id);  //call function

            return "Yes";            
        }

    }

    public function notification($user_id,$msg){

        $sav = new Notification;
        $sav->user_id = $user_id;
        $sav->msg = $msg;
        $sav->status = 1;
        $sav->save();
    }
}
