<?php
/**
 * 会员订单
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */

namespace Admin\Controller;

class MemberOrderController extends BaseController{

    	public function index(){
		$postdata=I('param.');
		$adminid=UID;
		if($adminid !=1){
			$data['id']=$adminid;
			$store_id=M('Admin')->where($data)->field('store_id')->find();
			$map['store_id']=$store_id;
		}
		if(empty($postdata['date_to'])){
				$date_tostr=strtotime('first day of next month');
				$postdata['date_to']=time_format($date_tostr,'Y-m-d');
		}
		if(empty($postdata['date_from'])) $postdata['date_from']=time_format(strtotime('-6 month',$date_tostr),'Y-m-d');
		$map['addtime'] = array(array('GT',strtotime($postdata['date_from'])),array('lt',strtotime($postdata['date_to'])));
		$order_info = M('OrderInfo')->where($map)->field("count(uid) as l_order,DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y年%m月') as l_month")->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y年%m月')")->select();
		//订单数统计
		$months[5]["time"]=time_format(NOW_TIME,'Y年m月');
		for($i=1;$i<6;$i++){$months[(5-$i)]["time"]=time_format(strtotime('-'.$i.' month'),'Y年m月');}
		foreach($order_info  as $k=>$v){
			foreach($months as $kk => $vv){
				if($vv["time"] == $v["l_month"]){
					$numarr[$vv['time']] = $v["l_order"];$numarr[$vv['time']] = $v["l_order"];
				}
			}
		}      
		$option_count = array(
			"legend"=>array('data'=>array("订单数统计")),
			"xaxis"=>array("type"=>"category","data"=>array_keys($numarr)),     
			"series"=>array(
				array("name"=>"订单数统计","type"=>"line","stack"=>"订单数统计",'itemStyle'=>array(),"data"=>array_values($numarr)),
			),
		);
			//配送方式统计
		/*$map['shipping_id'] = array(array('GT',0));	
		$order_info_s = M('OrderInfo')->where($map)->field("shipping_id")->select();
		foreach($order_info_s as $v){
			$pei_style[]=$v['shipping_id'];
		}
		$pei_style_num=array_count_values($pei_style);
		foreach($pei_style_num as $k=>$v){
			$kname=idtoname($k,'Shipping');
			$arr[$kname]=$v;
		}
		$pei_style_count = array(
			"legend"=>array('data'=>array("配送方式")),
			"xaxis"=>array("type"=>"category","data"=>array_keys($arr)),     
			"series"=>array(
				array("name"=>"配送方式","type"=>"bar","stack"=>"配送方式",'itemStyle'=>array(),"data"=>array_values($arr)),
			),
		);*/
		//支付方式统计
		$pay_info = M('OrderInfo')->field("pay_code")->select();
		foreach($pay_info as $k=>$v){
			$pay_style[]=$v['pay_code'];
		}
		$pay_style_num=array_count_values($pay_style);
		$i=0;
		$arrpay=array();
		foreach($pay_style_num as $k=>$v){
			$wherepay['code']=$k;
			$kname = M('Payment')->where("code='".$k."'")->field('name')->find();
			$arrpay[$kname['name']] = $v; 
			$i++;
		}
		$pay_style_count = array(
			"legend"=>array('data'=>array("支付方式")),
			"xaxis"=>array("type"=>"category","data"=>array_keys($arrpay)),  
			"series"=>array(
				array("name"=>"支付方式","type"=>"bar","stack"=>"支付方式",'itemStyle'=>array(),"data"=>array_values($arrpay)),
			),
		);
		$ec = new \Common\Vendor\Echarts();
		$this->assign('Area_count',$ec->show('Area_count', $option_count,'line','订单数统计'));
		$this->assign('Area_count_pei',$ec->show('Area_count_pei', $pei_style_count,'bar','配送方式'));
		$this->assign('Area_count_pay',$ec->show('Area_count_pay', $pay_style_count,'bar','支付方式'));
		$this->assign($postdata);
		$this->display();
	}

}