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;
using System.Data.SqlClient;

public partial class WTOSYSadmin_news_newadd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        if (!IsPostBack)
        {
            SqlConnection conn = CDatabase.wtoconn();
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from PackageCategory", conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            if (ds.Tables[0].Rows.Count != 0)
            {
                this.lbname.DataSource = ds;
                this.lbname.DataTextField = "BigCategory";
                this.lbname.DataValueField = "id";
                this.lbname.DataBind();
            }
            else
            {
                Response.Write("<script>alert('暂无上级类别;请先填表加；');location.href('Package.aspx')</script>");
            }
            conn.Close();
        }
            if (Request.QueryString["doing"] != null)
            {
                switch (Request.QueryString["doing"])
                {
                    case "add":
                        this.databind();
                        break;
                    case "edit":
                        this.databind2();  
                        this.addedit.Click += new EventHandler(editclick);
                            break;
                }
            }
            else
            {
                Response.Redirect("Package.aspx");
            }
    }
    protected void databind()
    {
        this.title.Text = "套餐添加";
        this.addedit.Click += new EventHandler(addclick);
    }
    protected void databind2()
    {
        if (!IsPostBack)
        {
            this.title.Text = "套餐编辑";
            SqlConnection conn2 = CDatabase.wtoconn();
            conn2.Open();
            SqlCommand smd = new SqlCommand("select * from Package where id=" + Request.QueryString["id"], conn2);
            SqlDataReader sdr = smd.ExecuteReader();
            sdr.Read();
            this.lbname.SelectedValue =Convert.ToString(sdr.GetInt32(1));
            this.newstitle.Text = sdr.GetString(3);
            this.Content.Text = Server.HtmlDecode(sdr.GetString(8));
            piclab.Text = sdr["pic"].ToString();
            this.derc.Text = sdr["b1"].ToString();
            if (sdr["b2"].ToString() == "y")
            {
                this.tjindex.Checked = true;
            }
            else
            {
                this.tjindex.Checked = false;
            }

            if (sdr.GetString(4) == "y")
            {
                this.y.Checked = true;
            }
            else
            {
                this.n.Checked = true;
            }
            sdr.Close();
            conn2.Close();
        }
    }

    protected void addclick(object sender, EventArgs e)
    {
        string biglbid, biglbname, adddate, isfp, Content, times,tj;
        biglbid = this.lbname.SelectedItem.Value;
        biglbname = this.lbname.SelectedItem.Text;
        if (y.Checked == true)
        {
            isfp = "y";
        }
        else
        {
            isfp = "n";
        }
        if (this.tjindex.Checked == true)
        {
            tj = "y";
        }
        else
        {
            tj = "n";
        }
        times = "1";
        Content = Server.HtmlEncode(this.Content.Text.Replace("'", "''"));
        string picname="";
        string hz = this.fu1.PostedFile.FileName;
        if (hz != "")
        {
            string S_hz = (hz.Substring(hz.LastIndexOf(".") + 1)).ToLower().Trim();
            string stpt = "";
            string stfilename = "";
            if (S_hz == "jpg" || S_hz == "gif")
            {
                stfilename = DateTime.Now.ToString("yyyyMMddHHmmssffffMM");
                stpt = Server.MapPath("~/upfile/") + stfilename + "." + S_hz;
                fu1.SaveAs(stpt);
                picname = stfilename + "." + S_hz;
                string sql1 = "insert into Package(biglbid,biglbname,title,adddate,isfp,Content,times,pic,pidall,b1,b2)";
                sql1 += " values(" + biglbid + ",'" + biglbname + "','" + this.newstitle.Text + "','" + DateTime.Now.ToString() + "','" + isfp + "','" + Content + "'," + times + ",'" + picname + "',' ','"+this.derc.Text+"','"+tj+"')";
                if (Doadmin.Exec(sql1))
                {
                   Response.Write("<script>alert('添加成功');location.href('Package.aspx')</script>");
                }
                else
                {
                Response.Write("<script>alert('此次操作失败')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('文件格式错误');location.href('Packageadd.aspx')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('图片不能为空');location.href('Packageadd.aspx')</script>");
        }
    }
    protected void editclick(object sender, EventArgs e)
    {
        string biglbid1, biglbname1, adddate1, isfp1, Content1, times1,tj;
        biglbid1 = this.lbname.SelectedItem.Value;
        biglbname1 = this.lbname.SelectedItem.Text;
        if (y.Checked == true)
        {
            isfp1 = "y";
        }
        else
        {
            isfp1 = "n";
        }
        if (tjindex.Checked == true)
        {
            tj = "y";
        }
        else
        {
            tj = "n";
        }
        Content1 = Server.HtmlEncode(this.Content.Text.Replace("'", "''"));

        string picname = "";
        string hz = this.fu1.PostedFile.FileName;
        string sql1 = "";
        if (hz != "")
        {
            string S_hz = (hz.Substring(hz.LastIndexOf(".") + 1)).ToLower().Trim();
            string stpt = "";
            string stfilename = "";
            if (S_hz == "jpg" || S_hz == "gif")
            {
                stfilename = DateTime.Now.ToString("yyyyMMddHHmmssffffMM");
                stpt = Server.MapPath("~/upfile/") + stfilename + "." + S_hz;
                fu1.SaveAs(stpt);
                picname = stfilename + "." + S_hz;
                sql1 = "update Package set biglbid=" + biglbid1 + ",biglbname='" + biglbname1 + "',title='" + this.newstitle.Text + "',adddate='" + DateTime.Now.ToString() + "',isfp='" + isfp1 + "',Content='" + Content1 + "',pic='" + picname + "',b1='"+this.derc.Text+"',b2='"+tj+"' where id=" + Request.QueryString["id"];

            }

        }
            else
            {
                sql1="update Package set biglbid=" + biglbid1 + ",biglbname='" + biglbname1 + "',title='" + this.newstitle.Text + "',adddate='" + DateTime.Now.ToString() + "',isfp='" + isfp1 + "',Content='" + Content1 + "',b1='"+this.derc.Text+"',b2='"+tj+"' where id=" + Request.QueryString["id"];
            }
                if (Doadmin.Exec(sql1))
                {
                 Response.Write("<script>alert('修改成功');location.href('Package.aspx')</script>");
                }
                else
                {
                    Response.Write(sql1);
                    //Response.Write("<script>alert('此次操作失败');location.href('Packageadd.aspx')</script>");
                }
    }
}
