<?php
/** App登录控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <xiaoq@570035208.com>
 */

namespace Wxsmall\Controller;
class IndexController extends BaseController {
	public function _initialize(){
        parent::_initialize ();
    }
    public function index(){
        $return=D('Index')->get_index();
        $this->ajaxReturn($return);
    }

    /**
     * 获取外卖分类和筛选
     */
    public function get_category(){
        $return['status']=1;
        GLOBAL $PurchaseCategory;
        if(empty($PurchaseCategory))$PurchaseCategory=getcache('PurchaseCategory');
        $PCategory=array();
        foreach($PurchaseCategory as $v){$PCategory[$v['pid']][]=$v;}
        $cate = $PCategory[0];
        if (empty($cate)) {
            $return['status'] = 0;
        } else {
            foreach ($cate as $k => $v) {
                $category[$k] = $v;
                $child = count($PCategory[$v['id']]);
                if ($child > 0) {
                    $category[$k]['child'] = $child;
                    $fir_cat_child = $PCategory[$v['id']];
                    foreach ($fir_cat_child as $ke => $va) {
                        $category[$k]['child_cate'][0] = array('id' => 0, 'name' => '全部');
                        $category[$k]['child_cate'][] = $va;
                        $sec_cat_child = $PCategory[$va['id']];
                        foreach ($sec_cat_child as $key => $val) {
                            $category[$k]['child_cate'][]['child_cate'][$key] = $this->format_cat_item($val);
                        }
                    }
                } else {
                    $category[$k]['child'] = 0;
                }
            }
        }
        $temp = C("LIST_TPL_WX");
        if ($temp == "shops_index_blue") {
            $category_info = array();
            foreach ($category as $key => $val) {
                $category_info[0]['id'] = "0";
                $category_info[0]['name'] = "店铺推荐";
                $category_info[] = $val;
            }
            $return['data']['cate_list'] = $category_info;
        } else {
            $return['data']['cate_list'] = $category;
        }
        $return['data']['screen_list'] = D("Takeout")->get_other(2);
        $return['data']['order_list'] = D("Takeout")->get_other(1);
        $return['data']['selfnum'] = C('WEB_SELFGO_NUM');
        $this->ajaxReturn($return);
    }
    public function format_cat_item($data=array()){
        $return=array();
        $return['id']=$data['id'];
        $return['name']=$data['name'];
        $return['pid']=$data['pid'];
        $return['type_id']=$data['type_id'];
        $return['img']=thumb($data['img'],350,350,'noimg_186x186.png');
        $return['url']=geturl('Goods/index',array('cat_id'=>$data['id']));
        $return['integralurl']=geturl('Integral/index',array('cat_id'=>$data['id'],'is_integral'=>1));
        return $return;
    }
/*    public function get_vindex(){
        $return=array('status'=>1);
       $shops_info=session('site_info');
        if($shops_info['end_time']<NOW_TIME){
            $return['end_status']=1;
            $this->ajaxReturn($return);
        }
       // $user_info=$this->checkuserauth();
        /**
         * 5-28 uu  $rank

        $postdata=I('param.');
        $rank =$postdata['uu'];
         $map['is_default'] = 1;
        $map['is_wxsmall'] = 1;
        $content = M('Dragedit')->where($map)->getField('value');
        $time= M('Dragedit')->where($map)->cache('tpl_mobile_default1')->getField('update_time');
        if($content){
            $flist = D('Common/Dragedit')->formatdiylist($content,'','',$rank);
        }else{
            $flist='{}';
        }
        $flist=json_decode($flist,true);
        foreach($flist as $key=>$val){
            //格式化为模板通用链接
            if($val['content']['nav_list']||$val['content']['poster_list']){
                if(isset($val['content']['nav_list'])){
                    $sonlist = $val['content']['nav_list'];
                    $stype = 'nav_list';
                }else if(isset($val['content']['poster_list'])){
                    $sonlist = $val['content']['poster_list'];
                    $stype = 'poster_list';
                }
                foreach($sonlist as $k=>$v){
                    $map=array();
                    $map['id']=$v['link']['url_navtype'];
                    $urlrules=M("UrlRules")->where($map)->field('theurl,module,app_mark')->find();
                    $jumtopurl = $jumtop_param = array();
                    if($v['link']['is_param']==1){
                        if($urlrules['app_mark']=='GoodsCategory'){
                            $jumtop_param[]='cate_id';
                            $jumtop_param[]=$v['link']['url_modulename'];
                        }
                        if($urlrules['app_mark']=='PromotAct'){
                            $jumtop_param[]='act_id';
                            $jumtop_param[]=$v['link']['url_modulename'];
                        }
                        if($urlrules['app_mark']=='PromotSale'){
                            $jumtop_param[]='act_id';
                            $jumtop_param[]=$v['link']['url_modulename'];
                        }
                    }else {
                        if($urlrules['app_mark']=='Goods' || $urlrules['app_mark']=='ArticleCat'|| $urlrules['app_mark']=='Article'  ){
                            $jumtop_param[]='id';
                            $jumtop_param[]=$v['link']['url_modulename'];
                        }elseif($urlrules['app_mark']=='GoodsCategory'){
                            $jumtop_param[]='cate_id';
                            $jumtop_param[]=$v['link']['url_modulename'];
                        }else {
                            $jumtopurl = explode('/', $urlrules['theurl']);
                            $count = count($jumtopurl);
                            if ($count > 2) {
                                $jumtop_param[] = $jumtopurl[3];
                                $jumtop_param[] = $jumtopurl[4];
                            }
                        }
                    }
                    $flist[$key]['content'][$stype][$k]['app_mark']=$urlrules['app_mark'];
                    $flist[$key]['content'][$stype][$k]['str']=$jumtop_param;
                    if($val['data_type']==6 || $val['data_type']==7) {
                        $img=format_url($v['img']);
                        $img=str_replace('https','http',$img);
                        $flist[$key]['content'][$stype][$k]['img'] =$img;
                    }
                }
            }
        }
        $list=D("App/InvoicingOrder")->arr_page($flist,$postdata['pagefrom'],4);

        $return['data']['HCinitlist']=$list['attr'];
        $return['data']['temp_id']=C("LIST_TPL_WX");
        $return['data']['pagefrom']=intval($list['pagefrom']);
        $return['data']['time']=$time;
        $return['data']['title_name']=C("WEB_SITE_NAME");
        //$return['HCshop'] = $user_info['HCshop'];//当前会员匹配的门店存储到客户端
        $this->ajaxReturn($return);
    }*/


    public function get_vindex(){
        $return=array('status'=>1);
        $postdata=I('param.');
        $rank =$postdata['uu'];
        $map['is_default'] = 1;
        $map['is_wxsmall'] = 1;
        $content = M('Dragedit')->where($map)->getField('value');
        $time= M('Dragedit')->where($map)->getField('update_time');
        if($content){
            $flist = D('Common/Dragedit')->formatdiylist($content,'','',$rank);
        }else{
            $flist='{}';
        }
        $flist=json_decode($flist,true);
        foreach($flist as $key=>$val){
            //格式化为模板通用链接
            if($val['content']['nav_list']||$val['content']['poster_list']){
                if(isset($val['content']['nav_list'])){
                    $sonlist = $val['content']['nav_list'];
                    $stype = 'nav_list';
                }else if(isset($val['content']['poster_list'])){
                    $sonlist = $val['content']['poster_list'];
                    $stype = 'poster_list';
                }
                foreach($sonlist as $k=>$v){
                    $map=array();
                    $map['id']=$v['link']['url_navtype'];
                    $urlrules=M("UrlRules")->where($map)->field('theurl,module,app_mark')->find();
                    $jumtopurl = $jumtop_param = array();
                    if($v['link']['is_param']==1){
                        if($urlrules['app_mark']=='GoodsCategory'){
                            $jumtop_param[]='cate_id';
                            $jumtop_param[]=$v['link']['url_modulename'];
                        }
                        if($urlrules['app_mark']=='MiniProgram'){
                            $jumtop_param[]='';
                            $jumtop_param[]=$v['url_param'];
                        }
                        if($urlrules['app_mark']=='PromotAct'){
                            $jumtop_param[]='act_id';
                            $jumtop_param[]=$v['link']['url_modulename'];
                        }
                        if($urlrules['app_mark']=='PromotSale'){
                            $jumtop_param[]='act_id';
                            $jumtop_param[]=$v['link']['url_modulename'];
                        }
                    }else {
                        if($urlrules['app_mark']=='Goods' || $urlrules['app_mark']=='ArticleCat'|| $urlrules['app_mark']=='Article'  ){
                            $jumtop_param[]='id';
                            $jumtop_param[]=$v['link']['url_modulename'];
                        }elseif($urlrules['app_mark']=='GoodsCategory'){
                            $jumtop_param[]='cate_id';
                            $jumtop_param[]=$v['link']['url_modulename'];
                        }else {
                            $jumtopurl = explode('/', $urlrules['theurl']);
                            $count = count($jumtopurl);
                            if ($count > 2) {
                                $jumtop_param[] = $jumtopurl[3];
                                $jumtop_param[] = $jumtopurl[4];
                            }
                        }
                    }
                    $flist[$key]['content'][$stype][$k]['app_mark']=$urlrules['app_mark'];
                    $flist[$key]['content'][$stype][$k]['str']=$jumtop_param;
                    if($val['data_type']==6 || $val['data_type']==7) {
                        $img=format_url($v['img']);
                        $img=str_replace('https','http',$img);
                        $flist[$key]['content'][$stype][$k]['img'] =$img;
                    }
                }
            }
        }
        $list=D("App/InvoicingOrder")->arr_page($flist,$postdata['pagefrom'],4);
        $return['stepnowtime']=$postdata['stepnowtime'];
        $return['data']['HCinitlist']=$list['attr'];
        $return['data']['temp_id']=C("LIST_TPL_WX");
        $return['data']['pagefrom']=intval($list['pagefrom']);
        $return['data']['time']=$time;
        $return['data']['title_name']=C("WEB_SITE_NAME");
        //$return['HCshop'] = $user_info['HCshop'];//当前会员匹配的门店存储到客户端
        $this->ajaxReturn($return);
    }
    public function get_shop_name(){
        $return=array('status'=>3,'msg'=>'参数错误');
        $shop_id=I('shop_id');
        if(empty($shop_id))$this->ajaxReturn($return);
        $shop_name=idtoname($shop_id,'Shops','name','id',false);
        if($shop_name){
            $return['data']['shop_name']=$shop_name;
            $return['data']['web_site_name']=getconfig('WEB_SITE_NAME');
            $return['status']=1;
        }
        $this->ajaxReturn($return);
    }

    /**拼团列表页
     * @return array
     */
    public function get_fight_group(){
        //  $user_info=$this->checkuserauth();
        //  $rand = $user_info['info']['rank_id'];
        $return=array('status'=>2,'msg'=>'没有更多数据');
        $param=I('param.');
        /* 
            6-29 
        */
        $rank=$param['uu'];
        $map=array();
        if(!empty($param['catid'])){
            $map['cat_id']=$param['catid'];
        }
        $goodslist=D('Common/PromotExt')->get_fight_list($map,'','',$param['page'],'',5,$rank);
        $return['data']['info']=$goodslist;
        $where = array();
        $where['pid']=0;
        $where['status']=1;
        $cat_list=M('PurchaseCategory')->where($where)->field('id,name')->order('sort,id desc')->select();
        $categroy=array();
        foreach($cat_list as $key=>$val){
            $categroy[0]['id']=0;
            $categroy[0]['name']="全部";
            $categroy[]=$val;
        }
        $return['data']['cat_list']=$categroy;
        $return['data']['title_name']=C("WEB_SITE_NAME");
        $return['data']['temp_id']=C("LIST_TPL_WX");
        if(empty($goodslist['list'])){
            $this->ajaxReturn($return);
        }
        foreach($goodslist['list'] as $key=>$val){
            $goodslist['list'][$key]['remarks']=format_decimal($val['remarks']);
            $img=format_url($val['img']);
            $img=str_replace('https','http',$img);
            $goodslist['list'][$key]['img']=$img;
        }
        $return['data']['list']=$goodslist['list'];

        $return['status']=1;
        $this->ajaxReturn($return);
    }
    // 地址列表
    public function address(){
        $getdata = I('param.');
        $result = D('Member')->address($getdata);
        $this->ajaxReturn($result);
    }
    // 根据市获取坐标
    public function geocoding(){
        $getdata = I('param.');
        $result = D('Common/Area')->geocoding($getdata,2);
        $this->ajaxReturn($result);
    }
}