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

namespace Uapp\Controller;
class IndexController extends BaseController {
	public function _initialize(){
        parent::_initialize ();
    }
    public function index(){
        $return=array('status'=>1,'msg'=>'');
        $data = D("Mobile/Index")->index();
        $floor_info = $data['floorinfo'];
        $floor_info_format = $articleformat = $navlistformat = array();
        foreach ($floor_info as $key => $val) {
            $floor_info[$key]['cat_name']=idtoname($val['bind_cateid'],'GoodsCategory');
            $floor_info[$key]['goods']=array();
            foreach ($data['index_list'] as $k => $v) {
                if(empty($val['recommend_cate'])){
                    $val['recommend_cate']=$val['bind_cateid'];
                }
                if($k==$val['recommend_cate']){
                    $floor_info[$key]['goods']=$v['list'];
                }
            }
            foreach($floor_info[$key]['goods'] as $kg=>$vg){
                $floor_info[$key]['goods'][$kg]['img']=thumb($vg['img'],500,500);
            }
        }
        //限时秒杀
        $promat_list=get_promot_goods(10);
        $end_time=$promat_list[0]['promote_end_time'];
        foreach($promat_list as $key=>$val){
            $promat_list[$key]['img']=thumb($val['img'],500,500);
            $promat_list[$key]['promote_price']=format_price($val['promote_price']);
            $promat_list[$key]['shop_price']=format_price($val['shop_price']);
        }
        //网站公告
        $article=article_list(2,8,'addtime desc');
        foreach($article['list'] as $key=>$val){
            $articleformat[$key]['title']=str_cut($val['title'],30,'');
            $articleformat[$key]['id']=$val['id'];
        }
        //导航
        $navlist = nav_list(5, 0, 'list');
        foreach($navlist as $k=>$v){
            $navlistformat[$k]['icon']=thumb($v['icon'],72,72,true);
            $navlistformat[$k]['name']=$v['name'];
            if(str_exists($v['url'],'http://')||str_exists($v['url'],'https://')){
                $navlistformat[$k]['urldata']['type'] = 'http';
                $navlistformat[$k]['urldata']['url'] = $v['url'];
                continue;
            }
            if(str_exists($v['url'],'url:')) {
                $arr = explode(':', $v['url']);
                $urlrules = M("UrlRules")->where('id=' . $arr[1])->find();
                if (empty($urlrules)) continue;
                $is_url = array();
                $is_url[]='module='.$urlrules['module'];
                if ($urlrules['is_module'] == 1 && !empty($arr[2])) {
                    //$is_url[]=$urlrules['urlparam'].'='.$arr[2];
                    $is_url[] = 'id=' . $arr[2];//转化为app统一的id
                }
                if ($urlrules['is_param'] == 1 && !empty($arr[3])) {
                    $is_url[] = $arr[3];//app只接收name和keyword，其它标识不解析
                }
                $isurl=implode('&',$is_url);//将数组拼接成字符串
                $navlistformat[$k]['urldata']['type'] = 'appurl';
                $navlistformat[$k]['urldata']['url'] = $isurl;
            }
        }
        $return['data']['floor_info']=$floor_info;
        $return['data']['navlist']=$navlistformat;
        $return['data']['promat_list']=$promat_list;
        $return['data']['article']=$articleformat;
        $return['data']['promote_end_time']=$end_time;
        $this->ajaxReturn($return);
    }
    public function get_vindex(){
        $return=array('status'=>1);
        $param = I('param.');
        // 7-5 
        if($param['uid']){
                   $ma['id'] = $param['uid'];
             $rankdata=M('Member')->where($ma)->field('rank_id')->find();
             $rank=$rankdata['rank_id']; 
         }else{
             $rank=0;
         }
        //
        $map['is_default'] = 1;
        $map['is_wxsmall'] = 2;
        if($param['id']&&$param['key']){
            //验证预览模板是否合法
            if(base64_encode(C('HANCHAO_KEY').$param['id'])==$param['key']){
                $map = array();
                $map['id'] = $param['id'];
            }
        }
        $content = M('Dragedit')->where($map)->cache('tpl_mobile_default2')->getField('value');
        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;
                    }
                }
            }
        }
        foreach ($flist as $key => $value) {
            if($value['data_type']==4 && $value['content']['goodslist'] && $value['content']['layout']==2){
                foreach ($value['content']['goodslist'] as $k => $v) {
                    $flist[$key]['content']['goodslist'][$k]['img']=$v['best_img'];
                }
            }
            // if($value['data_type']==4 && $value['content']['layout']==5)$flist[$key]['content']['layout']=1;
        }
        // echo "<pre/>";print_r($flist);exit();
        $return['HCinitlist']=$flist;
        $main_shop_id = get_wxsmall_shop_id();//用户选中的门店id
        $return['shop_list']=$shop_data;
        $return['main_shop_id']=$main_shop_id;
        $this->ajaxReturn($return);
    }
    public function getvindex(){
        $return=array('status'=>1);
        $param = I('param.');
        $map['is_default'] = 1;
        $map['is_wxsmall'] = 2;
        if($param['id']&&$param['key']){
            //验证预览模板是否合法
            if(base64_encode(C('HANCHAO_KEY').$param['id'])==$param['key']){
                $map = array();
                $map['id'] = $param['id'];
            }
        }
        $content = M('Dragedit')->where($map)->cache('tpl_mobile_default')->getField('value');
        if($content){
            $flist = D('Common/Dragedit')->formatdiylist($content);
        }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;
                    }
                }
            }
        }
        foreach ($flist as $key => $value) {
            if($value['data_type']==4 && $value['content']['goodslist']){
                foreach ($value['content']['goodslist'] as $k => $v) {
                    $flist[$key]['content']['goodslist'][$k]['img']=$v['best_img'];
                }
            }
        }
        echo "<pre/>";print_r($flist);exit();
        $return['HCinitlist']=$flist;
        $main_shop_id = get_wxsmall_shop_id();//用户选中的门店id
        $return['shop_list']=$shop_data;
        $return['main_shop_id']=$main_shop_id;
        $this->ajaxReturn($return);
    }
    public function adv_info(){
        $return=array('status'=>1);
        $array=array(
            array(
                '13',
            ),
            array(
                '14',
            ),array(
                '15',
            ),array(
                '16',
            ),
        );
        $adv_info=array();
        // foreach($array as $key=>$val){
        //     $adv_info[$val[0]]=showadsapp($val[0]);
        // }
        if($adv_info){
            $return['data']=$adv_info;
        }else{
            $return['status']=2;
            $return['msg']='未获取到数据';
        }
        $this->ajaxReturn($return);
    }

    /**
     * 获取外卖分类和筛选
     */
    public function get_category(){
        $param=I('param.');
        $return['status']=1;
        $site_id=C('appid');
            $map = array();
            $map['id'] = array('gt', 0);
            $map['status'] = 1;
            $map['pid'] = 0;
            $cate = M('PurchaseCategory')->where($map)->field('id,name')->order('id asc')->cache(true)->select();
            if (empty($cate)) {
                $return['status'] = 0;
            } else {
                foreach ($cate as $k => $v) {
                    $category[$k] = $v;
                    $fir_child_map = array();
                    $fir_child_map['pid'] = $v['id'];
                    $fir_child_map['status'] = 1;
                    $child = M('PurchaseCategory')->where($fir_child_map)->count();
                    if ($child > 0) {
                        $category[$k]['child'] = $child;
                        $fir_cat_child = M('PurchaseCategory')->field('id,name')->where($fir_child_map)->select();
                        foreach ($fir_cat_child as $ke => $va) {
                            $category[$k]['child_cate'][0] = array('id' => 0, 'name' => '全部');
                            $category[$k]['child_cate'][] = $va;
                            $sec_child_map = array();
                            $sec_child_map['pid'] = $va['id'];
                            $sec_child_map['status'] = 1;
                            $sec_cat_child = M('PurchaseCategory')->field('id,name')->where($sec_child_map)->select();
                            foreach ($sec_cat_child as $key => $val) {
                                $category[$k]['child_cate'][]['child_cate'][$key] = $this->format_cat_item($val);
                            }
                        }
                    } else {
                        $category[$k]['child'] = 0;
                    }
                }
            }
            //xyy 1.30
            $temp = C("LIST_TPL_APP");
            //xyy 1.30
            if ($temp == "shops_index_green") {
                $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("Wxsmall/Takeout")->get_other(2);
        $return['data']['order_list'] = D("Wxsmall/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 nearby(){
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $param['lng']=$param['xpoint'];
        $param['lat']=$param['ypoint'];
        $result = D('Wxsmall/Nearby')->get_shops_list($param);
        $this->ajaxReturn($result);
    }
    /**拼团列表页
     * @return array
     */
    public function get_fight_group(){
        $return=array('status'=>2,'msg'=>'没有更多数据');
        $param=I('param.');
        $map=array();
        if(!empty($param['catid'])){
            $map['cat_id']=$param['catid'];
        }
        // rank  7-5  
        if($param['uid']){
            $rankdata = M('Member')->where(array('id'=>$param['uid']))->field('rank_id')->find();
            $rank = $rankdata['rank_id'];
        }else{
            $rank=0;
        }
        $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['data']['web_goods_page']=C('WEB_GOODS_PAGE');
        $return['status']=1;
        $this->ajaxReturn($return);
    }
}