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

class Orderctrl extends CI_Controller
{
	public $Ordermdl;
	public $excel;
	public $pagination;
	public function __construct()
	{
		parent::__construct();
		$this->load->model('order/Ordermdl');
		$this->load->library('session');
		$this->load->library('upload');
		$this->load->library('pagination');
		$this->config->load('extra');
		$this->load->library('db_operations');
	}

	public function order_master()
	{
		if(sessionExist())
		{
			if($_GET['action'] == 'view')
			{
				$config 				= array();
				$config 				= $this->config->item('pagination');
				$config['total_rows'] 	= $this->Ordermdl->get_all_order_data_cnt();
				$config['base_url'] 	= base_url("order/orderctrl/order_master?search=true");

				foreach ($_GET as $key => $value)
				{
					if($key != 'search' && $key != 'offset')
					{
						$config['base_url'] .= "&" . $key . "=" .$value;
					}
				}

				$offset = (!empty($_GET['offset'])) ? $_GET['offset'] : 0;
				$this->pagination->initialize($config);

				$record['count'] 		= $offset;
				$record['total_rows'] 	= $config['total_rows'];
				$record['role'] 		= $this->config->item('role');
				$record['data']			= $this->Ordermdl->get_all_order_data($config['per_page'],$offset);
				$this->load->view('pages/order/order_master', $record);
			}
			elseif($_GET['action'] == 'edit')
			{
				$om_id = encrypt_decrypt("decrypt", $_GET['id'], SECRET_KEY);
				$record = $this->Ordermdl->get_order_data_for_edit($om_id);
				// echo "<pre>"; print_r($record); exit;
				$this->load->view('pages/order/order_form', $record);
			}
			elseif($_GET['action'] == 'print')
			{
				$om_id = encrypt_decrypt("decrypt", $_GET['id'], SECRET_KEY);
				$record = $this->Ordermdl->get_order_data_for_edit($om_id);
				// echo "<pre>"; print_r($record); exit;
				$this->load->view('pdfs/challan/challan_print', $record);
			}
		}
		else
		{
			redirect('loginctrl/logout');
		}
	}

	public function confirm_order($id)
	{
		if (sessionExist())
		{
			$result = $this->db_operations->data_update('order_trans', array('ot_status' => 1), 'ot_id', $id);
			echo !empty($result) ? 1 : 0;
		}
		else
		{
			redirect('loginctrl/logout');
		}

	}
}