<?php
error_reporting(E_ALL);
include "includes/config.php";
include "includes/session.php";
include "includes/functions.php";
include_once "classes/cls_user.php";
include_once "classes/cls_utils.php";
include_once "classes/cls_cart.php";
include_once "classes/cls_store.php";
include_once "classes/cls_products.php";
$_SESSION["SUBSCRIPTION_SHIPPING_DETAILS"] = array();

//$_SESSION["SHIPPING_DETAILS"][$cartObj[0]->artist_id] = array("preffered_shipping" => $shipValArr[0], "rate" => $shipValArr[1]);
$currency_attributes                =       displayCurrencyType();
$userid                             =       $_REQUEST['userId'];
$productid                          =       $_REQUEST['productId'];
$productoptionid                    =       $_REQUEST['product_option_Id'];
$action                             =       $_REQUEST['action'];
$wishId                             =       $_REQUEST['wishId'];
$sellerId                           =       $_REQUEST['sellerId'];
$subscription_shipping_price        =       $_REQUEST['subsciption_ship_price'];
$subscription_ship_name             =       $_REQUEST['subscription_ship_name'];

if($userid=='')
    {
        $userid         =       $_SESSION["sess_userid"];
    }
if($_SESSION["sess_userid"]!='')
 {
     $visitorId         =  '';
 }else{
     $visitorId         =   generalMethods::sessionid();
 }

switch($action)
{
    case "favourites" : 
    {
        // check favourites count 
        
        
        if($userid=='')
        {
           $data = array('flag'=>'notlogin');
                    echo json_encode($data);
                    exit; 
        }else{
            
         // Check the user have same account for seller 
         $sellerAccObj                   =    User::is_seller_account_activated($userid);
         $seller_activated_status        =   'N';
    //echopre($sellerAccObj);
        if($sellerAccObj->status==SUCCESS)
            {
                    $sellerAccObjVal            =   $sellerAccObj->data;
                    $seller_activated_status    =   $sellerAccObjVal->is_seller;
                    $activated_seller_id        =   $sellerAccObjVal->seller_id;
                    
                   $sellerObj  =    Store::get_seller_account_info($activated_seller_id);
                   //echopre($sellerObj);

            if($sellerObj->status==SUCCESS)
                    {
                        $sellerObjVal                       =   $sellerObj->data;
                        $vapproved                          =   $sellerObjVal->vapproved;
                        $product_seller_id                  =   $sellerObjVal->artist_id;
                        
                         if($product_seller_id==$sellerId)
                        {
                            $data = array('flag'=>'nopermission');
                            echo json_encode($data);
                            exit; 
                        }
                      
                    }
                    
                    
                   
            }         
        }
         $favourites_count   = User::user_favourites_count($userid,$productid,$productoptionid);
         
         if($favourites_count>0){
             $data = array('flag'=>'exists');
                    echo json_encode($data);
                    exit;
         }else{
             $objProductVo = new stdClass();
             $objProductVo->nProductId          = $productid;
             $objProductVo->nProductOptionId    = $productoptionid;
             $objProductVo->nUserId             = $userid;
             $objProductVo->seller_id           = Products::getProductSellerId($productid);
             $objProductVo->dDate               = date('Y-m-d : H:i:s');
             $resultData                        = User::add_to_favourites_list($objProductVo);
             if($resultData->status==SUCCESS)
             {
                 
                 $data = array('flag'=>SUCCESS);
                    echo json_encode($data);
                    exit;
             }
             
         }
         break;
    }
    
    // check stock exist to move the item to cart 
    case "checkstock" : 
    {
        // check favourites count 
        
        
        if($userid=='')
        {
           $data = array('flag'=>'notlogin');
                    echo json_encode($data);
                    exit; 
        }
         $stock_count   = Utils::check_product_stock_exists($productid,$productoptionid);
         
         if($stock_count>0){
             $data = array('flag'=>SUCCESS);
                    echo json_encode($data);
                    exit;
         }else{
           
              $data = array('flag'=>ERROR);
                    echo json_encode($data);
                    exit;
         }
         break;
    }
    
    // Delete favourite items
    
    case "deletefavouritelist" : 
    {
        // check favourites count 
        
        if($userid=='')
        {
           $data = array('flag'=>'notlogin');
                    echo json_encode($data);
                    exit; 
        }
         $resultData   = User::delete_user_favourite_list($wishId,$userid);
         
         if($resultData->status==SUCCESS)
             {
                 
                 $data = array('flag'=>SUCCESS);
                    echo json_encode($data);
                    exit;
             }else{
           
              $data = array('flag'=>ERROR);
                    echo json_encode($data);
                    exit;
         }
         break;
    }

    // Add sellers to favourites list
    case "sellerfavourites" :
    {
        // check favourites count
        if($userid=='')
        {
           $data = array('flag'=>'notlogin');
                    echo json_encode($data);
                    exit;
        }
         $seller_favourites_count   = User::seller_favourites_count($userid,$productid,$productoptionid);

         if($seller_favourites_count>0){
             $data = array('flag'=>'exists');
                    echo json_encode($data);
                    exit;
         }else{
             $objProductVo = new stdClass();
             $objProductVo->nUserId                 =   $userid;
             $objProductVo->nSellerId               =   $sellerId;
             $objProductVo->dDate                   =   date('Y-m-d : H:i:s');
             $resultData                            =   User::add_seller_to_favourites_list($objProductVo);

             if($resultData->status==SUCCESS)
             {

                 $data = array('flag'=>SUCCESS);
                    echo json_encode($data);
                    exit;
             }

         }
         break;
    }


    //  // Delete favourite sellers

    case "deletefavouritesellers" :
    {
        // check favourites count

        if($userid=='')
        {
           $data = array('flag'=>'notlogin');
                    echo json_encode($data);
                    exit;
        }
         $resultData   = User::delete_favourite_seller_list($wishId,$userid);

         if($resultData->status==SUCCESS)
             {

                 $data = array('flag'=>SUCCESS);
                    echo json_encode($data);
                    exit;
             }else{

              $data = array('flag'=>ERROR);
                    echo json_encode($data);
                    exit;
         }
         break;
    }


    // Check Product Already Subscribed to Cart

     case "subscription" :
    {
        // check favourites count
         
          if($userid=='')
        {
           $data = array('flag'=>'notlogin');
                    echo json_encode($data);
                    exit;
        }

         $cartObj       = Cart::checkProductAddedtoCart($userid,$visitorId,$productid,$productoptionid);

         if($cartObj->status==SUCCESS)
          {
                $cartObjVal     =  $cartObj->data;
                $cartId         =  $cartObjVal->cart_id;
                if($cartId>0)
                    {
                            $data = array('flag'=>'exists');
                            echo json_encode($data);
                            exit;
                    }
                    else{
                            $data = array('flag'=>SUCCESS);
                            echo json_encode($data);
                            exit;
                       }
          }else{

                            // Item not found in cart will insert data to cart
                            $data = array('flag'=>SUCCESS);
                            echo json_encode($data);
                            exit;
              }
        
         break;
    }

    // Vacation mode settings of seller

     case "vacationmodesetting" :
    {
        // check favourites count
        if($sellerId!=''){
          $sellerObj  =    Store::get_seller_account_info($sellerId);
          $seller_vacation_mode     ='N';

            if($sellerObj->status==SUCCESS)
                    {
                        $sellerObjVal                       =   $sellerObj->data;
                        $seller_vacation_mode		    =	$sellerObjVal->is_vacation_mode;


                    }

           $data = array('flag'=>$seller_vacation_mode);
                    echo json_encode($data);
                    exit;
        }

        break;
    }
    
    // make drop down for products and categories in copon code pages according to seller

    case "couponcode" :
    {
        $type       =       $_REQUEST['type'];
        if($type=='products'){
          $productList  =    Products::getProduct_list_Seller($sellerId);
          $properties = 'multiple = "multiple"';
          echo makeDropDownList("product_id[]",$productList, $_POST['product_id'], false, "textarea multiselect", $properties, $behaviors,'product_id');         
        }else if($type =='category'){
          $categoryList  =    Products::getCategory_list_Seller($sellerId);  
          $properties = 'multiple = "multiple"';
          echo makeDropDownList("category_id[]",$categoryList, $_POST['category_id'], false, "textarea multiselect", $properties, $behaviors,'category_id');
        }

        break;
    }
       

         // End settings

        default :
        {
            break;
        }
        
}
exit;
?>
