<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

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

class admin_item_editor_ajax {

	private $base_object;
	private $config;
	private $return;

	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->config = isset($base_object->config_object) ? $base_object->config_object : null;
		$this->layout_object = $layout_object;
	}

	function run() {
		// Start output buffering to catch any debug output
		ob_start();

		if (isset($_POST['action'])) {
			$item_object = new Item($this->base_object);

			switch ($_POST['action']) {
				case 'prices':
					if ($_POST['id']) {
						$item_object->id = $_POST['id'];

						$customerGroup = ($_POST['customerGroup']) ? $_POST['customerGroup'] : 1;

						$this->return['prices'] = $item_object->getPricesForCustomerGroup($customerGroup);
						unset($this->return['prices']->item_id);
						unset($this->return['prices']->customergroup_id);

						//$this->return['variants'] = $item_object->getVariantPricesForCustomerGroup($customerGroup, true, false, 0, 0, false);

					} else {
						$this->return = array();
					}
					echo json_encode($this->return);
					break;

				case 'itemList':
					$ret = array();
					if (isset($_POST['search']) && $_POST['search']) {
						$search = trim($_POST['search']);
						if (strlen($search) >= 2) {
							// Call Python search script directly!
							$python_script = dirname(__FILE__) . '/import/python_search.py';
							$command = "python3 " . escapeshellarg($python_script) . " " . escapeshellarg($search);
							$output = shell_exec($command);
							
							if ($output) {
								$ret = json_decode($output, true);
								if (!$ret) {
									$ret = array();
								}
							}
							
							// Fallback to old PHP method if Python fails
							if (empty($ret)) {
								$ret = $item_object->find_for_ajax($search, 20);
								
								// Convert PHP format to jQuery UI format
								$formatted_ret = array();
								if (is_array($ret)) {
									foreach ($ret as $item) {
										$status = ($item->active == 1) ? ' || aktiv' : ' || inaktiv';
										$formatted_ret[] = array(
											'id' => $item->id,
											'value' => $item->number,
											'label' => $item->name . ' (' . $item->number . $status . ')',
											'number' => $item->number,
											'name' => $item->name,
											'active' => $item->active
										);
									}
								}
								$ret = $formatted_ret;
							}
						}
					}
					
					// Ensure we always return valid JSON
					if (!$ret) {
						$ret = array();
					}
					
					// Clear any debug output that was captured
					ob_clean();
					
					header('Content-Type: application/json');
					echo json_encode($ret);
					break;

				case 'get_schema':
					$schema_id = ($_POST['schema_id']) ? $_POST['schema_id'] : 0;
					$item_object->set_schema_id($schema_id);
					$ret = $item_object->get_schema_data();

					echo json_encode($ret);
					break;
				case 'new_variant':
					$parent_id = ($_POST['parent_id']) ? $_POST['parent_id'] : 0;
					$data = array();

					// get parent item data
					$item_object->id = $parent_id;
					$parent_item_data = $item_object->get_data();
					$data['parent_id'] = $parent_id;

					// schema data
					$data['schema_id'] = $parent_item_data->schema_id;
					include_once './core/itemschema.class.php';
					$item_schema_object = new ItemSchema($this->base_object);
					$schema_data = $item_schema_object->get_data($data['schema_id']);
					$data['attribute_'.$schema_data->selectable_attribute_1] = ($_POST['attr1']) ? $_POST['attr1'] : '';
					if ($schema_data->selectable_attribute_2) {
						$data['attribute_'.$schema_data->selectable_attribute_2] = ($_POST['attr2']) ? $_POST['attr2'] : '';
					}

					$data['manufacturer_id'] = $parent_item_data->manufacturer_id;
					$data['name'] = $parent_item_data->name;
					$data['number'] = $parent_item_data->number;
					$data['detail_description'] = $parent_item_data->detail_description;
					$data['manufacturer_item_number'] = $parent_item_data->manufacturer_item_number;
					$data['default_image_file_name'] = $parent_item_data->default_image_file_name;
					$data['seo_title'] = $parent_item_data->seo_title;
					$data['seo_description'] = $parent_item_data->seo_description;
					$data['seo_keywords'] = $parent_item_data->seo_keywords;
					$data['ean_code'] = $parent_item_data->ean_code;
					$data['price_type'] = $parent_item_data->price_type;
					$data['shippable'] = $parent_item_data->shippable;
					$data['shippable_inventory'] = $parent_item_data->shippable_inventory;
					$data['tax_id'] = $parent_item_data->tax_id;


					$item_object->create($data);
					//$ret = $item_object->get_schema_data();

					echo json_encode($ret);
					break;

				case 'delete_variant':
					$id = ($_POST['id']) ? $_POST['id'] : 0;
					$item_object->delete_by_id($id);

					echo json_encode($ret);
					break;

				case 'get_item_variants':
					$id = ($_POST['id']) ? $_POST['id'] : 0;
					$item_object->id = $id;
					$data = $item_object->get_data(1, false);
					$ret = array();
					$this->layout_object->assign('item_data', $data);
					$ret['html'] =  $this->layout_object->fetch('admin_item_editor_variant_table.tpl');

					echo json_encode($ret);
					break;

				case 'new_inventory':
					$item_id = ($_POST['id']) ? $_POST['id'] : 0;
					
					if (!$item_id) {
						$ret = array('status' => 'error', 'message' => 'No item ID provided');
						echo json_encode($ret);
						break;
					}
					
					$data = array();
					$data['item_id'] = $item_id;
					$data['inventory_object_type_id'] = ($_POST['inventory_type']) ? $_POST['inventory_type'] : '';
					$data['amount'] = ($_POST['inventory_amount']) ? $_POST['inventory_amount'] : '';
					
					include_once './core/item_inventory.class.php';
					$inventory_object = new ItemInventory();
					$result = $inventory_object->add($data);
					
					if ($result === false) {
						$ret = array('status' => 'error', 'message' => 'Failed to add inventory: ' . $inventory_object->error);
					} else {
						$ret = array('status' => 'success', 'result' => $result);
					}
					echo json_encode($ret);
					break;
				case 'delete_item_item_assign':
					$item_parent_id = $_POST['item_parent_id'];
					$item_child_id = $_POST['item_child_id'];
					$result = $item_object->delete_item_item_assign($item_parent_id, $item_child_id);

					$ret = array('status' => $result ? 'success' : 'error');
					echo json_encode($ret);
					break;

				case 'get_inventory':
					$id = ($_POST['id']) ? $_POST['id'] : 0;
					$item_object->id = $id;
					$data = $item_object->get_data(1, false);
					$this->layout_object->assign('item_data', $data);
					$ret = array();
					// item inventory
					if ($id) {
						include_once './core/item_inventory.class.php';
						$inventory_object = new ItemInventory();
						$inventory = $inventory_object->get_all_item_inventory($id);
						$this->layout_object->assign('item_inventory', $inventory);
					}
					//$this->layout_object->assign('item_data', $data);
					$ret['html'] =  $this->layout_object->fetch('admin_item_editor_inventory_table.tpl');

					echo json_encode($ret);
					break;
			}
		}

		exit();

		return false;
	}

}

?>
