<?php
namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\Admin as AdminModel;
use app\common\model\Menu as MenuModel;

use app\common\validate\Admin as AdminValidate;
use think\exception\ValidateException;

class Admin extends BaseController
{
	
	public function adminList()
	{
		return view('admin/adminlist');
	}
	
	public function adminForm()
	{
		$id = input('id/d, 0');
		$admin = AdminModel::findOrEmpty($id);
		$menulist = MenuModel::where(['mtype'=>'admin','status'=>1])->order(['pid'=>'asc','priority'=>'asc','id'=>'asc'])->select()->toArray();
		$powerarr = array();
		$poweridsarr = ($admin->powerids==null||empty($admin->powerids)) ? [] : explode(",", $admin->powerids);
		if (!empty($menulist)){
			foreach ($menulist as $k=>$v){
				if ($v['pid']==0){
					$v['checked'] = false;
					$v['children'] = array();
					$v['spread'] = true;
					$powerarr[$v['id']] = $v;
				}else {
					$v['checked'] = in_array($v['id'], $poweridsarr);
					$powerarr[$v['pid']]['children'][] = $v;
				}
			}
		}
		return view('admin/adminform',[
			'admin'		=> $admin,
			'powerarr'	=> json_encode( array_values($powerarr) )
		]);
	}
	
	public function editAdmin()
	{
		$id = input('id/d');
		$vdata = array(
			'id'		=> $id,
			'adminname' => input('adminname/s'),
			'realname'  => input('realname/s'),
			'mobile'    => input('mobile/s')
		);
		try {
			validate(AdminValidate::class)->check($vdata);
		} catch (ValidateException $e) {
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> $e->getError()
			)));
		}
		$password = input('password/s');
		$role = input('role/d', 2);
		$powerids = input('powerids/s', "");
		if($role==1){
			$idsarr = MenuModel::where(['mtype'=>'admin','status'=>1])->order(['pid'=>'asc','priority'=>'asc','id'=>'asc'])->column('id');
			$powerids = implode(",", $idsarr);
		}
		$data = [
			'role'		=> $role,
			'adminname' => input('adminname/s', ""),
			'realname'  => input('realname/s', ""),
			'mobile'    => input('mobile/s', ""),
			'status'	=> input('status/d')==1 ? 1 : 2,
			'powerids'	=> $powerids,
			'remark'	=> input('remark', "")
		];
		if (empty($id)){
			$data['password'] 	= empty($password) ? md5("123456789") : md5($password);
			$data['joindate'] 	= time();
			$data['joinip'] 	= $_SERVER['SERVER_ADDR'];
			$data['lastdate'] 	= time();
			$data['lastip'] 	= $_SERVER['SERVER_ADDR'];
			$admin = AdminModel::create($data);
		}else{
			if (!empty($password)){
				$data['password'] = md5($password);
			}
			$admin = AdminModel::find($id);
			$admin->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	// 删除管理员
	public function delAdmin()
	{
		$access_admin = session('access_admin');
		$password = input('password');
		if ( $access_admin['password'] !== md5($password) ){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "操作密码验证失败"
			)));
		}
		$idarr = input('idarr/a');
		$admin = AdminModel::where('role',2)->whereIn('id',$idarr)->select();
		$result = $admin->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function listAdmin()
	{
		$limit = input('limit');
		$page = input('page');
		$map = array();
		$adminname = input('adminname');
		if (!empty($adminname)){
			$map['adminname'] = $adminname;
		}
		$realname = input('realname');
		if (!empty($realname)){
			$map['realname'] = $realname;
		}
		$mobile = input('mobile');
		if (!empty($mobile)){
			$map['mobile'] = $mobile;
		}
		$role = input('role');
		if (!empty($role)){
			$map['role'] = $role;
		}
		$list = AdminModel::where($map)->order('id','asc')->limit($limit)->page($page)->append(['status_text','role_text'])->select();
		$count = AdminModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	// 个人信息
	public function myInfo()
	{
		$access_admin = session('access_admin');
		$admin = AdminModel::find($access_admin['id']);
		return view('admin/myinfo',[
			'admin' => $admin
		]);
	}

	public function editMyInfo()
	{
		$access_admin = session('access_admin');
		AdminModel::update(['realname'=>input('realname'),'mobile'=>input('mobile'),'remark'=>input('remark')], ['id'=>$access_admin['id']]);
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}

	public function myPassword()
	{
		return view('admin/mypassword');
	}

	public function editMyPassword()
	{
		$access_admin = session('access_admin');
		$oldpassword = input('oldpassword');
		if ( $access_admin['password']!==md5($oldpassword) ){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "当前密码不正确。"
			)));
		}
		$password = input('password');
		$repassword = input('repassword');
		if ( $password!==$repassword ){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "两次输入的新密码不一致。"
			)));
		}
		AdminModel::update(['password'=>md5($password)], ['id'=>$access_admin['id']]);
		session('access_admin', null);
		echo json_encode(array(
			'code' 	=> 0
		));
	}

}
