<?php
/**
 * 类别管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class TypeController extends BaseController{
    public function _initialize() {
        parent::_initialize();  //RBAC 验证接口初始化
    }
	//菜单列表
	public function index(){		
		$Node = $this->gettyle();
		$array = array();
		// 构建生成树中所需的数据
		foreach($Node as $k => $r) {
		  if(empty($r['level']))$r['level']=0;
			$r['id']      = $r['id'];
			$r['name']    = $r['name'];
			$r['temp']    = $r['temp'];
			$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['edit']    = $r['level']==1 ? '<font color="#cccccc">修改</font>' : "<a href='".
U('edit',array('pid'=>$r['pid'],'id'=>$r['id']))."'>修改</a>";
			$r['del']     = $r['level']==1 ? '<font color="#cccccc">删除</font>' : "<a href=\"javascript:;\" onclick=\"$.showConfirm('" . L('SURE_DELETE') . "',function(){window.location.href='".U('delete',array('id'=>$r['id']))."'},'hc')\">删除</a>";
			$array[]      = $r;
		}
		$str  = "<tr class='tr'>
				    <td><input type='text' value='\$sort' size='3' name='sort[\$id]'></td>
				    <td>\$id</td> 
				    <td >\$spacer \$name</td> 
				    <td>\$temp</td> 
				    <td>\$status</td> 
					<td>
						\$submenu \$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(0, $str);
		$this->assign('html_tree',$html_tree);
		$this->display();
	}//添加菜单
	public function add(){		
			$Node = $this->gettyle();
			$pid = I('pid','intval',0);	//选择子菜单
			$array = array();
			foreach($Node as $k => $r) {
				$r['id']         = $r['id'];
				$r['name']       = $r['name'];
				$array[$r['id']] = $r;
			}
			$str  = "<option value='\$id' \$selected \$disabled >\$spacer \$name</option>";
			$Tree =new \Common\Vendor\Tree();
			$Tree->init($array);
			$select_categorys = $Tree->get_tree(0, $str, $pid);
			$this->assign('tpltitle','添加');
			$this->assign('select_categorys',$select_categorys);
			$this->display('edit');
	}
	public function edit(){
			$id = I('id','intval',0);
			$pid = I('pid','intval',0);	//选择子菜单
			if(!$id || !$pid)$this->error('参数错误!');            
			if($id==1)$this->error('根结点不允许编辑!');
			$allNode = $this->gettyle();
			$array = array();
			foreach($allNode as $k => $r) {
			     if($r['id']==$id)$info=$r;
				$r['id']         = $r['id'];
				$r['name']       = $r['name'];
				$array[$r['id']] = $r;
			}
			$str  = "<option value='\$id' \$selected \$disabled >\$spacer \$name</option>";
			$Tree = new \Common\Vendor\Tree();
			$Tree->init($array);
			$select_categorys = $Tree->get_tree(0, $str, $pid);
			$this->assign('tpltitle','编辑');
			$this->assign('select_categorys',$select_categorys);
			$this->assign($info);
			$this->display();
	}
    private function gettyle(){
        return M('Type')->where(1)->field(true)->select();
    }
}