<%@ Page Language="C#" enableEventValidation="false" enableViewStateMac="false" %>
    <script language="C#" runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            string action = MallZ.Common.Fetch.Get("action");
            string step = MallZ.Common.Fetch.Get("step");
            if (action == "shop_setup_directory_test")
            {
                checkwrite();
                Response.End();
            }
            else if (action == "Dir")
            {
                string path = HttpContext.Current.Server.MapPath("~/") + @"/install";
                System.IO.DirectoryInfo m_file = new System.IO.DirectoryInfo(path);
                if (m_file.Exists)
                {
                    Response.Write(@"<script>function selectDir(obj)");
		            Response.Write("{");
                    Response.Write("    opener.document.getElementById('UpdateDir').value = obj;");
                    Response.Write("    window.close();");
                    Response.Write(@"}</"+"script>");
                    foreach (System.IO.DirectoryInfo dif in m_file.GetDirectories())
                    {
                        if (dif.Name.ToLower() != "images" && dif.Name.ToLower() != "js")
                            Response.Write("<div style=\"cursor:pointer;\" onclick=\"return selectDir('" + dif.Name + "')\"><img src='images/install.png' width=16 height=16 border=0 /> " + dif.Name + "</div>");
                    }
                }
                Response.End();
            }
            else if (action == "rep")
            {
                MallZ.Install.Install install = new MallZ.Install.Install();
                if(install.UpdateAdmin())
                    Response.Write("<script>alert(\"成功恢复管理员帐号admin admin888\");location.href='index.aspx'</"+"script>");
            }
            if (step != string.Empty) 
            {
                if (MallZ.Common.Fetch.Get("allow") != "no")
                {
                    switch (step)
                    {
                        case "1":
                            SetSiteConfig();
                            break;
                        case "2":
                            CreateDataBasePrefix();
                            CreateDataBase("database.sql");
                            break;
                        case "3":
                            CreateDataBase("class.sql");
                            break;
                        case "4":
                            CreateDataBase("experdata.sql");
                            break;
                        case "5":
                            ReNameDir();
                            break;
                        case "6":
                            DeleteInstall();
                            break;
                    }
                }
                Response.End();
            }
        }

        /// <summary>
        /// 检测目录权限
        /// </summary>
        protected void checkwrite()
        {
            MallZ.Common.FileIO file = new MallZ.Common.FileIO();
            if (file.FileExists(file.GetTruePath("/Install/lock.txt")))
                Response.Write("系统已执行了安装，如果需要重新安装，请删除站点下的文件 /Install/lock.txt");
            
            MallZ.Install.Install install = new MallZ.Install.Install();
            install.UpDateConfigNode("SiteConfigInfo/SiteUrl", MallZ.Common.Fetch.Get("shopaddress"));
            install.CheckWrite(new string[] { "Label", "Template", "Skin", "UploadFile", "Specail", "AD" });
            if (install.ErrorMsg == string.Empty)
                Response.Write("OK");
            else
                Response.Write(install.ErrorMsg);
        }

        /// <summary>
        /// 更改Web.Config文件数据库连接串
        /// </summary>
        protected void SetSiteConfig()
        {
            MallZ.Install.Install install = new MallZ.Install.Install();
            if (!install.InstallDataBase(Request["server"].ToString(), Request["dbname"].ToString(), Request["uid"].ToString(), Request["pwd"].ToString()))
                Response.Write(install.ErrorMsg);
        }

        /// <summary>
        /// 设置数据库前缀
        /// </summary>
        protected void CreateDataBasePrefix()
        {
            MallZ.Install.Install install = new MallZ.Install.Install();
            string Prefix = "";
            if (Request["prefix"] != null)
            {
                Prefix = Request["prefix"].ToString().Trim();
                if (Prefix == "")
                    Prefix = "MZ_";
            }
            else {
                Prefix = "MZ_";
            }
            if (!install.CreateDataBasePrefix(Prefix))
                Response.Write(install.ErrorMsg);
        }
        
        
        /// <summary>
        /// 创建数据库
        /// </summary>
        protected void CreateDataBase(string pathname)
        {
            MallZ.Install.Install install = new MallZ.Install.Install();
            string Prefix = "";
            if (Request["prefix"] != null)
            {
                Prefix = Request["prefix"].ToString().Trim();
                if (Prefix == "")
                    Prefix = "MZ_";
            }
            else {
                Prefix = "MZ_";
            }
            if (!install.CreateDataBase(Server.MapPath("~/") + "install/" + pathname, Prefix))
                Response.Write(install.ErrorMsg);
        }
        
        /// <summary>
        /// 重命名后台目录 
        /// </summary>
        protected void ReNameDir()
        {
            MallZ.Install.Install install = new MallZ.Install.Install();
            if (!install.ReNameDir())
                Response.Write(install.ErrorMsg);
        }

        protected void DeleteInstall()
        {
            MallZ.Install.Install install = new MallZ.Install.Install();
            if (!install.DeleteInstall())
                Response.Write(install.ErrorMsg);
        }

        /// <summary>
        /// 是否可改后台目录
        /// </summary>
        private string IsEbledAdminDir
        {
            get {
                string m_oldpath = HttpContext.Current.Server.MapPath("~/") + "\\Admin";
                if (!System.IO.Directory.Exists(m_oldpath))
                    return "disabled";
                else
                    return "";
            }
        }
        
    </script>
