<?php
/** wap
 * 外卖控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <xiaol@sina.cn>
 */
namespace Mobile\Controller;
class TakeoutController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
        // $siteinfo=session('siteinfo');
		// if(empty($siteinfo['temp_lat'])){$this->redirect('ShopsTakeout/change_geo',array('type'=>1,'shop_id'=>I('id')));}
        if (!checksiteauth('WaiMai,MenDian')) {
            exit;
        }
		/*if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
			$this->redirect('Mobile/Login/perfect');
		}elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
			$this->error('您已经领取过会员卡了！');exit;
		}*/
        $param = I('param.');
        $param['action_name']=ACTION_NAME;
        $seo_info=D('Common/Takeout')->takeout_seo_info($param);
        $this->assign($seo_info);
    }
	public function index() {
        $siteinfo=session('siteinfo');
        $siteinfo['temp_name']=$siteinfo['temp_name']?$siteinfo['temp_name']:$siteinfo['site_name'];
        $this->assign('temp_name',$siteinfo['temp_name']?$siteinfo['temp_name']:'请选择');
        $data=D('Mobile/Takeout')->get_index();
        $this->assign($data);
        $list_data=D('Mobile/Takeout')->get_list();
        $this->assign($list_data);
        $html=$this->fetch('shops_list');
		$shop_data['list']=getShop(1,100);//获取可配送的门店列表
		if($shop_data['list']){
			$shop_data['count'] = count($shop_data['list']);
		}else{
			session('main_shop_id',null);
			$shop_data['count'] = 0;
		}
        $main_shop_id = session('main_shop_id');//用户选中的门店id
        $this->assign('tplseo_title','附近门店_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title',"附近门店");
		$this->assign('shop_list',$shop_data);
		$this->assign('main_shop_id',$main_shop_id);
        $this->assign('html',$html);
		$this->display();
	}
	public function lists(){
        $param=I('param.');$this->assign($param);
        $data=D('Mobile/Takeout')->get_list($param);
        $this->assign($data);
        $html=$this->fetch('shops_list');
        $this->assign('html',$html);
	// print_r($html);exit;
        $page_title=empty($param['cid'])?'全部分类':idtoname($param['cid'],'ShopsCategory');
        $this->assign('page_title',$page_title);
        // $this->get_filter();
        $this->display();
    }
    //门店页
    public function shops_index() {
        $this->assign('nowtime',NOW_TIME);
        $this->judge();
        $data=D('Mobile/Takeout')->get_cate_list();
        if($data['status'] == 0){
            $data['cate_goods_list'] = array();
        }else{
            $category_list_arr = array();
            foreach($data['cate_goods_list'] as $v){
                $category_list_arr[$v['id']] = $v;
            }
            $data['cate_goods_list'] = $category_list_arr;
        }
        $catid = I('catid');
        if($catid){
            $map = array();$map['id'] = $catid;
            $catpid = M('PurchaseCategory')->where($map)->getField('pid');
            if($catpid == 0){
                $this->assign('catid',$catid);
                $this->assign('child_id',0);
            }else{
                $map = array();$map['id'] = $catpid;$map['pid'] = 0;
                $cate_pid = M('PurchaseCategory')->where($map)->getField('id');
                $this->assign('catid',$cate_pid);
                $this->assign('child_id',$catid);
            }
        }else{
            $this->assign('catid',0);
        }
        $brandid = I('brandid');
        if($brandid){
            $this->assign('brandid',$brandid);
        }else{
            $this->assign('brandid',0);
        }
        $screenid=I('screenid');
        if($screenid){
            $this->assign('screenid',$screenid);
        }else{
            $this->assign('screenid',0);
        }

        if($catid){
            $catename=idtoname($catid,'PurchaseCategory','name','id',false);
        }else{
            $catename='推荐';
            $tpl = C('LIST_TPL');
            if($tpl!="shops_index_blue"){
                $catename='分类';
            }
        }

        $this->assign('cate_name',$catename);
        $this->assign($data);
        $shop_id = get_wxsmall_shop_id();
        $this->cart_list($shop_id);
        $this->assign('shop_id',$shop_id);
        $title = "商品";
        $description = '商品' . '_' . C('WEB_SITE_TITLE');
        $share_url=U('Mobile/Takeout/shops_index',array('eid'=>MID));
        $this->assign('title',$title);
        $this->assign('description',$description);
        $this->assign('tplseo_title',$description);
        $this->assign('share_url',$share_url);
        $tpl = C('LIST_TPL');
        if($tpl == 'shops_index_green'){}elseif($tpl == 'shops_index_yellow'){$tpl = 'shops_index_yellow';}else{$tpl = 'shops_index_blue';}
        $this->display($tpl);
    }
    public function get_cate_info($catid){
        if(!$catid)return false;
        $map = array();$map['id'] = $catid;
        $cat_info = M('PurchaseCategory')->where($map)->field('pid,id')->find();
        return $cat_info;
    }
    public function get_list_data(){
        $postdata = I('param.');
        $return = array('status'=>0,'data'=>'');
        $tpl = C('LIST_TPL');
        $shop_id = get_wxsmall_shop_id();
        $this->assign('shop_id',$shop_id);
        $data=D('Common/Takeout')->get_goods_list($postdata,1);

        if($postdata['childcatid']){$cate_id=$postdata['childcatid'];}elseif($postdata['catid']){
            $cate_id=$postdata['catid'];
        }
        if($cate_id){
            $catename=idtoname($cate_id,'PurchaseCategory','name','id',false);
        }else{
            $catename='店铺推荐';

            if($tpl!="shops_index_blue"){
                $catename='分类';
            }
        }

        $return['cate_name'] = $catename;
        $this->assign('cate_name',$catename);
        $this->assign('catid',$postdata['catid']);
        if($postdata['keyword']){
            $this->assign('keyword',$postdata['keyword']);
        }
        if($data['g_status']){
            $goods_list = $data['goods_list']['list'];
            if($goods_list){
                $this->assign('goods_data',$goods_list);
                $this->assign('page',$data['page']);
                $this->assign('sum_page',$data['goods_list']['totalPages']);
                $return['status'] = 1;
                $return['page'] = $data['page'];
            }
        }else{
            $goods_list = array();
            $this->assign('page',1);
            $this->assign('goods_data',$goods_list);
        }

        if($tpl == 'shops_index_green'){
            $fetch = 'goods_list_green';
        }elseif($tpl == 'shops_index_yellow'){$fetch = 'goods_list_yellow';}else{
            $fetch = 'goods_list';
        }
        $list_html=$this->fetch($fetch);
        $return['data'] = $list_html;
        if($data['sql'])$return['sql'] = $data['sql'];
        $this->ajaxReturn($return);
    }
    //商品内容页
    public function goods_index(){
        $id=I('id');
        $shop_id = session('main_shop_id');
        if(!$id || !$shop_id){$this->error('参数错误');}
        $data=D('Mobile/Takeout')->get_goods($id,$shop_id);
        if($data['status']==0){$this->error($data['info']);}
		$this->assign('attr',$data['goods_info']['format_spec']);
        $this->assign($data);
        $this->assign('is_list',0);
        $this->cart_list($shop_id);
        $this->display();
    }
    public function get_cart_list(){
        $data = array();
        $data['status'] = 0;
        $shop_id = get_wxsmall_shop_id();
        $return=D('Mobile/Takeout')->get_cart_list($shop_id);
        if($return['status'] == 1){
	  $data['status'] = 1;
	  $cart_info = $return['data'];
        }
        $this->assign($cart_info);
        $html=$this->fetch('footer_cart');
        // $this->assign('cart_html',$html);
		
        $data['data'] = $html;
        $this->ajaxReturn($data);
    }
	//购物车列表
	public function cart_list($shop_id){
        if(!$shop_id){
			$cart_info=array();
		}else{
			$return=D('Mobile/Takeout')->get_cart_list($shop_id);
			if($return['status'] == 1){
				$cart_info = $return['data'];
			}
		}
        $this->assign($cart_info);
        $html=$this->fetch('footer_cart');
        $this->assign('cart_html',$html);
    }
	//加入购物车
    public function add_cart(){
        $param=I('param.');
        $return=D('Mobile/Takeout')->add_cart($param);
        if($return['status'] == 1){
            $data = $return['data'];
        }else{
            $data = $return;
        }
        $this->ajaxReturn($data);
    }

    //规格底部购物车变动
    public function add_spec_cart(){
        $param=I('param.');
        $data=D('Mobile/Takeout')->add_spec_cart($param);
        $this->ajaxReturn($data);
    }
    //清空购物车
    public function clear_cart(){
        $param=I('param.');
        $data=D('Mobile/Takeout')->clear_cart($param);
        $this->ajaxReturn($data);
    }
    public function shops_info(){
        $id=I('id');
        if(!$id){$this->error('参数错误');}
		
        $data=D('Mobile/Takeout')->get_shops($id);
		// $shop_address = explode(',',$data['shops_info']['postion']);
        if($data['status']==0){$this->error($data['info']);}
		
		// $this->assign('shop_address',$shop_address);
        $this->assign($data);
        $this->assign('is_list',2);
        $this->cart_list($id);
        $this->display();
    }
    public function shops_sevice(){
        $id=I('id');
        if(!$id){$this->error('参数错误');}
        $data=D('Mobile/Takeout')->get_shops($id);
        if($data['status']==0){$this->error($data['info']);}
        $this->assign($data);
        $this->assign('is_list',2);
        $this->cart_list($id);
        $this->display();
    }
    //商品列表页获取商品属性
    public function attr(){
        $return = array();
        $goods_id = I('post.goods_id');
        $shop_id = get_wxsmall_shop_id();
        $return['status'] = 0;
		
        $member_info = checkuserauthtype(1);
        if(empty($member_info)){
            $return['msg'] = '请登录';
            $this->ajaxReturn($return);
        }
        if(!$shop_id){
            $return['msg'] = '库存不足！';
            $this->ajaxReturn($return);
        }
		
        if($goods_id){
            $data = D('Common/Takeout')->get_goods_info($goods_id,$shop_id);
            if(!$data['status']){
                $return['msg'] = $data['info'];
                $this->ajaxReturn($return);
            }
			
                  // echo "<pre>";print_r(json_decode($data['goods_info']['format_spec']['combinationprice']));exit;
            $return['goods_id'] = $goods_id;
            $return['format_market_price'] = $data['goods_info']['format_end_price'];
            $return['combination'] = json_decode($data['goods_info']['format_spec']['combination']);
            $return['combinationnum'] = $data['goods_info']['format_spec']['combinationnum'];
            $return['combinationprice'] = json_decode($data['goods_info']['format_spec']['combinationprice']);
            $return['combinationpricenum'] = $data['goods_info']['format_spec']['combinationpricenum'];
            if($data['goods_info']['format_spec']['newarr']){
                $return['status'] = 1;
                $html = "";
                foreach ($data['goods_info']['format_spec']['newarr'] as $key => $value) {
                    $html.= "<dl rel='".$key."'><dt>".$value['name']."</dt><dd>";
                    foreach ($value['options'] as $k => $v) {
                        $html.= "<span attr='".$v['id']."' ";
                        $html.= "onClick=\"init_choose(this)\" id=\"attr".$v['id']."\">".$v['optionsval']."</span>";
                    }
                    $html.="</dd></dl>";
                }
                $return['html'] = $html; 
				
                $sg_map = array();
                $sg_map['goods_id']=$goods_id;
                $sg_map['shop_id']=$shop_id;
                $sg_info=M('ShopsGoods')->where($sg_map)->getField('id');
                if(empty($sg_info)){$return['is_add'] = 0;}
                else{$return['is_add'] = 1;}
            }else{
                $return['status'] = 2;
                $return['msg'] = '无属性';
            }
        }else{
            $return['msg'] = '数据获取错误';
        }
        $this->ajaxReturn($return);
    }
	//结算
    public function order(){
        if(!MID){
            $this->redirect('Mobile/Login/index');
        }
        $addre_id=I('address_id');
        $ids=I('ids');
        $id=session('main_shop_id');session('jump',1);session('jump_paypass',1);
		if($id){session('ordershop_id',$id);}else{$id=session('ordershop_id');}
        if(!$id){$this->error('参数错误');}
        $data=D('Mobile/Takeout')->get_order($id,$addre_id,$ids);
        if($data['status']==0){$this->error($data['info'],$data['url']);}
        $ids = implode(',',$data['cart_info']['ids']);
        $this->assign('ids',$ids);
        $bonus = D('Common/Takeout')->mcouponsget($id,$data['cart_info']['cart_total_price'],$data['cart_info']['order_total_price']);
        $this->assign('bonus',$bonus);
        $this->assign('tplseo_title','外卖订单_'.C('WEB_SITE_TITLE'));

        $data['address_info']=$data['addr_info']['address_info'];
        $this->assign('dis',$data['addr_info']);
        $map=array();
        $map['id']=MID;
        $user_info=M("Member")->where($map)->field('paypassword')->find();
        //判断是否有支付密码
        if(empty($user_info['paypassword'])){
            $this->assign('userpaypass',1);
        }
        $this->assign($data);
        $this->assign('addres_id',$addre_id);
        $this->display();
    }
    public function count_total(){
        $param=I('param.');$return=array();$return['status']=1;
        $return=D('Mobile/Takeout')->count_order_price($param);
        if($return['status']!=0){
            $return['shop_id']=session('main_shop_id');
            $this->assign($return);
            $return['htmlp'] = $this->fetch('order_cart_ajaxp');
        }
        $this->ajaxReturn($return);
    }
    public function sub_order(){
        $param=I('param.');
        $return=D('Mobile/Takeout')->sub_order($param);
        $this->ajaxReturn($return);
    }
	// 立即领取
    public function get_coupons(){
        $id = I('post.id');
        if($id){
            $return = D('Common/Promot')->member_get_bonus($id);
        }
        $this->ajaxReturn($return);
    }
    //附近门店
    public function nearby_shop_map(){
        $siteinfo=session('siteinfo');
        //会员坐标
        $xpoint=$siteinfo['temp_lng'];
        $ypoint=$siteinfo['temp_lat'];
        $siteinfo['temp_name']=$siteinfo['temp_name']?$siteinfo['temp_name']:$siteinfo['site_name'];
        $this->assign('temp_name',$siteinfo['temp_name']?$siteinfo['temp_name']:'请选择');
        $list_data=D('Takeout')->get_list();
        foreach ($list_data['shops_list']['list'] as $k => $v) {
            if($v['distance']<3000){
                $shop_info[]=$v;
            }
        }
        foreach ($shop_info as $key => $value) {
            $shops_list[$key]['shop_id']=$value['id'];
            $shops_list[$key]['distance']=$value['distance'];
            $shops_list[$key]['name']=$value['name'];
            $shops_list[$key]['address']=$value['address'];
            $shops_list[$key]['xpoint']=$value['xpoint'];
            $shops_list[$key]['ypoint']=$value['ypoint'];
            $shops_list[$key]['tel']=$value['shops_call'];
        }
        $shop_data['list']=getShop(1,100);//获取可配送的门店列表
		if($shop_data['list']){
			$shop_data['count'] = count($shop_data['list']);
		}else{
			session('main_shop_id',null);
			$shop_data['count'] = 0;
		}
        $main_shop_id = session('main_shop_id');//用户选中的门店id
        
		$this->assign('shop_list',$shop_data);
		$this->assign('main_shop_id',$main_shop_id);
        $this->assign('xpoint',$xpoint);
        $this->assign('ypoint',$ypoint);
        $this->assign('shops_list',json_encode($shops_list));
        $this->assign('tplseo_title','附近门店'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','附近门店');
        $this->display();
    }

    /**
     *当前门店导航
     */
    public function get_shop_map(){
        $id=I("id");
        $siteinfo=session('siteinfo');
        //会员坐标
        $mxpoint=$siteinfo['temp_lng'];
        $mypoint=$siteinfo['temp_lat'];
        $siteinfo['temp_name']=$siteinfo['temp_name']?$siteinfo['temp_name']:$siteinfo['site_name'];
        $this->assign('temp_name',$siteinfo['temp_name']?$siteinfo['temp_name']:'请选择');
        $map=array();
        $map['id']=$id;
        $shop_info=M("Shops")->where($map)->field(true)->find();
        $this->assign($shop_info);
        $this->assign("mxpoint",$mxpoint);
        $this->assign("mypoint",$mypoint);
        $this->assign('tplseo_title','附近门店_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title',"附近门店");
        $this->display();
    }
    public function get_list_datas(){
        $postdata = I('param.');
        $return = array('status'=>0,'data'=>'');
        if( !empty($postdata['keyword'])){
            $history_num=10;
            $value=session('search_history');
            krsort($value);
            if(!in_array($postdata['keyword'],$value)){
                if(count($value)>=$history_num)array_shift($value);
                $value[]=$postdata['keyword'];
                $value=array_filter(array_unique($value));
                session("search_history",$value,3600*6);
            }
        }
        $shop_id = get_wxsmall_shop_id();
        $this->assign('shop_id',$shop_id);
        $data=D('Common/Takeout')->get_goods_list($postdata,1);
        if($postdata['childcatid']){$cate_id=$postdata['childcatid'];}elseif($postdata['catid']){
            $cate_id=$postdata['catid'];
        }
        if($cate_id){
            $catename=idtoname($cate_id,'PurchaseCategory','name','id',false);
        }else{
            $catename='分类';
        }
        $this->assign('cate_name',$catename);
        $this->assign('catid',$postdata['catid']);
        if($postdata['keyword']){
            $this->assign('keyword',$postdata['keyword']);
        }

        if($data['g_status']){
            $goods_list = $data['goods_list']['list'];
            if($goods_list){
                $this->assign('goods_data',$goods_list);
                $this->assign('page',$data['page']);
                $this->assign('sum_page',$data['goods_list']['totalPages']);
                $return['status'] = 1;
                $return['page'] = $data['page'];
            }
        }else{
            $goods_list = array();
            $this->assign('page',1);
            $this->assign('goods_data',$goods_list);
        }
        $tpl = C('LIST_TPL');
        $fetch = 'goods_list_yellow';
        $list_html=$this->fetch($fetch);
        $return['data'] = $list_html;
        $this->ajaxReturn($return);
    }
}