<?php
	!defined('DIRCMS_MEMBER') && exit('Access Denied!');
	$action=empty($action)?'manage':trim($action);
	require_once(DIRCMS_ROOT.'include/list.class.php');
	$l=new dircmslist();
	require_once(DIRCMS_ROOT.'include/admin/content.class.php');
	$c=new content();
	switch($action)
	{
		case 'manage':
			$where='userid='.intval($_userid);
			$where=isset($status)?$where." AND status=".intval($status):$where;
			$k=trim(preg_replace('/[!@#\$%\^&\*\(\):\'\"\{\}\[\]\.\?\,\/`\;]+/i','',$k));
			$where=!empty($k)?$where." AND orderid like '%".$k."%'":$where;
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'order',$where,$orderby,$page,18);
			$pages=$l->pages;
			include member_tlp('order_manage');
			break;
		case 'cancel':
			$r=$db->fetch_one("SELECT * FROM `".DB_PRE."order` WHERE `".DB_PRE."order`.`id`=".intval($id));
			if(!$r)showmsg('错误的请求!');
			if($r['username']!=$_username)showmsg('错误的请求!');
			if($r['status']==0) $db->update(DB_PRE.'order',array('status'=>4),'id='.intval($r['id'])); //////////只能取消未付款的订单
			showmsg('订单取消成功!');
			break;
		case 'pay':
			if(isset($do_submit))
			{
				$r=$db->fetch_one("SELECT * FROM `".DB_PRE."order` WHERE `".DB_PRE."order`.`id`=".intval($id));
				if(!$r)showmsg('该订单不存在!');
				if($r['username']!=$_username)showmsg('错误的请求!');
				if($r['status']!=0)showmsg('该订单已被取消或者已付款!');
				if(intval($r['amount'])>$_amount)showmsg('用户余额不足!');
				$yue=floatval($_amount-$r['amount']);
				$db->update(DB_PRE.'order',array('status'=>1),'id='.intval($r['id']));  //////////  设为已付款
				$db->update(DB_PRE.'member',array('amount'=>$yue),'id='.intval($_userid));  //////////  扣除用户费用
				$db->update(DB_PRE.'member_cache',array('amount'=>$yue),'id='.intval($_userid)); 
				$buylog = array(
					'userid'      => $_userid,
					'username'    => $_username,
					'type'		  => 'amount',
					'blance'      => $yue, ////////////  点数余额
					'number'      => intval(-$r['amount']), 				
					'inputid'     => $_userid,
					'inputer'     => $_username,
					'ip'		  => IP,
					'note'		  => '为订单【'.$r['orderid'].'】付款',
					'time'	      => TIME
				);
				$db->insert(DB_PRE.'pay_exchange',$buylog); ///////////////// 交易记录
				showmsg('付款成功!','index.php?file=order&action=manage');
			}
			$val=$db->fetch_one("SELECT * FROM `".DB_PRE."order` WHERE `".DB_PRE."order`.`id`=".intval($id));
			if(!$val)showmsg('该订单不存在!');
			if($val['username']!=$_username)showmsg('错误的请求!');
			if($val['status']!=0)showmsg('该订单已被取消或者已付款!');
			include member_tlp('order_pay');
			unset($val);
			break;
		case 'receive':
			$r=$db->fetch_one("SELECT * FROM `".DB_PRE."order` WHERE `".DB_PRE."order`.`id`=".intval($id));
			if(!$r)showmsg('该订单不存在!');
			if($r['username']!=$_username)showmsg('错误的请求!');
			if($r['status']!=2)showmsg('该订单尚未发货或者已被关闭，无法确认!');
			$db->update(DB_PRE.'order',array('status'=>3),'id='.intval($r['id']));
			showmsg('操作成功!','index.php?file=order&action=manage');
			break;
		case 'view':
			$val=$db->fetch_one("SELECT * FROM `".DB_PRE."order` WHERE `".DB_PRE."order`.`id`=".intval($id));
			if(!$val)showmsg('该订单不存在!');
			if($val['username']!=$_username)showmsg('错误的请求!');
			include member_tlp('order_view');
			unset($val);
			break;
		case 'editdeliver':
			 include member_tlp('order_editdeliver');
			 break;
		case 'onlinebuy':
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$val=$c->get(intval($goodsid));
			if(!$val || $verify!=md5(AUTH_KEY.$val['id'].$val['title'].$val['price']))showmsg('错误的请求!');
			$s=new select();
			$areaidselect=$s->getSelect('o[areaid]','配送区域',DB_PRE.'area',true,'area');
			$r=cache_read('cache_cargomethod.cache.php',DIRCMS_ROOT.'data/cache/');
			$cargomethodselect='<select name="o[cargomethodid]">';
			if($r)foreach($r as $_r)
			{
				$cargomethodselect.='<option value="'.$_r['id'].'">'.$_r['name'].'</option>';
			}
			$cargomethodselect.='</select>';
			include template('order_buyonline');
			break;
		case 'confirm':
			if(isset($do_submit))
			{
				$o['number']=intval($o['number']);
				//exit($o['number']);
				if($_SESSION['checkcode']!=$chkcode || !$_SESSION['checkcode'])showmsg('验证码不正确!',$forward);
				if($verify!=md5(AUTH_KEY.$o['goodsid'].$o['goodsname'].$o['price']))showmsg('错误的请求!');
				if(!preg_match('/^[\x80-\xff_a-zA-Z0-9]{1,15}$/i',$o['consignee']))showmsg('请输入正确的收货人!');
				if(!preg_match('/^[0-9\-\(\)\,]{3,20}$/i',$o['telephone']))showmsg('请输入正确的电话号码!');
				if(!preg_match('/^[0-9\-\(\)]{10,15}$/i',$o['mobile']))showmsg('请输入正确的手机号码!');
				$o['address']=sub_string($o['address'],100);
				if(!preg_match('/^[0-9]{5,7}$/i',$o['postcode']))showmsg('请输入正确的邮编!');
				if(!is_numeric($o['number']) || intval($o['number'])<1)showmsg('请输入正确的订购数量!');
				$o['ip']=IP;
				$o['userid']=$_userid;
				$o['username']=$_username;
				$o['time']=TIME;
				$o['date']=date('Y-m-d',TIME);
				$o['status']=0;
				$o['orderid']=date("YmdHis").str_pad( mt_rand(1,99999 ),5,"0",STR_PAD_LEFT);
				$cargomethod_r=cache_read('cache_cargomethod'.$o['cargomethodid'].'.cache.php',DIRCMS_ROOT.'data/cache/');
				if(!$cargomethod_r)showmsg('暂不支持此种送货方式!');
				$o['amount']=floatval($o['price'])*intval($o['number'])+intval($cargomethod_r['fee']);
				$o['carriage']=intval($cargomethod_r['fee']);
				$o['number']=intval($o['number']);
				$o['areaid']=intval($o['areaid']);
				$o['number']=intval($o['number']);
				$id=$db->insert(DB_PRE.'order',$o); 
				unset($o);
				showmsg('操作成功，转到付款界面!','index.php?job=order');
			}
			$o=array_map('trim',$o);
			$o=array_map('htmlspecialchars',$o);
			if($verify!=md5(AUTH_KEY.$o['goodsid'].$o['goodsname'].$o['price']))showmsg('错误的请求!');
			if(!preg_match('/^[\x80-\xff_a-zA-Z0-9]{1,15}$/i',$o['consignee']))showmsg('请输入正确的收货人!');
			if(!preg_match('/^[0-9\-\(\)\,]{3,20}$/i',$o['telephone']))showmsg('请输入正确的电话号码!');
			if(!preg_match('/^[0-9\-\(\)]{10,15}$/i',$o['mobile']))showmsg('请输入正确的手机号码!');
			$o['address']=sub_string($o['address'],100);
			if(!preg_match('/^[0-9]{5,7}$/i',$o['postcode']))showmsg('请输入正确的邮编!');
			if(!is_numeric($o['number']) || intval($o['number'])<1)showmsg('请输入正确的订购数量!');
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			$areaidselect=$s->getSelect('o[areaid]','配送区域',DB_PRE.'area',true,'area');
			$cargomethod_r=cache_read('cache_cargomethod'.$o['cargomethodid'].'.cache.php',DIRCMS_ROOT.'data/cache/');
			if(!$cargomethod_r)showmsg('暂不支持此种送货方式!');
			$o['amount']=floatval($o['price'])*intval($o['number'])+intval($cargomethod_r['fee']);
			$area_r=cache_read('cache_cargomethod'.$o['cargomethodid'].'.cache.php',DIRCMS_ROOT.'data/cache/');
			$forward=getcururl();
			include template('order_confirm');
			break;
		default:
			showmsg('参数有误,返回网站首页!',$DIRCMS['site_url']);
			break;
	}
?>