<?php
	if(!defined("ROOT_PATH")) exit("die Access ");
	class guestControl extends skymvc{
		public $userid;
		public function __construct(){
			parent::__construct();
			$this->loadmodel(array("guest","login","user"));
			$this->userid=$this->login->userid;
		}
		
		public function onDefault(){
			$where="";
			$url="/index.php?m=guest&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;
			$type_list=$this->guest->type_list();
			$data=$this->guest->select($option,$rscount);
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$this->smarty->assign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url,
					"type_list"=>$type_list
				)
			);
			$this->smarty->display("guest/index.html");
		}
		
		public function onList(){
			$where="";
			$url="/index.php?m=guest&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->guest->select($option,$rscount);
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$this->smarty->assign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url
				)
			);
			$this->smarty->display("guest/index.html");
		}
		
		public function onShow(){
			$id=get_post("id","i");
			if($id){
				$data=$this->guest->selectRow(array("where"=>"id={$id}"));
				
			}
			$this->smarty->assign(array(
				"data"=>$data
			));
			$this->smarty->display("guest/show.html");
		}
		/*店铺留言*/
		public function onShop(){
			$shoptpl="index";
			$this->loadModel(array("shop"));
			$shopid=get_post('shopid','i');
			if(!$shopid) $this->gomsg("数据出错","/index.php");
			$shop=$this->shop->selectRow(array("where"=>"shopid=$shopid"));
			if($shop['shoptpl']){
				$shoptpl=$shop['shoptpl'];
			}
			$this->smarty->assign(array(
				"shop"=>$shop,			 
			));
				
			$where=" shopid=".$shopid." ";
			if(get('op')=='my'){
				$where.=" AND userid=".$this->userid;
			}
			$url="/index.php?m=guest&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->guest->select($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					$uids[]=$v['userid'];					
				}
				if($uids) $us=$this->user->getuserByids($uids);
				$t_u=array(
					"nickname"=>"游客",
					"user_head"=>"/images/user_head.jpg"
				);
				 
				foreach($data as $k=>$v){
					if(isset($us[$v['userid']])){
						$u=$us[$v['userid']];
					}else{
						$u=$t_u;
					}
					$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("shop/".$shoptpl."/guest.html");
		}
		
		public function onAdd(){
			$id=get_post("id","i");
			if($id){
				$data=$this->guest->selectRow(array("where"=>"id={$id}"));
				
			}
			$this->smarty->assign(array(
				"data"=>$data
			));
			$this->smarty->display("guest/add.html");
		}
		
		public function onSave(){
			
			$id=get_post("id","i");
			$checkcode=post('checkcode','j');
			if(isset($_POST['checkcode']) && $checkcode!=$_SESSION['checkcode']){
				$this->gomsg($this->lang['checkcode_error']);
			}
			$data['shopid']=post('shopid','i');
			$data["siteid"]=SITEID;
			$data["title"]=post("title","h");
			$data["type_id"]=post("type_id","i");
			$data["userid"]=$this->login->userid;
			$data["nickname"]=post("nickname","h");
			$data["email"]=post("email","h");
			$data["telephone"]=post("telephone","h");
			$data["dateline"]=time();
			$data["content"]=post("content","h");
			if(empty($data['content'])){
				$this->gomsg("内容不能为空！");
			}
			if($id){
				$this->guest->update($data,"id='$id'");
			}else{
				$this->guest->insert($data);
			}
			$this->gomsg("保存成功");
		}
		
		public function onStatus(){
			$id=get_post('id',"i");
			$status=get_post("status","i");
			$this->guest->update(array("status"=>$status),"id=$id");
			exit(json_encode(array("error"=>0,"message"=>"状态修改成功")));
		}
		
		public function onDelete(){
			$id=get_post('id',"i");
			$this->guest->delete("id={$id}");
			exit(json_encode(array("error"=>0,"message"=>"删除成功")));
		}
		
		
	}

?>