<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Address
{
	function Address()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
	}
	
	function infolist(  )
	{
		
		$pageSize = 20 ;		
		$keywordval 	= $GLOBALS['Reque'] -> funpost("keyword");
		$pageNumval 	= $GLOBALS['Reque'] -> reqval["pageNum"];
		$searchtypeval 	= $GLOBALS['Reque'] -> reqval["searchtype"];
		
		//var_dump( $searchtypeval );
		
		if( $pageNumval > 1 )
		{
			$pageNum  = $pageNumval;
			$LIMITnum = ceil( ( $pageNumval -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}

		
		if($GLOBALS['Reque'] -> getval["keyword"] !='')
		{   
			$keywordval 	= $GLOBALS['Reque'] -> getval["keyword"];
			$keywordval  	= urldecode($keywordval); 
		}
		
		if($keywordval!='')
		{ 	
			$endArray["keyword"] =$keywordval;
			$endurl 	   .= '&keyword='.urlencode($keywordval);
			
			//$whereadd .= " AND (  add_name LIKE  '%".$keywordval."%' )";
			
			if( $searchtypeval =='' )
			{
				$whereadd .= " AND (  add_name LIKE  '%".$keywordval."%' )"; 
				$searchtypeval = 'add_name';
				$endArray["searchtype"] = 'add_name';
			}if($searchtypeval !='' )
			{
				$whereadd .= " AND  (  ".$searchtypeval." LIKE  '%".$keywordval."%' )";
				$endArray["searchtype"] =$searchtypeval;
			}
		}else{
			$searchtypeval = 'add_name';
			$endArray["searchtype"] = 'add_name';
		}
 

		$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"]  = 'add_id';
			$orderbyval = " `add_id` DESC  ";	
		}

		//var_dump( $searchtypeval );
		
		//帐号 	真实姓名 	Email地址 	登录次数 	加入时间 	最后登录时间
		
		$sqlQuery = " SELECT add_id, use_id ,add_name,  add_email, add_pcdall,add_address , add_mobile, add_phone , add_sign, add_time   FROM  ".SQL_PREFIX."address  WHERE 1  $whereadd  GROUP BY add_id ORDER BY  $orderbyval LIMIT  $LIMITnum , $pageSize ";
		
		$info = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."address  WHERE 1 $whereadd  ORDER BY `add_id`  ";
		
		$infocount = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=address&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"], $endArray );


		$GLOBALS['Templ']->assign('searchtype',$searchtypeval);
		$GLOBALS['Templ']->assign('listhtml',$listhtml);
		$GLOBALS['Templ']->assign('listval',$listval);
		$GLOBALS['Templ']->assign('listtype',$listtype);
		
		$GLOBALS['Templ']->assign('keywordval',$keywordval);
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('address_tpl.html');
		
		
	}
	
	function add( )
	{
		//$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24

		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=address&act=addinfo");
		$GLOBALS['Templ']->display('addressadd_tpl.html');
	}
	
	function addinfo()
	{
		$add_nameval 		= $GLOBALS['Reque'] -> funpost("add_name");
		$add_emailval 		= $GLOBALS['Reque'] -> funpost("add_email");
	
		$add_provinceval 	= $GLOBALS['Reque'] -> funpost("idprovince");
		$add_cityval 		= $GLOBALS['Reque'] -> funpost("idcity");
		$add_districtval 	= $GLOBALS['Reque'] -> funpost("iddistrict");
		$add_pcdallval		= $GLOBALS['Reque'] -> funpost("hiprovince").$GLOBALS['Reque'] -> funpost("hicity").$GLOBALS['Reque'] -> funpost("hidistrict");
		
		$hiprovince 		= $GLOBALS['Reque'] -> funpost("hiprovince");
		$hicity 			= $GLOBALS['Reque'] -> funpost("hicity");
		$hidistrict 		= $GLOBALS['Reque'] -> funpost("hidistrict");
		
		$add_addressval 	= $GLOBALS['Reque'] -> funpost("add_address");
		$add_postalcodeval 	= $GLOBALS['Reque'] -> funpost("add_postalcode");
		$add_mobileval 		= $GLOBALS['Reque'] -> funpost("add_mobile");
		$add_phoneval 		= $GLOBALS['Reque'] -> funpost("add_phone");
		
 
		$add_signval 		= $GLOBALS['Reque'] -> funpost("add_sign");
		
		
		$add_sendtimeval 	= $GLOBALS['Reque'] -> funpost("add_sendtime");
		$add_enabledval 	= $GLOBALS['Reque'] -> funpost("add_enabled");

		$add_timeval 	= time();
		
		if($add_nameval !='' and $add_emailval !='' )
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."address  WHERE add_name = '".$add_nameval."' ORDER BY `add_id` ";
			
			$info = $GLOBALS['MySql'] -> selectOne( $sqlQuery );
			
			$insertaddress ="INSERT INTO `".SQL_PREFIX."address` ( `add_name` , `add_email` , `add_province` , `add_city` , `add_district` ,`add_pcdall`,`add_address` ,`add_phone`, `add_postalcode` , `add_mobile` , `add_sign` , `add_sendtime`, `add_enabled`) VALUES (  '$add_nameval',  '$add_emailval',  '$add_provinceval', '$add_cityval',  '$add_districtval','$add_pcdallval','$add_addressval','$add_phoneval',  '$add_postalcodeval', '$add_mobileval',  '$add_signval', '$add_sendtimeval', '$add_enabledval')";
			$addressid = $GLOBALS['MySql'] -> insertSql($insertaddress);
			
			
			if( $info["CNUM"] < 1 )
			{

			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=address&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=address&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=address&act=add");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		}
			//var_dump($_REQUEST);
	}
	
	function del( )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
 
		$deletesql ="DELETE FROM `".SQL_PREFIX."address` WHERE `add_id` in ( $infoid )  ";
		$GLOBALS['MySql'] -> querySql($deletesql);
			
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=address&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	
	}
	
	function info(  )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$sqlQuery = " SELECT * FROM  ".SQL_PREFIX."address   WHERE  add_id = '".$infoid."' ORDER BY  `add_id` ";
		
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		

		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=address&act=editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('addressedit_tpl.html');
	
	}
 
	
	function editinfo( )
	{	
	
		$add_idval 	  = $GLOBALS['Reque'] -> funpost("add_id");	
		
		$add_nameval 		= $GLOBALS['Reque'] -> funpost("add_name");
		$add_emailval 		= $GLOBALS['Reque'] -> funpost("add_email");
	
		$add_provinceval 	= $GLOBALS['Reque'] -> funpost("idprovince");
		$add_cityval 		= $GLOBALS['Reque'] -> funpost("idcity");
		$add_districtval 	= $GLOBALS['Reque'] -> funpost("iddistrict");
		$add_pcdallval		= $GLOBALS['Reque'] -> funpost("hiprovince").$GLOBALS['Reque'] -> funpost("hicity").$GLOBALS['Reque'] -> funpost("hidistrict");
		
		$hiprovince 		= $GLOBALS['Reque'] -> funpost("hiprovince");
		$hicity 			= $GLOBALS['Reque'] -> funpost("hicity");
		$hidistrict 		= $GLOBALS['Reque'] -> funpost("hidistrict");
		
		$add_addressval 	= $GLOBALS['Reque'] -> funpost("add_address");
		$add_postalcodeval 	= $GLOBALS['Reque'] -> funpost("add_postalcode");
		$add_mobileval 		= $GLOBALS['Reque'] -> funpost("add_mobile");
		$add_phoneval 		= $GLOBALS['Reque'] -> funpost("add_phone");
		
 
		$add_signval 		= $GLOBALS['Reque'] -> funpost("add_sign");
		
		
		$add_sendtimeval 	= $GLOBALS['Reque'] -> funpost("add_sendtime");
		$add_enabledval 	= $GLOBALS['Reque'] -> funpost("add_enabled");


		$addsetUp = '';
		
		if($add_idval !='' and $add_emailval !='' )
		{
		
			$updateaddress = "UPDATE `".SQL_PREFIX."address` SET    add_name = '".$add_nameval."' ,add_email = '".$add_emailval."', add_province = '".$add_provinceval."' , add_city = '".$add_cityval."', add_district = '".$add_districtval."', add_pcdall = '".$add_pcdallval."', add_address = '".$add_addressval."', add_postalcode = '".$add_postalcodeval."', add_mobile = '".$add_mobileval."' , add_phone = '".$add_phoneval."' , add_sign = '".$add_signval."' , add_sendtime = '".$add_sendtimeval."'    WHERE  add_id = '".$add_idval."' ";
			
			$GLOBALS['MySql'] -> querySql( $updateaddress );


			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_editsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=address&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=address&act=info&id=".$add_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}

 

	
 
	
 
	
 
	
}

$Address = new Address();

?>