using System;
using System.Configuration;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using XS_BLL;

namespace XS_HttpServer_LR
{
    public partial class FrmSetConfig : Form
    {
        public FrmSetConfig()
        {
            InitializeComponent();
        }
        private void FrmSetConfig_Load(object sender, EventArgs e)
        {
            txt_conn_Type.Text = Global.DbType;
            txt_conn_DBUrl.Text = Global.DBUrl;

            txt_conn_DBName.Text = Global.DBName;

            txt_conn_DBUserName.Text = Global.DBUsetName;

            txt_conn_DBUserPwd.Text = UtilityBLL.GenerateMD5(Global.DBUserPwd);

            txt_sys_ReceiveUrl.Text = Global.Receive_HTTPURL;
            txt_sys_RequestUrl.Text = Global.upper_WMSURL;
            txt_sys_CT_RequestUrl.Text = Global.upper_CT_URL;
            txt_upTime.Text = Global.ThreadTime.ToString();
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_conn_DBUrl.Text))
            {
                MessageBox.Show("数据库服务器不能为空!!!");
                return;
            }
            if (string.IsNullOrEmpty(txt_conn_DBName.Text))
            {
                MessageBox.Show("数据库实例名不能为空!!!");
                return;
            }
            if (string.IsNullOrEmpty(txt_conn_DBUserName.Text))
            {
                MessageBox.Show("用户名不能为空!!!");
                return;
            }
            if (string.IsNullOrEmpty(txt_conn_DBUserPwd.Text))
            {
                MessageBox.Show("密码不能为空!!!");
                return;
            }
            if (MessageBox.Show("是否修改配置信息?并重启?", "提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string bdUserPwd = this.txt_conn_DBUserPwd.Text;
                if (UtilityBLL.GenerateMD5(Global.DBUserPwd) == bdUserPwd)
                {
                    bdUserPwd = Global.DBUserPwd;
                }
                string connstr = "Data Source=" + this.txt_conn_DBUrl.Text.Trim() + ";Initial Catalog=" + this.txt_conn_DBName.Text.Trim() + ";Persist Security Info=True;User ID=" + this.txt_conn_DBUserName.Text.Trim() + ";Password=" + bdUserPwd + ";Pooling=true;";
                if (UtilityBLL.IsDbConnected(connstr))
                {
                    Global.SetConnConfig("connstring", connstr);
                }
                else
                {
                    MessageBox.Show("立库数据库连接验证失败,无法修改数据库连接配置");
                }

                //string connstr02 = "Data Source=" + this.txt_conn_DBUrl_SJK.Text.Trim() + ";Initial Catalog=" + this.txt_conn_DBName_SJK.Text.Trim() + ";Persist Security Info=True;User ID=" + this.txt_conn_DBUserName_SJK.Text.Trim() + ";Password=" + bdUserPwd_SJK + ";Pooling=true;";

                string HTTPURL = txt_sys_ReceiveUrl.Text;
                string upperURL = txt_sys_RequestUrl.Text;
                string ThreadTime = txt_upTime.Text;
                string upper_CT_URL = txt_sys_CT_RequestUrl.Text;

                ConfigurationManager.RefreshSection("appSettings");  //刷新命名节
                System.Configuration.Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                //修改配置节点
                cfa.AppSettings.Settings["Receive_HTTPURL"].Value = HTTPURL;
                cfa.AppSettings.Settings["upper_WMSURL"].Value = upperURL;
                cfa.AppSettings.Settings["upper_CT_URL"].Value = upper_CT_URL;
                cfa.AppSettings.Settings["ThreadTime"].Value = ThreadTime;
                //保存
                cfa.Save();
                Application.ExitThread();
                Thread thtmp = new Thread(new ParameterizedThreadStart(Run));
                object appName = Application.ExecutablePath;
                Thread.Sleep(1);
                thtmp.Start(appName);
            }
        }

        private static void Run(Object appName)
        {
            Process ps = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = appName.ToString()
                }
            };
            ps.Start();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_test_Click(object sender, EventArgs e)
        {
            #region 数据连接验证
            string bdUserPwd = this.txt_conn_DBUserPwd.Text;
            if (UtilityBLL.GenerateMD5(Global.DBUserPwd) == bdUserPwd)
            {
                bdUserPwd = Global.DBUserPwd;
            }

            string connstr = "Data Source=" + this.txt_conn_DBUrl.Text.Trim() + ";Initial Catalog=" + this.txt_conn_DBName.Text.Trim() + ";Persist Security Info=True;User ID=" + this.txt_conn_DBUserName.Text.Trim() + ";Password=" + bdUserPwd + ";Pooling=true;";
            if (UtilityBLL.IsDbConnected(connstr))
            {
                MessageBox.Show("数据库测试成功。。。");
            }
            else
            {
                MessageBox.Show("数据库测试失败!!!");
                return;
            }
            #endregion
        }

        private void txt_conn_DBUserPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back)
            {
                if (UtilityBLL.GenerateMD5(Global.DBUserPwd) == txt_conn_DBUserPwd.Text)
                {
                    txt_conn_DBUserPwd.Text = "";
                }

            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Regex r = new Regex("^[0-9]{1,}$");
            if (!r.IsMatch(txt_upTime.Text))
            {
                MessageBox.Show("请输入数字!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txt_upTime.Text = "5";
            }
        }
    }
}