<?php
//打开get和post的封装
$getpost_power=true;
//打开系统库的支持
$iweb_power=true;
//打开session
$session_power=true;
//引入系统文件的支持文件
include_once(dirname(__file__)."/../includes.php");
//引入自己的配制文件
include_once(dirname(__file__)."/config.php");
//取得变量区
$accept_name=get_args("accept");
$accept_id=get_args("accept_id");
$gift=get_args("gift_img");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="Common.js"></script>
<title>礼品</title>
<style type="text/css">
*{
font-size:12px;
}
#zishu li{ float:left; margin-left:5px; list-style:none;}
#zishu .nav_a a{text-decoration:none; display:block; padding-right:8px; background:url(right.gif) no-repeat right bottom;margin-top:3px; color:#000; float:left;}
#zishu .nav_a span{font-size:12px;background: url(left.gif)  no-repeat  left bottom; display:block; line-height:23px; cursor:hand;padding-left:8px; float:left;}
#zishu li.active a{text-decoration:none; display:block; padding-right:8px; background:url(right.gif) no-repeat right top;margin-top:3px; color:#fff; float:left;}
#zishu li.active span{cursor:hand; font-size:12px;background:url(left.gif) 0 0 no-repeat; display:block; line-height:23px; padding-left:8px; cursor:hand; float:left;}
#gift_list{overflow:hidden; clear:both; border:#C2D9F2 solid 1px;margin-bottom:10px;padding:5px;}
#gift_friends{
	clear:both;
	border:1px solid #C2D9F2;
	position:absolute;
	display:none;
	left: 68px;*left: 70px;
	top: 45px;
	width: 86%;
	background:#FFFFFF;
}
#gift_info
{
background:none repeat scroll 0 0 #FDFDD5;
border:1px solid #EBDBA5;
margin-bottom:10px;
display:none;
padding:9px 100px;
width:62%;
}
#gift_list .giftbox
{
	width:72px;float:left;text-align:center;font-size:12px;
}
#gift_list span
{
	display:block;
}
#gift_page_list
{
	float:right;text-align:center;margin:10px 0 5px;
}
#gift_page_list a
{
	display:block;width:20px;height:20px;line-height:20px;float:left; border:1px #0096c6 solid;text-decoration:none;margin:0 5px;background-color:#dee7f7;color:#000;
}
#gift_page_list a.active{background-color:#0096c6;color:#fff;}
</style>
</head>
<body onload="getGifts(0)">
<div id="gift_info" ></div>
<form  name="gift" method="post" onsubmit="return check()">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="color:#333333;">
  <tr>
	<td width="60px" height="35px" align="right">送给：</td>
	<td><input  name="accept" style="border:1px #ccc solid;" readonly="readonly"/><span><a href="#" style="margin-left:5px;color:#005EAC;" id="gift_friends_button"  onclick="showFriends()">选择好友</a></span></td>
  </tr>
  <tr>
	<td align="right" valign="top">礼品：</td>
	<td>
		<ul id="zishu">
		  <li class="nav_a active"><a href="#" onclick="getGifts(0);changeStyle_gift(this);"><span>普通礼品</span></a></li>
		  <li class="nav_a"><a href="#" onclick="getGifts(1);changeStyle_gift(this);"><span>高级礼品</span></a></li>
		  <li class="nav_a"><a href="#" onclick="getGifts(2);changeStyle_gift(this);"><span>超级礼品</span></a></li>
		</ul>
		<div id="gift_list"></div>
	</td>
  </tr>
  <tr>
	<td align="right" valign="top">赠言：</td>
	<td><textarea name="msg" style="border:1px #ccc solid;" cols="36" rows="6" id="textfield"></textarea></td>
  </tr>
  <tr>
	<td height="35px">&nbsp;</td>
	<td>
	  <input type="submit" name="button" id="button" style="width:50px;" value="赠送" />
	</td>
  </tr>
</table>
<div id="gift_friends">
	<div style="background:#f7fbff; height:25px; line-height:25px;padding-left:8px;border-bottom:1px solid #C2D9F2;">我的好友</div>
	<div id="friend_list" style="overflow-y:scroll; height:220px;padding:8px;"></div>
	<div style="height:25px;text-align:center;background:#f7fbff;border-top:1px solid #C2D9F2;padding-top:3px;"><input type="button" value="确定"  onclick="hiddenFriends()"/></div>
</div>
</form>
<script>
//得到对应类型的礼品
function getGifts(type,index)
{
	//调用系统的Ajax类
	ajax=new Ajax(); 
	ajax.getInfo("gift_list.php","post","get","type="+type+"&index="+index,"gift_list"); 
	}
//得到好友列表
function getFriends()
{
	friends=new Ajax(); 
	friends.getInfo("friends.php","get","app","","friend_list"); 
}
//显示好友列表
function showFriends()
{
	document.getElementById('gift_friends').style.display='block';
	getFriends();
}
//隐藏好友列表
function hiddenFriends()
{
	var accept_ids=document.getElementsByName('accept_id');
	for(i=0;i<accept_ids.length;i++)
	{
		if(document.getElementsByName('accept_id')[i].checked)document.forms['gift'].accept.value=document.getElementById("gift_accept_"+document.getElementsByName('accept_id')[i].value).innerHTML;
	}
	document.getElementById('gift_friends').style.display='none';
}
//检测表单填写的正确性
function check()
{
	var select_flag=false;
	if(document.forms['gift'].accept.value=="")
	{
		document.getElementById('gift_info').style.display='block';
		document.getElementById("gift_info").innerHTML="还没有选择要赠送的对象!";
		return false;
	}
	for(i=0;i<document.forms['gift'].gift_img.length;i++)
	{
		if(document.forms['gift'].gift_img[i].checked)select_flag=true;
	}
	if(!select_flag)
	{
		document.getElementById('gift_info').style.display='block';
		document.getElementById("gift_info").innerHTML="还没有选择要赠送的礼品!";
		return false;
	}
	
}

function changeStyle_gift(obj){
	var tabList = document.getElementById('zishu').getElementsByTagName('li');
	for(i=0;i<tabList.length;i++){
		tabList[i].className = 'nav_a';
	}
	obj.parentNode.className += ' active';
}
</script>
<?php
//判断表单是否正常提交
if($accept_name!="" && $accept_id!="" && $gift!="")
{
	$msg=get_args("msg");
	//通过session得到用户ID
	$send_id=get_sess_userid();
	//通过session得到用户名
	$send_name=get_sess_username();
	date_default_timezone_set("Asia/Shanghai");
	$send_time=date('Y-m-d H:i:s');
	//通过表前缀得到表名
	$order=$table_prefix."order";
	$user=$table_prefix."user";
	//创建系统对数据库进行操作的对象
	$dbo=new dbex();
	//读写分离，进行读操作
	dbplugin('r');
	$sql="select score from $user where user_id=$send_id";
	//得到用户积分
	$user_info=$dbo->getRow($sql);
	$score=0;
	//判定礼品的类型，从而处理积分的变化
	if(strstr($gift,"general"))
		{
			$score=1;
		}
		else if(strstr($gift,"advanced"))
		{
			$score=-1;
		}
		else
		{
			$score=-3;
		}
		//判定剩下的积分是否还能发送礼品
	if((intval($user_info['score'])+$score)>=0)
	{
		//写数据
		dbplugin('w');
		//发送的礼品记录到数据库
		$sql="insert into $order(send_id,accept_id,send_name,accept_name,msg,gift,send_time) values('$send_id','$accept_id','$send_name','$accept_name','$msg','$gift','$send_time')";
		if($dbo->exeUpdate($sql));
		{
			//显示发送成功
			echo"<script>document.getElementById('gift_info').style.display='block';document.getElementById('gift_info').innerHTML='当前积分为[".($user_info['score']+$score)."],礼品成功发送!';</script>";
			//更新用户的积分
			$sql="update $user set score=score+$score where user_id=$send_id";
			$dbo->exeUpdate($sql);
		}
	}
	else
	{
		//当积分不够时提醒用户
		echo"<script>document.getElementById('gift_info').style.display='block';document.getElementById('gift_info').innerHTML='你的当前积分为[{$user_info[score]}],积分不够，只能通过发送普通礼品增加积分!';</script>";
	}	
}
?>
</body>
</html>
