<?php
/**
 * 导航管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class NavController extends BaseController
{
    public function index(){
        $map['device']=0;
        $map['navtype']=array('neq',4);
        $data = I('param.');$order = '';
        if(!empty($data['_order'])&&!empty($data['_field'])){$order = $data['_field']." ".$data['_order'].',';}
        $order .= 'sort asc';
        $nodeData = M('Nav')->where($map)->field(true)->order($order)->select();
	
        $Tree = new \Common\Vendor\Tree();
        $typearr = strtoarray(c('NAV_SETTING'));
        $Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        foreach ($nodeData as &$values) {
            $values['str_manage'] = '<a href="' . U('add', array('id' => $values['id'])) . '" class="operate">添加子菜单</a>&nbsp;&nbsp;<a class="operate" href="' . U('edit', array('id' => $values['id'])) . '">编辑</a>&nbsp;&nbsp;<a class="operate" href="javascript:;" onclick="$.showConfirm(\'' . L('SURE_DELETE') . '\',function(){window.location.href=\'' . U('delete', array('id' => $values['id'])) . '\'},\'hc\')">删除</a>';
            $values['nodetype'] = $typearr[$values['navtype']];
            $values['status'] = '<td onclick="status('.$values['id'].',\'status\')" id="status_'.$values['id'].'"><img src="Public/images/status_'.$values['status'].'.gif" /></td>';
        }
        $Tree->init($nodeData);
        $str = "<tr>
                <td><input value='\$id' name='id[]' type='checkbox' ></td>
	    <td>\$id</td>
		<td ondblclick=editeTable(this,'sort','\$id','sort')>\$sort</td>
		<td style='text-align:left;padding-left:10px;' title='\$name'>\$spacer\$name</td>
        <td>\$nodetype</td>
        \$status
		<td>\$str_manage</td>
		</tr>";
        $nodeString = $Tree->get_tree(0, $str);
        $this->assign('nodeString', $nodeString);
        $this->display();
    }
    public function mobileindex(){
        $map['device']=1;
        $data = I('param.');$order = '';
        if(!empty($data['_order'])&&!empty($data['_field'])){$order = $data['_field']." ".$data['_order'].',';}
        $order .= 'sort asc';
        $nodeData = M('Nav')->where($map)->field(true)->order($order)->select();
        $Tree = new \Common\Vendor\Tree();
        $typearr = strtoarray(c('NAV_SETTING'));
        $Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        foreach ($nodeData as &$values) {
            $values['str_manage'] = '<a href="' . U('add', array('id' => $values['id'],'device'=>1)) . '" class="operate">添加子菜单</a>&nbsp;&nbsp;<a class="operate" href="' . U('edit', array('id' => $values['id'],'type'=>'1','device'=>1)) . '">编辑</a>&nbsp;&nbsp;<a class="operate" href="javascript:;" onclick="$.showConfirm(\'' . L('SURE_DELETE') . '\',function(){window.location.href=\'' . U('delete', array('id' => $values['id'])) . '\'},\'hc\')">删除</a>';
            $values['nodetype'] = $typearr[$values['navtype']];
            $values['status'] = '<td onclick="status('.$values['id'].',\'status\')" id="status_'.$values['id'].'"><img src="Public/images/status_'.$values['status'].'.gif" /></td>'; 
        }
        $Tree->init($nodeData);
        $str = "<tr>
                 <td><input value='\$id' name='id[]' type='checkbox' ></td>
	    <td>\$id</td>
		<td ondblclick=editeTable(this,'sort','\$id','sort')>\$sort</td>
		<td style='text-align:left;padding-left:10px;' title='\$name'>\$spacer\$name</td>
        <td>\$nodetype</td>
        \$status          
		<td>\$str_manage</td>
		</tr>";
        $nodeString = $Tree->get_tree(0, $str);
        $this->assign('nodeString', $nodeString);
        $this->display();
    }

    /* 添加数据 */
    public function add()
    {
		
        $moudle = M('Nav');
        if (IS_POST) {
			
            $data=$moudle->create();
			$result = $moudle->add();
            savecache(CONTROLLER_NAME);
            $this->jumpresult($result);
        } else {
            $this->getNodeTree(I('id'));
            if(I('device')){$this->assign('device',I('device'));}
			$this->assign('type',I('type'));
            $this->display('edit');
        }
    }

    /* 编辑数据 */
    public function edit()
    {
		
        $moudle = D('Nav');
        if (IS_POST) {
            $data=$moudle->create();
			//print_r($data);exit;
            $result = $moudle->save();
            savecache(CONTROLLER_NAME);
            $this->jumpresult($result);
            $post_edit = I('post.');
            $id = $post_edit['id'] ? $post_edit['id'] : '';
            $category = $moudle->where('id=' . $id)->select();
            $cid = $category[0]['cid'];
            if ($cid) {
                $show_status = $post_edit['show_status'] ? $post_edit['show_status'] : '';
                $show['show_in_nav'] = $show_status;
                $goods_category = M('goods_category');
                $goods_category->where('id=' . $cid)->save($show);
            }
        } else {
            $info = $moudle->getById(I('id'));
            $info ? $this->assign($info) : $this->error(L('PLSEASE_SELECT_ROWS'));
            $this->assign('node', explode('/', $info['name']));
			$this->assign('type',I('type'));
            $this->getNodeTree($info['pid']);
            $this->display();
        }
    }

    /* 得到节点树 */
    private function getNodeTree($pid = '')
    {
        $map['navtype']=array('neq',4);
        $nodeData = M('Nav')->where($map)->field(true)->order('sort')->select();
        if ($nodeData) {
            $Tree = new \Common\Vendor\Tree();
            foreach ($nodeData as &$values) {
                $values['selected'] = $values['id'] == $pid ? 'selected="selected"' : '';
            }
            $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
            $Tree->init($nodeData);
            $parent_node = $Tree->get_tree(0, $str);
            $this->assign('navsetting', strtoarray(C('NAV_SETTING')));
            $this->assign('parent_node', $parent_node);
        }
    }
	public function _before_delete(){
		$id=I('id');
		if(isset($id)&&is_array($id)){
			foreach($id as $k=>$v){
				$result=check_delete($v,7,CONTROLLER_NAME);
				if(!$result['status'])unset($id[$k]);
			}
			if(empty($id)){$this->error($result['info']);exit;}
		}else{
			$result=check_delete($id,7,CONTROLLER_NAME);
			if(!$result['status']){$this->error($result['info']);exit;}
		}
		$this->id=$id;
	}
}
?>