using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
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;
using TopWinCMS.Install;
namespace TopWinCMS.WebUI.Install
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Wizard1.ActiveStepIndex = 0;
            }

        }

        protected void BT_OK_Click(object sender, System.EventArgs e)
        {
            bool suss = true;
            LB_Err.Text = "";
            try
            {
                XML.SaveSetting("DAL", RBL_DBType.Items[0].Selected == true ? "TopWinCMS.AccessDAL" : "TopWinCMS.SQLServerDAL", "Web.config");
                XML.SaveSetting("ConnectionAccess", TTB_ConnectionAccess.Text, "Web.config");
                XML.SaveSetting("ConnectionSqlServer", TTB_ConnectionSqlServer.Text, "Web.config");

            }
            catch//(System.Exception Ex) 
            {
                //throw Ex; 
                suss = false;
                LB_Err.Text += "请确保Web.config文件不是只读而且有可写权限。";
                MsgBox.Alert("请确保Web.config文件不是只读而且有可写权限。", UpdatePanel1);
            }



            if (suss)
            {
                MsgBox.Alert("设置成功！", UpdatePanel1);
            }
        }



        protected void BT_Test1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Data.OleDb.OleDbConnection conn;
                string strConnection;
                string DB;
                if (File.Exists(TTB_ConnectionAccess.Text))
                {
                    DB = TTB_ConnectionAccess.Text;
                }
                else
                {
                    DB = HttpRuntime.AppDomainAppPath + TTB_ConnectionAccess.Text;
                }

                strConnection = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = \"" + DB + "\"";

                conn = new System.Data.OleDb.OleDbConnection(strConnection);
                conn.Open();
                conn.Close();
                MsgBox.Alert("连接数据库成功！", UpdatePanel1);
            }
            catch
            {
                MsgBox.Alert("连接数据库失败！", UpdatePanel1);
            }
        }
        protected void BT_Test2_Click(object sender, EventArgs e)
        {
            try
            {
                System.Data.SqlClient.SqlConnection conn;
                string strConnection = TTB_ConnectionSqlServer.Text;
                conn = new System.Data.SqlClient.SqlConnection(strConnection);

                conn.Open();
                conn.Close();
                MsgBox.Alert("连接数据库成功！", UpdatePanel1);
            }
            catch
            {
                MsgBox.Alert("连接数据库失败！", UpdatePanel1);
            }
        }

        protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (e.NextStepIndex == 2)
            {
                string str = CheckServerSetting.CheckFile();
                if (str.Length == 0)
                    LB_RunTime.Text = "检查配置成功!";
                else
                    LB_RunTime.Text = str;
            }
            if (e.NextStepIndex == 3)
            {
                if (CheckServerSetting.IsVirtualDirectory())
                {
                    MsgBox.Alert("尽量不要将改程序部署在虚拟目录里面，因为有部分功能会出错", UpdatePanel1);
                }
                if (ConfigurationManager.AppSettings["DAL"] == "TopWinCMS.SQLServerDAL")
                {
                    RBL_DBType.Items[0].Selected = false;
                    RBL_DBType.Items[1].Selected = true;
                }
                else
                {
                    RBL_DBType.Items[0].Selected = true;
                    RBL_DBType.Items[1].Selected = false;
                }
                TTB_ConnectionAccess.Text = Config.ConnectionAccess;
                TTB_ConnectionSqlServer.Text = Config.ConnectionSqlServer;
            }
            if (e.NextStepIndex == 4)
            {
                try
                {
                    TB_SiteName.Text = Config.SiteName;
                    TB_UP.Text = Config.UP;
                    TB_WebSiteDomainName.Text = Config.DomainName;
                    TB_VirtualPath.Text = Config.VirtualPath;

                    TB_WatherMakePath.Text = Config.DrawingWaterPath;
                    NTB_transparent.Text = Config.DWPtransparent.ToString();
                    NTB_right.Text = Config.DWPright.ToString();
                    NTB_bottom.Text = Config.DWPbottom.ToString();
                    CB_Static.Checked = Config.Static;
                    CB_UrlRewrite.Checked = Config.UrlRewrite;

                }
                catch (Exception ex)
                {
                    MsgBox.Alert(ex.Message.Replace(@"\", @"\\"), UpdatePanel1);
                }
            }
            if (e.NextStepIndex == 5)
            {
                if (RBL_DBType.Items[0].Selected == true)
                {
                    CB_Init.Enabled = false;
                    LB_IniErr.Text = "Access 数据库可跳过该步骤。";
                }
            }
            if (e.NextStepIndex == 6)
            {
                HyperLink1.NavigateUrl = Config.BackGroundPath + "Login.aspx";
            }
        }

        protected void Wizard1_PreviousButtonClick(object sender, WizardNavigationEventArgs e)
        {

        }



        protected void Wizard1_SideBarButtonClick(object sender, WizardNavigationEventArgs e)
        {
            //MsgBox.Alert("禁止跳过步骤。", UpdatePanel1);
            //e.Cancel = true;
            int i = e.NextStepIndex - e.CurrentStepIndex;
            if (Math.Abs(i) != 1)
            {
                MsgBox.Alert("禁止跳过步骤。", UpdatePanel1);
                e.Cancel = true;
            }
        }

        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            Response.Redirect( Config.BackGroundPath + "Login.aspx" );
        }

        protected void BT_Save2_Click(object sender, EventArgs e)
        {
            Config.SiteName = TB_SiteName.Text;
            Config.UP = TB_UP.Text;
            Config.DomainName = TB_WebSiteDomainName.Text.Trim();
            Config.VirtualPath = TB_VirtualPath.Text.Trim();

            Config.DrawingWaterPath = TB_WatherMakePath.Text;
            Config.DWPtransparent = Convert.ToInt32(NTB_transparent.Text);
            Config.DWPright = Convert.ToInt32(NTB_right.Text);
            Config.DWPbottom = Convert.ToInt32(NTB_bottom.Text);
            Config.Static = CB_Static.Checked;
            Config.UrlRewrite = CB_UrlRewrite.Checked;

            MsgBox.Alert("保存成功!", UpdatePanel1);
        }

    

        protected void CB_Init_CheckedChanged(object sender, EventArgs e)
        {
            BT_Init.Enabled = CB_Init.Checked;
        }

        protected void BT_Init_Click(object sender, EventArgs e)
        {
            InitData i = new InitData();

            i.BuildObject();

            i.InsertMustData();

            MsgBox.Alert("操纵成功!", UpdatePanel1);
        }
    }
}