<?php
	if(!defined("ROOT_PATH")) exit("die Access ");
	class admin_activity_topicControl extends skymvc{
		
		public function __construct(){
			parent::__construct();
			$this->loadmodel(array("activity_topic","activity_topic_data","user","activity"));
		}
		
		public function onDefault(){
			$where="";
			$url="/index.php?m=admin_activity_topic&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_topic->select($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					$a=$this->activity->selectRow(array("where"=>"id=".$v['act_id'],"fields"=>"title,logo"));
					$v['act_title']=$a['title'];
					$v['act_logo']=$a['logo'];
					$u=$this->user->selectRow(array("where"=>"userid=".$v['userid']));
					$v['nickname']=$u['nickname'];
					$v['user_head']=$u['user_head'];
					$data[$k]=$v;
				}
			}
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$this->smarty->assign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url
				)
			);
			$this->smarty->display("activity_topic/index.html");
		}
		
		public function onShow(){
			$id=get_post("id","i");
			if($id){
				$data=$this->activity_topic->selectRow(array("where"=>"id={$id}"));
				
			}
			$this->smarty->assign(array(
				"data"=>$data
			));
			$this->smarty->display("activity_topic/show.html");
		}
		public function onAdd(){
			$id=get_post("id","i");
			if($id){
				$data=$this->activity_topic->selectRow(array("where"=>"id={$id}"));
				if(empty($data)) $this->gomsg("主题不存在",APPADMIN."?m=activity_topic");
				$data['content']=$this->activity_topic_data->selectOne(array("where"=>"id={$id}","fields"=>"content"));
				$activity=$this->activity->selectRow(array("where"=>" id=".$data['act_id'],"fields"=>"title as act_title,logo as act_logo"));
				$u=$this->user->selectRow(array("where"=>" userid=".$data['userid'],"fields"=>"nickname,user_head"));
				$data['act_title']=$activity['act_title'];
				$data['act_logo']=$activity['act_logo'];
				$data['nickname']=$u['nickname'];
				$data['user_head']=$u['user_head'];
			}
			$this->smarty->assign(array(
				"data"=>$data
			));
			$this->smarty->display("activity_topic/add.html");
		}
		
		public function onSave(){
			
			$id=get_post("id","i");
			$data["title"]=post("title","h"); 
			$sdata["content"]=post("content","x");
			$data['imgurl']=post('imgurl','h');
			if($id){
				$this->activity_topic->update($data,"id='$id'");
				$this->activity_topic_data->update($sdata,"id='$id'");
			}
			$this->gomsg("保存成功");
		}
		
		public function onStatus(){
			$id=get_post('id',"i");
			$status=get_post("status","i");
			$_GET['ajax']=true;
			$data=$this->activity_topic->selectRow(array("where"=>" id=$id AND status<99 "));
			if(empty($data)) exit(json_encode(array("error"=>1,"message"=>"主题不存在")));
			if($this->activity_topic->update(array("status"=>$status),"id=$id")){
				$num=0;
				if($status==2){
					$num=1;
				}elseif($data['status']==2){
					$num=-1;
				}
				$this->activity->changenum("topic_num",$num,"id=".$data['act_id']);
			}
			exit(json_encode(array("error"=>0,"message"=>"状态修改成功")));
		}
		
		public function onDelete(){
			$id=get_post('id',"i");
			$data=$this->activity_topic->selectRow(array("where"=>" id=$id AND status<99 "));
			if(empty($data)) exit(json_encode(array("error"=>1,"message"=>"主题不存在")));
			if($this->activity_topic->delete("id={$id}")){
				$this->activity_topic_data->delete("id={$id}");
				if($data['status']==2){
					$this->activity->changenum("topic_num",-1,"id=".$data['act_id']);
				}			
			}
			exit(json_encode(array("error"=>0,"message"=>"删除成功")));
		}
		
		
	}

?>