<?php
/**
 * @link         http://www.openzc.cn/
 * @author       hexipeng | e-mail:2017412656@qq.com
 * @copyright    Copyright (c) 2020, Openzc, Inc.
 * @license      http://www.openzc.cn/license.txt
 */
class cartModel{
	function Run(&$atts, &$refObj, &$fields){
		global $_vars,$product_class,$predata_class,$category_class,$productArray,$currencies;
	
		$attlist = "item=,newcart=,imgsizer=,bgcolor,separator=,type=,flag=";
		FillAtts($atts,$attlist);
		FillFields($atts,$fields,$refObj);
		extract($atts, EXTR_OVERWRITE);

		if($item){
			switch($item){
				case "attr":
					if($type=="son" && is_array($fields['attributes'])){

						$error=false;
							foreach($fields['attributes'] as $k => $v){
								$fields['attributes'][$k]['id']=$fields['id'];
								$fields['attributes'][$k]['options_values_price']=$currencies->display_price($v['options_values_price'],'');
								$fields['attributes'][$k]['options_id']=$k;
								$fields['attributes'][$k]['options_values_id']=$v['options_values_id'];
								if(!isset($v['products_options_name'],$v["products_options_values_name"]) && $error==false){
									$error=true;
								}
							}
							if($error==true){
								$options=$predata_class->getPredata(TABLE_PRODUCTS_OPTIONS);
								$options_values=$predata_class->getPredata(TABLE_PRODUCTS_OPTIONS_VALUES);
								$products_attributes=$predata_class->getPredata(TABLE_PRODUCGS_ATTRIBUTES);
								if(!$fields['attributes']){
									$fields['attributes']=$_SESSION['cart']->contents[$fields['id']]['attributes'];
								}
								$attributes=[];
								foreach($fields['attributes'] as $k => $v){
									$attributes[$k]["products_options_name"]=$options[$k]["products_options_name"];
									$attributes[$k]["products_options_values_name"]=$options_values[$v]["products_options_values_name"];
									$price=$products_attributes[$fields['id']]['options_values_price'];
									$attributes[$k]['options_values_price']=$currencies->display_price($price,'');
									
								}
								if(isset($_SESSION['cart']->contents[$fields['id']]['attributes_values'])){
										foreach($_SESSION['cart']->contents[$fields['id']]['attributes_values'] as $a => $b){
											if($options[$a]['products_options_type']==1){
												$attributes[$a]['products_options_values_name']=$b;
											}
										}
									}
								$fields['attributes']=$attributes;
							}
						return $fields['attributes'];
					}
				break;
				case "count":
					echo $_SESSION['cart']->count_contents();
				break;
				case "item":
					echo count($_SESSION['cart']->contents);
				break;
				case "total":
					echo $currencies->format($_SESSION['cart']->show_total());
				break;
				case "subtotal":
					echo $GLOBALS['ot_subtotal']->output[0]['text'];
				break;
				case "shipping_cost":
					echo $currencies->format($_SESSION['shipping']['cost']);
				break;
				
			}
		}
		else{
			
			$_SESSION['valid_to_checkout'] = true;
			$_SESSION['cart_errors'] = '';
			if(!$productArray){
				$productArray=$_SESSION['cart']->get_products(true);
			}
			if($flag=="order"){
				
				global $zv_orders_id;
				if($zv_orders_id){
					$orders_products_sql="select op.*,p.products_image as image,p.master_categories_id as category from ".TABLE_ORDERS_PRODUCTS." op left join ".TABLE_PRODUCTS." p on p.products_id=op.products_id where orders_id='".$zv_orders_id."'";
					$orders_products_data=openzcQuery($orders_products_sql);
					$orders_products_data=openzc_table_to_list($orders_products_data);
					$orders_products_attribues_sql="select * from ".TABLE_ORDERS_PRODUCTS_ATTRIBUTES." where orders_id='".$zv_orders_id."'";
					$rders_products_attribues_data=openzcQuery($orders_products_attribues_sql);
					$rders_products_attribues_data=openzc_table_to_list($rders_products_attribues_data);
					$productArray=[];
					foreach($orders_products_data as $k => $v){
						$v['id']=$v['products_id'];
						$v['quantity']=$v['products_quantity'];
						$v['final_price']=$currencies->display_price($v['final_price'],"");
						$attr_str=[];
						foreach($rders_products_attribues_data as $a => $b){
							if($v['orders_products_id']==$b['orders_products_id']){
								$v['attributes'][$b['products_options_id']]=$b['products_options_values_id'];
							}
						}
						$productArray[]=$v;
					}
					
				}
			}
			$pids=$this->get_products_ids($productArray);
			if($pids){
				$parameter=array("pid"=>$pids,"flag"=>true,"index_field"=>"products_id","imgsizer"=>$imgsizer,"bgcolor"=>$bgcolor,"separator"=>$separator);
				$products=$product_class->get_product_list($_GET,$parameter);
			}

			$categories=$predata_class->getPredata(TABLE_CATEGORIES);

			foreach($productArray as $k => $v){
				if($v['id']==$_SESSION['new_products_id_in_cart']){$new=$k;}
				$id=explode(":",$v['id'])[0];
				$productArray[$k]['sort_index']=$k+1;
				if($productArray[$k]['sort_index']%2==0){
					$productArray[$k]["sort_index_type"]="even";
				}else{
					$productArray[$k]["sort_index_type"]="odd";
				}
				if($k==0){
					$productArray[$k]['sort']="begin";
				}elseif($k==(count($productArray)-1)){
					$productArray[$k]['sort']="end";
				}else{
					$productArray[$k]['sort']="";
				}
				$cPath=$categories[$products[$id]['master_categories_id']]['path'];
				$productArray[$k]['categories_name']=$categories[$products[$id]['master_categories_id']]['categories_name'];
				$productArray[$k]['categories_link']=$categories[$products[$id]['master_categories_id']]['categories_link'];
				$productArray[$k]['products_image']=$products[$id]['products_image'];
				$productArray[$k]['products_quantity']=$products[$id]['products_quantity'];
				$productArray[$k]['products_name']=$products[$id]['products_name'];
				$productArray[$k]['products_link']=$products[$id]['products_link'];
				$productArray[$k]['products_edit_link']=zen_href_link(FILENAME_PRODUCT_INFO,'cPath='.$cPath.'&products_id='.$v['id']);
				$productArray[$k]['remove_link']=zen_href_link(FILENAME_SHOPPING_CART,'action=remove_product&product_id='.$v['id']);
				if(!$v['productsPriceEach']){
					$productArray[$k]['products_price']=$currencies->display_price($v['final_price'],'');
				}else{
					$productArray[$k]['products_price']=$v['productsPriceEach'];
				}
				$productArray[$k]['products_original_price']=$products[$id]["products_original_price"];
				$productArray[$k]['productPriceDiscount']=$products[$id]["productPriceDiscount"];
				$productArray[$k]['products_qty']=$_SESSION['cart']->contents[$v['id']]['qty'];
				$productArray[$k]['products_id']=$id;
				if(!isset($v['productsPrice'])){
					$pricenumber=$_SESSION['cart']->contents[$v['id']]['qty']*$v['final_price'];
					$productArray[$k]['products_price_total']=$currencies->display_price($pricenumber,"");
				}else{
					$productArray[$k]['products_price_total']=$v['productsPrice'];
				}
				if($products[$id]['product_is_free']==1){
					$productArray[$k]['products_price_total']=$currencies->display_price(0,"");
				}
			}
			if($newcart==true){
				$result[]=$productArray[$new];
				return $result;
			}
			
		    return $productArray;
		}
	}
	private function get_products_ids($productArray){
		foreach($productArray as $k => $v){
		   $v['id']=explode(":",$v['id']);
		   $id[]=$v['id'][0];
		}
		return join(",",$id);
	}
	
}


  
?>