<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;

/**
 *
 */
class AdminRoleController extends Controller {
	/**
	 * @return 管理员角色管理
	 */
	function index(Req $req) {
		$id = $req->id;
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/AdminRole/getRole?id=' . $id);
		$dt->setLang('system_user');
		$dt->addColsFields('id');
		$dt->addColsFields('admin_name', array('sort' => false)); //管理员名称
		$dt->addColsFields('role_name', array('sort' => false)); //角色名
		$dt->enableCheckBox();
		$dt->setPriKey('id');
		// $dt->setToolBar(array('priv_role'), array('width' => '200'));
		return view('admin.AdminRole/index', $dt->render());
	}
	function getRole(Req $req) {
		$list = intval($req->list) ? $req->list : '';
		$admin_id = $req->id;
		$admin_name = $req->admin_name;
		$role_name = $req->role_name;
		$where = array();
		if (!empty($admin_id)) {
			$where[] = array('admin_id', '=', $admin_id);
		}
		if (!empty($admin_name)) {
			$where[] = array('admin_name', '=', $admin_name);
		}
		if (!empty($role_name)) {
			$where[] = array('role_name', '=', $role_name);
		}
//		print_r($where);
//	    $filds=$req-
		$db = new \App\Models\Role;
		$data = $db->getRole($where, $list);
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);

	}
	function getEditColsModel() {
		return 'System_root';
	}
	function view(Req $req) {
		return $this->Edit($req);
	}

	//修改权限信息
	function Edit(Req $req) {
		$id = $req->id;
		if (empty($id)) {
			abort(404);
		}
		$root_db = new \App\Models\System_root;
		$menu = new \App\Models\NewMenu;
		if (!$req->isMethod('post')) {
			$root = $root_db->getRootDetails($id, 1);
			$menudata = $menu->getMenuAll();
			return view('admin.Adminroot/view', ['data' => $root, 'menu' => $menudata]);
		} else {
			$level = empty($req->level) ? '' : $req->level;
			$rootname = empty($req->rootname) ? '' : $req->rootname;
			$max_money = empty($req->max_money) ? '' : $req->max_money;
			if (empty($level) || empty($rootname) || empty($max_money)) {
				return responseToJson(-7010900102); //未提交修改信息
			}
			$data = array(
				'level' => $level,
				'name' => $rootname,
				'settingmoney' => $max_money,
			);
			$res = $root_db->editRoot($id, $data);
			return responseToJson($res);

		}
	}
	function getlist(Req $req) {
		$list = intval($req->limit) > 0 ? $req->limit : 10;
		$db = new \App\Models\System_root;
		$data = $db->getlist($list);
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

	/**
	 * 批量删除
	 * [delete description]
	 * @return [type] [description]
	 */
	function delete(Req $req) {
		$id = $req->input('id');
		if (empty($id)) {
			return responseToJson(-2001); //id����Ϊ��
		}
		$ids = explode(',', $id);
		if (!is_array($ids) && intval($ids) < 0) {
			return responseToJson(-2002); //id����
		}
		if (is_array($ids) && count($ids) > 0) {
			foreach ($ids as $k => $v) {
				if (intval($v) < 1) {
					unset($ids[$k]);
				}
			}
		}
		// echo '敬请期待';die;
		$rows = \App\Models\Role::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id����
		}
		return responseToJson(1); //id����
	}

	//添加角色
	function addRole(Req $req){
        $id = $req->id;
        if(intval($id)<0){
            abort(404);
        }
        $admin_db=new \App\Models\System_user;
        $role_db=new \App\Models\Role;
        $system_role=new \App\Models\System_root;
        $data=$role_db->gethasRole($id);
        $role_data=$system_role->getAllRole();
        foreach ($role_data as $k=>$v){
            if(in_array($v['id'],$data)){
                unset($role_data[$k]);
            }
        }
        $admin_info=$admin_db->getAdminInfo($id,1);
        if (!$req->isMethod('post')) {
            return view('admin.AdminRole/addRole',['admin'=>$admin_info,'data'=>$role_data]);
        }else{
            $role_id=$req->role_id;
            if(intval($role_id)<0){
                return responseToJson(-7082000322);//请选择添加角色
            }
            $role_info=$system_role->getInfo($role_id,1);
            if($role_info<0){
                return responseToJson($role_info);//请选择添加角色
            }
            $inset_data['role_id']=$role_info['id'];
            $inset_data['role_name']=$role_info['name'];
            $inset_data['admin_id']=$id;
            $inset_data['admin_name']=$admin_info['loginname'];
            $res=$role_db->addRole($inset_data);
            if($res<0){
                return responseToJson($res);
            }
            return responseToJson(1);
        }
    }
    //获取管理员该管理员所有权限的数组
    function getRoot(){
	    $admin_id=session('adminInfo.admin_id');
	    //获取所有角色的id
        $role_db=new \App\Models\Role;
        $role_ids=$role_db->gethasRole($admin_id);
        if($role_ids<0){
            return $role_ids;
        }
        //检验是否是超级管理员
        $root_db=new \App\Models\System_root;
        $res=$root_db->checkSupper($role_ids);
        if($res>0){
            return 1;
        }
        //获取权限代码ID
        $code_role_db=new \App\Models\Priv_roleModel;
        $code_ids=$code_role_db->getAllCode($role_ids);
        if($code_ids<0){
            return $code_ids;
        }
        $code_ids=array_unique($code_ids);
        //获取权限代码
        $code_db=new \App\Models\Priv_codeModel;
        $code_data=$code_db->getCode($code_ids);
        if($code_data<0){
            return $code_data;
        }
        return $code_data;

    }

}

?>