using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using TopWinCMS.BLL;
using TopWinCMS.Common;
using TopWinCMS.Model;
namespace TopWinCMS.WebUI.Admin.ContentManage
{
    public partial class Channel_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] != null)
            {
                this.Title = "修改频道";
                TD1.InnerText = "修改频道";
            }

            if (!IsPostBack)
            {
                RightsCheck.CheckWorkPower("S_8");

                string Action = Request["Action"];

                new BLL.ModelManage().BindDDL(this.DDL_ModelID, "外部频道");
                if (Action == "Add")
                {
                    TD1.InnerText = "添加频道";

                    LB_Path.Text = Config.SiteName + " →  <a href=\"Channel_Add.aspx\">添加频道</a>";
                    DDL_ModelID.SelectedIndex = 1;
                }
                else if (Action == "Edit")
                {

                    TD1.InnerText = "修改频道";


                    Edit_Load();
                    LB_Path.Text = Config.SiteName + " →  <a href=\"Channel_Add.aspx?Action=Edit&ID=" + Request.QueryString["ID"] + "\">修改频道</a>";
                }

            }
        }

        private void Edit_Load()
        {
            ChannelInfo model = new BLL.ChannelManage().EditLoad(Request["ID"]);
            if (model != null)
            {
                TTB_Name.Text = model.Name;
                TTB_DomainName.Text = model.DomainName;
                TB_URL.Text = model.URL;
                RBL_Target.SelectedValue = model.Target;
                TTB_Remark.Text = model.Remark;
                RBL_State.Items[0].Selected = model.Show == 1 ? true : false;
                RBL_State.Items[1].Selected = !RBL_State.Items[0].Selected;
                DDL_ModelID.SelectedValue = model.ModelID.ToString();
                TB_MetaKeyword.Text = model.MetaKeyword;
                TB_MetaDescription.Text = model.MetaDescription;
            }
            else
            {
                ErrClass.Redirect("频道不存在。");
            }
        }

        protected void BT_OK_Click(object sender, EventArgs e)
        {
            ChannelInfo model = new ChannelInfo();

            model.Name = TTB_Name.Text;
            model.DomainName = TTB_DomainName.Text;
            model.URL = TB_URL.Text;
            model.Target = RBL_Target.SelectedValue;
            model.Remark = TTB_Remark.Text;
            model.Show = int.Parse(RBL_State.SelectedValue);
            model.ModelID =int.Parse( DDL_ModelID.SelectedValue);
            model.MetaKeyword = TB_MetaKeyword.Text;
            model.MetaDescription = TB_MetaDescription.Text;

            if (Request["ID"] == null)
            {
                model.ID =  0 ;
            }
            else  
            {
                model.ID =int.Parse( Request["ID"]);
            }

            string Err = null;

            new ChannelManage().Add(model, ref Err);

            if (string.IsNullOrEmpty(Err) == false)
            {
                MsgBox.Alert(Err, UpdatePanel1);
            }
            else
            {
                MsgBox.Alert("操作成功！", UpdatePanel1);
            }

        }

        protected void DDL_ModelID_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool b = (DDL_ModelID.SelectedValue != "0");
            tr_DomainName.Visible = b;
            tr_MetaDescription.Visible = b;
            tr_MetaKeyword.Visible = b;
            tr_Remark.Visible = b;
            tr_URL.Visible = b;
        }
    }
}