<?php
class fenlei_guestControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	public function onInit(){
		M("login")->checkLogin();
	}
	public function onDefault(){
		
	}
	
	public function onIndex(){
		$userid=M("login")->userid;
		$list=M("mod_fenlei_guestindex")->select(array(
			"where"=>" userid=".$userid,
			"order"=>" createtime DESC"
		));
		if($list){
			foreach($list as $v){
				$uids[]=$v["touserid"];
			}
			$us=M("user")->getUserByIds($uids,"userid,user_head,nickname");
			
			foreach($list as $k=>$v){
				$v["to_user_head"]=$us[$v["touserid"]]["user_head"];
				$v["to_nickname"]=$us[$v["touserid"]]["nickname"];
				$v["timeago"]=timeago(strtotime($v["createtime"]));
				$list[$k]=$v;
			}
		}
	 
		$this->smarty->goAssign(array(
			"list"=>$list,			 
		));
		$this->smarty->display("fenlei_guest/index.html");
	}
	
	public function onUser(){
		$id=get("id","i");
		$fenlei=M("mod_fenlei")->selectRow("id=".$id);
		$userid=M("login")->userid;
		$touserid=get("touserid","i");
		$user=M("user")->getUser($userid);
		if($fenlei["userid"]==$userid){
			$this->goAll("不能跟自己咨询",1);
		}
		$this->smarty->goAssign(array(
			"fenlei"=>$fenlei,
			"touserid"=>$touserid
		));
		$this->smarty->display("fenlei_guest/user.html");
	}
	//根据分类获取对话 
	 
	public function onFenlei(){
		$id=get("id","i");
		$fenlei=M("mod_fenlei")->selectRow("id=".$id);
		$userid=M("login")->userid;
		$touserid=get("touserid","i");
		if($fenlei["userid"]!=$userid){
			$touserid=$userid;
			$user=M("user")->getUser($userid);
			$touser=M("user")->getUser($fenlei["userid"]);
		}else{
			$user=M("user")->getUser($userid);
			$touser=M("user")->getUser($touserid);
		}

		 
		$list=M("mod_fenlei_guest")->select(array(
			"where"=>" (userid=".$touserid." or touserid=".$touserid.") AND objectid=".$id." AND status=1 ",
			"order"=>" id DESC"
		));
		if($list){
			foreach($list as $k=>$v){
				
				if($v["userid"]==$userid){
					$v["isme"]=1;
					$v["nickname"]=$user["nickname"];
					$v["user_head"]=$user["user_head"];
				}else{
					$v["isme"]=0;
					$v["to_nickname"]=$touser["nickname"];
					$v["to_user_head"]=$touser["user_head"];
				}
				$list[$k]=$v;
			}
		}
		
		$this->smarty->goAssign(array(
			"list"=>$list,
			"fenlei"=>$fenlei
		));
		$this->smarty->display("fenlei_guest/fenlei.html");
	}
	
	public function onSave(){
		$id=get_post("id","i");
		 
		$fenlei=M("mod_fenlei")->selectRow("id=".$id);
		$userid=M("login")->userid;
		$touserid=get_post("touserid","i");
		if($fenlei["userid"]!=$userid){
			$touserid=$fenlei["userid"];
		}else{
			$this->goAll("不能跟自己咨询",1);
		}
		$content=post("content","h");
		M("mod_fenlei_guest")->insert(array(
			"userid"=>$userid,
			"touserid"=>$touserid,
			"content"=>$content,
			"objectid"=>$id,
			"status"=>1,
			"createtime"=>date("Y-m-d H:i:s")
		));
		//索引
		$row=M("mod_fenlei_guestindex")->selectRow("userid=".$userid." AND objectid=".$id);
		if($row){
			M("mod_fenlei_guestindex")->update(array(
				"userid"=>$userid,
				"touserid"=>$touserid,
				"content"=>$content,
				"objectid"=>$id,
				"createtime"=>date("Y-m-d H:i:s")
			),"id=".$row["id"]);
		}else{
			M("mod_fenlei_guestindex")->insert(array(
				"userid"=>$userid,
				"touserid"=>$touserid,
				"content"=>$content,
				"objectid"=>$id,
				"createtime"=>date("Y-m-d H:i:s")
			));
		}
		//商家
		$row=M("mod_fenlei_guestindex")->selectRow("touserid=".$userid." AND objectid=".$id);
		if($row){
			M("mod_fenlei_guestindex")->update(array(
				"userid"=>$touserid,
				"touserid"=>$userid,
				"content"=>$content,
				"objectid"=>$id,
				"createtime"=>date("Y-m-d H:i:s")
			),"id=".$row["id"]);
		}else{
			M("mod_fenlei_guestindex")->insert(array(
				"userid"=>$touserid,
				"touserid"=>$userid,
				"content"=>$content,
				"objectid"=>$id,
				"createtime"=>date("Y-m-d H:i:s")
			));
		}
		
		$this->goAll("保存成功");
	}
	
}
?>