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



class Cart extends MY_Controller {



    Public function __construct(){

        parent::__construct();

        $this->load->model("cart_model","kart");

    }

    public function showCartData()
	{
		echo "<pre>";
		print_r ($this->session->all_userdata());
		echo "</pre>";die;
	}



	public function addProductInCart()

	{
		//print_r($_POST); die;
		$data = $this->kart->addProductInCart($_POST);

		$is_user_logged_in = $this->session->userdata('is_user_logged_in');

        /*if($this->cart->contents() && $is_user_logged_in==true){
            $this->profileid = $this->session->userdata('profileid');
            $this->email = $this->session->userdata('email');
            $uc = $this->common->getsData('user_cart',array('userid'=>$this->profileid));
            if($uc){
            	$this->common->updateData('user_cart',array('date'=>date('Y-m-d H:i:s'),'send_mail'=>0),array('userid'=>$this->profileid));
            }else{
            	$this->common->insertData('user_cart',array('date'=>date('Y-m-d H:i:s'),'userid'=>$this->profileid,'email'=>$this->email,'send_mail'=>0));
            }
            foreach ($this->cart->contents() as $key => $value){
            	$ucp = $this->common->getsData('user_cart_product',array('userid'=>$this->profileid,'productid'=>$value["options"]['productid']));
	            if($ucp){
	            	
	            }else{
	            	$this->common->insertData('user_cart_product',array('productid'=>$value["options"]['productid'],'userid'=>$this->profileid));
	            }
            }
        }*/
		
		header('Content-type: application/json; charset=utf-8"');

		echo json_encode($data);die;

	}

	public function deleteProductFromCart()

	{
		//echo "string"; die;
		$data = $this->kart->deleteProductFromCart($_POST);
		checkCouponAmmount();
		/*$is_user_logged_in = $this->session->userdata('is_user_logged_in');
        if(empty($this->cart->contents()) && $is_user_logged_in==true){
            $this->profileid = $this->session->userdata('profileid');
            $this->common->deleteData('user_cart',array('userid'=>$this->profileid));
            $this->common->deleteData('user_cart_product',array('userid'=>$this->profileid));
        }*/
		header('Content-type: application/json; charset=utf-8"');

		echo json_encode($data);die;

	}

	public function destroyCart()

	{
		if($this->cart->destroy()){
			$this->session->unset_userdata('user_coupon');
		}

		redirect(base_url("shopping-cart"));

	}

	public function removeCoupon()
	{
		$this->session->unset_userdata('user_coupon');
		$this->session->unset_userdata('user_coupon_code');
		$this->session->unset_userdata('user_coupon_type');
		redirect($_SERVER['HTTP_REFERER']);
	}

	public function updateCart()
	{
		if($this->kart->updateCartProductQty($_POST))
		{
			checkCouponAmmount();
			redirect(base_url("shopping-cart"));
		}else{
			redirect("sign-in");
		}
	}

	public function updateCartHeader()
	{
		if($this->kart->updateCartProductQty($_POST))
		{
			checkCouponAmmount();
			redirect($_SERVER['HTTP_REFERER']);
		}else{
			redirect("sign-in");
		}
	}

	public function addProductInWishlist()
	{
		//print_r($_POST); die;
		$data = $this->kart->addProductInWishlist($_POST);
		$carthtml = '';
		if($this->wishlist->contents()){
			foreach ($this->wishlist->contents() as $key => $value) {
				$carthtml .= '<li>
                                <h4 class="pro-name">'.$value['options']['product_name'].'</h4>
                                <p class="quantity">'.$value['qty'].'</p>
                            </li>';
			}
		}
		$data['cartlist'] = $carthtml;
		header('Content-type: application/json; charset=utf-8"');
		echo json_encode($data);die;
	}

	public function deleteProductFromWishlist()
	{
		//echo "string"; die;
		$data = $this->kart->deleteProductFromWishlist($_POST);
		header('Content-type: application/json; charset=utf-8"');
		echo json_encode($data);die;
	}

}