<?php
require_once("global.php");

$linkdb=array(
			"廣告位列表"=>"?job=list",
			"我購買的廣告"=>"?job=mylist"
			);

if(!$lfjid)
{
	showerr("請先登錄");
}

if($job=='buy')
{
	$rsdb = $db->get_one(" SELECT * FROM `{$pre}sellad` WHERE id='$id' ");
	$_s2=$db->get_one("SELECT COUNT(*) AS Num FROM `{$pre}sellad_user` WHERE id='$id' AND endtime>$timestamp");
	if($_s2[Num]>=$rsdb[adnum]){
		showerr("很抱歉,本廣告位目前已沒位置了");
	}
	$lfjdb[money]=intval(get_money($lfjdb[uid]));

	if(!$rsdb){
		showerr("廣告位不存在");
	}
	if($step){
		if($postdb[price]<$rsdb[price]){
			showerr("你出價不能低于系統規定的最低價:$rsdb[price]");
		}elseif($postdb[price]>$lfjdb[money]){
			showerr("你出價不能大于你自身的積分");
		}elseif($rsdb[wordnum]&&strlen($postdb[adword])>$rsdb[wordnum]){
			showerr("你的廣告文字內容字數不能大于{$rsdb[wordnum]}個");
		}elseif($postdb[adword]===''){
			showerr("廣告文字內容不能為空");
		}
		if(!strstr($postdb[adlink],'http://')){
			$postdb[adlink]="http://$postdb[adlink]";
		}
		$postdb[adlink]=filtrate($postdb[adlink]);
		$postdb[adword]=filtrate($postdb[adword]);
		$postdb[endtime]=$timestamp+$rsdb[day]*3600*24;
		$db->query("INSERT INTO `{$pre}sellad_user` ( `uid` , `username` , `begintime` , `endtime` , `money` , `id`, `adlink`, `adword`) VALUES ('$lfjdb[uid]','$lfjdb[username]','$timestamp','$postdb[endtime]','$postdb[price]','$id','$postdb[adlink]','$postdb[adword]')");
		add_user($lfjuid,-$postdb[price]);	//扣除積分
		refreshto("?job=list","恭喜你,廣告提交succeed",1);
	}
}
elseif($job=='list')
{
	$lfjdb[money]=get_money($lfjdb[uid]);
	$query = $db->query("SELECT AD.* FROM `{$pre}sellad` AD ORDER BY AD.id DESC");
	while($rs = $db->fetch_array($query)){
		$_s2=$db->get_one("SELECT COUNT(*) AS Num FROM `{$pre}sellad_user` WHERE id='$rs[id]' AND endtime>$timestamp");
		$rs[AdNum]=$rs[adnum]-$_s2[Num];
		$rs[isclose]=$rs[isclose]?'關閉':'開放';
		$listdb[]=$rs;
	}
}
elseif($job=='sell_list')
{
	$rows=30;
	if(!$page){
		$page=1;
	}
	$min=($page-1)*$rows;
	$showpage=getpage("`{$pre}sellad_user`","WHERE id='$id'","?job=$job&id=$id","$rows");
	$query = $db->query("SELECT * FROM `{$pre}sellad_user` WHERE id='$id' ORDER BY ad_id DESC LIMIT $min,$rows");
	while($rs = $db->fetch_array($query)){
		if($rs[endtime]>$timestamp){
			$rs[state]='投放中';
		}else{
			$rs[state]='已過期';
		}
		$rs[begintime]=date("Y-m-d H:i",$rs[begintime]);
		$rs[endtime]=date("Y-m-d H:i",$rs[endtime]);
		$listdb[]=$rs;
	}
}
elseif($job=='mylist')
{
	$query = $db->query("SELECT AD.name,B.* FROM `{$pre}sellad` AD LEFT JOIN `{$pre}sellad_user` B ON AD.id=B.id WHERE B.uid='$lfjuid' ORDER BY B.ad_id DESC");
	while($rs = $db->fetch_array($query)){
		$rs[begintime]=date("Y-m-d H:i",$rs[begintime]);
		$rs[endtime]=date("Y-m-d H:i",$rs[endtime]);
		$listdb[]=$rs;
	}
}
elseif($action=='del')
{
	$db->query("DELETE FROM `{$pre}sellad_user` WHERE uid='$lfjuid' AND ad_id='$ad_id'");
	refreshto("$FROMURL","刪除succeed",1);
}
elseif($job=='edit')
{
	$rsdb = $db->get_one("SELECT AD.name,AD.wordnum,B.* FROM `{$pre}sellad` AD LEFT JOIN `{$pre}sellad_user` B ON AD.id=B.id WHERE B.uid='$lfjuid' AND ad_id='$ad_id'");

	if($step==2){
		if($rsdb[wordnum]&&strlen($postdb[adword])>$rsdb[wordnum]){
			showerr("你的廣告文字內容字數不能大于{$rsdb[wordnum]}個");
		}elseif($postdb[adword]===''){
			showerr("廣告文字內容不能為空");
		}
		if(!strstr($postdb[adlink],'http://')){
			$postdb[adlink]="http://$postdb[adlink]";
		}
		$postdb[adlink]=filtrate($postdb[adlink]);
		$postdb[adword]=filtrate($postdb[adword]);
		$db->query("UPDATE `{$pre}sellad_user` SET adword='$postdb[adword]',adlink='$postdb[adlink]' WHERE  uid='$lfjuid' AND ad_id='$ad_id'");
		refreshto("?job=mylist","修改succeed",1);
	}

	$rsdb[begintime]=date("Y-m-d H:i",$rsdb[begintime]);
	$rsdb[endtime]=date("Y-m-d H:i",$rsdb[endtime]);
}

require(dirname(__FILE__)."/"."head.php");
require(dirname(__FILE__)."/"."template/buy_sellad/list.htm");
require(dirname(__FILE__)."/"."foot.php");
?>