<?php

namespace App\Http\Controllers\DS;

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\PackageDetail;
use App\Model\FormStatus;
use App\Model\WorkOrder;
use App\Model\Docket;
use App\Model\Coverage; 
use App\Model\DealerClaim;
use App\Model\Commission;
use App\Model\Company;

class SalesController extends Controller
{
	/***
	/ Application Form List
	***/
	public function viewAppList(){

		$id = Auth::guard('agent')->id();
		$user = Staff::with('StaffDetail')->find($id);
		return view('sales.application-form-list',compact('user'));
	}


	public function gApplicationList1(){

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

		$forms = Form::with('Subscriber','PackageDetail','WorkOrder')->where('dealer_id', $user->_id)->orderBy('created_at','desc')->get();

		$i = 0; $street = ''; $n1 = '';
		$curr = Carbon::now()->getTimestamp();
		$nested_data = array();
		if(!empty($forms)){
			foreach ($forms as $c){
				if(empty($c->WorkOrder)){
					$i++;

					if($c->Subscriber->street != null){
						$street = $c->Subscriber->street;
					}

					$agentName = StaffDetail::where('_id',$c->dealer_id)->first();

					$n1 = '';
					$reg_time = $c->created_at;
					$expiry_date = $reg_time->addDays(3);    
					$expiry_date = $expiry_date->getTimestamp(); 

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

					$address = ''; $name = '';
					if($c->type_application == 'R'){
						$address = $c->Subscriber->building_name;
						$name = $c->Subscriber->name;
					}else if($c->type_application == 'B'){
						$address = '-';
						$name = $c->Subscriber->company_name;
					}

					array_push($nested_data, array(
						'index' => $n1.$i.$c->type_application,
						'name' => $name,
						'phone' => $c->Subscriber->phone1,
						'unit' => $c->Subscriber->unit_no,
						'building' => $address,
						'street' => $street,
						'postcode' => $c->Subscriber->postcode,
						'city' => $c->Subscriber->city,
						'sales' => $agentName->name,
						'action' => $c->_id
					));
				}
			}
		}
		
		return \DataTables::of($nested_data)->make(true);
	}

	public function gApplicationList2(){

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

		$forms = Form::with('Subscriber','PackageDetail','WorkOrder')->where('dealer_id', $user->_id)->orderBy('created_at','desc')->get();

		$i = 0; $street = ''; $n1 = '';
		$curr = Carbon::now()->getTimestamp();
		$nested_data = array();
		if(!empty($forms)){
			foreach ($forms as $c){
				if(!empty($c->WorkOrder)){

					if($c->WorkOrder->status != 'Completed') {
						$i++;

						if($c->Subscriber->street != null){
							$street = $c->Subscriber->street;
						}

						$agentName = StaffDetail::where('_id',$c->dealer_id)->first();

						$n1 = '';
						$reg_time = $c->created_at;
						$expiry_date = $reg_time->addDays(3);    
						$expiry_date = $expiry_date->getTimestamp(); 

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

						$address = ''; $name = '';
						if($c->type_application == 'R'){
							$address = $c->Subscriber->building_name;
							$name = $c->Subscriber->name;
						}else if($c->type_application == 'B'){
							$address = '-';
							$name = $c->Subscriber->company_name;
						}

						array_push($nested_data, array(
							'index' => $n1.$i.$c->type_application,
							'name' => $name,
							'phone' => $c->Subscriber->phone1,
							'unit' => $c->Subscriber->unit_no,
							'building' => $address,
							'street' => $street,
							'postcode' => $c->Subscriber->postcode,
							'city' => $c->Subscriber->city,
							'sales' => $agentName->name,
							'action' => $c->_id
						));
					}
				}
			}
		}
		
		return \DataTables::of($nested_data)->make(true);
	}

	public function gApplicationList3($year, $month){

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

		$forms = Form::with('Subscriber','PackageDetail','WorkOrder')->where('dealer_id', $user->_id)->orderBy('created_at','desc')->get();

		$mY = '';
		if($year != 'null' && $month != 'null'){
			$mY = $year.'-'.$month;
		}

		$i = 0; $street = ''; $n1 = '';
		$final = array(); $filter = array();

		$curr = Carbon::now()->getTimestamp();
		$nested_data = array();
		if(!empty($forms)){
			foreach ($forms as $f){
				if(!empty($f->WorkOrder)){

					if($f->WorkOrder->status == 'Completed') {

						$w = WorkOrder::with('Docket')->where('wo', $f->WorkOrder->wo)->first();
						if(!empty($w->Docket)){
							$dealer = StaffDetail::where('_id', $f->dealer_id)->first();
							$i++;

							$address = ''; $name = '';
							if($f->type_application == 'R'){
								$address = $f->Subscriber->building_name;
								$name = $f->Subscriber->name;
							}else if($f->type_application == 'B'){
								$address = '-';
								$name = $f->Subscriber->company_name;
							}

							array_push($nested_data, array(
								'index' => $i.$f->type_application,
								'wo' => $w->wo,
								'do' => $f->WorkOrder->docket_id,
								'activated' => $w->Docket->end_job,
								'name' => $name,
								'phone' => $f->Subscriber->phone1,
								'unit' => $f->Subscriber->unit_no,
								'building' => $address,
								'street' => $f->Subscriber->street,
								'postcode' => $f->Subscriber->postcode,
								'city' => $f->Subscriber->city,
								'sales' => $dealer->name,
								'action' => $f->_id
							));
						// }else {
						// 	$nested_data[] = $w;
						// }
						


						// if($mY != ''){
						// 	$docket = Docket::where('work_order_id', $c->WorkOrder->wo)->first();
						// 	$cDate = date('Y-m', strtotime($docket->end_job));
						// 	if($mY == $cDate){
						// 		$filter[] = $c->_id; 
						// 	}
						// 	$filter[] = $c->_id; 
						// }else {
						// }

						}
					}

				// if(!empty($filter)){

				// 	if(array_search($c->_id,$filter) !== False) {

				// 		$i++;

				// 		if($c->Subscriber->street != null){
				// 			$street = $c->Subscriber->street;
				// 		}

				// 		$agentName = StaffDetail::where('_id',$c->dealer_id)->first();

				// 		$n1 = '';
				// 		$reg_time = $c->created_at;
				// 		$expiry_date = $reg_time->addDays(3);    
				// 		$expiry_date = $expiry_date->getTimestamp(); 

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

				// 		$docket = Docket::where('work_order_id',$c->WorkOrder->wo)->first();
				// 		array_push($nested_data, array(
				// 			'index' => $n1.$i.$c->type_application,
				// 			'wo' => $c->WorkOrder->wo,
				// 			'do' => $docket->docket_id,
				// 			'activated' => $docket->end_job,
				// 			'name' => $c->Subscriber->name,
				// 			'phone' => $c->Subscriber->phone1,
				// 			'unit' => $c->Subscriber->unit_no,
				// 			'building' => $c->Subscriber->building_name,
				// 			'street' => $street,
				// 			'postcode' => $c->Subscriber->postcode,
				// 			'city' => $c->Subscriber->city,
				// 			'sales' => $agentName->name,
				// 			'action' => $c->_id
				// 		));

				// 	}
				}
			}
		}
		
		return \DataTables::of($nested_data)->make(true);
	}

	/***
	/ Application Form
	***/
	public function showResidentialForm(){

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

		return view('sales.residential-form',compact('user'));
	}

	public function showBusinessForm(){

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

		return view('sales.business-form',compact('user'));
	}

	/***
	/ Form
	***/

	public function getOrderHistory($id){

		$data = array(); 
		$order_history = FormStatus::where('form_id', $id)->get();

		foreach($order_history as $oh) {

			$data[] = array(
				'date' => Carbon::parse($oh->created_at)->setTimezone('Asia/Kuala_Lumpur')->toDateTimeString(),
				'status'  => $oh->status,
				'desc'   => $oh->desc
			);
		}

		$json = json_encode($data);
		return $json;
	}
}
