<?php

namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Validator;
use App\Customer;
use App\Property;
use App\Note;
use App\Http\Requests\SearchRequest;

class SearchController extends Controller
{

	public function index(SearchRequest $request)
    {
		
		$word = $request->word;
		
		$arrResult = array();
		
		// Customers

		$customers = Customer::where('company_id', $this->_getCompany()->id)
								->where(function($q) use ($word) {
									$q->where('name', 'like', '%' . $word . '%')
									->orWhere('contacts', 'like', '%' . $word . '%');
								})
								//->where('status', '!=', 0)
								->orderBy('name', 'asc')
								->get();
								
		$arrCustomer = array();
		
		foreach ($customers as $customer) {	
		
			if ($customer->status == 1) {
				$status = 'Active';
			} else if ($customer->status == 2) {
				$status = 'Lead';
			} else {
				$status = 'Archived';
			}
			
			$arrCustomer[] = array(
				'title' => $customer->name,
				'description' => array(
					'Status' => $status,
					'Primary Phone' => $customer->contacts['phone_primary'],
					'Secondary Phone' => (isset($customer->contacts['phone_secondary'])) ? $customer->contacts['phone_secondary'] : null,
					'Fax' => (isset($customer->contacts['fax'])) ? $customer->contacts['fax'] : null,
					'Email' => $customer->contacts['email']
				),
				'url' => 'customers/' . $customer->id
			);
		}
								
		$arrResult['Customers'] = $arrCustomer;
		
		// Properties
		
		$properties = Property::with('customer')->where('company_id', $this->_getCompany()->id)
								->where('location', 'like', '%' . $word . '%')
								->orderBy('customer_id', 'asc')->get();
								
		$arrProperty = array();
		
		foreach ($properties as $property) {			
			$arrProperty[] = array(
				'title' => $property->location['address'],
				'description' => array(
					'Customer' => $property->customer->name,
					'Gate Code' => (isset($property->general_informations['gate_code'])) ? $property->general_informations['gate_code'] : null,
					'Dogs?' => $property->has_dog_formatted,
					'Bodies of Water' => implode(', ', $property->bodies_of_water)
				),
				'url' => 'properties/' . $property->customer->id . '/detail/' . $property->id,
				'url_customer' => 'customers/' . $property->customer->id
			);
		}
								
		$arrResult['Properties'] = $arrProperty;
		
		// Notes
		
		$notes = Note::where('company_id', $this->_getCompany()->id)
								->where('content', 'like', '%' . $word . '%')
								->orderBy('created_at', 'desc')->get();
		
		$arrNote = array();
		
		foreach ($notes as $note) {	
		
			if ($note->notable_type == 'App\Customer') {
				$url = 'customers/' . $note->notable_id;
			} else if ($note->notable_type == 'App\Property') {
				$url = 'properties/' . $note->notable->customer->id . '/detail/' . $note->notable_id;
			} else if ($note->notable_type == 'App\WorkOrderVisit') {
				$url = 'work-order-visits/' . $note->notable->id;
			}
		
			$arrNote[] = array(
				'title' => 'By ' . $note->employee->name . ' on ' . $note->created_at_formatted,
				'description' => array(
					'Content' => $note->content,
					'Follow Up Until' => $note->follow_up_until
				),
				'url' => $url
			);
			
		}
		
		$arrResult['Notes'] = $arrNote;

		return response()->json($arrResult, 200);
	
    }
	
	public function searchCustomers(Request $request)
	{
		
		$word = $request->word;
		
		$arrCustomer = array();
		
		if ($word && strlen($word) >= 3) {
		
			$customers = Customer::where('company_id', $this->_getCompany()->id)
									->where('name', 'like', '%' . $word . '%')
										->where('status', '!=', 0)
											->orderBy('name', 'asc')
												->limit(10)
													->get();
				
			foreach ($customers as $customer) {	
				$arrCustomer[] = $customer->name;
			}
		
		}
		
		return response()->json($arrCustomer, 200);
		
	}
	
	public function getCustomerByName(Request $request)
	{
		
		$name = $request->name;
		
		$customer = null;
		
		if ($name && strlen($name) >= 3) {
		
			$customer = Customer::where('company_id', $this->_getCompany()->id)
									->where('name', $name)
										->where('status', '!=', 0)
											->orderBy('name', 'asc')
												->first();
														
		}
		
		return response()->json($customer, 200);
		
	}

}
