<?php
class paotuiControl extends skymvc{
	
	public function onDefault(){
		$sender=MM("paotui","paotui_sender")->get(SENDERID);
		if(!$sender["isauth"]){
			$this->goall("请先认证",11,0,"/sender.php?m=paotui_sender&a=auth");
		}
		$sites=M("sites")->selectRow("siteid=".SITEID);
		$this->smarty->goAssign(array(
			"sites"=>$sites
		));
		$this->smarty->display("index.html");
	}
	
	public function onList(){
		$status_list=MM("paotui","paotui")->status_list();
		$typelist=MM("paotui","paotui")->typelist();
		$typeid=get("typeid","i"); 
		$where=" status=0 AND ispay=1 AND siteid=".SITEID;
		if($typeid){
			$where.="AND typeid=".$typeid;
		}
		$list=M("mod_paotui")->select(array(
			"where"=>$where,
			"order"=>"createtime DESC"
		));
		$catlist=MM("paotui","paotui_category")->catlist();
		if($list){
			foreach($list as $k=>$v){
				if($v["status"]==0 && $v["ispay"]==0){
					$v["status_name"]="待支付";
				}else{
					$v['status_name']=$status_list[$v['status']];
				}
				if($v["catid"]){
					$v["catid_title"]=$catlist[$v["catid"]]["title"];
				}else{
					$v["catid_title"]="";
				}
				$v['ispay_name']=$v['ispay']==2?"已支付":"未支付";
				$v["typeid_name"]=$typelist[$v["typeid"]]["title"];
				$v["fromaddr"]=json_decode($v["fromaddr"]);
				$v["toaddr"]=json_decode($v["toaddr"]);
				$list[$k]=$v;
			}
		}
	 
		$this->smarty->goAssign(array(
			"list"=>$list,
			"typelist"=>$typelist,
			"typeid"=>$typeid
		));
		 
	
	}
	
	public function onOrder(){
		$id=get("id","i");
		$paotui=M("mod_paotui")->selectRow("id=".$id);
		if($paotui["status"]!=0 || $paotui["ispay"]!=1){
			$this->goAll("该单已经被抢了",1);
		}
		M("mod_paotui")->begin();
		
		$senderid=M("mod_paotui_order")->insert(array(
			"ptid"=>$id,
			"userid"=>$paotui["userid"],
			"senderid"=>SENDERID,
			"createtime"=>date("Y-m-d H:i:s"),
			"money"=>$paotui["money"]
		));
		M("mod_paotui")->update(array(
			"senderid"=>$senderid,
			"status"=>1
		),"id=".$id);
		
		M("mod_paotui")->commit();
		$this->goAll("抢单成功");
	}
	
	
	
}
?>