<?php
// +----------------------------------------------------------------------
// | 首页控制器
// +----------------------------------------------------------------------
// | ThinkOAO （Online And Offline）
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.hanchao9999.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: chang <13331202938@126.com>
// +----------------------------------------------------------------------
namespace Mobile\Controller;
class ShopsTakeoutController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        if (!checksiteauth('MenDian,WaiMai')) {
            exit;
        }
    }
	public function change_geo(){
		$param=I('param.');
		$geo=session('geo');
		$address_list = D('Common/Member')->get_address_list();
        //echo "<pre>";print_r($address_list);exit;
		$siteinfo=session('siteinfo')?session('siteinfo'):cookie('siteinfo');
		$areasite=D('Common/Area')->get_index_area_cache();
		$this->assign($areasite);
		$this->assign($geo);
		$this->assign($address_list);
		$this->assign($param);
		$this->assign('tplseo_title', '选择地址' . '_' . C('WEB_SITE_TITLE'));
	    $this->assign('page_title','选择地址');
	    $this->display();
	}
	public function nearby(){
		$param=I('param.');
		$this->assign($param);
		$param['shop_cate_id']=!empty($param['coid'])?$param['coid']:$param['cid'];
		$data=D('Common/Shops')->get_shops_list(1,1,$param,2);
		// dump($data);exit;
		$this->assign($data);
		$list_html=$this->fetch('location_list');
		$this->assign('list_html',$list_html);
		$this->get_first_cat();
		$this->assign('tplseo_title', '附近商家' . '_' . C('WEB_SITE_TITLE'));
		$geo=session('geo');
		$siteinfo=session('siteinfo')?session('siteinfo'):cookie('siteinfo');
		if(empty($geo['temp_name']) && empty($siteinfo['temp_name'])) $page_title='请选择地址';
		elseif($siteinfo['temp_name']) $page_title=$siteinfo['temp_name'];
		else $page_title=$geo['temp_name'];
		$this->assign('page_title',$page_title);
		$this->display();
	}
	public function ajax_shops_list(){
		$param=I('param.');
		$this->assign($param);
		// $param['shop_cate_id']=!empty($param['coid'])?$param['coid']:$param['cid'];
		// $data=D('Common/Shops')->get_shops_list(1,1,$param,2);
		$data=D('Common/Shops')->get_shops_list();
		$return=array();
		$return['status']=0;
		if(empty($data['list'])){
			$return['status']=0;
			$return['info']='没有数据了';
		}else{
			$this->assign($data);
			$return['status']=1;
			$list_html=$this->fetch('shops_list');
			$return['html']=$list_html;
		}
		$this->ajaxReturn($return);
    }
	public function get_first_cat(){
		$param=I('param.');
		$where['status']=1;
        		$where['pid']=0;
        		$order='is_index desc';
       	 	//获取分类
        		$header_cate=M('ShopsCategory')->where($where)->order($order)->field(true)->limit(4)->select();
        		foreach ($header_cate as $k => $val) {
        			if($param['cid']==$val['id']){
        				$new_header_cate[$val['id']]['currentname']=$val['name'];
        				$new_header_cate[$val['id']]['current']=1;
        			}
        			$new_header_cate[$val['id']]['name']=$val['name'];
        			$new_header_cate[$val['id']]['id']=$val['id'];
        		}
        		$this->assign('header_cate',$new_header_cate);	
	}
	public function set_siteinfo(){
        $param=I('param.');
        $result = D('Common/Area')->geocoding($param,1);//改变地区信息`
        if($result['result']['addressComponent']['city']){
            $Cashgeo['temp_name'] =$param['name']?$param['name']: $result['result']['pois']['0']['addr'];
            $Cashgeo['province'] = idtoname($result['result']['addressComponent']['province'],'Area','id','name');
            $Cashgeo['city_id'] = idtoname($result['result']['addressComponent']['city'],'Area','id','name');
            $Cashgeo['district'] = idtoname($result['result']['addressComponent']['district'],'Area','id','name');
            $Cashgeo['temp_lat'] = $param['ypoint'];
            $Cashgeo['temp_lng'] = $param['xpoint'];
            $Cashgeo['city'] = $result['result']['addressComponent']['city'];
            session('siteinfo',$Cashgeo);
            cookie('siteinfo',$Cashgeo);
            session('loc',1);
        }else{
           // $return['msg']=json_encode($result);
        }
        session('loc',1);
        save_session();
//		$info=session('siteinfo')?session('siteinfo'):cookie('siteinfo');
//		$info['temp_lat']=$param['ypoint'];
//		$info['temp_lng']=$param['xpoint'];
//		$info['temp_name']=$param['name'];
//		$info['type']=$param['name'];
//		session('siteinfo',$info);
//		cookie('siteinfo',$info);
		$geo_info['xpoint']=$param['xpoint'];
		$geo_info['ypoint']=$param['ypoint'];
		$geo_info['temp_name']=$param['name'];
		session('geo',$geo_info);
		cookie('geo',$geo_info);
		$return['status']=1;
		if($param['type']==1){
			$return['url']=U('Takeout/index');
		}
		if($param['shop_id']){
			$return['url']=U('Takeout/shops_index',array('id'=>$param['shop_id']));
		}
		if($param['type']==2){
			$return['url']=U('ShopsTakeout/nearby');	
		}elseif($param['type']==3){
			$return['url']=U('Index/index');
		}elseif($param['type']==4){
			$return['url']=U('Shop/mendian_index',array('id'=>$param['shop_id']));
		}
		$this->ajaxReturn($return);
	}
	public function change_address(){
		$map = $return = array();
		$return['status'] = 0;
		$return['msg'] = '地址选择失败！';
		$address_id = I('param.id');
		$memberinfo = checkuserauthtype();
		if ($memberinfo) {
			$uid = $memberinfo['id'];
			$map['uid'] = $uid;
		}else{
			$return['msg'] = '请登录！';
		}
		$map['id'] = $address_id;
		$address_info = M('MemberAddress')->where($map)->field(true)->find();
        $member_info['province'] = $address_info['province'];
        $member_info['city_id'] = $address_info['city_id'];
        $member_info['district'] = $address_info['district'];
        $member_info['temp_lng'] = $address_info['xpoint'];
		$member_info['temp_lat'] = $address_info['ypoint'];
		$member_info['temp_name'] = $address_info['address'];
		$member_info['city'] = idtoname($address_info['city'],'Area','name');
        session('siteinfo',$member_info);
        cookie('siteinfo',$member_info);
        session('loc',1);
        save_session();
		if($address_info){
			$return['status'] = 1;
			$return['url'] = U('Mobile/Index/index');
		}
		$this->ajaxReturn($return);
	}
}