<?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\Model\Form;
Use App\Model\DealerClaim;
Use App\Model\Docket;
Use App\Model\PackageDetail;
Use App\Model\Subscriber;
Use App\Model\WorkOrder;
use App\Staff;
use App\Model\StaffDetail;
use App\Model\Company;

use Carbon\Carbon;

class FormController extends BaseController
{
	/** 
	/* Schedule Function
	**/
	public function getFormStatistic(Request $request)
	{
		$dateS = Carbon::createFromFormat('Y-m-d', '2019-12-12');
        $start = $dateS->copy()->startOfDay();
        $dateE = Carbon::createFromFormat('Y-m-d', '2020-02-11');
        $end = $dateE->copy()->endOfDay();

        $result = array();
		$data = Form::with('PackageDetail', 'Subscriber')->whereBetween('created_at', array($start, $end))->get();
		if(count($data) > 0) {
			foreach($data as $f) {	

				$name = ''; $contact_no = '';
				$address = ''; $building = '';
				$labelD = '';

				if(!empty($f->dealer_id)){
					$c = Company::find($f->company_id);
					$labelD = $c->name;
				}else{
					$labelD = "";
				}

				if(!empty($f->Subscriber) && !empty($f->PackageDetail)){
					if($f->type_application == 'R'){
						$name = $f->Subscriber->name;
						$contact_no = $f->Subscriber->phone1;
						$building = $f->Subscriber->building_name;
						if($f->Subscriber->street != ''){
							$address = $f->Subscriber->street. ' , '.$f->Subscriber->postcode. ' , '.$f->Subscriber->city. ' , '.$f->Subscriber->state;
						}else {
							$address = $f->Subscriber->postcode. ' , '.$f->Subscriber->city. ' , '.$f->Subscriber->state;
						}
					}else if($f->type_application == 'B'){
						$name = $f->Subscriber->company_name;
						$contact_no = $f->Subscriber->company_num;
						$building = $f->Subscriber->street;
						$address = $f->Subscriber->postcode. ' , '.$f->Subscriber->city. ' , '.$f->Subscriber->state;
					}

					if($f->Subscriber->city == 'Jasin'){
						array_push($result,array(
							'type_application' => $f->type_application,
							"type_service" => $f->type_service,
							"customer_name"=> $name,
							"contact_no"=> $contact_no,
							"unit_no"=>$f->Subscriber->unit_no,
							"building"=>$building,
							"street"=>$address,
							"submitted_date" => $f->created_at->toDateTimeString(),
							'dealer' => $labelD,
						));		
					}
					
				}	

			}

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


	public function get_data_form(Request $request){

		$dateS = Carbon::createFromFormat('Y-m-d', '2020-01-01');
        $start = $dateS->copy()->startOfDay();
        $dateE = Carbon::createFromFormat('Y-m-d', '2020-12-31');
        $end = $dateE->copy()->endOfDay();

        $result = array();
        // $data = Form::with('PackageDetail', 'Subscriber')->where('dealer_id', 'exists', false)->whereBetween('created_at', array($start, $end))->get();

        // $data = Form::with('PackageDetail', 'Subscriber','WorkOrder')->where('type_service', '!=', 'Rectification')->whereBetween('created_at', array($start, $end))->get();

        // $data = Form::with('PackageDetail', 'Subscriber','WorkOrder')->where('type_service','New')->whereBetween('created_at', array($start, $end))->get();
		// $data = Form::with('PackageDetail', 'Subscriber','WorkOrder')->withTrashed()->get();
		$data = Form::with('PackageDetail', 'Subscriber','WorkOrder')->whereBetween('created_at', array($start, $end))->get();

        if(count($data) > 0) {
			foreach($data as $f) {	

				$name = ''; $contact_no = '';
				$address = ''; $building = '';
				$labelD = '';

				if(!empty($f->dealer_id)){
					$c = Company::find($f->company_id);
					$labelD = $c->name;
				}else{
					$labelD = "";
				}

				// $amount_claim="";
				// if(!empty($f->Subscriber->DealerClaim)){
				// 	$amount_claim = $f->Subscriber->DealerClaim->total_claim;
				// }
				$subscriber_id='';
				if(!empty($f->Subscriber) && !empty($f->PackageDetail)){
					if($f->type_application == 'R'){
						$name = $f->Subscriber->name;
						$subscriber_id = $f->Subscriber->subscriber_id;
						$contact_no = $f->Subscriber->phone1;
						$building = $f->Subscriber->building_name;
						if($f->Subscriber->street != ''){
							$address = $f->Subscriber->street. ' , '.$f->Subscriber->postcode. ' , '.$f->Subscriber->city. ' , '.$f->Subscriber->state;
						}else {
							$address = $f->Subscriber->postcode. ' , '.$f->Subscriber->city. ' , '.$f->Subscriber->state;
						}
					}else if($f->type_application == 'B'){
						$name = $f->Subscriber->company_name;
						$subscriber_id = $f->Subscriber->subscriber_id;
						$contact_no = $f->Subscriber->company_num;
						$building = $f->Subscriber->street;
						$address = $f->Subscriber->postcode. ' , '.$f->Subscriber->city. ' , '.$f->Subscriber->state;
					}

					$woD = '-'; $woId = 'none'; $natureWork = ''; $doId = ''; $woRate=""; $installer_name=""; $contractor_name="";
					if(!empty($f->WorkOrder)){
						$woId = $f->WorkOrder->wo;
						$natureWork = $f->WorkOrder->nature_work;
						$doId = $f->WorkOrder->docket_id;
						$installer = StaffDetail::where('_id',$f->WorkOrder->installer_id)->get();
							foreach($installer as $installer) {	
							$installer_name = $installer->name;
							}
						$contractor = Company::where('_id',$f->WorkOrder->contractor_id)->get();
							foreach($contractor as $contractor) {	
							$contractor_name = $contractor->name;
							}
						if(!empty($f->WorkOrder->Docket)){
							$woD = $f->WorkOrder->Docket->end_job;
							$woRate = $f->WorkOrder->Docket->Rating1;	
						}
					}
					
					array_push($result,array(
						'type_application' => $f->type_application,
						"type_service" => $f->type_service,
						"customer_name"=> $name,
						"subscriber_id" => $subscriber_id,
						"contact_no"=> $contact_no,
						"unit_no"=>$f->Subscriber->unit_no,
						"building"=>$building,
						"street"=>$address,
						"submitted_date" => $f->created_at->toDateTimeString(),
						"package_contract" => $f->PackageDetail->contract,
						"package_name" => $f->PackageDetail->name,
						'dealer' => $labelD,
						'wo' => $woId,
						'activated_date' => $woD,
						'installer_name' => $installer_name,
						'contractor_name' => $contractor_name,
					));		
					// array_push($result,array(
					// 	// 'Installer' => $installerId,
					// 	'Dealer Name' => $labelD,
					// 	"Customer Name"=> $name,
					// 	"Contact Number"=> $contact_no,
					// 	'Completion Date' => $woD,
					// 	'Type Application' => $f->type_application,
					// 	"Form Type" => $f->type_service,
					// 	"Package"=> $f->PackageDetail->contract,
					// 	"WO No" => $woId,
					// 	"DO No" => $doId,
					// 	"Service Type" => $f->PackageDetail->name,
					// 	"Building"=>$building,
					// 	"Rating" => $woRate,
					// 	// "Claim Amount" => $amount_claim,
					// ));	
				}	

			}
		}

		// return json_encode(count($result));
		return json_encode($result);
	}

	/*********************************** 
	/*   Form Deleted by Company
	***********************************/
	public function get_deleted_by_company($company_id)
	{

		$data = Form::with('PackageDetail', 'Subscriber')->withTrashed()->where('company_id', $company_id)->get();


		return $this->sendResponse($data, 'Form deleted by specific company');
	}
}
