using Newtonsoft.Json.Linq;
using ProductionControl.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace ProductionControl
{
    public partial class FrmSysSetting : Form
    {
        public FrmSysSetting()
        {
            InitializeComponent();
        }
        private void FrmSetParams_Load(object sender, EventArgs e)
        {
            initData();
        }
        private void initData()
        {
            string configPath = Application.StartupPath + "\\SysConfig.ini";
            string lsTmp = "";

            var comNameList = Utils.SSerialPort.getPortNames().ToList();
            comNameList.ForEach(comName =>
            {
                this.cobLight_PortNum.Items.Add(comName);
                this.cobTension_PortNum.Items.Add(comName);
                this.cobSmallAxis_ComName.Items.Add(comName);
            });

            //Skip 
            this.chkSkipAxis0.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipAxis0") == "1";
            this.chkSkipAxis1.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipAxis1") == "1";
            this.chkSkipAxis2.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipAxis2") == "1";
            this.chkSkipAxis3.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipAxis3") == "1";
            this.chkSkipTension.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipTension") == "1";
            this.chkSkipHeight.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipHeight") == "1";
            this.chkSkipLight.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipLight") == "1";
            this.chkSkipScannerGL.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipScannerGL") == "1";
            this.chkSkipScannerCC.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipScannerCC") == "1";
            this.chkSkipSmallAxis.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipSmallAxis") == "1";

            this.ckSkipSizeFailed.Checked = Util.ReadIniValue(configPath, "SKIP", "MarkErrorStop") == "1";
            this.cbOpenFlawDistribution.Checked = Util.ReadIniValue(configPath, "SKIP", "OpenFlawDistribution") == "1";
            this.cbOpenAxisXCal.Checked = Util.ReadIniValue(configPath, "SKIP", "OpenAxisXCalibration") == "1";

            //DEV
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_PulseOutMode");
            EnumUtil.BindToEnumName<AxisPulseOutMode>(this.cobAxis0PulseOutMode, typeof(AxisPulseOutMode), lsTmp == "" ? AxisPulseOutMode.O_CW_CCW : (AxisPulseOutMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_PulseOutMode");
            EnumUtil.BindToEnumName<AxisPulseOutMode>(this.cobAxis1PulseOutMode, typeof(AxisPulseOutMode), lsTmp == "" ? AxisPulseOutMode.O_CW_CCW : (AxisPulseOutMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_PulseOutMode");
            EnumUtil.BindToEnumName<AxisPulseOutMode>(this.cobAxis2PulseOutMode, typeof(AxisPulseOutMode), lsTmp == "" ? AxisPulseOutMode.O_CW_CCW : (AxisPulseOutMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_PulseOutMode");
            EnumUtil.BindToEnumName<AxisPulseOutMode>(this.cobAxis3PulseOutMode, typeof(AxisPulseOutMode), lsTmp == "" ? AxisPulseOutMode.O_CW_CCW : (AxisPulseOutMode)Convert.ToInt32(lsTmp));
             
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_HomeMode");
            EnumUtil.BindToEnumName<AxitHomeMode>(this.cobAxis0HomeMode, typeof(AxitHomeMode), lsTmp == "" ? AxitHomeMode.MODE11_LmtSearch_Ref : (AxitHomeMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_HomeMode");
            EnumUtil.BindToEnumName<AxitHomeMode>(this.cobAxis1HomeMode, typeof(AxitHomeMode), lsTmp == "" ? AxitHomeMode.MODE11_LmtSearch_Ref : (AxitHomeMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_HomeMode");
            EnumUtil.BindToEnumName<AxitHomeMode>(this.cobAxis2HomeMode, typeof(AxitHomeMode), lsTmp == "" ? AxitHomeMode.MODE11_LmtSearch_Ref : (AxitHomeMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_HomeMode");
            EnumUtil.BindToEnumName<AxitHomeMode>(this.cobAxis3HomeMode, typeof(AxitHomeMode), lsTmp == "" ? AxitHomeMode.MODE11_LmtSearch_Ref : (AxitHomeMode)Convert.ToInt32(lsTmp));

            var lstLightName = Utils.EnumUtil.GetNamesArr<LightDevNameEnum>();
            this.cobLight_Name.Items.AddRange(lstLightName);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Light_Name");
            if (string.IsNullOrWhiteSpace(lsTmp)) 
                this.cobLight_Name.SelectedIndex = 0;
            else 
                this.cobLight_Name.Text = ((LightDevNameEnum)Convert.ToInt32(lsTmp)).ToString();  
            this.cobLight_PortNum.Text = "COM" + Util.ReadIniValue(configPath, "DEV", "Light_PortNum");
            this.cobTension_PortNum.Text = "COM" + Util.ReadIniValue(configPath, "DEV", "Tension_PortNum");
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Tension_Offset");
            if (string.IsNullOrWhiteSpace(lsTmp))
                this.numOffset.Value = 0;
            else
                this.numOffset.Value = (decimal)double.Parse(lsTmp);
            this.cobSmallAxis_ComName.Text = Util.ReadIniValue(configPath, "DEV", "SmallAxis_ComName");

            this.cobAxis0HomeDir.SelectedIndex = Convert.ToInt32(Util.ReadIniValue(configPath, "DEV", "Axis0_HomeDir"));
            this.cobAxis1HomeDir.SelectedIndex = Convert.ToInt32(Util.ReadIniValue(configPath, "DEV", "Axis1_HomeDir"));
            this.cobAxis2HomeDir.SelectedIndex = Convert.ToInt32(Util.ReadIniValue(configPath, "DEV", "Axis2_HomeDir"));
            this.cobAxis3HomeDir.SelectedIndex = Convert.ToInt32(Util.ReadIniValue(configPath, "DEV", "Axis3_HomeDir"));

            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelLow");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_HomeVelLow = JArray.Parse(lsTmp);
                this.numAxis0HomeVelLow.Value = Axis_HomeVelLow.Count > 0 ? (decimal)Axis_HomeVelLow[0] : 0;
                this.numAxis1HomeVelLow.Value = Axis_HomeVelLow.Count > 1 ? (decimal)Axis_HomeVelLow[1] : 0;
                this.numAxis2HomeVelLow.Value = Axis_HomeVelLow.Count > 2 ? (decimal)Axis_HomeVelLow[2] : 0;
                this.numAxis3HomeVelLow.Value = Axis_HomeVelLow.Count > 3 ? (decimal)Axis_HomeVelLow[3] : 0; 
            }
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelHigh");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_HomeVelHigh = JArray.Parse(lsTmp);
                this.numAxis0HomeVelHigh.Value = Axis_HomeVelHigh.Count > 0 ? (decimal)Axis_HomeVelHigh[0] : 0;
                this.numAxis1HomeVelHigh.Value = Axis_HomeVelHigh.Count > 1 ? (decimal)Axis_HomeVelHigh[1] : 0;
                this.numAxis2HomeVelHigh.Value = Axis_HomeVelHigh.Count > 2 ? (decimal)Axis_HomeVelHigh[2] : 0;
                this.numAxis3HomeVelHigh.Value = Axis_HomeVelHigh.Count > 3 ? (decimal)Axis_HomeVelHigh[3] : 0;
            }
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeAcc");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_HomeAcc = JArray.Parse(lsTmp);
                this.numAxis0HomeAcc.Value = Axis_HomeAcc.Count > 0 ? (decimal)Axis_HomeAcc[0] : 0;
                this.numAxis1HomeAcc.Value = Axis_HomeAcc.Count > 1 ? (decimal)Axis_HomeAcc[1] : 0;
                this.numAxis2HomeAcc.Value = Axis_HomeAcc.Count > 2 ? (decimal)Axis_HomeAcc[2] : 0;
                this.numAxis3HomeAcc.Value = Axis_HomeAcc.Count > 3 ? (decimal)Axis_HomeAcc[3] : 0;
            }
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeDec");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_HomeDec = JArray.Parse(lsTmp);
                this.numAxis0HomeDec.Value = Axis_HomeDec.Count > 0 ? (decimal)Axis_HomeDec[0] : 0;
                this.numAxis1HomeDec.Value = Axis_HomeDec.Count > 1 ? (decimal)Axis_HomeDec[1] : 0;
                this.numAxis2HomeDec.Value = Axis_HomeDec.Count > 2 ? (decimal)Axis_HomeDec[2] : 0;
                this.numAxis3HomeDec.Value = Axis_HomeDec.Count > 3 ? (decimal)Axis_HomeDec[3] : 0;
            }

            this.txtHeightDev_IP.Text = Util.ReadIniValue(configPath, "DEV", "HeightDev_IP");
            this.numHeightDev_Port.Value = Convert.ToInt32(Util.ReadIniValue(configPath, "DEV", "HeightDev_Port")); 
            this.numHeightDev_SafeValue.Value = Convert.ToDecimal(Util.ReadIniValue(configPath, "DEV", "HeightDev_SafeValue"));
             
            lsTmp = Util.ReadIniValue(configPath, "DEV", "HeightDev_BaseList");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray HeightDev_BaseList = JArray.Parse(lsTmp);
                this.numHeightBase0.Value = HeightDev_BaseList.Count > 0 ? (decimal)HeightDev_BaseList[0] : 0;
                this.numHeightBase1.Value = HeightDev_BaseList.Count > 1 ? (decimal)HeightDev_BaseList[1] : 0;
                this.numHeightBase2.Value = HeightDev_BaseList.Count > 2 ? (decimal)HeightDev_BaseList[2] : 0;
                this.numHeightBase3.Value = HeightDev_BaseList.Count > 3 ? (decimal)HeightDev_BaseList[3] : 0; 
                this.numHeightBase4.Value = HeightDev_BaseList.Count > 4 ? (decimal)HeightDev_BaseList[4] : 0;
            }

            this.txtIO_DevNum.Text = Util.ReadIniValue(configPath, "DEV", "IOCard_DeviceNum");
            this.txtDefect_Path.Text = Util.ReadIniValue(configPath, "DEV", "Defect_SavePath"); 
            this.txtDefect_Small_Path.Text = Util.ReadIniValue(configPath, "DEV", "Defect_Small_SavePath"); 
            this.txtDefect_Compress_SavePath.Text = Util.ReadIniValue(configPath, "DEV", "Defect_Compress_SavePath");

            //int liTmp = -1;
            double ldTmp = -1;
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_SavePath_AutoClear");
            //if (Util.IsNumber(lsTmp)) liTmp = Convert.ToInt32(lsTmp);
            ldTmp = Convert.ToDouble(lsTmp);
            chkDefect_Path_AutoClear.Checked = (ldTmp > -1);
            if (ldTmp > -1) numDefect_Path_AutoClear.Value = (decimal)ldTmp;

            lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_Compress_SavePath_AutoClear");
            //if (Util.IsNumber(lsTmp)) liTmp = Convert.ToInt32(lsTmp);
            ldTmp = Convert.ToDouble(lsTmp);
            chkDefect_Compress_SavePath_AutoClear.Checked = (ldTmp > -1);
            if (ldTmp > -1) numDefect_Compress_SavePath_AutoClear.Value = (decimal)ldTmp;

            lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_Small_SavePath_AutoClear");
            //if (Util.IsNumber(lsTmp)) liTmp = Convert.ToInt32(lsTmp);
            ldTmp = Convert.ToDouble(lsTmp);
            chkDefect_Small_Path_AutoClear.Checked = (ldTmp > -1);
            if (ldTmp > -1) numDefect_Small_Path_AutoClear.Value = (decimal)ldTmp;


            this.chkSaveAllDefectImg.Checked= (Util.ReadIniValue(configPath, "DEV", "SaveAllDefectImg") == "1");
            this.chkMakeTag.Checked= (Util.ReadIniValue(configPath, "DEV", "MakeTag") == "1");
            this.txtSizeEnginePath.Text= Util.ReadIniValue(configPath, "DEV", "SizeEnginePath");

            this.txtSizeBmp_SavePath.Text = Util.ReadIniValue(configPath, "DEV", "SizeBmp_SavePath");
            this.txtSizeBmp_Compare_SavePath.Text = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Compare_SavePath");
            this.txtSizeBmp_Zoom_Image_SavePath.Text = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Zoom_Image_SavePath");

            lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_SavePath_AutoClear");
            //if (Util.IsNumber(lsTmp)) liTmp = Convert.ToInt32(lsTmp);
            ldTmp = Convert.ToDouble(lsTmp);
            chkSizeBmp_SavePath_AutoClear.Checked = (ldTmp > -1);
            if (ldTmp > -1) numSizeBmp_SavePath_AutoClear.Value = (decimal)ldTmp;

            lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Compare_SavePath_AutoClear");
            //if (Util.IsNumber(lsTmp)) liTmp = Convert.ToInt32(lsTmp);
            ldTmp = Convert.ToDouble(lsTmp);
            chkSizeBmp_Compare_SavePath_AutoClear.Checked = (ldTmp > -1);
            if (ldTmp > -1) numSizeBmp_Compare_SavePath_AutoClear.Value = (decimal)ldTmp;

            lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Zoom_Image_SavePath_AutoClear");
            //if (Util.IsNumber(lsTmp)) liTmp = Convert.ToInt32(lsTmp);
            ldTmp = Convert.ToDouble(lsTmp);
            chkSizeBmp_Zoom_Image_SavePath_AutoClear.Checked = (ldTmp > -1);
            if (ldTmp > -1) numSizeBmp_Zoom_Image_SavePath_AutoClear.Value = (decimal)ldTmp;

            //线宽PT补偿
            lsTmp = Util.ReadIniValue(configPath, "DEV", "PT_Offset");
            if (string.IsNullOrWhiteSpace(lsTmp))
                this.numPToffset.Value = 0;
            else
                this.numPToffset.Value = (decimal)double.Parse(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "LineWidth_Offset");
            if (string.IsNullOrWhiteSpace(lsTmp))
                this.numLineWidthoffset.Value = 0;
            else
                this.numLineWidthoffset.Value = (decimal)double.Parse(lsTmp);

            //DB
            this.txtDBCon.Text = Util.ReadIniValue(configPath, "DB", "DBConStr");
            //HTTP 
            this.txtLocalIp.Text = Util.ReadIniValue(configPath, "HTTP", "LocalIp");
            lsTmp = Util.ReadIniValue(configPath, "HTTP", "LocalHttpPort");
            if (Util.IsNumber(lsTmp))this.numHttpPort.Value = Convert.ToInt32(lsTmp);
            //lsTmp = Util.ReadIniValue(configPath, "HTTP", "LocalSocketPort");
            //if (Util.IsNumber(lsTmp)) this.numSocketPort.Value = Convert.ToInt32(lsTmp); 
            //LOG
            this.txtLogPath.Text = Util.ReadIniValue(configPath, "LOG", "LogPath"); 
        }
       

        private void tsbtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string configPath = Application.StartupPath + "\\SysConfig.ini";

                //DEV  
                string lsLight_Name = this.cobLight_Name.Text.Trim();
                if (lsLight_Name == "")
                    throw new Exception("请选择光源提供商！");
                LightDevNameEnum Light_Name = Utils.EnumUtil.Convert2Enum<LightDevNameEnum>(lsLight_Name);
                string Light_PortNum = this.cobLight_PortNum.Text.Trim();
                string Tension_PortNum = this.cobTension_PortNum.Text.Trim();
                decimal Tension_Offset = this.numOffset.Value;
                string SmallAxis_ComName = this.cobSmallAxis_ComName.Text.Trim();
                if (Light_PortNum == "" || Tension_PortNum == "" || SmallAxis_ComName == "")
                    throw new Exception("请设置设备COM端口！");

                string HeightDev_IP = this.txtHeightDev_IP.Text;
                int HeightDev_Port = (int)this.numHeightDev_Port.Value;
                if(HeightDev_IP=="")
                    throw new Exception("请设置厚度检测设备IP地址！");
                decimal HeightDev_SafeValue=this.numHeightDev_SafeValue.Value; 

                string IOCard_DeviceNum = this.txtIO_DevNum.Text.Trim();
                if (IOCard_DeviceNum == "")
                    throw new Exception("请设置I/O设备编号 ！");

                string Defect_SavePath = this.txtDefect_Path.Text.Trim();
                if (Defect_SavePath == "")
                    throw new Exception("请设置缺陷大图保存路径 ！"); 
                string Defect_Compress_SavePath = this.txtDefect_Compress_SavePath.Text.Trim();
                if (Defect_Compress_SavePath == "")
                    throw new Exception("请设置缺陷压缩大图保存路径 ！"); 
                string Defect_Small_SavePath = this.txtDefect_Small_Path.Text.Trim();
                //if (Defect_Small_SavePath == "")
                //    throw new Exception("请设置缺陷小图保存路径 ！");

                //DB
                string DBConStr = this.txtDBCon.Text.Trim();
                if (DBConStr == "")
                    throw new Exception("请设置DB连接字符串 ！");
                //LOG
                string LogPath = this.txtLogPath.Text.Trim();
                if (LogPath == "")
                    throw new Exception("请设置日志保存路径 ！");

                //最后统一保存
                //Skip
                Util.WriteIniValue(configPath, "SKIP", "SkipAxis0", this.chkSkipAxis0.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipAxis1", this.chkSkipAxis1.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipAxis2", this.chkSkipAxis2.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipAxis3", this.chkSkipAxis3.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipTension", this.chkSkipTension.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipHeight", this.chkSkipHeight.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipLight", this.chkSkipLight.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipScannerGL", this.chkSkipScannerGL.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipScannerCC", this.chkSkipScannerCC.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipSmallAxis", this.chkSkipSmallAxis.Checked ? "1" : "0");

                Util.WriteIniValue(configPath, "SKIP", "MarkErrorStop", this.ckSkipSizeFailed.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "OpenFlawDistribution", this.cbOpenFlawDistribution.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "OpenAxisXCalibration", this.cbOpenAxisXCal.Checked ? "1" : "0");
                //DEV 
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis0_PulseOutMode", ((int)EnumUtil.GetSelectedItemToEnum<AxisPulseOutMode>(this.cobAxis0PulseOutMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis1_PulseOutMode", ((int)EnumUtil.GetSelectedItemToEnum<AxisPulseOutMode>(this.cobAxis1PulseOutMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis2_PulseOutMode", ((int)EnumUtil.GetSelectedItemToEnum<AxisPulseOutMode>(this.cobAxis2PulseOutMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis3_PulseOutMode", ((int)EnumUtil.GetSelectedItemToEnum<AxisPulseOutMode>(this.cobAxis3PulseOutMode)).ToString());
               
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis0_HomeMode", ((int)EnumUtil.GetSelectedItemToEnum<AxitHomeMode>(this.cobAxis0HomeMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis1_HomeMode", ((int)EnumUtil.GetSelectedItemToEnum<AxitHomeMode>(this.cobAxis1HomeMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis2_HomeMode", ((int)EnumUtil.GetSelectedItemToEnum<AxitHomeMode>(this.cobAxis2HomeMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis3_HomeMode", ((int)EnumUtil.GetSelectedItemToEnum<AxitHomeMode>(this.cobAxis3HomeMode)).ToString());
                 
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis0_HomeDir", this.cobAxis0HomeDir.SelectedIndex.ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis1_HomeDir", this.cobAxis1HomeDir.SelectedIndex.ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis2_HomeDir", this.cobAxis2HomeDir.SelectedIndex.ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis3_HomeDir", this.cobAxis3HomeDir.SelectedIndex.ToString());

                JArray HeightDev_BaseList = new JArray()
                {
                    numHeightBase0.Value,numHeightBase1.Value,numHeightBase2.Value,numHeightBase3.Value,numHeightBase4.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "HeightDev_BaseList", HeightDev_BaseList.ToString().Replace("\r\n", ""));


                Utils.Util.WriteIniValue(configPath, "DEV", "Light_Name", ""+(int)Light_Name);
                Utils.Util.WriteIniValue(configPath, "DEV", "Light_PortNum", Light_PortNum.Substring(3));
                Utils.Util.WriteIniValue(configPath, "DEV", "Tension_PortNum", Tension_PortNum.Substring(3));
                Utils.Util.WriteIniValue(configPath, "DEV", "Tension_Offset", Tension_Offset.ToString());

                Utils.Util.WriteIniValue(configPath, "DEV", "SmallAxis_ComName", SmallAxis_ComName);
                 
                Utils.Util.WriteIniValue(configPath, "DEV", "HeightDev_IP", HeightDev_IP);
                Utils.Util.WriteIniValue(configPath, "DEV", "HeightDev_Port", HeightDev_Port.ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "HeightDev_SafeValue", HeightDev_SafeValue.ToString());
                JArray Axis_HomeVelLow = new JArray()
                {
                    numAxis0HomeVelLow.Value,numAxis1HomeVelLow.Value,numAxis2HomeVelLow.Value,numAxis3HomeVelLow.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_HomeVelLow", Axis_HomeVelLow.ToString().Replace("\r\n",""));
                
                JArray Axis_HomeVelHigh = new JArray()
                {
                    numAxis0HomeVelHigh.Value,numAxis1HomeVelHigh.Value,numAxis2HomeVelHigh.Value,numAxis3HomeVelHigh.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_HomeVelHigh", Axis_HomeVelHigh.ToString().Replace("\r\n", ""));
                
                JArray Axis_HomeAcc = new JArray()
                {
                    numAxis0HomeAcc.Value,numAxis1HomeAcc.Value,numAxis2HomeAcc.Value,numAxis3HomeAcc.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_HomeAcc", Axis_HomeAcc.ToString().Replace("\r\n", ""));

                JArray Axis_HomeDec = new JArray()
                {
                    numAxis0HomeDec.Value,numAxis1HomeDec.Value,numAxis2HomeDec.Value,numAxis3HomeDec.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_HomeDec", Axis_HomeDec.ToString().Replace("\r\n", ""));

                Utils.Util.WriteIniValue(configPath, "DEV", "IOCard_DeviceNum", IOCard_DeviceNum); 

                Utils.Util.WriteIniValue(configPath, "DEV", "Defect_SavePath", Defect_SavePath);
                Utils.Util.WriteIniValue(configPath, "DEV", "Defect_SavePath_AutoClear", (chkDefect_Path_AutoClear.Checked ? (double)numDefect_Path_AutoClear.Value : -1)+"");
                Utils.Util.WriteIniValue(configPath, "DEV", "Defect_Compress_SavePath", Defect_Compress_SavePath);
                Utils.Util.WriteIniValue(configPath, "DEV", "Defect_Compress_SavePath_AutoClear", (chkDefect_Compress_SavePath_AutoClear.Checked ? (double)numDefect_Compress_SavePath_AutoClear.Value : -1) + "");
                Utils.Util.WriteIniValue(configPath, "DEV", "Defect_Small_SavePath", Defect_Small_SavePath);
                Utils.Util.WriteIniValue(configPath, "DEV", "Defect_Small_SavePath_AutoClear", (chkDefect_Small_Path_AutoClear.Checked ? (double)numDefect_Small_Path_AutoClear.Value : -1) + "");

                Utils.Util.WriteIniValue(configPath, "DEV", "SaveAllDefectImg", this.chkSaveAllDefectImg.Checked ? "1" : "0");
                Utils.Util.WriteIniValue(configPath, "DEV", "MakeTag", this.chkMakeTag.Checked ? "1" : "0");
                Utils.Util.WriteIniValue(configPath, "DEV", "SizeEnginePath", this.txtSizeEnginePath.Text.Trim()); 
                Utils.Util.WriteIniValue(configPath, "DEV", "SizeBmp_SavePath", this.txtSizeBmp_SavePath.Text.Trim());
                Utils.Util.WriteIniValue(configPath, "DEV", "SizeBmp_SavePath_AutoClear", (chkSizeBmp_SavePath_AutoClear.Checked ? (double)numSizeBmp_SavePath_AutoClear.Value : -1) + "");
                Utils.Util.WriteIniValue(configPath, "DEV", "SizeBmp_Compare_SavePath", this.txtSizeBmp_Compare_SavePath.Text.Trim());
                Utils.Util.WriteIniValue(configPath, "DEV", "SizeBmp_Compare_SavePath_AutoClear", (chkSizeBmp_Compare_SavePath_AutoClear.Checked ? (double)numSizeBmp_Compare_SavePath_AutoClear.Value : -1) + "");
                Utils.Util.WriteIniValue(configPath, "DEV", "SizeBmp_Zoom_Image_SavePath", this.txtSizeBmp_Zoom_Image_SavePath.Text.Trim());
                Utils.Util.WriteIniValue(configPath, "DEV", "SizeBmp_Zoom_Image_SavePath_AutoClear", (chkSizeBmp_Zoom_Image_SavePath_AutoClear.Checked ? (double)numSizeBmp_Zoom_Image_SavePath_AutoClear.Value : -1) + "");

                decimal PT_Offset = this.numPToffset.Value;
                decimal LW_Offset = this.numLineWidthoffset.Value;
                Utils.Util.WriteIniValue(configPath, "DEV", "PT_Offset", PT_Offset.ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "LineWidth_Offset", LW_Offset.ToString());
                //DB
                Utils.Util.WriteIniValue(configPath, "DB", "DBConStr", DBConStr);
                //HTTP 
                Utils.Util.WriteIniValue(configPath, "HTTP", "LocalIp",this.txtLocalIp.Text.Trim());
                Utils.Util.WriteIniValue(configPath, "HTTP", "LocalHttpPort", this.numHttpPort.Value.ToString()); 
                //LOG
                Utils.Util.WriteIniValue(configPath, "LOG", "LogPath", LogPath);


                //
                this.Hide();
                MessageBox.Show("保存成功，生效需重启程序！");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

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

        private void txtDefect_Path_Click(object sender, EventArgs e)
        {
            string path = FileUtil.selectFolder(Application.StartupPath);
            if (!string.IsNullOrWhiteSpace(path))
                this.txtDefect_Path.Text = path;
        }
         

        private void txtLogPath_Click(object sender, EventArgs e)
        {
            string path = FileUtil.selectFolder(Application.StartupPath);
            if (!string.IsNullOrWhiteSpace(path))
                this.txtLogPath.Text = path;
        }
        private void txtSizeEnginePath_Click(object sender, EventArgs e)
        {
            string path = FileUtil.selectFolder(Application.StartupPath);
            if (!string.IsNullOrWhiteSpace(path))
                this.txtSizeEnginePath.Text = path;
        }
       
        private void txtSizeBmp_Path_Click(object sender, EventArgs e)
        {
            string path = FileUtil.selectFolder(Application.StartupPath);
            if (!string.IsNullOrWhiteSpace(path))
                this.txtSizeBmp_SavePath.Text = path;
        }
        private void txtSizeBmp_Compare_SavePath_Click(object sender, EventArgs e)
        {
            string path = FileUtil.selectFolder(Application.StartupPath);
            if (!string.IsNullOrWhiteSpace(path))
                this.txtSizeBmp_Compare_SavePath.Text = path;
        }
        private void txtSizeBmp_Zoom_Image_SavePath_Click(object sender, EventArgs e)
        {
            string path = FileUtil.selectFolder(Application.StartupPath);
            if (!string.IsNullOrWhiteSpace(path))
                this.txtSizeBmp_Zoom_Image_SavePath.Text = path;
        }
        private void txtDefect_Small_Path_Click(object sender, EventArgs e)
        {
            string path = FileUtil.selectFolder(Application.StartupPath);
            if (!string.IsNullOrWhiteSpace(path))
                this.txtDefect_Small_Path.Text = path;
        }
        private void txtDefect_Compress_SavePath_Click(object sender, EventArgs e)
        {
            string path = FileUtil.selectFolder(Application.StartupPath);
            if (!string.IsNullOrWhiteSpace(path))
                this.txtDefect_Compress_SavePath.Text = path;
        }
        
        private void btnClearDefect_Small_Path_Click(object sender, EventArgs e)
        {
            this.txtDefect_Small_Path.Text = "";
        }

        private void btnClearDefect_Path_Click(object sender, EventArgs e)
        {
            this.txtDefect_Path.Text = "";
        }

        private void btnClearDefect_Compress_SavePath_Click(object sender, EventArgs e)
        {
            txtDefect_Compress_SavePath.Text = "";
        }

        private void btnClearSizeBmp_Path_Click(object sender, EventArgs e)
        {
            txtSizeBmp_SavePath.Text = "";
        }

        private void btnClearSizeBmp_Compare_SavePath_Click(object sender, EventArgs e)
        {
            txtSizeBmp_Compare_SavePath.Text = "";
        }
        private void btnSizeBmp_Zoom_Image_SavePath_Click(object sender, EventArgs e)
        {
            txtSizeBmp_Zoom_Image_SavePath.Text = "";
        }
        private void btnBackDB_Click(object sender, EventArgs e)
        {
            try
            {
                string lsPath=Utils.FileUtil.selectFolder();
                if (string.IsNullOrWhiteSpace(lsPath))
                    return;

                lsPath += "\\DB" + DateTime.Now.ToString("yyyyMMddHHmm") + ".bak";
                Service.InitDB.BackupDataBase(lsPath);
                MessageBox.Show("备份成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void chkMakeTag_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
