<?php
/**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class OrderAction extends BaseAction{
	function pay(){
		$orderId = getgpc('orderId');
		if(!$orderId || !is_numeric($orderId)){ Msg::show('订单号不存在'); }
		load_class("order");
		$om = new OrderModel();
		$orderInfo = $om->getInfo($orderId);
		if(!$orderInfo){ Msg::show('订单不存在'); }
		$info['paymentId'] = $orderInfo['paymentId'];
		$info['paytypeId'] = $orderInfo['paytypeId'];
		$info['payStatusId'] = $orderInfo['payStatusId'];
		$info['iStatus'] = $orderInfo['iStatus'];
		$info['amount'] = $orderInfo['amountNow'];
		$info['divideTypeId'] = $orderInfo['divideTypeId'];
		$info['divideNum'] = $orderInfo['divideNum'];
		if($info['amount']>0 && $info['iStatus']=='3'  && ($info['payStatusId']==4 || $info['payStatusId']==21)){
			$pay = new Pay();
			$pay->order($info['paymentId'],$info['amount'],$orderInfo['id']);
		}else{
			Msg::show('订单状态错误');
		}

	}

	function prints(){
		$this->checkAdmin();
		$id = getgpc("id");
		load_class("order");
		$order = new OrderModel();
		assign("orderInfo",$order->getInfo($id));
		$pageInfo['title'] = '购物清单';
		assign("pageInfo",$pageInfo);
		display('order/prints');
	}

	function logistics(){
		$inajax=getgpc('inajax');
		$act=getgpc('act');
		$number =getgpc('number');
		if($inajax=='1'){
			$ret=array('success'=>false,'message'=>'禁止访问');
			if($act=='query'){
				if(!$number || !is_numeric($number)){
					$ret['message']='订单号错误';
				}else{
					$url = 'http://app.ueidc.com/logistics/query';
					$param='number='.$number.'&com=heibao';
					$http = new http($url);
					$http->setPost($param);
					$http->createCurl();
					$resp = $http->responseText();
					if($resp && strpos($resp,'?xml')){
						$doc = new DOMDocument();
						$doc->loadXML($resp);
						$returncode = $doc->getElementsByTagName('returncode')->item(0)->nodeValue;
						if($returncode=='200'){
							$ret=array('success'=>true,'message'=>'查询成功');
							$items = $doc->getElementsByTagName('result')->item(0)->getElementsByTagName("item");
							$tr='';
							foreach($items as $item){
								$tr.='<tr><td>'.($item->getElementsByTagName("datetime")->item(0)->nodeValue).'</td><td>'.($item->getElementsByTagName("message")->item(0)->nodeValue).'</td></tr>';
							}
							$ret['data']=$tr;
						}else{
							$ret['message']=$doc->getElementsByTagName('failreason')->item(0)->nodeValue;
						}

					}else{
						$ret['message']='查询失败，系统错误，请稍候再试';
					}
				}
			}
			if(!$ret['success']){
				$ret['data']='<tr colspan="2">'.$ret['message'].'</tr>';
			}
			echo json_encode($ret);
			exit;
		}
		display('order/logistics.html');
	}

	function service(){
		BaseAction::checkLogin();
		$orderId=getgpc('id');
		$gid=getgpc('gid');
		if(!$orderId || !is_numeric($orderId)){Msg::show('订单信息错误');}
		if(!$gid || !is_numeric($gid)){Msg::show('商品信息错误');}
		load_class('order');
		$om = new OrderModel();
		$orderInfo = $om->getInfo($orderId);
		if(!$orderInfo || $orderInfo['userId']!=$_SESSION['userId']){Msg::show('订单信息错误');}
		$goodsInfo = false;
		foreach($orderInfo['goods'] as $goods){
			if($goods['id']==$gid){$goodsInfo=$goods;break;}
		}
		if(!$goodsInfo){Msg::show('商品信息错误');}
		$inajax=getgpc('inajax','P');
		if($inajax=='1'){
			$ret=array('success'=>false,'message'=>'非法操作');
			$goodsId = $goodsInfo['goodsId'];
			$sql = "select id from qk_order_service where orderId='$orderId' and orderGoodsId='".$gid."' and iStatus=0 and userId='".$_SESSION['userId']."'";
			if($GLOBALS['db']->getOne($sql)){
				$ret['message']='当前订单商品已有正在处理中的服务单，不可重复申请';
			}else{
				$typeId=getgpc('typeId','P');
				$content=getgpc('content','P');
				$name=getgpc('name','P');
				$mobile=getgpc('mobile','P');
				$c=true;
				if(!$typeId || !is_numeric($typeId) || ($typeId!='2' && $typeId!='1')){$ret['message']='请选择服务类型';$c=false;}
				if($c && !$content){$ret['message']='请填写问题描述';$c=false;}
				if($c && utf8_strlen($content)>500){$ret['message']='问题描述不能超过500字';$c=false;}
				if($c && !$name){$ret['message']='请填写联系人姓名';$c=false;}
				if($c && utf8_strlen($name)>50){$ret['message']='联系人姓名不得超过50字';$c=false;}
				if($c && (!$mobile || !is_numeric($mobile) || !preg_match("/^1[0-9]{10}$/",$mobile))){$ret['message']='请准确填写联系人手机号码';$c=false;}
				if($c){
					$sql = "insert into qk_order_service set  userId='".$_SESSION['userId']."',typeId='$typeId',orderId='".$orderId."',orderGoodsId='$gid',goodsId='$goodsId',content='".$content."',name='$name',mobile='$mobile',posttime=UNIX_TIMESTAMP()";
					$GLOBALS['db']->query($sql);
					$ret=array('success'=>true,'message'=>'申请成功，请等待客服处理');
				}
			}
			echo json_encode($ret);exit;
		}

		$orderInfo['goodsInfo'] = $goodsInfo;
		assign('orderInfo',$orderInfo);
		display('order/service.html');
	}
}
?>