<?
class menu
{
	private $ddadmin;
	private $duoduo;
	function __construct($ddadmin, $duoduo)
	{
		$this->ddadmin = $ddadmin;
		$this->duoduo = $duoduo;
	}

	function get_menu()
	{

		$menu_id = $this->duoduo->select('menu as a,menu_access as b', 'a.id', 'a.`mod`="webset" and a.`act`="center" and a.id=b.menu_id and b.role_id="' . $this->ddadmin['role_id'] . '"');
		if (!$menu_id) {
			$menu = $this->duoduo->select('menu as a,menu_access as b', 'a.`mod`,a.`act`', 'a.id=b.menu_id and b.role_id="' . $this->ddadmin['role_id'] . '" and hide=0 and a.act<>"" and a.mod<>"" order by a.id asc');
			$_GET['go_mod'] = $menu['mod'];
			$_GET['go_act'] = $menu['act'];
		} else {
			$_GET['go_mod'] = 'webset';
			$_GET['go_act'] = 'center';
		}
		$res['index']['mod'] = $_GET['go_mod'];
		$res['index']['act'] = $_GET['go_act'];
		$sql = "select a.* from " . BIAOTOU . "menu as a," . BIAOTOU . "menu_access as b where a.id=b.menu_id and b.role_id='" . $this->ddadmin['role_id'] . "' and a.hide=0 and a.mod<>'fun' order by a.parent_id asc,a.listorder desc,a.sort desc,a.id asc";

		$query = $this->duoduo->query($sql);
		while ($row = $this->duoduo->fetch_array($query)) {
			$menu = [];
			if ($row['parent_id'] == 0) {
				$menu['title'] = $row['title'];
				$menu['icon'] = $row['icon'] ? "layui-icon " . $row['icon'] : '';
				$menu['href'] = "";
				$menu['target'] = "_self";
				$menu['node'] = $row['node'];
				$menu_arr[] = $menu;
			} else {
				//获取当前父菜单索引
				foreach ($menu_arr as $key => $value) {
					if ($value['node'] == $row['node'])
						$index = $key;
				}

				$menu['title'] = $row['title'];
				$menu['icon'] = $row['icon'] ? "layui-icon " . $row['icon'] : '';
				$menu['href'] = $row['url'] ? $row['url'] : u($row['mod'], $row['act']);
				$menu['target'] = "_self";
				$menu_arr[$index]['child'][] = $menu;
			}
		}

		$res['data'] = $menu_arr;


		return $res;
	}

	public function qzmenu($id = 0)
	{

		$arr = $this->duoduo->select_all('menu', 'id,title,`parent_id`,node,`mod`,`act`,listorder', '1="1" order by parent_id asc,listorder desc,`mod` desc');
		foreach ($arr as $a) {
			$menu = array();
			if ($a['parent_id'] == 0) {
				$menu['title'] = $a['title'];
				$menu['spread'] = 0;
				$menu['node'] = $a['node'];
				$menu['id'] = $a['id'];

				// $checkid=$this->duoduo->select("menu_access","id","role_id=".$id." and menu_id=".$a['id']);
				// if ($checkid>0){
				// 	 $menu['checked']=true;
				// }
				$menus[] = $menu;
				// $menus[$a['id']]['children']=array();
				// $menus[$a['id']]['title']=$a['title'];
				// $menus[$a['id']]['id']=$a['id'];
				// $menus[$a['id']]['node']=$a['node'];
			} else {

				foreach ($menus as $key => $value) {
					if ($value['node'] == $a['node'])
						$index = $key;
				}

				$menu['title'] = $a['title'];
				$menu['spread'] = 1;
				$menu['id'] = $a['id'];
				$menu['parent_id'] = $a['parent_id'];
				$checkid = $this->duoduo->select("menu_access", "id", "role_id=" . $id . " and menu_id=" . $a['id']);
				if ($checkid > 0) {

					$menu['checked'] = true;
				}
				$menus[$index]['children'][] = $menu;
			}
		}


		return $menus;
	}

	// public function get_menu_arr(){
	//     $arr=$this->duoduo->select_all('menu','id,title,`parent_id`,listorder','parent_id=0  order by listorder desc,id desc,`mod` desc');

	// }
}
