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


class OrderCtrl extends CI_Controller
{
	public function __construct($config = 'rest')
	{
		header('Access-Control-Allow-Origin: *');
		header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
		parent::__construct();
		$this->load->model('Common_m');
	}

	public function curlApi($url = '')
	{

		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);

		$response = curl_exec($ch);
		curl_close($ch);

		return $response;
	}

	public function user_order()
	{
		chk_login();
		$user_id = $_SESSION['user_id'];

		$data['orders'] = json_decode($this->curlApi("https://myadmin.mali-mart.com/api/user-order/$user_id"));

		$this->load->view('templates/1_head.php');

		$this->load->view('templates/2_nav.php');

		$this->load->view('templates/3_sidebar_menu_left.php');

		$this->load->view('user/product_order', $data);

		$this->load->view('templates/5_footer.php');

		$this->load->view('templates/6_script_end.php');
	}
	public function cancel_order()
	{
		$order_id = $this->input->get('order_id');
		$response = json_decode($this->curlApi("https://myadmin.mali-mart.com/api/user-cancel-order/$order_id"));
		if ($response->msg == 1) {
			$this->session->set_flashdata(array("type" => "success", "response" => "Your order successfully cancel"));
		} else {
			$this->session->set_flashdata(array("type" => "danger", "response" => "something went wrong / server error"));
		}
		redirect('OrderCtrl/user_order');
	}

	public function userOrderDetails()
	{
		chk_login();

		$order_id = $this->input->get('order_id');

		$responseData = json_decode($this->curlApi("https://myadmin.mali-mart.com/api/order-details/$order_id"));

		$data['order'] = $responseData->order;

		$data['orderDetails'] = $responseData->order_details;



		$this->load->view('templates/1_head.php');

		$this->load->view('templates/2_nav.php');

		$this->load->view('templates/3_sidebar_menu_left.php');

		$this->load->view('user/order_detail', $data);

		$this->load->view('templates/5_footer.php');

		$this->load->view('templates/6_script_end.php');
	}

	public function marchant_order()
	{
		chk_login();

		$user_id = $_SESSION['user_id'] + 1000;
		$data = [];

		$orders = json_decode($this->curlApi("https://myadmin.mali-mart.com/api/marchant-order/$user_id"));
		if (!empty($orders)) {

			$data['pending'] = $orders->pending;
			$data['approved'] = $orders->approved;
		}


		$this->load->view('templates/1_head.php');

		$this->load->view('templates/2_nav.php');

		$this->load->view('templates/3_sidebar_menu_left.php');

		$this->load->view('marchant/order', $data);

		$this->load->view('templates/5_footer.php');

		$this->load->view('templates/6_script_end.php');
	}
	public function marchant_order_details($order_id)
	{

		chk_login();

		$user_id = $_SESSION['user_id'] + 1000;

		$data['order_details'] = json_decode($this->curlApi("https://myadmin.mali-mart.com/api/marchant/order-details/$user_id/$order_id"));

		$this->load->view('templates/1_head.php');

		$this->load->view('templates/2_nav.php');

		$this->load->view('templates/3_sidebar_menu_left.php');

		$this->load->view('marchant/order_details', $data);

		$this->load->view('templates/5_footer.php');

		$this->load->view('templates/6_script_end.php');
	}


	public function marcentUserInfo()
	{
		$userID = $this->input->post('user_id');
		$marcentID = $this->input->post('marcent_id');

		$response = [];

		if (!empty($userID) && !empty($marcentID)) {
			$query = $this->db->query("SELECT users_login.mobile_num,users_detail.name FROM `users_login` LEFT JOIN users_detail ON users_login.user_id=users_detail.id WHERE user_id=" . $userID);
			$response['user'] = $query->result()[0];
			$query = $this->db->query("SELECT users_login.mobile_num,users_detail.name FROM `users_login` LEFT JOIN users_detail ON users_login.user_id=users_detail.id WHERE user_id=" . $marcentID);
			$response['marcent'] = $query->result()[0];
			$response['success'] = true;
		} else {
			$response['success'] = false;
		}
		echo json_encode($response);
	}

	public function marcentReturn()
	{

		$marcentID = 1000 + $_SESSION['user_id'];

		$data['pdt_return'] = json_decode($this->curlApi("https://myadmin.mali-mart.com/api/marcent-return-product/$marcentID"));

		$this->load->view('templates/1_head.php');

		$this->load->view('templates/2_nav.php');

		$this->load->view('templates/3_sidebar_menu_left.php');

		$this->load->view('marchant/pdt_return', $data);

		$this->load->view('templates/5_footer.php');

		$this->load->view('templates/6_script_end.php');
	}

	public function productReturnBlnc($userId, $blnc)
	{

		$result = $this->db->query("UPDATE `total_balance` SET cashblnc=(cashblnc+$blnc) WHERE user_id=$userId");
		$response = $result ? 1 : 0;
		echo json_encode($response);
	}
}
