<?php
class orderAction
{
	public function index()
	{
		//必须开通店铺
		$B2cOrder = new B2cOrder();
		$B2cProduct=new B2cProduct();
		$B2cBase=new B2cBase();
		$Params = mvc::$URL_PARAMS;
		LibTpl::Set('title','订单管理-跑街的');
		LibTpl::Set('LeftMenu','order');
		$perPage=10;
		$where=sprintf(' and ord_shop_id=%d and ord_seller_delete=0 and ord_user_delete=0',$_SESSION['_shopid']);
		LibTpl::Set('Params',$Params);
		$where.=(isset($Params['userId'])&&LibFc::Int($Params['userId']))? sprintf(' and ord_uid=%d',LibFc::Int($Params['userId'])) : '';
		$nowPage=isset($Params['page']) ? $Params['page'] : 1;
		$status=isset($Params['status']) ? $Params['status'] : '';
		LibTpl::Set('status', $status);
		switch ($status) {			
			case 'creat':
				$where.=" and ord_status='creat'";
				break;
			case 'paied':
				$where.=" and ord_status='paied'";
				break;
			case 'send':
				$where.=" and ord_status='send'";
				break;
			case 'cancel':
				$where.=" and ord_status='cancel'";
				break;
			case 'refund':
				if(count($reOrder['data']))
				{
					$where.=sprintf(' and ord_id in(%s)',implode(',',$reOrder['data']));
				}else
				{
					$where.=' and ord_id <-1';
					// goto end;
				}
				break;		
			case 'completed':
				$where.=" and ord_status='completed'";
				break;
		}
		//配件名称和配件ID处理	
		if(isset($Params['keyword'])&&!empty($Params['keyword']))
		{
			$keywordArr=LibFc::keyword($Params['keyword']);
			if(count($keywordArr))
			{
				$keywordStr="'".implode("','",$keywordArr)."'";
				$keywordWhere=sprintf(' and key_txt in(%s)',$keywordStr);
				$keywordProData=$B2cProduct->get('bc_keyword right join bc_product_keyword on prk_key_id=key_id',['prk_pro_id'],$keywordWhere);
			}
			if(!count($keywordArr)||!count($keywordProData['data']))
			{
				//关键字搜索为空，搜索名称
				$nameOrder=$B2cProduct->get('bc_product left join or_order_product on orp_pro_id=pro_id',['orp_ord_id'],sprintf(" and orp_pro_name like '%%%s%%' group by orp_ord_id",LibFc::Escape($Params['keyword'])));
				if(count($nameOrder['data']))
				{
					$orderResArr=array_column($nameOrder['data'], 'orp_ord_id');
					$where.=sprintf(' and ord_id in(%s)',implode(',',$orderResArr));
				}
				else
				{
					$where.=' and ord_id <-1';
				}
				// $orderListData['data']=[];
				// goto end;
			}else
			{
				$keywordPro=array_unique(array_column($keywordProData['data'],'prk_pro_id'));
				$where.=count($keywordPro)?sprintf(" and orp_pro_id in (%s)",implode(',',$keywordPro)):'';
			}
		}	
		$where.=(isset($Params['orderId'])&&!empty($Params['orderId'])) ? sprintf(" and ord_id =%d",LibFc::Escape($Params['orderId'])): '';
		$baseOrderBy='ord_add_time desc';
		$orderBy=$baseOrderBy;
		// echo $where;
		$orderListData=$B2cOrder->getOrderList($nowPage,$perPage,$orderBy,$where);
		// print_r($orderListData['data']);
		LibTpl::Set('totalPage',ceil($orderListData['data'][0]['total']/$perPage));
		LibTpl::Set('orderList', $orderListData['data']);
		LibTpl::Set('footMenu', 'order');
		if(!empty($Params['isAjax']))
		{

			$res=['status'=>true,'data'=>[]];
			if($orderListData['data'][0]['total']>0)
			{
				$res=['status'=>true,'data'=>$orderListData['data']];
			}
			LibFc::ajaxJsonEncode($res);
		}
		else
		{
			LibTpl::Put();
		}
	}
	function getEditorder()
	{
		$B2cOrder = new B2cOrder();
		$B2cProduct=new B2cProduct();
		$B2cBase=new B2cBase();
		$Params = mvc::$URL_PARAMS;
		$res=['status'=>false,'data'=>'参数错误'];
		if(LibFc::Int($Params['ordId']))
		{
			$res=$B2cOrder->getOrderDetail($Params['ordId'],$_SESSION['_shopid']);
			LibTpl::Set('order',$res['data']);
			$res['data']=LibTpl::Fetch('/order/editOrPrice.php');
			$res['status']=true;
		}
		LibFc::ajaxJsonEncode($res);
	}
	function editOrder()
	{
		$B2cOrder = new B2cOrder();
		// print_r($_POST);
		// die;
		$res=$B2cOrder->editorOrderPrice($_SESSION['_shopid'], $_POST['discount'],$_POST['price'], $_POST['frtPrice'], $_POST['orderId']);
		if($res['status'])
		{
			$res=$B2cOrder->get('or_order',['ord_id','ord_freight_money','ord_pay_money'],sprintf(' and ord_id=%d',$_POST['orderId']),true);
		}
		LibFc::ajaxJsonEncode($res);
	}
}