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.OleDb;

public partial class admini_ArtModify : BasePage
{
   // http://wlm.wan1ba.com/api?uid=admin&uname=admin&sex=1&email=&lc=&key=3VKWTOD4&yx=ddz_1&sig=md5(admin+3VKWTOD4+YF30L5MN)
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!CheckLogin()) return;
       // System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
    

        AjaxPro.Utility.RegisterTypeForAjax(typeof(admini_ArtModify));
        if (!IsPostBack)
        {
            string s = Request.QueryString["aID"];
            if (!string.IsNullOrEmpty(s))
            {
                int f = 0;
                if (int.TryParse(s, out f))
                {
                    this.Button1.Visible = false;
                    this.Button2.Visible = true;
                    ReadData(s);
                }

            }
            else
            {
                this.Button1.Visible = true;
                this.Button2.Visible = false;
            }

        }
    }

    //5~1~a~s~p~x
    void ReadData(string s)
    {
        int id = int.Parse(s);
        string sql = "SELECT * FROM ARTICLE WHERE ID = "+id;
        try
        {
            OleDbDataReader dr = SqlHelper.ExecuteReader(sql);
            if (dr.Read())
            {
                this.hID.Value = dr["id"].ToString();
                this.ahit.Value = dr["arthit"].ToString();
                this.aFrom.Value = dr["artFrom"].ToString();
                this.aDesc.Value = dr["artDesc"].ToString();
                this.aAuthor.Value = dr["artAuthor"].ToString();
                this.aTime.Value = dr["artTime"].ToString();
                this.atitle.Value = dr["artTitle"].ToString();
                this.FreeTextBox1.Text = dr["artText"].ToString();
            }
        }
        catch (Exception ex)
        {
            this.ClientScript.RegisterStartupScript(typeof(string), Guid.NewGuid().ToString(), "<script>alert('数据读取错误！');location.href='ArticleLB.aspx'</script>");
           // Response.Redirect("ArticleLB.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql = "INSERT INTO ARTICLE (ARTTITLE,ARTDESC,ARTTIME,ARTSTATUS,ARTAUTHOR,ARTTOP,ARTTEXT,ARTHIT,ARTFROM) values ('{0}','{1}','{2}',true,'{3}',true,@text,{4},'{5}')";
        AddOrUpdate(sql);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string sql = "Update article set ARTTITLE='{0}',ARTDESC='{1}',ARTTIME='{2}',ARTAUTHOR='{3}',ARTTEXT=@text,ARTHIT={4},ARTFROM='{5}' where id="+int.Parse(this.hID.Value);
        AddOrUpdate(sql);
    }

    //5-1~a-s-p-x
    void AddOrUpdate(string sql)
    {
        string title = Server.HtmlEncode(this.atitle.Value);
        string text = this.FreeTextBox1.Text.Trim();
        string desc = this.aDesc.Value;
        string aA = this.aAuthor.Value;
        string aT = this.aTime.Value;
        string aF = this.aFrom.Value;
        string aH = this.ahit.Value;
        if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(text) || string.IsNullOrEmpty(desc))
        {
            this.RegisterStartupScript("", "<script>alert('不能为空！');</script>");
            return;
        }
        string sql1 = string.Format(sql, title, desc, aT, aA, int.Parse(aH), aF);
        try
        {
            int k = SqlHelper.ExecuteSql(sql1, text);
            if (k > 0)
            {
                
                WebClass.ReMoveCacheByKey("NewArticle");
                this.RegisterStartupScript("", "<script>alert('成功');location.href='ArticleLB.aspx'</script>");

            }//5!1!a!s!p!x
            else
                this.RegisterStartupScript("", "<script>alert('失败');</script>");
        }
        catch (Exception ex)
        {
            this.RegisterStartupScript("", "<script>alert(\"失败!" + ex.Message + "\");</script>");
        }
    }

   
}
