using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace JuSNS.Web.inc.Ajax.gift
{
    public partial class save : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.ReturnIsNotEmail() != string.Empty)
            {
                Response.Write("<span class=\"font14\">您需要激活电子邮件后才能进行此操作。</span><a href=\"" + rooDir + "/EmailActive.aspx?r=" + this.UserID.ToString() + "\">点这里激活邮箱</a>");
                Response.End();
            }
            string uID = Request.QueryString["rID"];
            string gID = Request.QueryString["gid"];
            string SendUserID = UserID.ToString();
            string pType = Request.QueryString["gtype"];
            string cont = Request.QueryString["content"] + "";
            DateTime CreatTime = DateTime.Now;
            if (!JuSNS.Common.Input.IsInteger(uID))
            {
                Response.Write("请选择好友，不允许自己输入！");
                Response.End();
            }
            if (uID == UserID.ToString())
            {
                Response.Write("自己赠送给自己？不能自己赠送给自己！");
                Response.End();
            }
            if (!JuSNS.Common.Input.IsInteger(gID))
            {
                Response.Write("礼物选择不正确！");
                Response.End();
            }
            if (!JuSNS.Common.Input.IsInteger(pType))
            {
                Response.Write("选择赠送给方式！");
                Response.End();
            }
            if (!JuSNS.Common.Input.IsInteger(pType))
            {
                Response.Write("选择赠送给方式！");
                Response.End();
            }

            JuSNS.Home.Gift gt = new JuSNS.Home.Gift();
            //开始扣除积分
            //得到用户积分
            JuSNS.Model.UserInfo mdl = new JuSNS.Model.UserInfo();
            JuSNS.Home.User US = new JuSNS.Home.User();
            mdl = US.GetUserInfo(UserID);
            int IPoint = mdl.integral;
            //得到礼物积分
            IDataReader dr = gt.GiftInfo(Convert.ToInt32(gID));
            if(dr.Read())
            {
                int Point = Convert.ToInt32(dr["Point"].ToString());
                if (Point > 0)
                {
                    if (IPoint < Point)
                    {
                        Response.Write("<b>您的积分不足以购买此礼物！<a href=\"" + rooDir + "/help?r=GetPoint\" target=\"_blank\" style=\"cursor:help;\">查看积分政策</a></>");
                        Response.End();
                    }
                    else
                    {//扣除积分 
                        US.updateinte(UserID, Point,0, 1, "赠送别人礼物(" + dr["gName"].ToString() + ")。");
                    }
                }
            }
            dr.Close();
            if (gt.SaveGift(Convert.ToInt32(uID), Convert.ToInt32(gID), UserID, Convert.ToInt16(pType), cont, CreatTime) > 0)
            {
                Response.Write("succ");
                Response.End();
            }
        }
    }
}
