<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Product
{
	function Product()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		//$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/login/actlogin");
	}
	
	function infolist()
	{
		$pageSize = 20 ;
		
		$keywordval 	= $GLOBALS['Reque'] -> reqval["keyword"];
		$prs_idval 		= $GLOBALS['Reque'] -> funpost("prs_id");
		$pageNumval 	= $GLOBALS['Reque'] -> getval["pageNum"];
		
		if( $pageNumval > 1 )
		{
			$pageNum  = $pageNumval;
			$LIMITnum = ceil( ( $pageNumval -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}

		
		if($prs_idval !='' and $prs_idval !='A')
		{
			$prs_idval 	= $prs_idval;
		}else{
			$prs_idval = $GLOBALS['Reque'] -> getval["prs_id"];
		}
		
		if($prs_idval!='' and $prs_idval !='A' )
		{ 
			 
			 $endArray["prs_id"] = $prs_idval;
			 
			 $endurl .= '&prs='.$prs_idval;
			 
			 $sidsql  = " SELECT prs_id FROM ".SQL_PREFIX."prosort WHERE prs_parentid = ".$prs_idval." or  prs_id = ".$prs_idval;
			 
		     $sortInfo 	  = $GLOBALS['MySql'] -> selectInfo( $sidsql );
			 
			 //var_dump( $sortInfo  );
			 $sinid  = '';
			if($sortInfo!='')
			{
				foreach( $sortInfo as $skey => $sval )
				{
					if($skey==0)
					{
						$sinid = $sval["prs_id"];
					}else{
						$sinid .= ','.$sval["prs_id"];
					}
				}
				$whereadd .= " AND ( p.prs_id IN ( ".$sinid." ) )";
			}
		 }else{ 
		 	$endurl 	   .= '&prs=A'; 	
		 }
		
		
		if($GLOBALS['Reque'] -> reqval["keyword"] !='')
		{   
			$keywordval 	= $GLOBALS['Reque'] -> reqval["keyword"];
			$keywordval  	= urldecode($keywordval); 
		}
		if($keywordval!='')
		{ 	
			$endArray["keyword"] =$keywordval;
			$endurl 	   .= '&keyword='.urlencode($keywordval);
			$whereadd .= " AND ( p.pro_name LIKE  '%".$keywordval."%' )";  
		}

		$listval  = $GLOBALS['Reque'] -> getval["listval"];
		$listtype = $GLOBALS['Reque'] -> getval["listtype"];

		$orderbyval = "";
		if($listval!='' and $listtype =='DESC' )
		{
			$endArray["listtype"] = 'DESC';
			$endArray["listval"]  = $listval;
			$orderbyval = $listval ."  DESC ";
			$listhtml = '';	//反向排序参数
			
		}elseif($listval!='' )
		{
			$orderbyval = $listval;
			$listhtml = 'DESC';
			
			$endArray["listtype"] = '';
			$endArray["listval"]  = $listval;

		}else{
			$endArray["listtype"] = 'DESC';
			$endArray["listval"]  = 'pro_uptime';
			$orderbyval = " p.pro_uptime DESC , `pro_id` DESC ";	
		}
		
		
		//序号 	商品标题 	商品分类 	数量 	销售价 	是否显示 	更新时间
		$sqlQuery = " SELECT p.pro_id, p.pro_name,p.prs_id,p.pro_num, p.pro_unit, p.pro_sellprice, p.pro_enabled , p.pro_uptime   FROM  ".SQL_PREFIX."product as p    WHERE 1 $whereadd  GROUP BY p.pro_id ORDER BY $orderbyval LIMIT  $LIMITnum , $pageSize ";
		
		$info 		= $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$sqlQuery 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."product as p  WHERE 1 $whereadd  ORDER BY p.`pro_id`  ";
		
		$infocount 	= $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		//var_dump($info);
		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=product&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] , $endArray );
		
		$GLOBALS['Templ']->assign('listhtml',$listhtml);
		$GLOBALS['Templ']->assign('listval',$listval);
		$GLOBALS['Templ']->assign('listtype',$listtype);

		
		
		$GLOBALS['Templ']->assign('prs_idval',$prs_idval);
		$GLOBALS['Templ']->assign('keywordval',$keywordval);
		
		$this -> getSort();
		
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		
		
		//部分不缓存   
		//function smarty_block_uncache($param, $content, $smarty){   
		//return $content;   
		//}   
		//$smarty->register_block('uncache', 'smarty_block_uncache',false);
		
		$GLOBALS['Templ']->display('product_tpl.html');
		
	}
	
	function getSort()
	{
		$sqlQuery = " SELECT prs_id, prs_name,prs_parentid  FROM  ".SQL_PREFIX."prosort  WHERE 1 and prs_enabled = 1 GROUP BY prs_id ORDER BY `prs_list` limit 1000 ";
		
		$infosm = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$psInfo = array();
		foreach ($infosm as $rowkey => $rowval)
		{
				if($rowval["prs_parentid"] == 0)
				{
					$psInfo[$rowval["prs_id"]]['prs_id'] = $rowval["prs_id"] ;
					$psInfo[$rowval["prs_id"]]['prs_name'] = $rowval["prs_name"] ;
					$psInfo[$rowval["prs_id"]]['prs_parentid'] = $rowval["prs_parentid"] ;
				}else{
					$psInfo[$rowval["prs_parentid"]]['array'][] = $rowval;	
				}
				$psInfoval[$rowval["prs_id"]] = $rowval["prs_name"] ;
		}
		
		$GLOBALS['Templ']->assign('psInfoval',$psInfoval);		//列排表显示使用
		
		$GLOBALS['Templ']->assign('psInfo',$psInfo);		
		//return $psInfo;
	}
	
	
	function add()
	{
		///$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24
		/*
		$sqlQuery = " SELECT prs_id, prs_name,prs_parentid  FROM  ".SQL_PREFIX."prosort  WHERE 1 and prs_enabled = 1 GROUP BY prs_id ORDER BY `prs_list` limit 1000 ";
		
		$infosm = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$asInfo = array();
		   foreach ($infosm as $rowkey => $rowval)
		   {
		   		if($rowval["prs_parentid"] == 0)
				{
					$asInfo[$rowval["prs_id"]]['prs_id'] = $rowval["prs_id"] ;
					$asInfo[$rowval["prs_id"]]['prs_name'] = $rowval["prs_name"] ;
					$asInfo[$rowval["prs_id"]]['prs_parentid'] = $rowval["prs_parentid"] ;
				}else{
					$asInfo[$rowval["prs_parentid"]]['array'][] = $rowval;	
				}
		   }
		   */
		$this -> getSort();
		
		//$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		
		$GLOBALS['Helpe'] -> load_ckeditor( $textName = 'pro_content' );	//加载文本编辑器

		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=product&act=addinfo");
		$GLOBALS['Templ']->display('productadd_tpl.html');
	}
	
	function addinfo()
	{
		
		$pro_nameval 		= $GLOBALS['Reque'] -> funpost("pro_name");
		$pro_pinyinval 		= $GLOBALS['Reque'] -> funpost("pro_pinyin");
		$prs_idval 			= $GLOBALS['Reque'] -> funpost("prs_id");
		$pro_barcodeval 	= $GLOBALS['Reque'] -> funpost("pro_barcode");
		$pro_specval 		= $GLOBALS['Reque'] -> funpost("pro_spec");	
		$pro_numval 		= $GLOBALS['Reque'] -> funpost("pro_num");
		
		$pro_unitval 		= $GLOBALS['Reque'] -> funpost("pro_unit");
		$pro_productionval 	= $GLOBALS['Reque'] -> funpost("pro_production");
		$pro_sellpriceval 	= $GLOBALS['Reque'] -> funpost("pro_sellprice");
		$pro_marketpriceval = $GLOBALS['Reque'] -> funpost("pro_marketprice");
		$pro_lowestpriceval = $GLOBALS['Reque'] -> funpost("pro_lowestprice");
		$pro_buypriceval 	= $GLOBALS['Reque'] -> funpost("pro_buyprice");
		$pro_contentval 	= $_POST["pro_content"];
		///$pro_contentval 	= $GLOBALS['Reque'] -> funpost("pro_content");	
		$pro_enabledval 	= $GLOBALS['Reque'] -> funpost("pro_enabled");	


		$pro_imgarrayval 		= $GLOBALS['Reque'] -> funpost("pro_img");
		
		//var_dump($_REQUEST);
		//echo "<br />";
		//var_dump($pro_imgarrayval);
		
		$pro_imgval = $pro_imgarrayval[1];
		
		
		//exit();
		
		if( $pro_nameval !='' and $prs_idval !='' and $pro_sellpriceval !='' and $pro_contentval != '' )
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."product  WHERE pro_name = '".$pro_nameval."' ORDER BY `pro_id` ";
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{
				
				$pro_uptimeval = $pro_addtimeval 	= time();
				$use_idval			= $_SESSION["emplyeeUser"]["use_id"];
				
				$insertpro =" INSERT INTO `".SQL_PREFIX."product` (`pro_name` , `pro_pinyin` , `prs_id` , `pro_barcode` , `pro_spec` , `pro_num` ,`pro_img`, `pro_unit` , `pro_production` , `pro_sellprice`  , `pro_marketprice` , `pro_lowestprice` , `pro_buyprice` , `pro_content` ,`pro_addtime`,`pro_uptime` ,`use_id` ) VALUES (  '$pro_nameval', '$pro_pinyinval', '$prs_idval', '$pro_barcodeval', '$pro_specval', '$pro_numval','$pro_imgval', '$pro_unitval', '$pro_productionval', '$pro_sellpriceval', '$pro_marketpriceval', '$pro_lowestpriceval', '$pro_buypriceval', '$pro_contentval', '$pro_addtimeval','$pro_uptimeval','$use_idval')";

				$proinsertId = $GLOBALS['MySql'] -> insertSql($insertpro);
				
				
				//系统得自动生动编码
				if( $pro_barcodeval == '')
				{
					$this -> buildCode( $proinsertId );
				}
				foreach($pro_imgarrayval as $pikey => $pival )
				{
					if($pival!='' and $pikey!=1 )
					{
						$pri_urlval = $pival;
						$insertimg =" INSERT INTO `".SQL_PREFIX."proimg` (`pro_id` , `pri_url` , `pri_desc`  ) VALUES (  '$proinsertId', '$pri_urlval', '$pri_descval')";

						$imgId = $GLOBALS['MySql'] -> insertSql($insertimg);
					}
				
				}
				
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=product&act=infolist");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
			}else{
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_isrealyerror']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=product&act=add");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
			}
			
		}else{
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_losssusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=product&act=add");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		}
			//var_dump($_REQUEST);
	}
	
	function del()
	{
		
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$deletesql ="DELETE FROM `".SQL_PREFIX."product` WHERE `".SQL_PREFIX."product`.`pro_id` in ( $infoid ) ";
		$GLOBALS['MySql'] -> querySql($deletesql);
		
		//删除图片表
		$insertimg =" DELETE FROM `".SQL_PREFIX."proimg` WHERE `pro_id` = $infoid ";
		$imgId = $GLOBALS['MySql'] -> querySql($insertimg);
		
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=product&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		
	}
	



	
	
	
	function info()
	{
		//echo md5('ljj123456');
		//$infoid = $infoArray[0];
		$idval = $GLOBALS['Reque'] -> getval["id"];
		
		if( $idval!='' )
		{
			$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."product  WHERE pro_id = '".$idval."' ORDER BY `pro_id` ";
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=product&act=editinfo");
			
			$info["pro_imgval"][1] = $info["pro_img"]; 
			$sqlQuery = " SELECT pri_id , pri_url  FROM  ".SQL_PREFIX."proimg  WHERE pro_id = $idval  GROUP BY pri_id ORDER BY `pri_id` limit 10 ";
			
			$imgInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);

			foreach ($imgInfo as $imgkey => $imgval)
			{
				if($imgval["pri_url"] != '')
				{
					$info["pro_imgval"][$imgkey+2] =  $imgval["pri_url"];
					$info["pri_imgid"][$imgkey+2]  =  $imgval["pri_id"];
				}
			}
			
			//var_dump($info);
			
			$GLOBALS['Templ'] -> assign('info',$info);
		}
		
		$this -> getSort();
		
		$GLOBALS['Helpe'] -> load_ckeditor( $textName = 'pro_content' ,$textValue = $info["pro_content"] );	//加载文本编辑器
		
		
		$GLOBALS['Templ'] -> display('productedit_tpl.html');
	
	}
	
	
	function editinfo()
	{	
	
		$pro_idval 			= $GLOBALS['Reque'] -> funpost("pro_id");	
		$pro_nameval 		= $GLOBALS['Reque'] -> funpost("pro_name");
		$pro_pinyinval 		= $GLOBALS['Reque'] -> funpost("pro_pinyin");
		$prs_idval 			= $GLOBALS['Reque'] -> funpost("prs_id");
		$pro_barcodeval 	= $GLOBALS['Reque'] -> funpost("pro_barcode");
		$pro_specval 		= $GLOBALS['Reque'] -> funpost("pro_spec");	
		$pro_numval 		= $GLOBALS['Reque'] -> funpost("pro_num");
		
		//$pro_imgval 		= $GLOBALS['Reque'] -> funpost("pro_img");
		$pro_unitval 		= $GLOBALS['Reque'] -> funpost("pro_unit");
		$pro_productionval 	= $GLOBALS['Reque'] -> funpost("pro_production");
		$pro_sellpriceval 	= $GLOBALS['Reque'] -> funpost("pro_sellprice");
		$pro_marketpriceval = $GLOBALS['Reque'] -> funpost("pro_marketprice");
		$pro_lowestpriceval = $GLOBALS['Reque'] -> funpost("pro_lowestprice");
		$pro_buypriceval 	= $GLOBALS['Reque'] -> funpost("pro_buyprice");
		$pro_contentval 	= $_POST["pro_content"];
		//$pro_contentval 	= $GLOBALS['Reque'] -> funpost("pro_content");	
		$pro_enabledval 	= $GLOBALS['Reque'] -> funpost("pro_enabled");	

		$pro_imgarrayval 		= $GLOBALS['Reque'] -> funpost("pro_img");
		
		//var_dump($_REQUEST);
		//echo "<br />";
		//var_dump($pro_imgarrayval);
		
		$pro_imgval = $pro_imgarrayval[1];
		
		
		if($pro_idval !='' and  $pro_nameval !='' and $prs_idval !='' and $pro_sellpriceval !='' and $pro_contentval != '' )
		{
			$pro_uptimeval =  time();
			$updatesql = " UPDATE `".SQL_PREFIX."product` SET  pro_name = '".$pro_nameval."' , pro_pinyin = '".$pro_pinyinval."' , prs_id = '".$prs_idval."', pro_barcode = '".$pro_barcodeval."', pro_spec = '".$pro_specval."', pro_num = '".$pro_numval."', pro_img = '".$pro_imgval."', pro_unit = '".$pro_unitval."', pro_production = '".$pro_productionval."', pro_sellprice = '".$pro_sellpriceval."', pro_marketprice = '".$pro_marketpriceval."', pro_lowestprice = '".$pro_lowestpriceval."', pro_buyprice = '".$pro_buypriceval."' , pro_content = '".$pro_contentval."' , pro_uptime = '".$pro_uptimeval."' WHERE  pro_id = '".$pro_idval."' ";
			
			$GLOBALS['MySql'] -> querySql( $updatesql );
			
			$insertimg =" DELETE FROM `".SQL_PREFIX."proimg` WHERE `pro_id` = $pro_idval ";
			$imgId = $GLOBALS['MySql'] -> querySql($insertimg);
			
			 
			
			foreach($pro_imgarrayval as $pikey => $pival )
			{
				if($pival!='' and $pikey!=1 )
				{
					$pri_urlval = $pival;
					
					$insertimg =" INSERT INTO `".SQL_PREFIX."proimg` (`pro_id` , `pri_url` , `pri_desc`  ) VALUES (  '$pro_idval', '$pri_urlval', '$pri_descval')";
					
					$imgId = $GLOBALS['MySql'] -> insertSql($insertimg);
					}
				
			}
			
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_editsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=product&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
				
		}else{
		
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_losssusse'] );
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=product&act=info&id=".$pro_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}
	
	function show()
	{
		//$indel = implode(',', $delArray);

		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$upshow = " UPDATE `".SQL_PREFIX."product` SET pro_enabled = 1  WHERE  `pro_id` in ( $infoid ) ";

		$GLOBALS['MySql'] -> querySql($upshow);
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_showsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=product&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function hide()
	{
		//$indel = implode(',', $delArray);
		
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		/*
		if($idval)
		{
			$infoid = implode(',', $idval);
		}*/

		$uphide = " UPDATE `".SQL_PREFIX."product` SET pro_enabled = 0  WHERE `pro_id` in ( $infoid ) ";

		$GLOBALS['MySql'] -> querySql($uphide);
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_hidesusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=product&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	
	
	function buildCode( $proid )
	{
		if($proid)
		{
			$znum = 10 - strlen($proid);
			$str = '';
			
			for( $zi=0;  $zi < $znum; $zi++)
			{
				$str ='0'.$str;
			}
			
			$str = 'NU'.$str.$proid;
			
			$updatesql = "UPDATE `".SQL_PREFIX."product` SET pro_barcode = '".$str."' WHERE  pro_id = '".$proid."' ";	
			
			$GLOBALS['MySql'] -> querySql( $updatesql );
			
			return $str;
			
		}else{
			return false;
		}
		
	}
}

//nmon_x86_rhel4

$Product = new Product();

?>