<?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 Hash;
use Carbon\Carbon;
use Crypt;
use File;
use Mail;

class MynixController extends BaseController
{

	public function workOrderStatistics(){

		$pc = count(WorkOrder::where('status','Pending Contractor')->get());
		$pi = count(WorkOrder::where('status','Pending Installer')->get());
		$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());

		$data = array();
		$data[] = array("status" => "Pending Contractor", "statistic" => $pc);
		$data[] = array("status" => "Pending Installer", "statistic" => $pi);
		$data[] = array("status" => "Pending Non Prelaid", "statistic" => $pp);
		$data[] = array("status" => "Success Non Prelaid", "statistic" => $sp);
		$data[] = array("status" => "Reschedule", "statistic" => $rs);
		$data[] = array("status" => "Suspend", "statistic" => $ss);
		$data[] = array("status" => "Completed", "statistic" => $cm);

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

	}

	public function workOrderList(){

		$data = array();
		$installer = ''; $type = ''; $n1 = '';
		$curr = Carbon::now()->getTimestamp();

		$work_order = WorkOrder::orderBy('created_at', 'desc')->get();
		if(!empty($work_order)){
			foreach ($work_order as $key => $w) {
			
				$reg_time = $w->created_at;
				$expiry_date = $reg_time->addDays(3);    
				$expiry_date = $expiry_date->getTimestamp(); 

				if($curr < $expiry_date) {
					$n1 = "New/";
				}else{
					$n1 = "";
				} 

				$form = Form::with('PackageDetail','Subscriber')->first();
				if(!empty($form)){
					$company = Company::where('_id',$w->contractor_id)->first();

					$staff = Staff::with('StaffDetail')->where('_id',$w->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;
						}
						
					}

					if($form->type_application == 'R'){
						$type = 'Residential';
					}else if($form->type_application == 'B'){
						$type = 'Business';
					}

					array_push($data,array(
						"work_order_id" => $n1.$w->wo,
						"type" => $type,
						"contractor" => $company->name,
						"installer" => $installer,
						"title" => $w->nature_work,
						"appoint_date_time"=>date("m/d/Y H:i", strtotime($w->dateTimeStart)),
						"cus_name"=> $form->Subscriber->name,
						"contact_person" => $form->Subscriber->name,
						"contact_no"=> $form->Subscriber->phone1,
						"technical" => $form->Subscriber->name,
						"address"=> $address,
						"status" => $w->status,
					));
				}
			}

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


	public function getCustomerList(){

		$data = array(); $i = 1; 
		$n1 = ''; $address = ''; $building = ''; $status = '';
		$curr = Carbon::now()->getTimestamp();

		$form_submitted = Form::with('PackageDetail','Subscriber','WorkOrder')->withTrashed()->orderBy('created_at', 'desc')->get(); 
		if(!empty($form_submitted)){
			foreach ($form_submitted as $key => $v) {

				if(!empty($v->Subscriber)){
					if(empty($v->WorkOrder)){

						$i++;
						$reg_time = $v->created_at;
						$expiry_date = $reg_time->addDays(3);    
						$expiry_date = $expiry_date->getTimestamp(); 

						if($curr < $expiry_date) {
							$n1 = "New/";
						}else{
							$n1 = "";
						} 

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

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

					if(!empty($v->deleted_at)){
						$status = 'Rejected';
					}else if($v->status_email == 'unverified') {
						$status = 'New';
					}else if($v->status_email == 'verified') {
						$status = 'Accepted';
					}

					array_push($data, array(
						'formT' => $n1.$i.$v->type_application,
						'type' => $v->type_service,
						'subscriber_id' => $v->Subscriber->subscriber_id,
						'name' => $v->Subscriber->name,
						'phone' => $v->Subscriber->phone1,
						'email' => $v->Subscriber->email,
						'address' => $address,
						'postcode' => $v->Subscriber->postcode,
						'city' => $v->Subscriber->city,
						'status' => $status,
						'date_submitted' => Carbon::parse($v->created_at)->toDateTimeString()
					));
				}
			}

			return $this->sendResponse($data, 'Get Customer List Successfully');

		}else{
			return $this->sendError('', 'No Data Been Found!');
		}
	}

	public function customerStatistic(){

		$finalD1 = array(); $finalD2 = array(); $finalD3 = array(); $finalD4 = array();
		$form_submitted1 = Form::with('PackageDetail','Subscriber','WorkOrder')->withTrashed()->orderBy('created_at', 'desc')->get(); 
		if(!empty($form_submitted1)){
			foreach ($form_submitted1 as $key => $v) {
				if(!empty($v->Subscriber)){
					if(empty($v->WorkOrder)){
						$finalD1[] = $v;
					}
				}
			}
		}

		$form_submitted2= Form::onlyTrashed()->get(); 
		if(!empty($form_submitted2)){
			foreach ($form_submitted2 as $key => $v) {
				if(!empty($v->Subscriber)){
					if(empty($v->WorkOrder)){
						$finalD2[] = $v;
					}
				}
			}
		}

		$form_submitted3= Form::get(); 
		if(!empty($form_submitted3)){
			foreach ($form_submitted3 as $key => $v) {
				if(!empty($v->Subscriber)){
					if(empty($v->WorkOrder)){
						if($v->status_email == 'verified') {
							$finalD3[] = $v;
						}else if($v->status_email == 'unverified'){
							$finalD4[] = $v;
						}
					}
				}
			}
		}

		$total = count($finalD1);
		$new = count($finalD4);
		$reject = count($finalD2);
		$accepted = count($finalD3);

		$data = array();
		$data[] = array("status" => "New", "statistic" => $new);
		$data[] = array("status" => "Rejected", "statistic" => $reject);
		$data[] = array("status" => "Accepted", "statistic" => $accepted);
		$data[] = array("status" => "Total", "statistic" => $total);

		return $this->sendResponse($data, 'Get Registration Statistic Successfully');

	}
}
