<?php 

/**
 * 名称：首页控制器
 * 作者：荷塘月色
 * 邮箱：kujiw@qq.com
 * 说明：
 */

namespace Admin\Controller;
class IndexController extends CommonController {

	public function index(){
		if (IS_POST) {
			$pid = I('pid',0,'intval');
			$menus = D('Menu','ViewModel') -> where('status=1 and type="admin"') -> order('sort desc') -> select();
			foreach ($menus as $key => $value) {
				$menus[$key]['url'] = empty($value['module'])?'':U($value['module'].'/'.$value['controller'].'/'.$value['action'],$value['param']);
			}
			$menus = data2subtree($menus,$pid);
			$this -> ajaxReturn($menus);
		}elseif (IS_GET) {
			$this -> username = session('username');
			$this -> display();
		}
	}

	// 前端缓存数据
	public function asyncdata(){
		if (IS_POST) {
			$cate = M('Datacate') -> where(array('status'=>array('eq',1))) -> getField('id,field');
			$data = M('Datadict') -> where(array('status'=>array('eq',1),'category_id'=>array('in',array_keys($cate)))) -> getField('id,text,pid,value,category_id,instruction,sort,status');
			$tmp = array();
			foreach ($data as $key => $value) {
				$d = array();
				$d['id'] = (string)$value['value'];
				$d['pid'] = isset($data[$value['pid']])?(string)$data[$value['pid']]['value']:'';
				$d['text'] = $value['text'];
				$d['instruction'] = $value['instruction'];
				$tmp[$cate[$value['category_id']]][$value['value']] = $d;
			}
			$this -> ajaxReturn($tmp);
		}
	}

	public function runtime(){
		if (IS_POST) {
			deldir(RUNTIME_PATH);
			$this -> success('成功清理系统缓存！');
		}
	}
}