using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using TopWinCMS.Model;
using TopWinCMS.Common;
using TopWinCMS.BLL;
using TopWinCMS.Model.Enum;

namespace TopWinCMS.WebUI.Admin.ArticleGather
{
    public partial class RuleNode : System.Web.UI.Page
    {
        ArticleGatherManage MyClassBLL = new ArticleGatherManage();
        ArticleGatherInfo model = new ArticleGatherInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                DDL_Node.DataSource = MyClassBLL.BindNode(int.Parse(Request.QueryString["PID"]));
                DDL_Node.DataTextField = "ID";
                DDL_Node.DataValueField = "ID";
                DDL_Node.DataBind();

                if (DDL_Node.Items.Count == 0)
                {
                    Action = "Add";
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                }
            }
        }

        private string Action
        {
            set
            {
                ViewState["Action"] = value;
            }
            get
            {
                return ViewState["Action"] == null ? string.Empty : ViewState["Action"].ToString();
            }
        }

     

        private void Bind(int ID)
        {
            model = MyClassBLL.GetRules(ID).Find(delegate(ArticleGatherInfo a) { return (a.ID == ID); });

            TB_ClassArea.Text = model.ClassArea;
            TB_ClassNextPage.Text = model.ClassNextPage;
            TB_ClassPrePage.Text = model.ClassPrePage;
            TB_ClassLastPage.Text = model.ClassLastPage;
            CB_IsLastPageUrl.Checked = model.IsLastPageUrl == 1 ? true : false;
            TB_ShowArticleUrl.Text = model.ShowArticleUrl;
            TB_Title.Text = model.Title;
            TB_Content.Text = model.Content;
            TB_ShowArticleNextUrl.Text = model.ShowArticleNextUrl;
            TB_From.Text = model.ComeFrom;
            TB_Author.Text = model.Author;
            TB_Date.Text = model.UpdateTime;

        }

        protected void BT_OK_Click(object sender, EventArgs e)
        {
            if (Action == "Add")
            {
                model.Name = string.Empty;
                model.StartUrl = string.Empty;
                model.ClassArea = TB_ClassArea.Text;
                model.ClassNextPage = TB_ClassNextPage.Text;
                model.ClassPrePage = TB_ClassPrePage.Text;
                model.ClassLastPage = TB_ClassLastPage.Text;
                model.IsLastPageUrl = CB_IsLastPageUrl.Checked == true ? 1 : 0;
                model.ShowArticleUrl = TB_ShowArticleUrl.Text;
                model.Title = TB_Title.Text;
                model.Content = TB_Content.Text;
                model.ShowArticleNextUrl = TB_ShowArticleNextUrl.Text;
                model.ComeFrom = TB_From.Text;
                model.Author = TB_Author.Text;
                model.UpdateTime = TB_Date.Text;
                model.Filter = string.Empty;
                model.GatherNum = 0;
                model.DownImg = 0;
                model.ClassID = 0;
                model.Pass = 0;
                model.Replace = string.Empty;
                model.GatherPicArticle = 0;
                model.DrawWaterMark = 0;
                model.IsExpert = 1;
                model.PID = int.Parse(Request.QueryString["PID"]);
               

                MyClassBLL.Add(model, UpdatePanel1);
            }
            else if (Action == "Edit")
            {
                model.ID = int.Parse(this.DDL_Node.SelectedValue);
                model.Name = string.Empty;
                model.StartUrl = string.Empty;
                model.ClassArea = TB_ClassArea.Text;
                model.ClassNextPage = TB_ClassNextPage.Text;
                model.ClassPrePage = TB_ClassPrePage.Text;
                model.ClassLastPage = TB_ClassLastPage.Text;
                model.IsLastPageUrl = CB_IsLastPageUrl.Checked == true ? 1 : 0;
                model.ShowArticleUrl = TB_ShowArticleUrl.Text;
                model.Title = TB_Title.Text;
                model.Content = TB_Content.Text;
                model.ShowArticleNextUrl = TB_ShowArticleNextUrl.Text;
                model.ComeFrom = TB_From.Text;
                model.Author = TB_Author.Text;
                model.UpdateTime = TB_Date.Text;
                model.Filter = string.Empty;
                model.GatherNum = 0;
                model.DownImg = 0;
                model.ClassID = 0;
                model.Pass = 0;
                model.Replace = string.Empty;
                model.GatherPicArticle = 0;
                model.DrawWaterMark = 0;
                model.IsExpert = 1; 
       

                MyClassBLL.Edit(model, UpdatePanel1);
            }
        }


        protected void BT_GoToEdit_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            Action = "Edit";
            Bind(int.Parse(DDL_Node.SelectedValue));
        }

        protected void BT_AddNew_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            Action = "Add";
        }
    }
}
