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.system.gift
{
    public partial class add : JuSNS.UI.Page.ManagePage
    {
        protected string sort = "sys";
        JuSNS.Home.Gift gt = new JuSNS.Home.Gift();
        protected string oldpic = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.QueryString["ID"];
                string xClassID = "0";
                if (!string.IsNullOrEmpty(id) && JuSNS.Common.Input.IsInteger(id))
                {
                    IDataReader dr = gt.GiftInfo(Convert.ToInt32(id));
                    if (dr.Read())
                    {
                        this.gName.Text = dr["gname"].ToString();
                        this.opic.Value = dr["picture"].ToString();
                        oldpic = dr["picture"].ToString();
                        this.point.Text = dr["point"].ToString();
                        this.Content.Text = dr["content"].ToString();
                        this.GiftID.Value = dr["ID"].ToString();
                        xClassID = dr["ClassID"].ToString();
                    }
                    dr.Close();
                }
                DataTable dt = gt.GetGiftClassT();
                
                if (dt != null && dt.Rows.Count > 0)
                {
                    this.ClassID.DataSource = dt;
                    this.ClassID.DataTextField = "CName";
                    this.ClassID.DataValueField = "ID";
                    this.ClassID.DataBind();
                    this.ClassID.SelectedIndex = 0;
                    if (!string.IsNullOrEmpty(id) && JuSNS.Common.Input.IsInteger(id))
                    {
                        getSelect(ClassID, xClassID);
                    }
                }
                dt.Clear(); dt.Dispose();
            }
        }

        public void getSelect(DropDownList select, string str)
        {
            for (int i = 0; i < select.Items.Count; i++)
            {
                if (str == select.Items[i].Value)
                {
                    select.SelectedValue = select.Items[i].Value;
                    break;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                int GID = Convert.ToInt32(GiftID.Value);
                string GName = this.gName.Text;
                string Picture = string.Empty;
                string filename = string.Empty;
                if (this.fpicture.HasFile)
                {
                    JuSNS.Common.Input.UploadFile(this.fpicture.PostedFile, "template/images/gift", out filename);
                    Picture = filename;
                }
                else
                {
                    if (GID == 0)
                    {
                        PageError("请选择图片", "index.aspx");
                    }
                    else
                    {
                        Picture = this.opic.Value;
                    }
                }
                int point = Convert.ToInt32(this.point.Text);
                string Content = this.Content.Text;
                int ClassID = Convert.ToInt32(this.ClassID.SelectedValue);
                int n = gt.addGift(GName, Picture, point, Content, ClassID, GID);
                if (n== 2)
                {
                    PageError("礼物图片已经存在", "index.aspx");
                }
                else if(n==1)
                {
                    Response.Redirect("index.aspx");
                }
                else
                {
                    PageError("添加失败", "index.aspx");
                }
            }
        }
    }
}
