<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class AdminrootController extends Controller {
	/**
	 * @return 管理员权限管理
	 */
	function index(Req $req) {
		$request=array();
		$request['level'] = isset($req->level) ? trim($req->level) : null;
		$request['name'] = isset($req->name) ? trim($req->name) : null;
		$request['money_small'] = isset($req->money_small) ? trim($req->money_small) : null;
		$request['money_big'] = isset($req->money_big) ? trim($req->money_big) : null;
		
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/Adminroot/getlist');
		$dt->setLang('system_user');
		$dt->addColsFields('id');
		$dt->addColsFields('name', array('edit' => true, 'sort' => false, 'width' => '250')); //角色名称
		$dt->addColsFields('level', array('edit' => true)); //权限等级
		$dt->addColsFields('settingmoney', array('edit' => true, 'width' => '250')); //最大操作金额
		$dt->addColsFields('priv_role', array('templet' => '#waterTool', 'sort' => false, 'width' => '350'));
		$dt->enableCheckBox();
		$dt->setPriKey('id');
		// $dt->setToolBar(array('priv_role'), array('width' => '200'));
		return view('admin.Adminroot/index', $dt->render($request));
	}
	function getEditColsModel() {
		return 'System_root';
	}
	function view(Req $req) {
		return $this->Edit($req);
	}

	//修改权限信息
	function Edit(Req $req) {
		$id = $req->id;
		if ($id < 1) {
			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);
			 $log = array(
                session('adminInfo.admin_name'),
                $rootname, 
                $level,
                $max_money
            );
            OperationLog(session('adminInfo.admin_id'), 'EditqxsystemAdd', $log);
			return responseToJson($res);

		}
	}
	function getlist(Req $req) {
		$list = intval($req->limit) > 0 ? $req->limit : 10;
		$page = !empty($req->page) ? $req->page : 1;
		$field = !empty($req->field) ? $req->field : 'id';
		$order = !empty($req->order) ? $req->order : 'asc';

		$name = !empty($req->name) ? $req->name : '';
		$level = $req->level;
		$money_small = !empty($req->money_small) ? $req->money_small : '';
		$money_big = !empty($req->money_big) ? $req->money_big : '';

		$where = array();
		if (!empty($name)) {
			$where[] = array('name', 'like', "%" . $name . "%");
		}
		if ($level === 0 || ($level != '')) {
			$where[] = array('level', '=', $level);
		}

		if (!empty($money_small)) {
			$where[] = array('settingmoney', '>=', $money_small);
		}
		if (!empty($money_big)) {
			$where[] = array('settingmoney', '<=', $money_big);
		}

		$db = new \App\Models\System_root;
		$data = $db->getRootlist($where, 1, $list, $field, $order);
		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]);
				}
			}
		}
		$rows = \App\Models\System_root::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id����
		}
		return responseToJson(1); //id����
	}

	//修改权限信息
	/*function Edit(Req $req) {
		$id = $req->id;
		if (empty($id)) {
			abort(404);
		}
		$root_db = new \App\Model\System_root;
		$menu = new \App\Model\NewMenu;
		if (!$req->isMethod('post')) {
			$root = $root_db->getRootDetails($id, 1);
			$menudata = $menu->getMenuAll();
			return view('admin.SystemManage/editroot', ['data' => $root, 'menu' => $menudata]);
		} else {
			$pwd = $req->password;
			$data = array(
				'status' => $req->status,
			);
			if (!empty($pwd)) {
				$pwddata = GenPassword($pwd);
				$data['password'] = $pwddata['password'];
				$data['encryption'] = $pwddata['encryption'];
			}

		}
	}*/
}

?>