<?php

namespace App\Controllers;
use App\Models\User_model;
use App\Models\Dashboard_model;
use App\Models\Common_model;
use App\Models\Member_credits_model;
use App\Models\Packages_and_pricing_model;
use App\Models\Credits_usage_tracking_model;
use App\Models\Email_template_model;
use App\Models\Variable_model;
use App\Models\Rule_model;
use App\Models\Cart_token_model;
use App\Models\Cart_model;
class Cart_controller extends BaseController
{
	public $session;
	public function __construct()
	{
		$this->session = session();
		$this->UserModel = new User_model();
		$this->dModel = new Dashboard_model();
		$this->cModel = new Common_model();
		$this->creditsModel = new Member_credits_model();
		$this->PackagesModel = new Packages_and_pricing_model();
		$this->CreditsUsageTrackingModel = new Credits_usage_tracking_model();
		$this->MailModel = new Email_template_model();
		$this->VariableModel = new Variable_model();
		$this->RuleModel = new Rule_model();
		$this->Cart_token_model = new Cart_token_model();
        $this->cartModel = new Cart_model();
		helper(['form', 'url']);
	}
    public function index()
    {
        $pack_data['pack_data'] = $this->credits_pack();
        if (!session()->has('logged_user')) 
        $pack_data['token'] = '';
        else
        $pack_data['token'] = $this->session->get('logged_user');
        echo view('membership.php' , $pack_data);
    }

	public function credits_pack()
	{
		$data =  $this->PackagesModel->where('package_type', 'Presales' )->findAll();
		return $data;
	}

    // public function add_package_cartdata()
	// {        
    //    if (!session()->has('logged_user')) 
    //    {
    //         $token = rand(10 , 99 ).rand(10 , 99 ).rand(10 , 99 );
    //         if (session()->has('token')) 
    //         {
    //         $tokendata =  $this->Cart_token_model->where('md5(token)',  $this->session->get('token') )->find();
    //         $data['token'] = $tokendata[0]['token'];
    
    //             foreach ($tokendata as $key => $value) 
    //             {
    //                 if ($value['primary_id'] == $this->request->getVar('id')) 
    //                 {
    //                     $count = $tokendata[$key]['count'] + 1;
    //                     $this->Cart_token_model->where('id', $tokendata[$key]['id'] )->set(array( 'count' => $count ))->update();            
    //                 } 
    //                 else 
    //                 {
    //                     if (count($tokendata) == 1) 
    //                     {
    //                         $data['primary_id'] = $this->request->getVar('id');
    //                         $data['table_name'] = 'packages_and_pricing';
    //                         $data['count'] = 1;
    //                         $this->Cart_token_model->save($data);                    
    //                     }
    //                 }
    //             }
    //         }
    //         else 
    //         {
    //         $this->session->set('user_token',$token);
    //         $this->session->set('token',md5($token));
    //         $data['token'] = $token;
    //         $data['primary_id'] = $this->request->getVar('id');
    //         $data['table_name'] = 'package_and_pricing';
    //         $data['count'] = 1;
    //         $this->Cart_token_model->save($data);
    
    //         }
    
    
    //         if (session()->has('token')) {
    //         $tokendata =  $this->Cart_token_model->where('md5(token)', $this->session->get('token') )->find();
    //         $data['token'] = md5($tokendata[0]['token']);
    //         }
    //         else {
    //         $data['token'] = md5($token);
    //         }
    //         echo json_encode($data);
    //    } 
    //    else 
    //    {
    //     $data =  $this->cartModel->where('md5(member_id)', $this->request->getVar('c_token') )->where('pack_id', $this->request->getVar('id') )->find();
    //     if ($data) 
    //     {
    //         $this->cartModel->where('md5(member_id)', $this->request->getVar('c_token') )->where('pack_id', $this->request->getVar('id') )->set(array( 'count' => $data[0]['count'] + 1 ))->update(); 

    //         $val['token'] = md5($this->session->get('logged_user')); 
    //         echo json_encode($val);
    //     } 
    //     else 
    //     {
    //         $data['member_id'] = $this->session->get('logged_user');
    //         $data['pack_id'] = $this->request->getVar('id');
    //         $data['count'] = 1;
    //         $this->cartModel->save($data);
    //         $val['token'] = md5($this->session->get('logged_user')); 
    //         echo json_encode($val);
    //     }
    //    }
       
	// }





public function add_package_cartdata()
	{        
       if (!session()->has('logged_user')) 
       {
            $token = rand(10 , 99 ).rand(10 , 99 ).rand(10 , 99 );
            if (session()->has('token')) 
            {
            $tokendata =  $this->Cart_token_model->where('md5(token)',  $this->session->get('token') )->find();
              if($tokendata)
              {
                $data['token'] = $tokendata[0]['token'];
    
                foreach ($tokendata as $key => $value) 
                {
                    if ($value['primary_id'] == $this->request->getVar('id')) 
                    {
                        $count = $tokendata[$key]['count'] + 1;
                        $this->Cart_token_model->where('id', $tokendata[$key]['id'] )->set(array( 'count' => $count ))->update();            
                    } 
                    else 
                    {
                        if (count($tokendata) == 1) 
                        {
                            $data['primary_id'] = $this->request->getVar('id');
                            $data['table_name'] = 'packages_and_pricing';
                            $data['count'] = 1;
                            $this->Cart_token_model->save($data);                    
                        }
                    }
                }
              }
              else
              {
                $this->session->set('user_token',$token);
                $this->session->set('token',md5($token));
                $data['token'] = $token;
                $data['primary_id'] = $this->request->getVar('id');
                $data['table_name'] = 'package_and_pricing';
                $data['count'] = 1;
                $this->Cart_token_model->save($data);
              }
            }
            else 
            {
            $this->session->set('user_token',$token);
            $this->session->set('token',md5($token));
            $data['token'] = $token;
            $data['primary_id'] = $this->request->getVar('id');
            $data['table_name'] = 'package_and_pricing';
            $data['count'] = 1;
            $this->Cart_token_model->save($data);
    
            }
    
    
            if (session()->has('token')) {
            $tokendata =  $this->Cart_token_model->where('md5(token)', $this->session->get('token') )->find();
            $data['token'] = md5($tokendata[0]['token']);
            }
            else {
            $data['token'] = md5($token);
            }
            echo json_encode($data);
       } 
       else 
       {
        $data =  $this->cartModel->where('md5(member_id)', $this->request->getVar('c_token') )->where('pack_id', $this->request->getVar('id') )->find();
        if ($data) 
        {
            $this->cartModel->where('md5(member_id)', $this->request->getVar('c_token') )->where('pack_id', $this->request->getVar('id') )->set(array( 'count' => $data[0]['count'] + 1 ))->update(); 

            $val['token'] = md5($this->session->get('logged_user')); 
            echo json_encode($val);
        } 
        else 
        {
            $data['member_id'] = $this->session->get('logged_user');
            $data['pack_id'] = $this->request->getVar('id');
            $data['count'] = 1;
            $this->cartModel->save($data);
            $val['token'] = md5($this->session->get('logged_user')); 
            echo json_encode($val);
        }
       }
       
	}

    public function view_cart($token = null)
	{
        if (!session()->has('logged_user')) 
        {
            if (session()->has('token')) 
            {
                  $token = session()->get('token');   
            }
            $data =  $this->Cart_token_model->where('md5(token)', $token )->findAll();
            if ($data) 
            {
                $array = [];
                foreach ($data as $key => $value) {
                    $arr = $this->PackagesModel->where('id', $value['primary_id'] )->find();
                    array_push($array , $arr[0]);
                }
                $val['pack_id'] = $data;
                $val['gst'] = $this->RuleModel->where('rule_key', 3 )->find();
                $val['pst'] = $this->RuleModel->where('rule_key', 4 )->find();
                $val['tokendata'] = $array;
                $val['token'] = $data[0]['token']; 
                echo view('cart.php',$val);
            }
            else
            {
                $val['token'] = ''; 
                $val['msg'] = 'Your cart is empty';
                echo view('cart.php',$val);
                // $this->index();
            }
        }
        else
        {
            
            $token = md5($this->session->get('logged_user'));
            $data =  $this->cartModel->where('md5(member_id)', $token )->findAll();
            if ($data) 
            {
                $array = [];
                foreach ($data as $key => $value) {
                    $arr = $this->PackagesModel->where('id', $value['pack_id'] )->find();
                    array_push($array , $arr[0]);
                }
                $val['pack_id'] = $data;
                $val['gst'] = $this->RuleModel->where('rule_key', 3 )->find();
                $val['pst'] = $this->RuleModel->where('rule_key', 4 )->find();
                $val['tokendata'] = $array;
                $val['token'] = $this->session->get('logged_user');
                echo view('cart.php',$val);
            }
            else
            {
                $val['token'] = $this->session->get('logged_user');
                $val['msg'] = 'Your cart is empty';
                echo view('cart.php',$val);
                // $this->index();
            }
        }
	}

    public function continue_shopping($token)
	{
        if (!session()->has('logged_user')) 
        {
            $tokendata =  $this->Cart_token_model->where('md5(token)', $token )->find();
            if($tokendata)
            {
            $data['token'] = $tokendata[0]['token']; 
            $data['pack_data'] = $this->credits_pack();
            echo view('membership.php',$data);
            }
            else
            {
                $this->index(); 
            }
        }
        else
        {
            $tokendata = $this->cartModel->where('md5(member_id)', $token )->findAll();
            if ($tokendata) 
            {
                $data['token'] = $tokendata[0]['member_id']; 
                $data['pack_data'] = $this->credits_pack();
                echo view('membership.php',$data);
            } 
            else 
            {
                $this->index();
            }
        }
	}

    public function remove_product($id)
	{
        if (!session()->has('logged_user')) 
        {
            $tokendata =  $this->Cart_token_model->where('md5(id)', $id )->findAll();
            $deletedata =  $this->Cart_token_model->where('md5(id)', $id )->delete();
            return redirect()->to(base_url()."view_cart/".md5($tokendata[0]['token']));
        }
        else
        {
            $deletedata =  $this->cartModel->where('md5(id)', $id )->delete();
            return redirect()->to(base_url()."view_cart/".md5($this->session->get('logged_user')));
        }
	}

    public function change_count_onclick()
	{
        if (!session()->has('logged_user')) 
        {
          $this->Cart_token_model->where('id', $this->request->getVar('id') )->set(array( 'count' => $this->request->getVar('count') ))->update();  
          echo true;  
        }
        else
        {
          $this->cartModel->where('id', $this->request->getVar('id') )->set(array( 'count' => $this->request->getVar('count') ))->update();  
          echo true;  
        }   
              

	}
	


	// -----------------------------------------------
}
