<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Comment;
use App\Models\ActivityLog;
use App\Models\Task;
use Auth;
use Illuminate\Support\Facades\File;

class UCommentController extends Controller
{
    public function remove_comment(Request $req){

    	$id = $req->id;
        $task_id = $req->task_id;
        $del = Comment::find($id);

        if (File::exists('assets/document/task/'.$del->url) && !empty($del->url))
            {
                File::delete('assets/document/task/'.$del->url);
            }

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

        $del->delete();

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

    public function edit_comment_form(Request $req){

    	$id = $req->id;

    	$fet = Comment::find($id);

    	$data['comment'] = $fet->comment; 
    	$data['id'] = $id;
    
    	return $data;
    }

    public function save_update_comment(Request $req){

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

        $sav = Comment::find($id);
        $sav->comment = $comment;
            //activity log
            $task_info = Task::find($task_id);
            $task_name = $task_info->name;
            $proj_id  = $task_info->proj_id;
            $msg = "$comment -edited from $task_name.";
            $this->task_log($msg,$task_id,$proj_id);  //call function
        $sav->save();

        $data = $this->fetch_comments($task_id);  // call function  
        return $data;      
    }

    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 add_task_doc(Request $req)
    {
        $file = $req->file('file');
        $task_id = $req->task_id;

        date_default_timezone_set("Asia/Dhaka");
        $assign_time = date('Y-m-d H:i:s');
        
        if ($req->hasFile('file')){
            $extension = $file->getClientOriginalExtension();
            $os = array("jpg", "jpeg", "png", "gif");
            if (in_array($extension, $os)){
                $name = $file->getClientOriginalName();
                $newFileName = "task_".date('d_m_y_h_m_s').".".$extension;;
                $fileee = $file->move('assets/document/task/', $newFileName);

                $sav = new Comment;
                $sav->comment = $name;
                $sav->url = $newFileName;
                $sav->task_id = $task_id;
                $sav->date_time = $assign_time;
                $sav->post_by = Auth::user()->id;
                $sav->save();
            }
        }
    }

    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->proj_id = $proj_id;
        $sav->task_id = $task_id;
        $sav->logged_user_id = Auth::user()->id;
        $sav->created_at = $assign_time;
        $sav->save();
    }

}
