<?php 
class member_manager_Controller extends Base_Controller {
	function __construct(){
		parent::__construct();
		$this->memberGroup	=	new Member_Group_Model();
		$this->member		=	new Member_manager_Model();
	}
	function index($page=1,$showlines=30){
		if($this->input->post()){
			$search	=	$this->input->post('search');
		}
		$this->view				=	new View("admin/manager/list");
		$this->pagination = new Pagination(array(
	     	'base_url'    		=> 	BASEURI.'member_manager/index/', // base_url will default to current uri
		    'total_items'    	=> 	$this->member->getMemberList(false,$showlines,@$search)->count(), // use db count query here of course
		    'items_per_page' 	=> 	$showlines, 		// it may be handy to set defaults for stuff like this in config/pagination.php
		    'style'          	=> 	'classic', 	// pick one from: classic (default), digg, extended, punbb, or add your own!
			'current_page'		=>	$page,
		));
		 
		
		$this->view->memberList	=	$this->member->getMemberList($page,$showlines,@$search)->result_array();
		$this->display();
	}
	function edit($member_id=false){
		if($this->input->post()){
			$member	=	$this->input->post('form');
			$pwd	=	$this->input->post("pwd");
			if($pwd['password']!='')	$member['password']	=	md5($pwd['password']);
			$this->member->updateMember($member,$this->input->post("user_id"));
			$this->main->message		=	"修改会资料成功";
			$this->main->message_type	=	"warning";
			$this->index();
		}
		else{
			$this->view			=	new View("admin/manager/edit");
			$this->view->member	=	$this->member->getMember($member_id);
			$this->view->group	=	$this->memberGroup->selectGroup("form[group_id]",$this->view->member->group_id);
			$this->main->is_date=	true;
			$this->main->formAction		=	"member_manager/edit";
			$this->main->is_validator	=	true;
			$this->display();
		}
	}
	function add(){
		if($this->input->post()){
			$member	=	$this->input->post('form');
			$member['regtime']	=	time();
			$member['password']	=	md5($member['password']);
			//检查同名
			if($this->member->insertMember($member)){
				$this->main->message		=	"添加会员".$member['user_name']."成功";
				$this->main->message_type	=	"warning";
			}
			else{
				$this->main->message		=	"会员名称已经存在";
				$this->main->message_type	=	"warning";
			}
			$this->index();
		}
		else{
			$this->view			=	new View("admin/manager/add");
			$this->view->group	=	$this->memberGroup->selectGroup("form[group_id]");
			$this->main->formAction="member_manager/add";
			$this->main->is_date		=	true;
			$this->main->is_validator	=	true;
			$this->display();
		}
	}
	function delete($targetid=false){
		if($this->input->post()){
			$user_id	=	$this->input->post('select_id');
		}
		else{
			$user_id[]	=	$targetid;
		}
		$this->member->deleteUser($user_id);
		$this->main->message		=	"添加会员成功";
		$this->main->message_type	=	"success";
		$this->index();	
	}
	function review($status=0){
		if($this->input->post()){
			$user_id	=	$this->input->post('select_id');
		}
		else{
			$user_id[]	=	$targetid;
		}
		$this->member->reviewUser($user_id,$status);
		$this->main->message		=	"审核会员成功";
		$this->main->message_type	=	"success";
		$this->index();	
	}
}