<?php
/**
 * 会员中心
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <1070309178@qq.com>
 */
namespace Mobile\Controller;
use Think\Controller;
class ShopCenterController extends ShopBaseController {
	public function _initialize(){
		parent::_initialize ();
        if(!checksiteauth('MenDian')){ exit;}
        if(!MID){
			cookie('forward',U('Mobile/'.CONTROLLER_NAME.'/'.ACTION_NAME));
			$this->redirect('Mobile/Login/shop_index');
		}
	}
	public function index(){
		$map = $u_map = $o_map = $j_map = array();
		$u_map['id']=MID;
		$info=M('Shops')->where($u_map)->field(true)->find();
		$this->assign('info',$info);
		$map['shop_id']=MID;
		$t_begin=mktime(0,0,0,date('m'),date('d'),date('Y'));
		$t_end=NOW_TIME;
		
		// 订单总数和今日新增订单数
		$o_map = $map;
		$o_map['type'] = 14;
		$order_count=M('OrderInfo')->where($o_map)->field('id')->count();
		$j_map = $o_map;
		$j_map['addtime'] = array(array('gt',$t_begin),array('lt',$t_end));
		$j_order_count = M('OrderInfo')->where($j_map)->field('id')->count();
		$this->assign('order_count',$order_count);
		$this->assign('j_order_count',$j_order_count);
		// 今日新增营业额
		$m_map = $map;
		$m_map['type'] = 1;
		$m_map['change_type'] = 0;
		$m_map['change_time'] = array(array('gt',$t_begin),array('lt',$t_end));
		$money_sum = M('ShopbalanceLog')->where($m_map)->sum('change_num');
		$this->assign('money_sum',$money_sum);
		// 今日新增会员数
		$user_map['user_shop'] = MID;
		$user_map['card_time'] = array(array('gt',$t_begin),array('lt',$t_end));
		$user_sum = M('Member')->where($user_map)->field('id')->count();
		$this->assign('user_sum',$user_sum);
		// 今日新增预约数
		$yy_map = $map;
		$yy_map['type'] = 11;
		$yy_map['addtime'] = array(array('gt',$t_begin),array('lt',$t_end));
		$yy_sum = M('OrderInfo')->where($yy_map)->field('id')->count();
		$this->assign('yy_sum',$yy_sum);
		// 总顾客和今日新增顾客数
		$gk_sum = M('ShopsCustomer')->where($map)->count();
		$this->assign('gk_sum',$gk_sum);
		$gk_map = $map;
		$gk_map['add_time'] = array(array('gt',$t_begin),array('lt',$t_end));
		$gk_count = M('ShopsCustomer')->where($gk_map)->field('id')->count();
		$this->assign('gk_count',$gk_count);
		//缺货提醒
        $this->qh_num(MID);
		
        $this->assign('tplseo_title','门店中心'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','门店中心');
		$this->display();
	}
	/**
     * 缺货商品数量
     */
    private function qh_num($shop_id){
        if($shop_id){
            $goods_list = M('ShopsGoodsStock')->where('shop_id = '.$shop_id)->field(true)->select();
            foreach ($goods_list as $key => $value) {
                if($value['goods_number'] <= $value['qh_count'] && $value['qh_count'] != 0){
                    $list[$key] = $value;
                }
            }
            $this->assign('qh_count',count($list));
        }
    }
	public function customer(){
		$map['shop_id']=MID;
		$list=M('ShopsCustomer')->where($map)->field(true)->select();

		$this->assign('list',$list);
        $html=$this->fetch('c_list');
		$this->assign('html',$html);

        $this->assign('tplseo_title','门店顾客'.'_'.C('WEB_SITE_TITLE'));
        $this->display();
	}
	public function customer_ajax(){
		$keyword = I('keyword');
		$return=array();$return['status']=1;
		if($keyword){
			$whereor['id'] = array('like', '%' . $keyword . '%');
            $whereor['username'] = array('like', '%' . $keyword . '%');
            $whereor['_logic'] = 'or';
			$map_ids['_complex'] = $whereor;
		}

		$ids=M('Member')->where($map_ids)->getField('id',true);

		if(empty($ids)){
			$return['status']=0;
			$return['info']='没有改相关信息的顾客';
			$this->ajaxReturn($return);
		}
		$map['user_id'] = array('in',$ids);
		$map['shop_id']=MID;
		$list=M('ShopsCustomer')->where($map)->field(true)->select();

        if($list){
            $return['keyword']=$keyword;
            $this->assign('list',$list);
            $return['html'] = $this->fetch('c_list');
        }
		$this->ajaxReturn($return);
	}
	public function user(){
		$map['user_shop']=MID;
		$list=M('Member')->where($map)->field(true)->select();

		$this->assign('list',$list);
        $html=$this->fetch('u_list');
		$this->assign('html',$html);

        $this->assign('tplseo_title','门店会员'.'_'.C('WEB_SITE_TITLE'));
        $this->display();
	}
	public function user_ajax(){
		$keyword = I('keyword');
		$return=array();$return['status']=1;
		if($keyword){
			$whereor['id'] = array('like', '%' . $keyword . '%');
            $whereor['username'] = array('like', '%' . $keyword . '%');
            $whereor['_logic'] = 'or';
			$map['_complex'] = $whereor;
		}

		$map['user_shop']=MID;
		$list=M('Member')->where($map)->field(true)->select();

        if($list){
            $return['keyword']=$keyword;
            $this->assign('list',$list);
            $return['html'] = $this->fetch('u_list');
        }
		$this->ajaxReturn($return);
	}
	public function user_info(){
		$id=I('uid');
		if($id){
			$map['id']=$id;
			$user_info=M('Member')->where($map)->field(true)->find();
			$this->assign($user_info);
		}
        $this->assign('tplseo_title','会员详情'.'_'.C('WEB_SITE_TITLE'));
        $this->display();
	}
	public function views_list($type = 0){
        $this->assign('tplseo_title','我的足迹'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','我的足迹');
		$goods_ids=session(MID+'_footer_history');
		if(!empty($goods_ids)){
			$page=I('param.page');
			$page=$page?$page:1;
			$map['id']=array('in',$goods_ids);
			$info=page_list('Goods',$map,$order,'id,goods_name,img,shop_price,market_price',10,'',$page);
			if ($type == 1) return $info;
			$this->assign($info);
		}
		$this->display();
	}
	public function get_views_list(){
        $return['status'] = 0;
        $data = $this->views_list(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('views_page_list');
        $return['data'] = $list_html;
        $this->ajaxReturn($return);
    }
    public function order_manage(){
        $this->assign('page_title','订货单管理');
        $this->display();
    }

 //    public function customer(){
	// 	$keyword = I('keyword');
	// 	if($keyword){
	// 		$map['']
	// 	}
	// 	$map['shop_id']=MID;
	// 	$list=M('ShopsCustomer')->where($map)->field(true)->select();

	// 	$this->assign('list',$list);
 //        $html=$this->fetch('list');
 //        $this->assign('html',$html);
 //        $this->assign('tplseo_title','门店顾客'.'_'.C('WEB_SITE_TITLE'));
 //        $this->display();
	// }
	// public function user(){
	// 	$keyword = I('keyword');
	// 	if($keyword){
	// 		$whereor['id'] = array('like', '%' . $keyword . '%');
 //            $whereor['username'] = array('like', '%' . $keyword . '%');
 //            $whereor['_logic'] = 'or';
	// 		$map['_complex'] = $where;
	// 	}
	// 	$map['user_shop']=MID;
	// 	$list=M('Member')->where($map)->field(true)->select();

	// 	$this->assign('list',$list);
 //        $html=$this->fetch('list');
 //        $this->assign('tplseo_title','门店会员'.'_'.C('WEB_SITE_TITLE'));
 //        $this->display();
	// }
}