<?php
	if(!defined("ROOT_PATH")) exit("die Access ");
	class activity_userControl extends skymvc{
		public $userid;
		public function __construct(){
			parent::__construct();
			$this->loadmodel(array("activity_user","activity","login","user"));
			$this->userid=$this->login->userid;
			if(!in_array(strtolower(get('a')),array('','default','list','show'))){
				$this->login->checkLogin();
			}
		}
		
		public function onDefault(){
			$act_id=get('act_id','i');
			$activity=$this->activity->selectRow(array("where"=>"id=".$act_id));
			if(empty($activity)){
				$this->gomsg("活动不存在",R("/index.php?m=activity"));
			}
			$where=" act_id=".$act_id." AND status=2 ";
			$url="/index.php?m=activity_user&a=default";
			$limit=20;
			$start=get("per_page","i");
			$option=array(
				"start"=>intval(get_post('per_page')),
				"limit"=>$limit,
				"order"=>" id DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=$this->activity_user->select($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					$uids[]=$v['userid'];
				}
				$us=$this->user->getUserByIds($uids);
				foreach($data as $k=>$v){
					$v['user_head']=$us[$v['userid']]['user_head'];
					$v['nickname']=$us[$v['userid']]['nickname'];
					$data[$k]=$v;
				}
			}
			$pagelist=$this->pagelist($rscount,$limit,$url);
			
			$this->smarty->assign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url,
					"activity"=>$activity
				)
			);
			$this->smarty->display("activity_user/index.html");
		}
		
		public function onShow(){
			$id=get_post("id","i");
			if($id){
				$data=$this->activity_user->selectRow(array("where"=>"id={$id}"));
				
			}
			$this->smarty->assign(array(
				"data"=>$data
			));
			$this->smarty->display("activity_user/show.html");
		}
		public function onAdd(){
			$id=get_post("id","i");
			if($id){
				$data=$this->activity_user->selectRow(array("where"=>"id={$id}"));
				
			}
			$this->smarty->assign(array(
				"data"=>$data
			));
			$this->smarty->display("activity_user/add.html");
		}
		
		public function onSave(){
			$act_id=get_post('act_id','i');
			$data=$this->activity->selectRow(array("where"=>" id='$act_id' AND status=2 "));

			if(!$data or ($data['endtime']<time())){
				$this->gomsg("很抱歉，该活动已取消或者已结束");
			}
			//判断是否已经参与
			if($this->activity_user->selectRow(array("where"=>" act_id='$act_id' AND userid=".$this->userid." "))){
				$this->gomsg("你已经申请过了",R("/index.php?m=activity&a=show&id=".$act_id));
			}
			$data=array();
			$id=get_post("id","i");
			$data["siteid"]=SITEID;
			$data["act_id"]=post("act_id","i");
			$data["userid"]=$this->userid;
			$data["telephone"]=post("telephone","h");
			$data["truename"]=post("truename","h");
			$data["info"]=post("info","h");
			$data["logo"]=post("logo","h");

			if($id){
				$this->activity_user->update($data,"id='$id'");
			}else{
				$data["dateline"]=time();
				$this->activity_user->insert($data);
			}
			$this->gomsg("保存成功",R("/index.php?m=activity&a=show&id=".$act_id));
		}
		
		
		public function oncpuser(){
			$act_id=intval(get('act_id'));
			$data=$this->activity->selectRow(array("where"=> "id=".$act_id." "));
			$start=get('per_page','i');
			$url="index.php?m=activity_user&a=cpuser&act_id=".$act_id;
			$where=" act_id=".$act_id." ";
			$option=array(
				"where"=>$where,
				"start"=>$start,
				"limit"=>20,
				"order"=>"id DESC"
			);
			$rscount=true;
			$data=$this->activity_user->select($option,$rscount);
			$pagelist=$this->pagelist($rscount,20,$url);
			$activity=$this->activity->selectRow(array("where"=>"id=".$act_id)); 
			$this->smarty->assign(array(
				"list"=>$data,
				"activity"=>$activity,
				"rscount"=>$rscount,
				"pagelist"=>$pagelist
			));
			$this->smarty->display("activity_user/cpuser.html");
					 
		}
		
		public function onStatus(){
			$id=get_post('id',"i");
			$status=get_post("status","i");
			$this->activity_user->update(array("status"=>$status),"id=$id");
			exit(json_encode(array("error"=>0,"message"=>"状态修改成功")));
		}
		
		public function onDelete(){
			$id=get_post('id',"i");
			$this->activity_user->delete("id={$id}");
			exit(json_encode(array("error"=>0,"message"=>"删除成功")));
		}
		
			
	public function onjoin(){
			
			$act_id=get('act_id',"i");
			$data=$this->activity->selectRow(array("where"=>" id='$act_id' AND status=2 "));

			if(!$data or ($data['endtime']<time())){
				$this->gomsg("很抱歉，该活动已取消或者已结束");
			}
			//判断是否已经参与
			if($this->activity_user->selectOne(array("where"=>" act_id='$act_id' AND userid=".$this->userid." "))){
				$this->gomsg("你已经申请过了",R("/index.php?m=activity&a=show&id=".$act_id));
			}
			
			$this->smarty->assign(array(
				"data"=>$data,
				"activity"=>$data
			)); 
 
			$this->smarty->display("activity_user/join.html");
	}
 


		
		
	}

?>