<?php
namespace Smi\Controller;
use Think\Controller;
header("Content-type:text/html;charset=utf-8");
class MenuController extends BaseController 
{
	protected $business_id;

    public function __construct()
    {
        parent::__construct();

        $this->business_id = session('admin_id');
    }

	/**
	 * 添加菜单
	 */
	public function add()
	{
		$data = I('post.');

		if (!empty($data) && !empty($data['title'])) {
			$add = array();
			if ($data['level'] == 1) {// 添加一级节点
				// 检测url合法性
				$preg_arr = array();
				$preg = '/^\/[a-zA-Z]*\/[a-zA-Z]*$/';
				preg_match($preg, $data['url'], $preg_arr);
				if (empty($preg_arr)) {
					alertMsg('url不合法，例：/Smi/Menu');die;
				}

				$auth_name = split('/', trim($data['url'], '/'));
				$add['auth_name'] = strtolower($auth_name[1]).'_node';
				$add['pid'] = 0;
				$add['level'] = 1;
			} elseif ($data['level'] == 2) {// 添加二级节点
				// 检测url合法性
				$preg_arr = array();
				$preg = '/^\/[a-zA-Z]*\/[a-zA-Z]*\/[a-z_A-Z]*$/';
				preg_match($preg, $data['url'], $preg_arr);
				if (empty($preg_arr)) {
					alertMsg('url不合法，例：/Smi/Menu/add');die;
				}

				$auth_name = split('/', trim($data['url'], '/'));
				$add['auth_name'] = strtolower($auth_name[1]).'_'.$auth_name[2].'_node';
				$add['pid'] = $data['pid_1'];
				$add['level'] = 2;
			} elseif ($data['level'] == 3) {// 添加三级节点
				// 检测url合法性
				$preg_arr = array();
				$preg = '/^\/[a-zA-Z]*\/[a-zA-Z]*\/[a-z_A-Z]*$/';
				preg_match($preg, $data['url'], $preg_arr);
				if (empty($preg_arr)) {
					alertMsg('url不合法，例：/Smi/Menu/add');die;
				}

				$auth_name = split('/', trim($data['url'], '/'));
				$add['auth_name'] = strtolower($auth_name[1]).'_'.$auth_name[2].'_node';
				$add['pid'] = $data['pid_2'];
				$add['level'] = 3;
			}
			
			$add['remark'] = $data['remark'];
			$add['status'] = $data['status'];
			$add['url'] = $data['url'];
			$add['title'] = $data['title'];
			$add['create_time'] = time();

			$result = M('node')->add($add);

			if ($result) {
				$this->success('添加权限节点成功');die;
			} else {
				$this->error('添加权限节点失败');die;
			}
		}

		$p_1_list = M('node')->where(array('pid' => 0, 'level' => 1))->select();// 一级节点
		$p_2_list = M('node')->where('pid <> 0 AND level = 2')->select();// 二级节点

		$this->assign('p_1_list', $p_1_list);
		$this->assign('p_2_list', $p_2_list);
		$this->assign('op', '添加');
		$this->display();
	}

	/**
	 * 编辑菜单
	 */
	public function edit(‘$id)
	{
		$data = I('post.');

		if (!empty($data) && !empty($data['title'])) {
			$add = array();
			if ($data['level'] == 1) {// 添加一级节点
				// 检测是否跨节点编辑
				$node = M('node')->where(array('id' => $data['id']))->find();
				if ($node['level'] != 1) {
					alertMsg('不能跨节点编辑！');die;
				}

				// 检测url合法性
				$preg_arr = array();
				$preg = '/^\/[a-zA-Z]*\/[a-zA-Z]*$/';
				preg_match($preg, $data['url'], $preg_arr);
				if (empty($preg_arr)) {
					alertMsg('url不合法，例：/Smi/Menu');die;
				}

				$auth_name = split('/', trim($data['url'], '/'));
				$save['auth_name'] = strtolower($auth_name[1]).'_node';
				$save['pid'] = 0;
				$save['level'] = 1;
			} elseif ($data['level'] == 2) {// 添加二级节点
				// 检测是否跨节点编辑
				$node = M('node')->where(array('id' => $data['id']))->find();
				if ($node['level'] != 2) {
					alertMsg('不能跨节点编辑！');die;
				}

				// 检测url合法性
				$preg_arr = array();
				$preg = '/^\/[a-zA-Z]*\/[a-zA-Z]*\/[a-z_A-Z]*$/';
				preg_match($preg, $data['url'], $preg_arr);
				if (empty($preg_arr)) {
					alertMsg('url不合法，例：/Smi/Menu/add');die;
				}

				$auth_name = split('/', trim($data['url'], '/'));
				$save['auth_name'] = strtolower($auth_name[1]).'_'.$auth_name[2].'_node';
				$save['pid'] = $data['pid_1'];
				$save['level'] = 2;
			} elseif ($data['level'] == 3) {// 添加三级节点
				// 检测是否跨节点编辑
				$node = M('node')->where(array('id' => $data['id']))->find();
				if ($node['level'] != 3) {
					alertMsg('不能跨节点编辑！');die;
				}

				// 检测url合法性
				$preg_arr = array();
				$preg = '/^\/[a-zA-Z]*\/[a-zA-Z]*\/[a-z_A-Z]*$/';
				preg_match($preg, $data['url'], $preg_arr);
				if (empty($preg_arr)) {
					alertMsg('url不合法，例：/Smi/Menu/add');die;
				}

				$auth_name = split('/', trim($data['url'], '/'));
				$save['auth_name'] = strtolower($auth_name[1]).'_'.$auth_name[2].'_node';
				$save['pid'] = $data['pid_2'];
				$save['level'] = 3;
			}
			
			$save['remark'] = $data['remark'];
			$save['status'] = $data['status'];
			$save['url'] = $data['url'];
			$save['title'] = $data['title'];

			$result = M('node')->where(array('id' => $data['id']))->save($save);

			if ($result) {
				$this->success('编辑权限节点成功');die;
			} else {
				$this->error('编辑权限节点失败');die;
			}
		}

		$p_1_list = M('node')->where(array('pid' => 0, 'level' => 1))->select();// 一级节点
		$p_2_list = M('node')->where('pid <> 0 AND level = 2')->select();// 二级节点

		$node = M('node')->where(array('id' => $id))->find();// 当前节点

		switch ($node['level']) {
			case 1:
				# code...
				break;
			
			case 2:
				$this->assign('pid_1', $node['pid']);
				break;

			case 3:
				foreach ($p_2_list as $key => $value) {
					if ($value['id'] == $node['pid']) {
						$this->assign('pid_1', $value['pid']);
						break;
					}
				}
				$this->assign('pid_2', $node['pid']);
				break;
			
			default:
				# code...
				break;
		}

		$this->assign('p_1_list', $p_1_list);
		$this->assign('p_2_list', $p_2_list);
		$this->assign('node', $node);
		$this->assign('op', '编辑');
		$this->display('add');
	}

	/**
	 * 菜单列表
	 */
	public function menu_list()
	{
		$node_1_list = M('node')->where(array('pid' => 0, 'level' => 1))->select();// 一级节点
		$node_2_list = M('node')->where('pid <> 0 AND level = 2')->select();// 二级节点
		$node_3_list = M('node')->where('pid <> 0 AND level = 3')->select();// 二级节点

		if (!empty($node_1_list)) {
			foreach ($node_1_list as $key => &$value) {
				$value['menu_list'] = array();
				if (!empty($node_2_list)) {
					foreach ($node_2_list as $k => $v) {
						$v['menu_list'] = array();
						if ($v['pid'] == $value['id']) {
							if (!empty($node_3_list)) {
								foreach ($node_3_list as $kk => $vv) {
									if ($vv['pid'] == $v['id']) {
										$v['menu_list'][] = $vv;
									}
								}
							}
							$value['menu_list'][] = $v;
						}
					}
				}
			}
		}

		$this->assign('list', $node_1_list);
		$this->display();
	}

	/**
	 * 删除节点
	 */
	public function delete($id)
	{
		$node = M('node')->where(array('id' => $id))->find();// 当前节点

		// 一二级节点检测是否存在子节点
		if ($node['level'] < 3) {
			$is_has = M('node')->where(array('pid' => $node['id']))->find();
			if (!empty($is_has)) {
				alertMsg('该节点存在子节点，无法删除！');die;
			}
		}

		$result = M('node')->where(array('id' => $id))->delete();
		if ($result) {
			$this->success('删除节点成功');
		} else {
			alertMsg('删除节点失败!');
		}
	}
}