<?php

	/***************************************************************\
	|																|
	|	房哨（专业的房产运营ERP管理系统）								|
	|																|
	|	© 2019 http://fs.ytsofts.cn/ All rights reserved.			|
	|																|
	|	本系统由淮南市银泰软件科技有限公司提供技术支持					|
	|																|
	|	QQ号：984784483												|
	|																|
	\***************************************************************/

	namespace Erpadmin\Controller;
	use Think\Controller;
	class MenuController extends ErpadminbaseController {

		public function index(){
			$result = M('Menu')->where()->order(array("listorder" => "ASC"))->select();
			$tree 	= new \Org\Util\Tree();
			$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
			$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
			$newmenus=array();
			foreach ($result as $m){
				$newmenus[$m['id']]=$m;
			}
			foreach ($result as $n=> $r) {
				$result[$n]['levels'] = $this->_get_level($r['id'], $newmenus);
				$result[$n]['parentid_node'] = ($r['parentid']) ? ' class="child-of-node-' . $r['parentid'] . '"' : '';
				$result[$n]['style'] = empty($r['parentid']) ? '' : 'display:none;';
				if ($result[$n]['levels']<2) {
					$result[$n]['str_manage'] = '<a class="layui-btn layui-btn-normal layui-btn-xs"  onclick="layer_show(\'添加\',\''.U('add',array('parentid'=>$r['id'])).'\',\'600\',\'400\')" > 添加</a>';
				}
				$result[$n]['str_manage'] .='<a class="layui-btn layui-btn-warm layui-btn-xs"  onclick="layer_show(\'修改\',\''.U('edit',array('id'=>$r['id'])).'\',\'600\',\'400\')" > 修改</a>';
				$result[$n]['status'] = $r['status'] ? "显示" : "隐藏"; 
				if ($r['type']==1) {
					$result[$n]['types'] = '菜单';
				}elseif ($r['type']==2) {
					$result[$n]['types'] = '菜单+节点';
				}elseif ($r['type']==3) {
					$result[$n]['types'] = '外部链接';
				}
			}
			$tree->init($result);
			$str = "<tr id='node-\$id' \$parentid_node style='\$style'>
				<td style='padding-left:20px;'>\$id</td>
				<td>\$spacer\$listorder-\$name</td>
				<td>\$node_id</td>
				<td>\$types</td>
				<td>\$status</td>
				<td>\$str_manage</td>
			</tr>";
			$categorys = $tree->get_tree(0, $str);
			$this->assign("categorys", $categorys);
			$this->display(); 
		}

		/**
			* 获取递归数组深度
			* @param $id
			* @param $array
			* @param $i
		*/

		protected function _get_level($id, $array = array(), $i = 0) {
			if ($array[$id]['parentid']==0 || empty($array[$array[$id]['parentid']]) || $array[$id]['parentid']==$id){
				return  $i;
			}else{
				$i++;
				return $this->_get_level($array[$id]['parentid'],$array,$i);
			}
		}

		public function add(){
			if (IS_POST) {
				$data['name'] = I('post.name');
				$data['parentid'] = I('post.parentid');
				$data['listorder'] = I('post.listorder');
				$data['app'] = I('post.app');
				$data['model'] = I('post.model');
				$data['action'] = I('post.action');
				$data['data'] = I('post.data');
				$data['remark'] = I('post.remark');
				$data['status'] = I('post.status');
				$data['icon'] = I('post.icon');
				$data['type'] = I('post.type');
				$data['node_id'] = I('post.node_id');
				$data['blank'] = I('post.blank');
				$level = M('Menu')->where(array('gongsiid'=>session("gongsiid"),'id'=>$data['parentid']))->getField('level');
				$data['level']=$level+1;//重要的等级 
				$isok = M('Menu')->add($data);
				if ($isok) {
					$this->success('添加成功!');
				}else{
					$this->error('添加失败!');
				}
			}else{
				$tree = new \Org\Util\Tree();
				$parentid = I("get.parentid",0,'intval');
				$result = M('Menu')->where()->order(array("listorder" => "ASC"))->select();
				foreach ($result as $r) {
					$r['selected'] = $r['id'] == $parentid ? 'selected' : '';
					$r['disabled'] = $r['level'] == 3 ? 'disabled' : '';
					$array[] = $r;
				}
				$str = "<option value='\$id' \$selected \$disabled>\$spacer \$name</option>";
				$tree->init($array);
				$select_categorys = $tree->get_tree(0, $str);
				$this->assign("select_categorys", $select_categorys);
				$this->display();
			}
		}

		public function edit(){
			if (IS_POST) {
				$id = I("post.id",0,'intval');
				$data['name'] = I('post.name');
				$data['parentid'] = I('post.parentid');
				$data['listorder'] = I('post.listorder');
				$data['app'] = I('post.app');
				$data['model'] = I('post.model');
				$data['action'] = I('post.action');
				$data['status'] = I('post.status');
				$data['data'] = I('post.data');
				$data['remark'] = I('post.remark');
				$data['icon'] = I('post.icon');
				$data['type'] = I('post.type');
				$data['node_id'] = I('post.node_id');
				$data['blank'] = I('post.blank');
				$level = M('Menu')->where(array('id'=>$data['parentid']))->getField('level');
				$data['level']=$level+1;//重要的等级 
				$isok = M('Menu')->where(array('id'=>$id))->save($data);
				if ($isok) {
					$this->success('添加成功!');
				}else{
					$this->error('添加失败!');
				}
			}else{
				$tree = new \Org\Util\Tree();
				$id = I("get.id",0,'intval');
				$this->menu = M('Menu')->where(array('id'=>$id))->find();
				$parentid = $this->menu['parentid'];
				$result = M('Menu')->where()->order(array("listorder" => "ASC"))->select();
				foreach ($result as $r) {
					$r['selected'] = $r['id'] == $parentid ? 'selected' : '';
					$r['disabled'] = $r['level'] == 3 ? 'disabled' : '';
					$array[] = $r;
				}
				$str = "<option value='\$id' \$selected \$disabled>\$spacer \$name</option>";
				$tree->init($array);
				$select_categorys = $tree->get_tree(0, $str);
				$this->assign("select_categorys", $select_categorys);
				$this->display();
			}
		}

	}