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.news
{
    public partial class add : JuSNS.UI.Page.ManagePage
    {
        protected string sort = "news";
        JuSNS.Home.News ns = new JuSNS.Home.News();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.TxtCreatTime.Text = DateTime.Now.ToString();
                string ID = Request.QueryString["r"];
                int xClassID = 0;
                if (!string.IsNullOrEmpty(ID))
                {
                    JuSNS.Model.NewsInfo mdl = new JuSNS.Model.NewsInfo();
                    mdl = ns.GetNewsId(Convert.ToInt32(ID), 0);
                    this.TxtTitle.Text = mdl.Title;
                    this.TxtContent.Value = mdl.Content;
                    this.TxtCreatTime.Text = mdl.CreatTime.ToString();
                    if (mdl.islock == 1)
                    {
                        this.TxtisLock.Checked = true;
                    }
                    this.TxtClick.Text = mdl.click.ToString();
                    if (mdl.isSys == 1)
                    {
                        this.TxtIsSys.Checked = true;
                    }
                    this.NewsID.Value = mdl.ID.ToString();
                    this.TxtUserID.Value = mdl.UserID.ToString();
                    this.uppic.Value = mdl.Pic;
                    xClassID = mdl.ClassID;
                }

                DataTable dt = ns.GetNewsClassD(0);

                if (dt != null && dt.Rows.Count > 0)
                {
                    this.TxtClassID.DataSource = dt;
                    this.TxtClassID.DataTextField = "className";
                    this.TxtClassID.DataValueField = "ID";
                    this.TxtClassID.DataBind();
                    this.TxtClassID.SelectedIndex = 0;
                    getSelect(TxtClassID, xClassID.ToString());
                }
                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 ID = Convert.ToInt32(this.NewsID.Value);
                string title = this.TxtTitle.Text;
                string Content = this.TxtContent.Value;
                int UserID = Convert.ToInt32(this.TxtUserID.Value);
                int ClassID = Convert.ToInt32(this.TxtClassID.SelectedValue);
                DateTime CreatTime = DateTime.Parse(this.TxtCreatTime.Text);
                int islock = 0;
                if (this.TxtisLock.Checked)
                {
                    islock = 1;
                }
                int isSys = 0;
                if (this.TxtIsSys.Checked)
                {
                    isSys = 1;
                }
                string Picture = this.uppic.Value;
                string filename = string.Empty;
                string s_savepath = "uploads/news";
                if (this.supfile.HasFile)
                {
                    JuSNS.Common.Input.UploadFile(this.supfile.PostedFile, s_savepath, out filename);
                    Picture = s_savepath + "/" + filename;
                }
                string IP = JuSNS.Common.Public.GetClientIP();
                int click = Convert.ToInt32(this.TxtClick.Text);
                int shareNum = 0;
                JuSNS.Model.NewsInfo NS = new JuSNS.Model.NewsInfo();
                NS.ClassID = ClassID;
                NS.ID = ID;
                NS.Title = title;
                NS.Content = Content;
                NS.UserID = UserID;
                NS.CreatTime = CreatTime;
                NS.islock = islock;
                NS.isSys = isSys;
                NS.Pic = Picture;
                NS.IP = IP;
                NS.click = click;
                NS.shareNum = shareNum;
                if (ns.UpdateNews(NS, ID) > 0)
                {
                    Response.Redirect("index.aspx");
                }
            }
        }
    }
}
