using System;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using SocoShop.Common;
using SocoShop.Business;
using SocoShop.Entity;
using SkyCES.EntLib;

namespace SocoShop.Web
{
    public partial class GroupBuyAdd : System.Web.UI.Page
    {
        protected ProductInfo product = new ProductInfo();
        protected GroupBuyInfo groupBuy = new GroupBuyInfo();
        protected int buyCount = 0;
        /// <summary>
        /// 页面加载方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int id = RequestHelper.GetQueryString<int>("ID");
                if (Sessions.UserID == 0)
                {
                    ScriptHelper.Alert("请先登录", "/User/Login.aspx?RedirectUrl=/Plugins/Activity/GroupBuy/GroupBuyAdd.aspx?ID=" + id.ToString());
                }            
                groupBuy = GroupBuyBLL.ReadGroupBuy(id);
                if (Sessions.MoneyLeft < groupBuy.Price)
                {
                    ScriptHelper.Alert("您账户余额不够该团购活动商品价格，请先充值", "/User/UserRecharge.aspx");
                }
                if (UserGroupBuyBLL.ReadUserGroupBuyByUser(Sessions.UserID).ID > 0)
                {
                    ScriptHelper.Alert("您已经参加该活动了，请不要重复参加");
                }
                if (groupBuy.StartDate > DateTime.Now)
                {
                    ScriptHelper.Alert("该团购活动未开始，不能购买");
                }
                if (groupBuy.EndDate < DateTime.Now)
                {
                    ScriptHelper.Alert("该团购活动已经结束，不能购买");
                }
                buyCount = UserGroupBuyBLL.ReadUserGroupBuyCount(id);
                product = ProductBLL.ReadProduct(groupBuy.ProductID);
                RegionID.DataSource = RegionBLL.ReadRegionUnlimitClass();
                UserInfo user = UserBLL.ReadUser(Sessions.UserID);
                Consignee.Text = user.UserName;
                Tel.Text = user.Tel;
                Mobile.Text = user.Mobile;
                Address.Text = user.Address;
                RegionID.ClassID = user.RegionID;
                Head.Title = "确认商品团购";
            }
        }

        /// <summary>
        /// 提交按钮点击方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            int id = RequestHelper.GetQueryString<int>("ID");
            groupBuy = GroupBuyBLL.ReadGroupBuy(id);
            UserGroupBuyInfo userGroupBuy = new UserGroupBuyInfo();
            userGroupBuy.GroupBuyID = id;
            userGroupBuy.Date = RequestHelper.DateNow;
            userGroupBuy.IP = ClientHelper.IP;
            userGroupBuy.BuyCount = RequestHelper.GetForm<int>("BuyCount");
            userGroupBuy.OrderID = 0;
            userGroupBuy.UserID = Sessions.UserID;
            userGroupBuy.UserName = Sessions.UserName;
            userGroupBuy.Consignee =  StringHelper.AddSafe(RequestHelper.GetForm<string>("Consignee"));
            userGroupBuy.RegionID = RegionID.ClassID;
            userGroupBuy.Address =  StringHelper.AddSafe(RequestHelper.GetForm<string>("Address"));
            userGroupBuy.ZipCode =  StringHelper.AddSafe(RequestHelper.GetForm<string>("ZipCode"));
            userGroupBuy.Tel = StringHelper.AddSafe( RequestHelper.GetForm<string>("Tel"));
            userGroupBuy.Email = Sessions.UserEmail;
            userGroupBuy.Mobile =  StringHelper.AddSafe(RequestHelper.GetForm<string>("Mobile"));
            UserGroupBuyBLL.AddUserGroupBuy(userGroupBuy);
            Sessions.MoneyLeft = Sessions.MoneyLeft - groupBuy.Price * userGroupBuy.BuyCount;
            UserAccountRecordBLL.AddUserAccountRecord(-groupBuy.Price * userGroupBuy.BuyCount, 0, "参加团购活动：" + groupBuy.Name, Sessions.UserID, Sessions.UserName);
            ScriptHelper.Alert("购买成功!", "GroupBuyDetail.aspx?ID=" + userGroupBuy.GroupBuyID);
        }
    }
}
