<?php

namespace App\Http\Controllers\Contractor;

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 viewContractorDashboard()
    {
    	$id = Auth::guard('contractor')->id();
		$user = Staff::with('StaffDetail')->find($id);

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

		$today = Carbon::today();

        return view('contractor.view_dashboard', compact('user','pp','sp','rs','ss','cm','today'));
    }

    public function getAllCalendar(Request $request){

    	$id = Auth::guard('contractor')->id();
		$user = Staff::with('StaffDetail')->find($id);

    	$event = array(); $color = '';
    	$cuw = $request->wk;

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

    		$wo = WorkOrder::where('contractor_id',$user->StaffDetail->company_id)->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('/contractor/work-order/generate-pdf/'.$w->wo),
    					'color' => $color,
    					'allDay' => false
    				));
    			}
    		}

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

    		$wo = WorkOrder::where('status',$request->status)->where('contractor_id',$user->StaffDetail->company_id)->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('/contractor/work-order/generate-pdf/'.$w->wo),
    					'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);
    }
}