using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace CompanyServer
{
    public partial class 基础配置 : Form
    {
        private string allData = "";
        public 基础配置()
        {
            InitializeComponent();
            数据初始化();
        }

        private void 数据初始化()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("companytype", typeof(string));
            dt.Columns.Add("connectstring", typeof(string));

            allData = ServerClass.Read();
            if (!string.IsNullOrEmpty(allData))
            {
                string[] arrayData = allData.Split(new string[] { "%%" }, StringSplitOptions.RemoveEmptyEntries);
                if (arrayData.Length > 0)
                {
                    foreach (string sArray in arrayData)
                    {
                        string[] array = sArray.Split(new string[] { "##" }, StringSplitOptions.RemoveEmptyEntries);
                        if (array.Length > 1)
                        {
                            DataRow dr = dt.Rows.Add();
                            dr["companytype"] = array[0];
                            dr["connectstring"] = array[1];
                        }
                    }
                }
            }
            
            this.gridControl1.DataSource = dt;
        }




        private void 保存_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.类型.Text))
            {
                MessageBox.Show("请填写类型!", "友情提醒", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                this.类型.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.连接池.Text))
            {
                MessageBox.Show("请填写连接池!", "友情提醒", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                this.连接池.Focus();
                return;
            }


            if (allData.Contains(this.类型.Text))
            {
                MessageBox.Show("类型重复!", "友情提醒", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                this.连接池.Focus();
                return;
            }
            string stxt = this.类型.Text.TrimEnd().TrimStart() + "##" + this.连接池.Text.TrimEnd().TrimStart() + "%%";

            if (ServerClass.WriteTxtFull(stxt))
            {
                MessageBox.Show("保存成功!", "友情提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.类型.Text = "";
                this.连接池.Text = "";
            }
            else
            {
                MessageBox.Show("保存失败!", "友情提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            数据初始化();
        }

        private void 删除_Click(object sender, EventArgs e)
        {
            DataRow dr = this.gridView1.GetDataRow(this.gridView1.FocusedRowHandle);
            if (dr == null)
                return;
            if (MessageBox.Show("确认要删除码!", "友情提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;
            string stxt = dr["companytype"].ToString() + "##" + dr["connectstring"].ToString() + "%%";
            ServerClass.WriteTxt(allData.Replace(stxt, ""));
            this.gridView1.DeleteRow(this.gridView1.FocusedRowHandle);
        }

        private void 开机启动_Click(object sender, EventArgs e)
        {
            //开机自动启动设置
            try
            {
                string path = Application.ExecutablePath;
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
                rk2.SetValue("JcShutdown", path);
                rk2.Close();
                rk.Close();
                MessageBox.Show("设置成功!");}
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                Console.WriteLine(exception);
                throw;
            }
      

         
        }

     
    }
}
