<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class warehouse extends Admin_Controller
{

	function __construct()
	{
		parent::__construct();
	}

	public function index()
	{



		$warehouse = reindex_by_key($this->db->query("SELECT *  FROM `warehouse`")->result(), 'ID');

		$this->template
			->set('view', 'admin/warehouse/index')
			->set('warehouse', $warehouse)
			->render();
	}

	public function action()
	{

		$name    = $this->input->post('name');
		$address = $this->input->post('address');
		$status  = $this->input->post('status');


		// if(!empty($name) && !empty($type)){
		$ware_data = array(
			'Name'      => $name,
			'Address'   => $address,
			'Status'    => $status,
		);

		$add_row = $this->general_model->insertRow('warehouse', $ware_data);

		redirect("admin/warehouse", "refresh");
	}

	public function changeStatus()
	{
		$data = array();
		if (!empty($_POST) && isset($_POST)) {
			$warehouse       = $_POST['warehouse'];
			$status = $this->db->query("SELECT `Status` FROM `warehouse` WHERE `ID` = $warehouse ")->row();
			$warehouse_status = 0;
			if ($status->Status == 0) {
				$warehouse_status = 1;
			} else {
				$warehouse_status = 0;
			}


			$updateData = array(
				'Status' => $warehouse_status,
			);
			$update_warehouse = $this->general_model->updateRowById('warehouse', 'ID',  $_POST['warehouse'], $updateData);
			$data['result'] = "true";
			// debug($data); 
			echo json_encode($data);
		} else {

			$data["result"] = "false";
			echo json_encode($data);
		}
	}
	public function view()
	{
		$warehouse_id = $this->uri->segment('4');
		$warehouse_items = reindex_by_key($this->db->query("SELECT warehouse_items.*, products.id, products.name, products.code 
		FROM `warehouse_items` 
		LEFT JOIN `products` ON warehouse_items.product_id = products.id
		WHERE warehouse_items.warehouse_id = $warehouse_id
		GROUP BY warehouse_items.product_id	")->result(), 'id');

		foreach ($warehouse_items as $item) {

			$delivery_amount = $this->db->query("SELECT sum(unit) as amount
			FROM `order_items` 
			WHERE `store_id` = $warehouse_id AND `product_id` = $item->id
			GROUP BY product_id	")->row();
            if(!empty($delivery_amount) && isset($delivery_amount)){
				$amount = $delivery_amount->amount; 
				$item->delivery_amount = $amount; 
			}else{ 
			$item->delivery_amount = 0; 
			}


			
		}

		// debug($warehouse_items);

		$this->template
			->set('view', 'admin/warehouse/view')
			->set('warehouse_items', $warehouse_items)
			->set('warehouse_id', $warehouse_id )
			->render();
	}




	// public function action()
	// {
	// 	$data = (object)$_POST;
	// 	// debug($data);

	// 	$customerId     = $this->input->post('customer');
	// 	$comment        = $this->input->post('comment');
	// 	$total          = $this->input->post('total');
	// 	$date           = $this->input->post('order_date');
	// 	$total_discount = $this->input->post('total_discount');


	// 	$orderData = array(
	// 		'customerId'  => $customerId,
	// 		'total'       => $total,
	// 		'discount'    => $total_discount,
	// 		'status'      => 0,
	// 		'date'        => $date,
	// 		'comment'     => $comment,
	// 	);
	// 	// debug($date);
	// 	$order_id = $this->general_model->insertRow('orders', $orderData);
	// 	if (!empty($order_id)) {

	// 		$items = (object)$data->items;
	// 		for ($i = 0; $i < count($data->items['product']); $i++) {
	// 			$productid = $items->product[$i];
	// 			 $product_details = $this->db->query("SELECT `name`,`quantity` FROM `products` WHERE `id` = '$productid'")->row();
	// 			// debug($product_details);
	// 			$orderItemsData = array(
	// 				'order_id'      => $order_id,
	// 				'product_id'    => $items->product[$i],
	// 				'customer_id'   => $customerId,
	// 				'item_name'     => $product_details->name,
	// 				'price'         => $items->price[$i],
	// 				'unit'          => $items->unit[$i],
	// 				'discount'      => $items->discount[$i],
	// 				'full_price'    => $items->full_price[$i],
	// 				'date_creation'  => $date,
	// 				'last_update'    => date('Y-m-d H:i:s')
	// 			);
	// 			$item_insert = $this->general_model->insertRow('order_items', $orderItemsData);

	// 			$finall_amount = $product_details->quantity - $items->unit[$i];
	// 			$updateData= array(
	// 				'quantity' => $finall_amount,
	// 				'lastUpdate' => date('Y-m-d H:i:s')
	// 			);

	// 			$update_product= $this->general_model->updateRowById('products', 'id', $items->product[$i], $updateData);
	// 		}
	// 		redirect('admin/orders', 'refresh');
	// 	}



	// 	redirect('admin/orders', 'refresh');
	// }

	// public function view()
	// {
	// 	$id = intval($this->uri->segment(4));
	// 	$order= $this->db->query("SELECT * FROM `orders` WHERE ID = $id" )->row();
	// 	$customerId = $order->customerId;
	// 	$customer= $this->db->query("SELECT * FROM `customers` WHERE customerId = $customerId")->row();
	// 	$order_items  = $this->db->query("SELECT * FROM `order_items` WHERE order_id = $id" )->result();


	// 	$this->template
	// 		->set('view', 'admin/orders/view')
	// 		->set('order_items', $order_items)
	// 		->set('order', $order)
	// 		->set('customer', $customer)

	// 		->render();
	// }
}
