<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class AdminmanageController extends Controller {
	/**
	 * @return 管理员管理
	 */
	function index(Req $req) {
		$request=array();
		$request['loginname'] = isset($req->loginname) ? trim($req->loginname) : null;
		$request['role_name'] = isset($req->role_name) ? trim($req->role_name) : null;

		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/manager/adminInfo');
		$dt->setLang('system_user');
		$dt->addColsFields('loginname',array('sort'=>false));
		$dt->addColsFields('role_name',array('sort'=>false));
		$dt->addColsFields('status', array('sort'=>false,'templet' => '#closeTool'));
		$dt->addColsFields('add_time');
	//	$dt->enableCheckBox();
		$dt->addColsFields('operation',array('templet'=>'#opt'));
		// $dt->setToolBar(array('delete'));
		return view('admin.Adminmanage/index', $dt->render($request));
	}

	/**
	 * 关闭管理员接口
	 */
	function GameClose() {
		$status = Request::has('status') ? Request::get('status') : '';
		$loginname = Request::has('loginname') ? Request::get('loginname') : '';
		if (empty($loginname)) {
			return responseToJson(-5030002031);
		}
		if ($status < 0) {
			return responseToJson(-5030002032);
		}

		$db = new \App\Models\System_user;
		$data = array(
			'status' => $status,
		);
		if ($status) {
			$data['status'] = 1;
		} else {
			$data['token'] = '';
			$data['status'] = '0';
		}
		$res = $db->closeGame($data, $loginname);
		return responseToJson($res);
	}

	//修改管理员信息
	function Edit(Req $req) {
		$id = $req->id;
		$list = isset($req->limit) ? $req->limit : 10;
		if (empty($id)) {
			abort(404);
		}
		$db = new \App\Models\System_user;
		$root_db = new \App\Models\System_root;
		if (!$req->isMethod('post')) {
			$data = $db->getAdminInfo($id, 1);
			$root = $root_db->getlist($list);

//			$data['name'] = $root['name'];
			return view('admin.Adminmanage/edit', ['data' => $data, 'root' => $root['data']]);
		} else {
			$loginname = $req->loginname;
			$pwd = $req->password;
			$repwd = $req->repassword;

			$twopwd = $req->password_2;
			$retwopwd = $req->repassword_2;
			$data = array(
				'status' => $req->status,
			);
			if (!empty($req->root_id)) {
				$data['root_id'] = $req->root_id?intval($req->root_id):4;
			}
			if (!empty($pwd)) {
				if ($pwd != $repwd) {
					return responseToJson(-7010000522); //两次密码不一致
				}
				$pwddata = GenPassword($pwd);
				$data['password'] = $pwddata['password'];
				$data['encryption'] = $pwddata['encryption'];
			}
			if (!empty($twopwd)) {
				if ($twopwd != $retwopwd) {
					return responseToJson(-7010000522); //两次密码不一致
				}
				$twopwddata = GenPassword($twopwd);
				$data['password_2'] = $twopwddata['password'];
				$data['encryption_2'] = $twopwddata['encryption'];
			}
			$res = $db->updateInfo($data, $id);
			$log = array(
				session('adminInfo.admin_name'),
				$loginname,
			);
			OperationLog(session('adminInfo.admin_id'), 'editAdmin', $log);
			return responseToJson($res);
		}
	}

	function Add(Req $req) {
		$root_db = new \App\Models\System_root;
		$list = isset($req->limit) ? $req->limit : 10;
		if (!$req->isMethod('post')) {
			$root = $root_db->getlist($list);
			$data['root_id'] = '';
			$data['status'] = 1;
			return view('admin.Adminmanage/add', ['data' => $data, 'root' => $root['data'], 'dis' => 1]);
		} else {
			$loginname = $req->loginname;
			$pwd = $req->password;
			$repwd = $req->repassword;
			$twopwd = $req->password_2;
			$retwopwd = $req->repassword_2;
			$level = $req->root_id?intval($req->root_id):4;
			$status = $req->status;
			if (empty($loginname) || empty($pwd) || empty($twopwd)) {
				return responseToJson(-7010007022); //必填不能为空
			}
			if ($pwd != $repwd || $twopwd != $retwopwd) {
				return responseToJson(-7010000522); //两次密码不一致
			}
			$data['loginname'] = $loginname;
			$data['status'] = $status;
			$data['root_id'] = $level;
			$pwddata = GenPassword($pwd);
			$data['password'] = $pwddata['password'];
			$data['encryption'] = $pwddata['encryption'];
			$twopwddata = GenPassword($pwd);
			$data['password_2'] = $twopwddata['password'];
			$data['encryption_2'] = $twopwddata['encryption'];
			$data['add_time'] = date('Y-m-d H:i:s', time());
			$checkname = \App\Models\System_user::checkAdminname($data['loginname']);
			if($checkname<0){
				return responseToJson($checkname);
			}
			$db = new \App\Models\System_user;
			$res = $db->addAdmin($data);
			$log = array(
				session('adminInfo.admin_name'),
				$loginname,
			);
			OperationLog(session('adminInfo.admin_id'), 'addAdmin', $log);
			return responseToJson($res);

		}

	}

	/**
	 * 批量删除
	 * [delete description]
	 * @return [type] [description]
	 */
	function delete(Req $req) {
		$loginname = '';
		$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]);
				}
			}
		}
		$admin_users = \App\Models\System_user::whereIn('id', $ids)->get();
		$rows = \App\Models\System_user::whereIn('id', $ids)->delete();
		foreach ($admin_users as $key => $value) {
			$loginname.=$value->loginname.',';
		}
		$log = array(
				session('adminInfo.admin_name'),
				$loginname,
			);
		OperationLog(session('adminInfo.admin_id'), 'deleteAdmin', $log);
		if (!$rows) {
			return responseToJson(-2003); //id����
		}
		return responseToJson(1); //id����
	}
	//获取权限代码
	function getRootCode(){
	    $admin_id=session('adminInfo.admin_id');
	    $role_id=\App\Models\System_user::select('root_id')->where('id',$admin_id)->first();
        $role_id=$role_id->toArray();
        $role_id=$role_id['root_id'];
        $data=\App\Models\Priv_roleModel::where('role_id',$role_id)->get();
        return $data->toArray();
//        dump($data->toArray());
    }

}

?>