<?php
/**
 * @author xiaogg QQ:756663992
 * @copyright 2017
 */
namespace Admin\Controller;
class IndexController extends BaseController {
    public function index(){
        $this->menurole=D('AuthRule')->getRuleArray();
        $this->menu($this->menurole);
        $param=I('param.site_url');
        if(empty($param)){
            $url=U("Public/main");
        }else{
            $fulldomain = 'http://' . $_SERVER['HTTP_HOST'] . '/admin.php?s=/'.$param;
            //$url= U($param);
            $url=$fulldomain;
        }
        $this->assign('url',$url);
        //检测弹窗计划任务是否启用
        $map=array();
        $map['status'] = 1;
        $map['classname'] ='Popup';
        $check=M('Cron')->where($map)->count();
        //所有店铺
       // $list=M('Shops')->field('id,name')->select();
       // $this->assign('list',$list);
        //是否是超管
        $is_root=is_administrator(UID);
        $this->assign('is_root',$is_root);
		$this->assign('checkPopup', intval($check));
        $this->display();
    }
	private function menu($ruleData=''){
		$menu_sname = 'menucache' . $this->admin_info['id'];
		$menus=S($menu_sname);
        if(isset($_REQUEST['tag'])){
            $id = intval($_REQUEST['tag']);
        }

        if(empty($id)){$id = intval(S($menu_sname.'id'));}
		if (empty($menus)) {
			$ruleData =empty($ruleData)? D('AuthRule')->getRuleArray():$ruleData;
			$doweixin = array();$i=0;
            foreach($ruleData as $onekey=>$onev){
                if(empty($id) && $i==0)$id=$onekey;
                $menu=array();
    			foreach ($ruleData[$onekey]['child'] as $key => $action) {    			 
    				if ($action['child']) {$menu[$key]['navs'] = $this->getpos($action['child']);}
    				$menu[$key]['name'] = $action['title'];
					$menu[$key]['id'] = $action['id'];
					$menu[$key]['icon'] = urldecode($action['icon']);
    				++$i;
    			}
                $menus[$onekey]=$menu;
            }S($menu_sname,$menus,60*60*24);S($menu_sname.'id',$id,60*60*24);
		}
		$this->assign('menus', $menus);
		$this->assign('id', $id);
	}
	private function getpos($val) {
		$allruleData = getcache('AuthRule');
		foreach ($val as $k => $v) {
			$tags = $this->getallpos($v['id'], $allruleData);
			$val[$k]['current'] = trim(substr($tags, 0, -2));
			$val[$k]['name'] = str_replace('allrules','index',$v['name']);
		}
		return $val;
	}
	protected function getallpos($tag, $allruleData) {
		if (!$tag) {
			return false;
		}
		$tag = $this->getallpos($allruleData[$tag]['pid'], $allruleData) . $allruleData[$tag]['title'] . ' > ';
		return $tag;
	}
	private function check_shop_num(){
		$map=array();
		$map['site_id'] = C('appid');
		$map['status'] = 1;
		$shop_max_num = M('SiteInfo')->where($map)->getField('shop_max_num');
		
		$map=array();
		$map['id'] = array('gt',0);
		$shop_num = M('Shops')->where($map)->count();
		if($shop_max_num > 0 && $shop_num == 0){
			$ret = D('App/ShopLogin')->addparentid(C('appid'),1,'1111');
			if($ret){
				$url='http://'.$_SERVER['HTTP_HOST'].'/admin.php?s=/Shops/first_index';
				// $url = U('Shops/first_index',array('is_content'=>7,'topid'=>229,'leftid'=>112));
				$this->success('您还没添加仓库',$url);
			}
		}
	}
}
?>