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 System.Text;
using TopWinCMS.BLL;
using TopWinCMS.Common;
using TopWinCMS.Model;
namespace TopWinCMS.WebUI.Admin.UDT
{
    public partial class UDT_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!IsPostBack)
            {
                this.Wizard1.ActiveStepIndex = 0;


                if (Request["ID"] != null)
                {
                    RightsCheck.CheckWorkPower("L_3");
                    //“修改”界面
                    Edit_Load();

                 
                }
                else
                {
                    RightsCheck.CheckWorkPower("L_1");
                    //“添加”界面
                    RBL_Type.SelectedValue = Request["Type"];


                    if (RBL_Type.SelectedValue == "0")
                    {
                        LB_Path.Text = "<a href='UDT_Manage.aspx'>标签管理</a> → 超级标签";
                        this.Title = "超级标签";
                    }
                    if (RBL_Type.SelectedValue == "1")
                    {
                        LB_Path.Text = "<a href='UDT_Manage.aspx'>标签管理</a> → Ajax标签";
                        this.Title = "Ajax标签";
                    }
                    if (RBL_Type.SelectedValue == "2")
                    {
                        LB_Path.Text = "<a href='UDT_Manage.aspx'>标签管理</a> → Js调用标签";
                        this.Title = "Js调用标签";
                        tr_JsPath.Visible = true;
                    }
                    else
                    {
                        tr_JsPath.Visible = false;
                    }
                }



            }
        }


        #region 添加或修改
        private void Add()
        {
            MyStringBuilder strErrMsg = new MyStringBuilder();

            string strErr = null;
            UDTInfo model = new UDTInfo();

            model.Name = TB_Name.Text.Trim();
            model.DataType = DDL_DataType.SelectedValue;
            model.DataSource = TB_DataSource.Text.Trim();
            model.SqlScript = TB_SqlScript.Text.Trim();
            model.Type = RBL_Type.SelectedValue;
            model.JsPath = TB_JsPath.Text.Trim();
            model.URLType = RBL_URLType.SelectedValue;
           
            model.ShortTitle = TB_ShortTitle.Text;
            model.Length = NTB_Length.Text;
            model.ItemTemplet = TB_ItemTemplet.Text.Trim();
            model.EmptyTemplet = TB_EmptyTemplet.Text.Trim();
            model.Remark = TB_Remark.Text.Trim();

            model.Tag = TB_Tag.Text.Trim();
            if (Request["ID"] == null)
            {
                model.ID = "0";
            }
            else
            {
                model.ID = Request["ID"];
            }
            new UDTManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) != true)
            {
                MsgBox.Alert(strErrMsg.ToString(), UpdatePanel1);
                return;
            }
            else
            {
                MsgBox.Alert("录入成功！", UpdatePanel1);
            }
        }
        #endregion

        #region 加载
        private void Edit_Load()
        {
            UDTInfo model = new UDTManage().EditLoad(Request["ID"]);
            if (model != null)
            {
                if (model.Type == "3")
                {
                    Response.Redirect("UDT_JS.aspx?ID=" + Request["ID"]);
                }
                TB_Name.Text = model.Name;
                DDL_DataType.SelectedValue = model.DataType;
                TB_DataSource.Text = model.DataSource;
                TB_SqlScript.Text = model.SqlScript;
                TB_ShortTitle.Text = model.ShortTitle;
                NTB_Length.Text = model.Length;
                TB_ItemTemplet.Text = model.ItemTemplet;
                TB_EmptyTemplet.Text = model.EmptyTemplet;
                TB_Remark.Text = model.Remark;
                RBL_URLType.SelectedValue = model.URLType;
                TB_Tag.Text = model.Tag;
                RBL_Type.SelectedValue = model.Type;
                TB_JsPath.Text = model.JsPath;


                this.Title = "修改" + model.Name;

                if (RBL_Type.SelectedValue == "2")
                {

                    tr_JsPath.Visible = true;
                }
                else
                {
                    tr_JsPath.Visible = false;
                }

                if (RBL_Type.SelectedValue == "0")
                {
                    LB_Path.Text = "<a href='UDT_Manage.aspx'>标签管理</a> → 超级标签 → 修改" + model.Name;
                    
                }
                if (RBL_Type.SelectedValue == "1")
                {
                    LB_Path.Text = "<a href='UDT_Manage.aspx'>标签管理</a> → Ajax标签 → 修改" + model.Name;
                  
                }
                if (RBL_Type.SelectedValue == "2")
                {
                    LB_Path.Text = "<a href='UDT_Manage.aspx'>标签管理</a> → Js调用标签 → 修改" + model.Name;
                  
                }
            }
            else
            {
                //转向错误页面
                ErrClass.Redirect("数据不存在。");
            }
        }
        #endregion

        protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            string strErr = null;
            switch (e.CurrentStepIndex)
            {
                case 0:
                    if (TB_Name.Text.Trim().Length == 0)
                    {
                        strErr = "输入标签名称";

                    }
                    if (Request["Type"] == "2" && TB_JsPath.Text.Trim().Length == 0)
                    {
                        strErr = "请输入Js保存的路径";
                    }
                    break;
                case 1:
                    if (TB_DataSource.Text.Trim().Length == 0 && DDL_DataType.SelectedIndex > 1)
                    {
                        strErr = "输入连接字符";

                    }
                    if (TB_SqlScript.Text.Trim().Length == 0)
                    {
                        strErr = "输入Sql代码";

                    }
                    break;
                case 2:
                    if (TB_ItemTemplet.Text.Trim().Length == 0)
                    {
                        strErr = "循环项模板";

                    }
                    break;
            }

            if (strErr != null)
            {
                MsgBox.Alert(strErr, this.UpdatePanel1);
                e.Cancel = true;
            }
            else
            {
                switch (e.CurrentStepIndex)
                {
                    case 2:
                        TB_TiShi.Text = string.Format("<TW:TopWinCMS ID=\"{0}\"></TW:TopWinCMS>", TB_Name.Text);

                        if (TB_ItemTemplet.Text.Trim().Length > 0)
                        {
                            StringBuilder sbItemTemplet = new StringBuilder();
                            sbItemTemplet.AppendFormat("<TW:UDT ID=\"{0}\" URLType=\"{1}\" ShortTitle=\"{2}\" Length=\"{3}\">\r\n", TB_Name.Text.Trim(), RBL_URLType.SelectedValue, TB_ShortTitle.Text.Trim(), NTB_Length.Text.Trim());

                            sbItemTemplet.AppendLine("<ItemTemplet>");
                            sbItemTemplet.AppendLine(TB_ItemTemplet.Text.Trim());
                            sbItemTemplet.AppendLine("</ItemTemplet>");

                            if (TB_EmptyTemplet.Text.Trim().Length > 0)
                            {
                                sbItemTemplet.AppendLine("<EmptyTemplet>");
                                sbItemTemplet.AppendLine(TB_EmptyTemplet.Text.Trim());
                                sbItemTemplet.AppendLine("</EmptyTemplet>");
                            }
                            sbItemTemplet.AppendLine("<SqlTemplet>");
                            sbItemTemplet.AppendLine(TB_SqlScript.Text.Trim());
                            sbItemTemplet.AppendLine("</SqlTemplet>");
                            sbItemTemplet.AppendLine("</TW:UDT>");
                            TB_Tag.Text = sbItemTemplet.ToString();
                        }
                        break;
                }
            }
        }

        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            Add();

            if (RBL_Type.SelectedValue == "2")
            {
                new TopWinCMS.BLL.PageFactory.Pages.UDT().SaveJs(TB_JsPath.Text.Trim(), TB_Tag.Text.Trim());
            }
        }
    }
}