<?php
namespace app\admin\controller;
use think\App;
use think\facade\Session;
use think\facade\View;
use think\facade\Request;

class admin extends Base{
	
	//admin首页
    public function index(){
		return View::fetch();
    }

	//admin首页
    public function add(){
		$list=\app\admin\model\AuthRule::auth_group_list();
		$list_content=\app\admin\model\AuthRule::auth_content_group_list();
		View::assign('list',$list);
		View::assign('list_content',$list_content);
		return View::fetch();
    }

    public function edit(){
		$tdata=Request::param();
		$info=\app\admin\model\Admin::edit();
		$list=\app\admin\model\AuthRule::auth_group_list();
		$list_content=\app\admin\model\AuthRule::auth_content_group_list();
		View::assign('info',$info);
		View::assign('list',$list);
		View::assign('list_content',$list_content);
		return View::fetch();
    }

    public function admin_edit(){
		$admin_id = Session::get('admin_id');
		$info=\app\admin\model\Admin::find($admin_id);
		View::assign('info',$info);
		return View::fetch();
    }
    public function admin_save(){
		IS_AJAX(Request::isAjax());
		$tdata=Request::param();
		$password=trim($tdata['password']);
		$pwd=trim($tdata['pwd']);
		if($password!='' and $password!=$pwd){
			$data =['ok' =>'2','msg' =>'密码不一致','info'=>$tdata];
			return json($data);
		}
		
		if($password!=''){
			$tdata['password']=pass_encrypt($password);
		}else{
			unset($tdata['password']);
		}
		$admin_id = Session::get('admin_id');
		$tdata['id']=$admin_id;
		$images_id=\app\admin\model\Admin::strict(false)->update($tdata);
		
		//添加日志
		$log=\app\admin\model\Log::insert('修改管理员:数据ID='.$tdata['id'].'');
		
		$data =['ok' =>'1','msg' =>'成功','info'=>$tdata];
		return json($data);
    }
	
	
    public function index_list(){
		$list=\app\admin\model\Admin::admin_list();
		$list = $list->toArray();

		$data['code']=0;
		$data['count']=$list['total'];
		$data['data']=$list['data'];
		return json($data);
    }
	
    public function save(){
		//IS_AJAX(Request::isAjax());
		$tdata=Request::param();
		$admin_id=Session::get('admin_id');

		$tq_info=\app\admin\model\Admin::admin_info(1,$admin_id);
		$is_info=\app\admin\model\Admin::admin_info(2,$tdata['name']);
		
		if(isset($tdata['id'])){
			$dq_info=\app\admin\model\Admin::admin_info(1,$tdata['id']);
		}

		$value['is_super']=$tq_info['is_super'];
		
		if(isset($tdata['id'])){
			$value['are']=1;
			//修改时判断账号
			if($dq_info['name']!=$is_info['name']){
				$value['is_info']=$is_info['id'];
			}
		}else{
			$value['are']=0;
			$value['is_info']=$is_info['id'];
		}
		
		$value['name']=$tdata['name'];
		$value['password']=$tdata['password'];
		$value['pwd']=$tdata['pwd'];
		
		$validate=new \app\admin\validate\Admin();

		if(!$validate->check($value)) {
			$data =['error' =>'1','msg' =>$validate->getError()];
			return json($data);
		}


		$save_id=\app\admin\model\Admin::admin_save($tdata);
		
		//添加日志
		if(isset($tdata['id'])){
			$log=\app\admin\model\Log::insert('修改管理员:数据ID='.$tdata['id'].'');
		}else{
			$log=\app\admin\model\Log::insert('添加管理员:数据ID='.$save_id.'');
		}
		
		$data =['ok' =>'1','msg' =>'成功','info'=>$save_id];
		return json($data);
    }


}
