using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TopWinCMS.Common;
using TopWinCMS.BLL;
using TopWinCMS.PlugIn.Indexer;
using System.Diagnostics;
namespace TopWinCMS.WebUI.Admin.FullTextIndex
{
    public partial class Build : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Licenses.License.CurrentEdition == TopWinCMS.Licenses.Edition.免费开源版)
            {
                Literal1.Text = "<div style=\" color:#F00;font-weight:bold; font-size:18px; text-align:center;\">免费版不支持此功能.</div>";
                BT_OK.Enabled = false;
                BT_OK1.Enabled = false;
            }
            if (!IsPostBack)
            {
                RightsCheck.CheckWorkPower("R_1");

                ShowMessage();
                ShowProgress();
            }
        }
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            ShowMessage();
            ShowProgress();
        }

        private void ShowProgress()
        {


            string TatolID = "Index_Tatol";
            string ProgressID = "Index_Progress";
            string js;

            if (CacheHelper.Get<string>("Index_Message") != null)
            {
                js = string.Format("PercentChange({0},{1},'Bar1');", CacheHelper.Get<string>(ProgressID), CacheHelper.Get<string>(TatolID));


                System.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "ProgressJS", js, true);
            }

        }

        private void ShowMessage()
        {
            string js = null;
             
            if (CacheHelper.Get<string>("Index_Message") != null)
            {
                js = string.Format("Message('{0}');", CacheHelper.Get<string>("Index_Message"));
            }
            else
            {
                js = string.Format("Message('无任务启动');");
            }

            if (string.IsNullOrEmpty(js) == false)
                MyJavascript.RegJS(UpdatePanel1, js);
        }

        protected void BT_OK_Click(object sender, EventArgs e)
        {
            string IndexPath = Server.MapPath(@"~\FullTextIndex");

            int ThreadSleepTime = Int32.Parse(RBL_Seep.SelectedValue);

            IndexData indexdata = new IndexData(IndexPath, ThreadSleepTime);

            indexdata.ThreadIndexContent();

            MsgBox.Alert("已在后台运行", this.UpdatePanel1);
        }

        protected void BT_OK1_Click(object sender, EventArgs e)
        {
            ProcessStartInfo the_StartInfo = the_StartInfo = new ProcessStartInfo();
            the_StartInfo.FileName = Server.MapPath(@"~\bin\TopWinCMS Indexer.exe"); ;

            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //打包文件存放目录

            Process the_Process = new Process();
            the_Process.StartInfo = the_StartInfo;
            the_Process.Start();
            the_Process.WaitForExit();
            the_Process.Close();

            MsgBox.Alert("已在后台运行,你可以离开本页", this.UpdatePanel1);
        }
    }
}