<?php
/**
 * 地区管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Admin\Controller;
class AreaController extends BaseController {
	public function _initialize(){
		parent::_initialize ();
    }
	//地区菜单列表
	public function index(){
		$pid=I('pid')?I('pid'):1;$ppid=0;
        $map=array();
        $map['pid']=$pid;
        $map['id']=$pid;
        $map['_logic']='or';
        $Area=M('Area')->where($map)->field('id,pid,name')->cache(true)->select();
		foreach($Area as $k => $r) {
		  if($r['id']==$pid)$ppid=$r['pid'];
		  if(empty($r['level']))$r['level']=0;
			$r['id']      = $r['id'];
			$r['name']    = $r['name'];
			$r['status']  = $r['status']==1 ? '<font color="red">√</font>' :'<font color="blue">×</font>';
			$r['submenu'] =  "<a href='".U('add',array('pid'=>$r['id']))."'>添加子菜单</a>";
			$r['next'] =  "<a href='".U('index',array('pid'=>$r['id']))."'>下级子菜单</a>";
			$r['edit']    = $r['level']==0 ? '<font color="#cccccc">修改</font>' : "<a href='".U('edit',array('id'=>$r['id'],'pid'=>$r['pid']))."'>修改</a>";
			$r['del']     = $r['level']==0 ? '<font color="#cccccc">删除</font>' : "<a href=\"javascript:;\" onclick=\"$.showConfirm('" . L('SURE_DELETE') . "',function(){window.location.href='".U('delete',array('id'=>$r['id']))."'},'hc')\">" . L('DELETE') . "</a>";
			$array[]      = $r;
		}
		$str  = "<tr class='tr'>
				    <td ondblclick=editeTable(this,'sort','\$id','sort')>\$sort</td>
				    <td>\$id</td> 
				    <td >\$spacer \$name</td> 
				    <td>\$status</td> 
					<td>
						\$submenu \$next \$edit \$del
					</td>
				  </tr>";
  		$Tree = new \Common\Vendor\Tree();
		$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		$Tree->init($array);
		$html_tree = $Tree->get_tree($ppid, $str);
		$this->assign('html_tree',$html_tree);
		$this->display();
	}
    public function _before_insert(){exit;}
    public function _before_update(){exit;}
    public function updatecache(){
        $map=array();$map['status']=1;
        $Area=M('Area')->where($map)->field('id,pid,name')->cache(true)->select();
		$Tree = new \Common\Vendor\Tree();
        $treemini=$Tree->init($Area);
        $treearray=$Tree->get_tree_json(1);
		$arearegiondata = $this->formatareadata($Area);
		file_put_contents(ROOT_PATH.'/Public/data/arearegiondata.json',json_encode($arearegiondata));
        file_put_contents(ROOT_PATH.'/Public/data/areadata.json',json_encode($treearray));
        $this->success(L('DO_OK'));
    }
	public function formatareadata($data){
		$newarr = array();
		if(empty($data))return $newarr;
		foreach($data as $v){
			if($v['level']<4){//保留省市县三级
				if($v['pid']==1){
					if(!empty($v['region']))$v['region'] = idtoname($v['region'],'Type','name','id');
					$newarr['region'][$v['region']][$v['id']]['i'] = $v['id'];//带地理区域省级分类
					$newarr['region'][$v['region']][$v['id']]['n'] = $v['name'];
					$newarr['general'][$v['pid']][$v['id']]['i'] = $v['id'];//普通省级分类
					$newarr['general'][$v['pid']][$v['id']]['n'] = $v['name'];
				}else{
					$newarr[$v['pid']][$v['id']]['i'] = $v['id'];
					$newarr[$v['pid']][$v['id']]['n'] = $v['name'];	
				}
				if($v['level']==1){
					$newarr['letter'][$v['letter']][$v['id']]['i'] = $v['id'];//带拼音省级分类
					$newarr['letter'][$v['letter']][$v['id']]['n'] = $v['name'];
				}
			}
		}
		return $newarr;
    }
	public function _before_delete(){exit;} 
}