<?php
include('action.abs.php');
class user extends action {
		function user() {
				parent :: action();
		}

		function userlist() {
				$db = APP :: ADP('db');
				$db->setTable('admin_user');
				
				$page = (int)V('g:page', 1);
				$each = (int)V('g:each', 15);
				$offset = ($page -1) * $each;
				$keyword = $db->escape(V('g:keyword', ''));

				$select_count = 'SELECT COUNT(*) FROM ' . $db->getPrefix() . T_ADMIN_USER . '';
				$count = $db->getOne($select_count);

				$pager = APP :: N('pager');
				$page_param = array('currentPage'=> $page, 'pageSize' => $each, 'recordCount' => $count, 'linkNumber' => 10);
				$pager->setParam($page_param);
				TPL :: assign('pager', $pager->makePage());

				$where = '';
				if ($keyword) {
					$where = ' WHERE `real_name` LIKE "%' . $keyword . '%" OR `name` LIKE "%' . $keyword . '%"';
				}
				
				$sql = 'SELECT * FROM ' . $db->getPrefix() . T_ADMIN_USER . $where . ' ORDER BY `id` DESC LIMIT ' . $offset . ',' . $each;
				$rs = $db->query($sql);
				TPL :: assign('list', $rs);

				TPL :: assign('states', array('0'=>'正常','1'=>'暂停使用'));
				TPL :: display('admin/user/user_list', '', 0, false);
		}

		function del() {
			$id = V('r:id', 0);
			if (strpos(',', $id)) {
				$id = explode(',', $id);
			} else {
				$id = (array) $id;
			}
			if ($this->_getUid() !=1 || in_array($this->_getUid(), $id)) {
				$this->_error('不能对自己进行删除操作', array('userlist'));
			}

			$db = APP :: ADP('db');
			$db->setTable('admin_user');
			
			if ($db->delete($id)) {
				$this->_redirect('userlist');
			}
			$this->_error('删除失败',  array('userlist'));
		}

		function repassword() {
			$db = APP :: ADP('db');
			$db->setTable('admin_user');
			if ($this->_isPost()) {
				$id = (int)V('p:id', 0);
				$old_pwd = trim(V('p:old_pwd'));
				$new_pwd = trim(V('p:pwd'));
				$re_pwd = trim(V('p:re_pwd'));
				if (!$old_pwd && $this->_getUid() != 1) {
					$this->_error('请输入原密码', URL('admin/user.repassword', 'id='. $id));
				}
				if (!$new_pwd) {
					$this->_error('请输入新密码', URL('admin/user.repassword', 'id='. $id));
				}
				if ($new_pwd !== $re_pwd) {
					$this->_error('两次输入的新密码不一致', URL('admin/user.repassword', 'id='. $id));
				}
				if ($this->_getUid() != 1) {
						if ($new_pwd == $old_pwd) {
							$this->_error('输入的新密码请不要跟旧密码相同', URL('admin/user.repassword', 'id='. $id));
						}
						if ($new_pwd == $old_pwd) {
							$this->_error('输入的新密码请不要跟旧密码相同', URL('admin/user.repassword', 'id='. $id));
						}
						$sql = 'SELECT pwd FROM ' . $db->getTable(T_ADMIN_USER) . ' WHERE id="' . $id . '"';
						$p = $db->getOne($sql);
						if (!$p) {
							$this->_error('不存在的用户', URL('admin/user.repassword', 'id='. $id));
						}
						if (md5($old_pwd) !== $p) {
							$this->_error('输入的原密码不正确', URL('admin/user.repassword', 'id='. $id));
						}
				}
				$data = array(
							'pwd' => md5($new_pwd)
						);
				if (!$db->save($data, $id)) {
					$this->_error('修改密码失败,可能新密码与旧密码相同', URL('admin/user.repassword', 'id='. $id));
				}
				$this->_redirect('userlist');
			}
			$rs = $db->get(V('g:id'));
			if (!$rs) {
				$this->_error('不存在的用户', URL('admin/user.repassword', 'id='. $id));
			}
			TPL :: assign('info', $rs);
			TPL :: display('admin/user/repassword', '', 0, false);
		}

		function state() {
			$id = (int)V('g:id', 0);
			$state = (int)V('g:state', 0);
			if ($this->_getUid() !=1 || $id == $this->_getUid()) {
				$this->_error('不能设置自己状态', array('userlist'));
			}
			$state = $state == 1 ? 1 : 0;
			$db = APP :: ADP('db');
			$db->setTable('admin_user');
			$data = array('state' => $state);
			$rs = $db->save($data, $id);
			if ($rs === false) {
				$this->_error('设置失败', array('userlist'));
			}
			$this->_redirect('userlist');
		}

		function edit() {
			$db = APP :: ADP('db');
			$db->setTable('admin_user');
			if ($this->_isPost()) {
					$real_name = trim(V('p:real_name'));
					$id = (int)V('p:id');
					$data = array(
							'real_name' => $real_name
					);
					if (!$db->save($data, $id)) {
						$this->_error('保存失败', URL('admin/user.edit', 'id='. $id));
					}
					$this->_redirect('userlist');
			}
			$info = $db->get(V('g:id'));

			TPL :: assign('states', array('0'=>'正常','1'=>'暂停使用'));
			TPL :: assign('info', $info);
			TPL :: display('admin/user/user_add','', 0, false);
		}

		function add() {
			if ($this->_isPost()) {
				$db = APP :: ADP('db');
				$db->setTable('admin_user');
				$name = trim(V('p:name'));
				$pwd = trim(V('p:pwd'));
				$re_pwd = trim(V('p:re_pwd'));
				$real_name = trim(V('p:real_name'));
				if (empty($name)) {
					$this->_error('用户名不能为空', array('add'));
				}
				if (empty($pwd)) {
					$this->_error('密码不能为空', array('add'));
				}
				if ($pwd !== $re_pwd) {
					$this->_error('两次输入的密码不一致', array('add'));
				}
				if (empty($real_name)) {
					$this->_error('请填写姓名', array('add'));
				}
				$sql = 'SELECT `name` FROM ' . $db->getPrefix() . T_ADMIN_USER . ' WHERE `name`="' . $name . '"';
				$rst = $db->getOne($sql);
				if ($rst) {
					$this->_error('输入已经存在的登录帐号', array('add'));
				}
				$data = array(
						'name' => $name,
						'pwd' => md5($pwd),
						'real_name' => $real_name,
						'add_time' => time(),
						'state' => 1
						);
				if ($db->save($data)) {
					$this->_redirect('userlist');
				}
				$this->_error('添加失败', array('add'));
			}
			TPL :: display('admin/user/user_add', '', 0, false);
		}
}
