<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\controller;
use admin\model\user AS model;
use \temmoku\admin;
use \temmoku\db;
use \temmoku\lib\pages;
class user extends admin{
	
	public function add(){
		if('post'===$_POST['step']){
			hook_listen('admin_user_add_begin',$_POST);
			if(!$_POST['username']){
				$this->err('没有填写用户名');
			}
			if(!$_POST['password']){
				$this->err('没有填写密码');
			}
			if(!$_POST['email']){
				$this->err('没有填写email');
			}
			if(!$_POST['mobile']){
				$this->err('没有填写手机号码');
			}
			$return=(new model)->GET_User_Name($_POST['username']);
			if('1'===$return['Result']){
				$this->err('已经存在用户');
			}
			$rand=_rand(4);
			$arr=[
				'username'=>$_POST['username'],
				'groupid'=>$_POST['groupid'],
				'password'=>md5(md5($_POST['password']).$rand),
				'rand'=>$rand,
				'email'=>$_POST['email'],
				'mobile'=>$_POST['mobile'],
				'regtime'=>time(),
				'state'=>1
			];
			db::insert(jab.'user')->cols($arr)->query();
			hook_listen('admin_user_add_end',$_POST);
			$this->success('用户添加成功');
		}
		$this->display(tpl('user/add'));
	}
	public function edit(){
		
		$data=db::select('*')->from(jab.'user')->where("uid='$_GET[uid]'")->row();
		if('edit'===$_POST['step']){
			hook_listen('admin_user_edit_begin',$_POST);
			if($_POST['password'] != $data['password']  ){
				$_POST['password']=md5(md5($_POST['password']).$data['rand']);
			}
			
			if($_POST['thumb']){
				if($data['thumb'] != $_POST['thumb']){
					if($data['thumb']){
						$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$data['thumb'];
	    				@unlink($oldthumb);
					}
					$_POST['thumb']=UploadFiles_MOVE($_POST['thumb']);
				}
			}else{
				if($data['thumb']){
					$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$data['thumb'];
	    			@unlink($oldthumb);
				}
			}
			$not_login_star_time=$_POST['not_login_star_time'] ? strtotime($_POST['not_login_star_time']) : 0;
			$not_login_end_time=$_POST['not_login_end_time'] ? strtotime($_POST['not_login_end_time']) : 0;
			$arr=[
				'username'=>$_POST['username'],
				'thumb'=>$_POST['thumb'],
				'groupid'=>$_POST['groupid'],
				'password'=>$_POST['password'],
				'email'=>$_POST['email'],
				'mobile'=>$_POST['mobile'],
				'integral'=>$_POST['integral'],
				'state'=>$_POST['state'],
				'is_login'=>$_POST['is_login'],
				'not_login_star_time'=>(int)$not_login_star_time,
				'not_login_end_time'=>(int)$not_login_end_time,
			];
			db::update(jab.'user')->cols($arr)->where("uid='$_POST[uid]'")->query();
			hook_listen('admin_user_edit_end',$_POST);
			$this->Success('操作成功');
		}
		$this->assign('edit', $data);
		$this->display(tpl('user/edit'));
	}
	public function state(){
		$_POST['state']=intval($_POST['state']);
		hook_listen('admin_user_state_begin',$_POST);
		if(is_array($_POST['uiddb'])){
			$_uiddb=[];
			foreach($_POST['uiddb'] as $uid){
				$uid=\intval($uid);
				if(!$uid){
					continue;
				}
				$_uiddb[]=$uid;
			}
			
			$uidDB=implode(',',$_uiddb);
			db::update(jab.'user')->cols(['state'=>$_POST['state']])->where("uid IN ($uidDB)")->query();
		}else{
			db::update(jab.'user')->cols(['state'=>$_POST['state']])->where("uid='$_POST[uid]'")->query();
		}
		hook_listen('admin_user_state_end',$_POST);
		$this->Success('操作成功');
	}
	public function del(){
		hook_listen('admin_user_del_begin',$_POST);
		if(is_array($_POST['uiddb'])){
			$_uiddb=[];
			foreach($_POST['uiddb'] as $uid){
				$uid=\intval($uid);
				if(!$uid){
					continue;
				}
				$_uiddb[]=$uid;
			}
			$uidDB=implode(',',$_uiddb);
			db::delete(jab.'user')->where("uid IN ($uidDB)")->query();
		}else{
			db::delete(jab.'user')->where("uid='$_POST[uid]'")->query();
		}
		hook_listen('admin_user_del_end',$_POST);
		$this->Success('操作成功');
	}
	
	public function index(){
		!isset($_GET['page']) && $_GET['page']=1;
		$rows='30';
		$_data=db::select('*')->from(jab.'user')->orderByDESC(['uid'])->setPaging($rows)->page($_GET['page'])->query();
		foreach($_data as $row){
			$row['groupname']=C('USER_GROUP.'.$row['groupid'])['name'];
			$data[]=$row;
		}
		$Get_Page=(new Pages)->Pages(jab."user",$WHERE,"/admin/user/index/",$rows,$_GET['page']);
		$this->assign('data', $data);
		$this->assign('Get_Page', $Get_Page);
		$this->display(tpl('user/index'));
	}
}
?>
