<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Storage;
use App\todoBackups;
use App\todoComments;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
class todoBackupController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
	$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
	return view('todos/todoBackup');
}

public function setstatus(Request $request){
	$backupid = $request->input('backupid');
	$backup = todoBackups::whereId($backupid)->first();
	$status = 0;
	$solvedBy = NULL;
	$completedDate = NULL;
	$message = "";
	switch ($backup->status) {
		case 0:
			$status = 1;
			$completedDate = Carbon::now();
			$solvedBy = Auth::user()->id;
		break;
		case 2:
			$status = 1;
			$completedDate = Carbon::now();
			$solvedBy = Auth::user()->id;
		break;
	}
	$backup->update([
		'status' => $status,
		'completed_at' => $completedDate,
		'solvedBy' => Auth::user()->id,
	]);

	if($status == 1) {
		$message = $backup->hostname." has been marked as solved by ".$backup->solver->firstname." ".$backup->solver->lastname;
	}
	elseif ($status == 0) {
		$message = $backup->hostname." has been marked as open";
	}

	return response()->json([
		'message' => $message,
	]);
}


public function setassign(Request $request){
	$backupid = $request->input('backupid');
	$backup = todoBackups::whereId($backupid)->first();
	$status = 0;
	$assigned = NULL;
	$message = "";
	if($backup->status === 0) {
		$status = 2;
		$assigned = Auth::user()->id;
	};
	$backup->update([
		'status' => $status,
		'assigned' => $assigned,
	]);
	if($status == 2) {
		$message = $backup->hostname." has been assigned to ".$backup->assignee->firstname." ".$backup->assignee->lastname;
	}
	elseif ($status == 0) {
		$message = $backup->hostname." assigment has been removed";
	}

		return response()->json([
		'message' => $message,
	]);
}


public function fetchBackupData(Request $request)
{
	if($request->ajax()) {
		return todoBackups::with('assignee','solver')->withCount('comments')->get();
	}

	return view('todos/todoBackup');
}

public function fetchEntryComments(Request $request)
{

	$backupid = $request->input('backupid');
	if($request->ajax()) {
		$messages = todoComments::with('owner')
		->where('relationID',$backupid)
		->where('relationType','backup')
		->orderBy('created_at','DESC')
		->get();

		$hostname = todoBackups::whereId($backupid)
		->first()
		->hostname;

		return array('messages' => $messages,'hostname' => $hostname,'backupid' => $backupid);
	}

	return view('todos/todoBackup');
}


public function saveComment(Request $request){
	$backupid = $request->input('backupid');
	$message = $request->input('message');
	$user = Auth::user()->id;
	
$comment = new todoComments;
$comment->userID = $user;
$comment->relationID = $backupid;
$comment->relationType = 'backup';
$comment->comment = $message;
$comment->save();

if($comment) {
			return response()->json([
		'message' => "Comment saved",
	]);
};

}


}

