<?php

namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use App\Employee;
use Illuminate\Support\Facades\Mail;

class RoutesController extends Controller
{
	
	public function getTotalWorkOrderVisits($employee, $date)
	{

		$total = \DB::table('work_order_visit_employees')
										->join('work_order_visits', 'work_order_visit_employees.work_order_visit_id', '=', 'work_order_visits.id')
											->select('work_order_visits.id', 'work_order_visits.date')
												->where('work_order_visit_employees.employee_id', $employee)
													->where('work_order_visits.date', $date)
														->where('work_order_visits.status', '!=', '0')
															->count();
												
		return response()->json(array(
			'id' => $employee,
			'total' => $total
		), 200);
		
	}
	
	public function get_route(Request $request)
	{
		
		$employee = Employee::find($request->employee);

		if (!$employee || $employee->company_id != $this->_getCompany()->id) {
			return response()->json([
				'title' => 'Employee',
				'message' => 'Record not found',
			], 404);
		}
		
		if ($request->date) {
			$date = $request->date;
		} else {
			$date = date('Y-m-d');
		}
		
		$work_order_visits = $employee->work_order_visits->where('status', '!=', '0')->where('date', $date);
		
		$arrWorkOrderVisits = $this->_formatWorkOrderVisits($date, $employee, $work_order_visits);
	
		return response()->json($arrWorkOrderVisits, 200);
		
	}
	
	private function _formatWorkOrderVisits($date, $employee, $work_order_visits)
	{
		
		$arrWorkOrderVisits = array(
			'date' => date('m/d/Y', strtotime($date)),
			'employee_name' => $employee->name,
			'employee_color' => $employee->preferences['color'],
			'points' => array()
		);
		
		$counter = 1;
		
		foreach ($work_order_visits as $work_order_visit) {
			
			/*$services = '';
			foreach ($work_order_visit->services as $service) {
				$services .= $service->name . ' (' . $service->pivot->qty . '), ';
			}
			
			$products = '';
			foreach ($work_order_visit->products as $product) {
				$products .= $product->name . ' (' . $product->pivot->qty . '), ';
			}
			
			$counter_notes = 1;
			$customer_notes = '';
			foreach ($work_order_visit->work_order->customer->getPinnedNotes() as $note) {
				$customer_notes .= '[' . $counter_notes . '] ' . $note->content . ' ';
				$counter_notes++;
			}
			
			$property_notes = '';
			foreach ($work_order_visit->work_order->property->getPinnedNotes() as $note) {
				$property_notes .= '[' . $counter_notes . '] ' . $note->content . ' ';
				$counter_notes++;
			}
			
			$work_order_visit_notes = '';
			foreach ($work_order_visit->getPinnedNotes() as $note) {
				$work_order_visit_notes .= '[' . $counter_notes . '] ' . $note->content . ' ';
				$counter_notes++;
			}
			
			$notes = $customer_notes . $property_notes . $work_order_visit_notes;*/
			
			$arrWorkOrderVisits['points'][] = array(
				'work_order_visit_id' => $work_order_visit->id,
				'work_order_visit_number' => $work_order_visit->number,
				'work_order_id' => $work_order_visit->work_order->id,
				'work_order_number' => $work_order_visit->work_order->number,
				'start_time' => ($work_order_visit->start_time == '00:00:00') ? 'ANYTIME' : date('h:i A', strtotime($work_order_visit->start_time)),
				'end_time' => ($work_order_visit->end_time == '00:00:00') ? 'ANYTIME' : date('h:i A', strtotime($work_order_visit->end_time)),
				'status' => $work_order_visit->status_formatted,
				'order' => $counter,
				'customer' => $work_order_visit->work_order->customer->name,
				'property_address' => $work_order_visit->work_order->property->location['address'],
				'property_lat' => $work_order_visit->work_order->property->location['lat'],
				'property_lng' => $work_order_visit->work_order->property->location['lng'],
				'has_dog' => $work_order_visit->work_order->property->has_dog_formatted,
				'address_type' => $work_order_visit->work_order->property->address_type_formatted,
				'gate_code' => ($work_order_visit->work_order->property->general_informations['gate_code']) ? $work_order_visit->work_order->property->general_informations['gate_code'] : '-',
				/*'services' => $services,
				'products' => $products,
				'notes' => $notes*/
			);
			
			$counter++;
			
		}
		
		return $arrWorkOrderVisits;
		
	}

}
