<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Requests\RegisterRequest;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Support\Facades\Auth;

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


Use Hash;
use Carbon\Carbon;
use Crypt;
use File;
use Mail;

class WorkOrderMobile extends BaseController
{

	function random_code($limit) {
	    return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit);
	}

	/** 
	/* Schedule Function
	**/
	public function getSchedule(Request $request)
	{
		$data = array();
		$loginUser = Staff::with('StaffDetail')->where('api_token', $request->api_token)->first();
		if(!empty($loginUser)) {
			$schedule = WorkOrder::where('installer_id',$loginUser->_id)->where('status','Pending Installer')->get();
			foreach($schedule as $b) {	

				array_push($data,array(
					"Title" => $b->nature_work,
					"Date"=>date("m/d/Y", strtotime($b->dateTimeStart)),
					"Time"=>date("H:i", strtotime($b->dateTimeStart)),
					"DateTimeStart"=>$b->dateTimeStart,
					"DateTimeEnd"=>$b->dateTimeEnd,
					"status" => $b->status,
				));			

			}

			return $this->sendResponse($data, 'Get Schedule Successfully');
		}else {
			return $this->sendError('Invalid User', 'User Not Exist!');
		}
	}

	public function getWorkOrderDetail(Request $request){
		$data = array();

		$loginUser = Staff::with('StaffDetail')->where('api_token', $request->api_token)->first();

		if(!empty($loginUser)) {

			$wo = WorkOrder::where('installer_id',$loginUser->_id)->where('status','Pending Installer')->get();

			if(!empty($wo)){
				foreach($wo as $w){
					$form = Form::with('Subscriber','PackageDetail')->where('_id',$w->_id)->first();
					$company = Company::where('_id',$w->contractor_id)->first();
					
					$address = '';
					if($form->type_application == 'R'){
						if($form->Subscriber->street != ''){
							$address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
						}else {
							$address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
						}
						
					}else if($form->type_application == 'B'){

						if($form->Subscriber->unit_no != ''){
							$address = $form->Subscriber->unit_no. ', '.$form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
						}else {
							$address = $form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
						}
						
					}

					$pp = ''; $noP = 'N/A'; $nP = '';

					if($w->no_phone != ''){
						$noP = $w->no_phone;
					}

					if($w->need_phone != ''){
						$nP = $w->need_phone;
					}

					array_push($data,array(
						"id" =>  $w->wo,
						"formT" => $form->type_application,
						"contractor_id" => $company->name,
						"installer_id" => $loginUser->StaffDetail->name,
						"title" => $w->nature_work,
						"address"=> $address,
						"imageUrl"=>'assets/activation_icon_nRead1.png',
						"appoint_date"=>date("m/d/Y", strtotime($w->dateTimeStart)),
						"appoint_time"=>date("H:i", strtotime($w->dateTimeStart)),
						"cus_name"=> $form->Subscriber->name,
						"contact_person" => $form->Subscriber->name,
						"contact_no"=> $form->Subscriber->phone1,
						"technical" => $form->Subscriber->name,
						"status" => $w->status,
						'pppoe' => $w->pppoe_username,
						'package' => $form->PackageDetail->name,
						'onu' => $w->onu,
						'router' => $w->router,
						'no_phone' => $noP,
						'n_phone' => $nP,
						"note" => $w->remarks_custservice,
					));
				}

				return $this->sendResponse($data, 'Get Work Order Successfully');

			}else {
				return $this->sendResponse('', 'No Data');
			}
			
		}else {
			return $this->sendError('Invalid User', 'User Not Exist!');
		}
	}

	public function getDocketDetail(Request $request)
	{

		$data = array(); $installer = '';
		$loginUser = Staff::with('StaffDetail')->where('api_token', $request->api_token)->where('roles_access','Installer')->first();
		if(!empty($loginUser)) {

			$docket = Docket::with('WorkOrder')->where('installer_id',$loginUser->_id)->where('docket_id', $request->docket_id)->first();
			if(!empty($docket)){
				$form = Form::with('PackageDetail','Subscriber')->where("_id",$docket->WorkOrder->_id)->first();
				if(!empty($form)){

					$company = Company::where('_id',$docket->WorkOrder->contractor_id)->first();
					$staff = Staff::with('StaffDetail')->where('_id',$docket->installer_id)->first();
					if(!empty($staff)){
						$installer = $staff->StaffDetail->name;
					}else{
						$installer = '';
					}

					$address = '';
					if($form->type_application == 'R'){
						if($form->Subscriber->street != ''){
							$address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
						}else {
							$address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
						}
						
					}else if($form->type_application == 'B'){

						if($form->Subscriber->unit_no != ''){
							$address = $form->Subscriber->unit_no. ', '.$form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
						}else {
							$address = $form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
						}
						
					}

					$total =array($docket->Rating1, $docket->Rating2, $docket->Rating3);
					$total_rating = array_sum($total);	

					$data = array(
						
						
						// "contractor_id" =>  $company->name,
						// "installer_id" =>  $installer,
						// "customer_id" =>  $customer_id,
						
						// "status" =>  $status,
						
						
						
						
						"cus_name"=> $form->Subscriber->name,
						"address"=> $address,
						"contact_no"=> $form->Subscriber->phone1,
						"technical" => $company->name,
						"installer_name" => $installer,
						"title" =>  $docket->nature_work,
						"docket_id" => $docket->docket_id,
						"wo_id" =>  $docket->work_order_id,
						"status" => $docket->WorkOrder->status,
						"start_datetime"=> $docket->WorkOrder->dateTimeStart,
						"end_datetime" => $docket->end_job,
						"cable_read" =>  $docket->cable_read,
						"router" => $docket->router_serial_number,
						"mac_router" => $docket->mac_router,
						"modem_sn" => $docket->modem_sn,
						"type_testing" => $docket->type_testing,
						"condition" => $docket->condition,
						"note" => $docket->Note,
						"total_rating" => $total_rating,	
					);

					return $this->sendResponse($data, 'Successfull Get Docket Detail');
				}
			}
		}else {
			return $this->sendError('Invalid User', 'User Not Exist!');
		}
	}

	public function getDocket(Request $request)
	{

		$data = array();
		$loginUser = Staff::with('StaffDetail')->where('api_token', $request->api_token)->where('roles_access','Installer')->first();
		if(!empty($loginUser)) {

			$docket = Docket::with('WorkOrder')->where('installer_id',$loginUser->_id)->get();
			$company = Company::where('_id',$loginUser->StaffDetail->company_id)->first();

			if(!empty($docket)){
				foreach($docket as $a) {

					$nw = '';
					if(!empty($a->WorkOrder->nature_work)){
						$nw = $a->WorkOrder->nature_work;
					}

					array_push($data,array(
						"docket_id" =>  $a->docket_id,
						"wo_id" =>  $a->work_order_id,
						"contractor_id" => $company,
						"installer_id" => $loginUser->StaffDetail->name,
						"title" => $nw,
						"imageUrl"=>'assets/activation_icon_nRead1.png',
						"end_date"=>date("m/d/Y", strtotime($a->end_job)),
						"end_time"=>date("H:i", strtotime($a->end_job)),
						"cable_read" => $a->cable_read	
					));
				}
				return $this->sendResponse($data, 'Successfull');
			}	
		}else {
			return $this->sendError('Invalid User', 'User Not Exist!');
		}
	}

	public function updateWorkOrder(Request $request){

		$loginUser = Staff::with('StaffDetail')->where('api_token', $request->api_token)->where('roles_access','Installer')->first();
		if(!empty($loginUser)) {

			$wo = WorkOrder::where('wo',$request->nid)->first();
			if(!empty($wo)){

				$wo->remarks_installer = $request->postres.". ".$request->note;
				$wo->status = $request->type;
				$wo->save();

				return $this->sendResponse('Success', 'Work Order '.$request->nid.' been update successfull');

			}else{
				return $this->sendError('Failed', 'Work Order Not Exist!');
			}

		}else {
			return $this->sendError('Invalid User', 'User Not Exist!');
		}
	}

	public function getQuestions(){

		$question = Question::all();
		if(!empty($question)) {
			return $this->sendResponse($question, 'Get all questions successfull');
		}else{
			return $this->sendError('', 'No Questions!');
		}
	}
}
