<?php

namespace App\Http\Controllers\CustomerService;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

use Carbon\Carbon;
use Validator;
use PDF;

use App\Staff;
use App\Model\StaffDetail;
use App\Model\Form;
use App\Model\Subscriber;
use App\Model\WorkOrder;
use App\Model\PackageDetail;
use App\Model\Company;

class CalendarController extends Controller
{

    public function index()
    {
    	$id = Auth::guard('cs')->id();
		$user = Staff::with('StaffDetail')->find($id);

		$pp = count(WorkOrder::where('status','Pending Non Prelaid')->get());
		$sp = count(WorkOrder::where('status','Success Non Prelaid')->get());
		$rs = count(WorkOrder::where('status','Reschedule')->get());
		$ss = count(WorkOrder::where('status','Suspend')->get());
		$cm = count(WorkOrder::where('status','Completed')->get());

		$company = Company::where('team','CBB')->get();
		$today = Carbon::today();
    	return view('customer-service.view_calendar', compact('user','pp','sp','rs','ss','cm', 'company','today'));
    }

    public function getAllCalendar(Request $request){

    	$event = array(); $color = '';

    	$cuw = $request->wk;

    	if($request->customer == ''){

	    	if($request->status == '' && $request->company == ''){

	    		$wo = WorkOrder::all();
	    		if(!empty($wo)){
	    			foreach ($wo as $key => $w) {
	    		
			    		if($w->wo == $cuw){
	    					$color = '#5C2983'; 
	    				}else if($w->wo != $cuw) {
	    					if($w->status == "Completed") {
				    			$color = '#00C853'; 
				    		}else if($w->status == "Suspend") {
				    			$color = '#DE006E'; 
				    		}else if($w->status == "Reschedule") {
				    			$color = '#FF6D00'; 
				    		}else if($w->status == "Success Non Prelaid"){
				    			$color = '#2962FF'; 
				    		}else if($w->status == "Pending Non Prelaid"){
				    			$color = '#FFD600'; 
				    		}else if($w->status == "Pending Contractor") {
				    			$color = '#6D4C41';
				    		}else if($w->status == "Pending Installer"){
				    			$color = '#546E7A';
				    		}
	    				}

						array_push($event, array(
							'id' => $w->wo,
							'title'  => $w->wo,
							'start' => Carbon::createFromTimestamp(strtotime($w->dateTimeStart))->toRfc2822String(),
							'end' => Carbon::createFromTimestamp(strtotime($w->dateTimeEnd))->toRfc2822String(),
							'url'   => url('/customer-service/work-order/generate-pdf/'.$w->wo),
							'color' => $color,
							'allDay' => false
						));
		    		}
	    		}

	    	}else if($request->status != '' && $request->company == ''){

	    		$wo = WorkOrder::where('status',$request->status)->get();
	    		if(!empty($wo)){
	    			foreach ($wo as $key => $w) {

	    				if($w->wo == $cuw){
	    					$color = '#5C2983'; 
	    				}else if($w->wo != $cuw) {
	    					if($w->status == "Completed") {
				    			$color = '#00C853'; 
				    		}else if($w->status == "Suspend") {
				    			$color = '#DE006E'; 
				    		}else if($w->status == "Reschedule") {
				    			$color = '#FF6D00'; 
				    		}else if($w->status == "Success Non Prelaid"){
				    			$color = '#2962FF'; 
				    		}else if($w->status == "Pending Non Prelaid"){
				    			$color = '#FFD600'; 
				    		}else if($w->status == "Pending Contractor") {
				    			$color = '#6D4C41';
				    		}else if($w->status == "Pending Installer"){
				    			$color = '#546E7A';
				    		}
	    				}

						array_push($event, array(
							'id' => $w->wo,
							'title'  => $w->wo,
							'start' => Carbon::createFromTimestamp(strtotime($w->dateTimeStart))->toRfc2822String(),
							'end' => Carbon::createFromTimestamp(strtotime($w->dateTimeEnd))->toRfc2822String(),
							'url'   => url('/customer-service/work-order/generate-pdf/'.$w->wo),
							'color' => $color,
							'allDay' => false
						));
		    		}
	    		}

	    	}else if($request->status == '' && $request->company != ''){

	    		$wo = WorkOrder::where('contractor_id', $request->company)->get();
	    		$cuw = '';

	    		if($request->has('wk')){
	    			$cuw = $request->wk;
	    		}

	    		if(!empty($wo)){
	    			foreach ($wo as $key => $w) {

	    				if($w->wo == $cuw){
	    					$color = '#5C2983'; 
	    				}else if($w->wo != $cuw) {
	    					if($w->status == "Completed") {
				    			$color = '#00C853'; 
				    		}else if($w->status == "Suspend") {
				    			$color = '#DE006E'; 
				    		}else if($w->status == "Reschedule") {
				    			$color = '#FF6D00'; 
				    		}else if($w->status == "Success Non Prelaid"){
				    			$color = '#2962FF'; 
				    		}else if($w->status == "Pending Non Prelaid"){
				    			$color = '#FFD600'; 
				    		}else if($w->status == "Pending Contractor") {
				    			$color = '#6D4C41';
				    		}else if($w->status == "Pending Installer"){
				    			$color = '#546E7A';
				    		}
	    				}

						array_push($event, array(
							'id' => $w->wo,
							'title'  => $w->wo,
							'start' => Carbon::createFromTimestamp(strtotime($w->dateTimeStart))->toRfc2822String(),
							'end' => Carbon::createFromTimestamp(strtotime($w->dateTimeEnd))->toRfc2822String(),
							'url'   => url('/customer-service/work-order/generate-pdf/'.$w->wo),
							'color' => $color,
							'allDay' => false
						));
		    		}
	    		}

	    	}else if($request->status != '' && $request->company != ''){

	    		$wo = WorkOrder::where('contractor_id', $request->company)->where('status',$request->status)->get();
	    		if(!empty($wo)){
	    			foreach ($wo as $key => $w) {
	    				
		    			if($w->wo == $cuw){
	    					$color = '#5C2983'; 
	    				}else if($w->wo != $cuw) {
	    					if($w->status == "Completed") {
				    			$color = '#00C853'; 
				    		}else if($w->status == "Suspend") {
				    			$color = '#DE006E'; 
				    		}else if($w->status == "Reschedule") {
				    			$color = '#FF6D00'; 
				    		}else if($w->status == "Success Non Prelaid"){
				    			$color = '#2962FF'; 
				    		}else if($w->status == "Pending Non Prelaid"){
				    			$color = '#FFD600'; 
				    		}else if($w->status == "Pending Contractor") {
				    			$color = '#6D4C41';
				    		}else if($w->status == "Pending Installer"){
				    			$color = '#546E7A';
				    		}
	    				}

						array_push($event, array(
							'id' => $w->wo,
							'title'  => $w->wo,
							'start' => Carbon::createFromTimestamp(strtotime($w->dateTimeStart))->toRfc2822String(),
							'end' => Carbon::createFromTimestamp(strtotime($w->dateTimeEnd))->toRfc2822String(),
							'url'   => url('/customer-service/work-order/generate-pdf/'.$w->wo),
							'color' => $color,
							'allDay' => false
						));
		    		}
	    		}

	    	}

	    }else if($request->customer == 'New Customer'){

	    	$form = Form::with('Subscriber','WorkOrder','PackageDetail')->get();
	    	$url = '';

	    	if(!empty($form)){
	    		foreach($form as $f){
	    			if(empty($f->WorkOrder)){
	    				if($f->type_application == 'R' && !empty($f->Subscriber->subscriber_id)){
	    					$color = '#582483';
	    					$url = '/customer-service/residential/generate-pdf/'.$f->Subscriber->subscriber_id;
	    				}else if($f->type_application == 'B' && !empty($f->Subscriber->subscriber_id)){
	    					$color = '#E07027';
	    					$url = '/customer-service/business/generate-pdf/'.$f->Subscriber->subscriber_id;
	    				}

	    				if(!empty($f->PackageDetail)){
	    					array_push($event, array(
								'id' => $f->Subscriber->subscriber_id,
								'title'  => $f->Subscriber->subscriber_id,
								'start' => Carbon::createFromTimestamp(strtotime($f->PackageDetail->rfs))->toRfc2822String(),
								'end' => '',
								'url'   => url($url),
								'color' => $color,
								'allDay' => false
							));
	    				}
	    			}
	    		}
	    	}

	    }

    	return response()->json($event);
    }

    public function getCompanyColor(Request $request){

    	$datass = array(); 
    	$company = Company::where("_id",$request->company)->first();

    	if(!empty($company)){
    		array_push($datass, array(
	    		'name' => '('.$company->name.')',
	    		'bg' => $company->color,
	    		'text' => '#fff'
	    	));

    	}else {
    		array_push($datass, array(
	    		'name' => '',
	    		'bg' => '#fff',
	    		'text' => '#5F5F5F'
	    	));
    	}

    	return response()->json($datass);
    }
}