<?php 
class usersClassAction extends Action
{
	public function defaultAction()
	{
		
	}
	
	public function dataAction()
	{
		$limit= (int)$this->get('limit','1');
		$sort = $this->get('sort');
		$dir  = $this->get('dir');
		$order= '`sort` asc';
		if($sort && $dir)$order= '`'.$sort.'` '.$dir.'';
		$barr = m('users')->getlimit('1=1', (int)$this->get('page','1'),'*',$order, $limit);
		
		
		$rows  = $barr['rows'];
		$total = count($rows);
		$barr = array(
			'rows' => $rows,
			'totalCount'=> $barr['count'],
			'success' => true,
		);
		return $barr;
	}
	
	public function formAction()
	{
		$id = (int)$this->get('id','0');
		$this->smartydata['id'] = $id;
		$rs['name'] = '';
		$rs['user'] = '';
		$rs['mobile'] = '';
		$rs['email'] = '';
		$rs['face']  = '';
		$rs['type'] = '0';
		$rs['faceimg'] = 'images/noface.png';
		if($id>0){
			$rs = m('users')->getone('`id`='.$id.'');
			$rs['faceimg'] = 'images/noface.png';
			if($rs['face'])$rs['faceimg'] = $rs['face'];
		}
		$this->smartydata['rs'] = $rs;
	}
	
	public function savecheckAction()
	{
		$name = $this->post('name');
		$user = $this->post('user');
		$pass = $this->post('pass');
		$id = (int)$this->post('id','0');
		
		if(m('users')->rows("`user`='$user' and `id`<>'$id'")>0)return '用户名已经存在了';
		
		$uarr= array(
			'name' 	=> $name,
			'user' 	=> $user,
			'mobile' 	=> $this->post('mobile'),
			'email' 	=> $this->post('email'),
			'face' 	=> $this->post('face'),
			'type' 	=> (int)$this->post('type'),
		);
		$where = '`id`='.$id.'';
		if($id==0){
			if(isempt($pass))$pass = '123456';
			$where = '';
			$uarr['status']=1;
			$uarr['regdt'] = $this->rock->now;
		}
		if(!isempt($pass)){
			$rand = rand(100000,999999);
			$uarr['pass'] = md5($pass.$rand);
			$uarr['rand'] = $rand;
		}
		
		$bo 	= m('users')->record($uarr, $where);
		if(!$bo)return $this->db->error();
		
		return 'ok';
	}
	
}