<?php

namespace App\Http\Controllers\Client;

use Illuminate\Http\Request;

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

class CProjectController extends Controller
{
    public function index(){

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

    	$user_id = Auth::user()->id;
    	$data['projj'] = AssignProject::with('proj_name','one_user','assigned')->where('user_id',$user_id)->orderBy('id','desc')->get();

        return view('client.cprojects',$data);
    }

    public function add_project(Request $request){
    	
    	$proj_name = $request->proj_name;
    	$description = $request->description;
    	date_default_timezone_set("Asia/Dhaka"); 
        $cdate = date('Y-m-d H:i:s');
        $client_id = Auth::user()->id;

    	$sav = new Project;
    	$sav->name = $proj_name;
    	$sav->client_id = $client_id;
    	$sav->description = $description;
    	$sav->created_time = $cdate;
    	$sav->created_by = $client_id;
    	$sav->save();

    	$lastInsertedId = $sav->id;

    		$sav = new AssignProject;
            $sav->proj_id = $lastInsertedId;
            $sav->user_id = $client_id;
            $sav->assign_time = $cdate;
            $sav->assign_by = $client_id;
            $sav->active = 1;
            $sav->save();
        
        $msg = "Project:$proj_name created.";
        $this->project_log($msg,$lastInsertedId);  //call function

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

    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
            }
        }
    }

    public function remove_proj($id)
    {
        $del = Project::find($id);
            $del->tasks()->delete();
            $del->documents()->delete();
            $del->proj_assign()->delete();
        $del->delete();

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

    public function remove_proj_document($id)
    {
        $del = ProjAttachment::find($id);
       
            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();
            }

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

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

        $data['tasks'] = Task::where('status',1)->where('proj_id',$id)->get();
        $data['in_process'] = Task::where('status',2)->where('proj_id',$id)->get();
        $data['complete'] = Task::where('status',3)->where('proj_id',$id)->get();
        $data['check'] = Task::where('status',4)->where('proj_id',$id)->get();
        $data['bug'] = Task::where('status',5)->where('proj_id',$id)->get();
        $data['done'] = Task::where('status',6)->where('proj_id',$id)->get();

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

        return view('client.cproject_details',$data);
    }

    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();
    }
}
