<?php

include_once './core/customer.class.php';

class admin_customer {
	private $base_object;
	private $config;
	private $layout_object;
	
	public function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->config = $base_object->config_object;
		$this->layout_object = $layout_object;
	}
	
	public function run() {		
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} else {
			$this->default_action();
		}
		
		if ($action == 'get_customer_by_string') {
			$this->get_customer_by_string();
		} elseif ($action == 'get_customer_addresses_by_id') {
			$this->get_customer_addresses_by_id();
		} else {
			$this->default_action();
		}
	}
	
	private function default_action() {
		$return['status'] = 'error';
		$return['message'] = "ERROR: Unbekannter Methodenaufruf.";
				
		echo json_encode($return);
	}
	
	private function get_customer_by_string() {
		if (isset($_GET['data'])) {
			$search_string = $_GET['data'];
		} else {
			$search_string = false;
		}
		
		if ($search_string) {
			$data = $_GET['data'];
			$customer = $this->base_object->customer;
			$result = $customer->find_customer_like($data);
			
			if ($result) {
				$return = array('status' => 'success', data => $result);
			} else {
				$return = array('status' => 'success', data => array());
			}
		} else {
				$return = array('status' => 'error', 'message' => 'missing argument');
		}
		
		echo json_encode($return);
		exit();
	}
	
	// deprecated, new version in customer_actions
	private function get_customer_addresses_by_id() {
		$id = $_GET['id'];
		
		if ($id) {
			if (Customer::has_id($id)) {
				$customer = $this->base_object->customer;
				$customer_name = Customer::get_name_by_id($id, false);
				
				$result = $customer->get_address_by_id($id);
				$customer_data = $customer->get_by_id($id);
				
				
				$return['status'] = 'success';
				$return['message'] = "Kunden gefunden.";
				$return['data'] = $result;
				$return['name'] = $customer_name;
			} else {
				$return['status'] = 'error';
				$return['message'] = 'Id nicht vergeben.';
			}
		} else {
			$return['status'] = 'error';
			$return['message'] = "Keine Daten �bergeben.";
		}
		
		echo json_encode($return);
	}
}