using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBFactory;
using Microsoft.VisualBasic;
namespace SystemConfig
{
    public partial class FrmRouteDetailEdit : Form
    {
        DBOperator dbo = new DBOperator();
        bool IfUpdate = false;
        bool IFInsert = false;
        string CommSql = "SELECT F_DetailIndex AS 详细路径记录索引,T_Base_Route_Detail.F_RouteKindIndex AS 路径索引,F_RouteName AS 路径名称," +
                        "T_Base_Route_Detail.F_RouteDetailIndex AS 一条路径内的详细步骤索引,T_Base_Device.F_DeviceName AS 设备名称," +
                        "T_Base_Route_Detail.F_Orders AS 命令集,T_Base_Route_Detail.F_EndNode AS 是端头节点设备,T_Base_Route_Detail.F_WaitPallet" +
                        " AS 此节点可以暂停托盘,F_AheadDetect AS 提前检测光电组, F_AssociateDeviceIndex AS 关联设备和命令,  "+
                        "F_LockedDeviceIndex AS 运行时锁定设备,  F_BlockDevice AS 阻挡设备和命令集,F_AheadTrigger AS 提前触发设备和命令 "+
                        " FROM T_Base_Route_Detail,T_Base_Route_Kind,T_Base_Device where T_Base_Route_Detail.F_RouteKindIndex =" +
                        " T_Base_Route_Kind.F_RouteKindIndex and T_Base_Route_Detail.F_DeviceIndex = T_Base_Device.F_DeviceIndex ";
        private static FrmRouteDetailEdit _formInstance;
        public static FrmRouteDetailEdit FormInstance
        {
            get 
            {
                if (_formInstance==null)
                {
                    _formInstance = new FrmRouteDetailEdit();
                    
                }
                return  _formInstance; 
            }
            set { FrmRouteDetailEdit._formInstance = value; }
        }

        public FrmRouteDetailEdit()
        {
            InitializeComponent();
            _formInstance = this;

            
        }

        private void FrmRouteDetailEdit_Load(object sender, EventArgs e)
        {
            ////////////////路径索引
            DataSet ds = dbo.ExceSQL("SELECT F_RouteKindIndex, F_RouteKind FROM T_Base_Route_Kind");
            DataView dv = ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                this.cbRouteKindIndex.ValueMember = "F_RouteKindIndex";
                this.cbRouteKindIndex.DisplayMember = "F_RouteKindIndex";
                this.cbRouteKindIndex.DataSource = dv;
                
            }
            this.cbRouteKindIndex.Text = "";
            /////////////////设备名称
            DataSet ds1 = dbo.ExceSQL("SELECT F_DeviceIndex, F_DeviceName, F_DeviceKindIndex FROM T_Base_Device order by F_DeviceName");
            DataView dv1 = ds1.Tables[0].DefaultView;
            if (dv1.Count > 0)
            {
                this.cbDeviceIndex.ValueMember = "F_DeviceIndex";
                this.cbDeviceIndex.DisplayMember = "F_DeviceName";
                this.cbDeviceIndex.DataSource = dv1;
                
            }
            this.cbDeviceIndex.Text = "";
            ///////////////////关联设备
            DataSet ds2 = dbo.ExceSQL("SELECT F_DeviceIndex, F_DeviceName, F_DeviceKindIndex FROM T_Base_Device  order by F_DeviceName");
            DataView dv2 = ds2.Tables[0].DefaultView;
            if (dv2.Count > 0)
            {
                this.cbAssociateDeviceIndex.ValueMember = "F_DeviceIndex";
                this.cbAssociateDeviceIndex.DisplayMember = "F_DeviceName";
                this.cbAssociateDeviceIndex.DataSource = dv2;

            }
            this.cbAssociateDeviceIndex.Text = "";
            ///////////////////被锁定设备
            DataSet ds3 = dbo.ExceSQL("SELECT F_DeviceIndex, F_DeviceName, F_DeviceKindIndex FROM T_Base_Device  order by F_DeviceName");
            DataView dv3 = ds3.Tables[0].DefaultView;
            if (dv3.Count > 0)
            {
                this.cbLockedDeviceIndex.ValueMember = "F_DeviceIndex";
                this.cbLockedDeviceIndex.DisplayMember = "F_DeviceName";
                this.cbLockedDeviceIndex.DataSource = dv3;

            }
            this.cbLockedDeviceIndex.Text = "";
            ///////////////////提前检测光电组
            DataSet ds4 = dbo.ExceSQL("SELECT F_DeviceIndex, F_DeviceName, F_DeviceKindIndex FROM T_Base_Device Where F_DeviceKindIndex=18 order by F_DeviceName");
            DataView dv4 = ds4.Tables[0].DefaultView;
            if (dv4.Count > 0)
            {
                this.cbAheadDetect.ValueMember = "F_DeviceIndex";
                this.cbAheadDetect.DisplayMember = "F_DeviceName";
                this.cbAheadDetect.DataSource = dv4;

            }
            this.cbAheadDetect.Text = "";
            ///////////////////阻挡设备
            DataSet ds5 = dbo.ExceSQL("SELECT F_DeviceIndex, F_DeviceName, F_DeviceKindIndex FROM T_Base_Device  order by F_DeviceName");
            DataView dv5 = ds5.Tables[0].DefaultView;
            if (dv5.Count > 0)
            {
                this.cbBlockDevice.ValueMember = "F_DeviceIndex";
                this.cbBlockDevice.DisplayMember = "F_DeviceName";
                this.cbBlockDevice.DataSource = dv5;

            }
            this.cbBlockDevice.Text = "";
            ///////////////////提前触发设备
            DataSet ds6 = dbo.ExceSQL("SELECT F_DeviceIndex, F_DeviceName, F_DeviceKindIndex FROM T_Base_Device  order by F_DeviceName");
            DataView dv6 = ds6.Tables[0].DefaultView;
            if (dv6.Count > 0)
            {
                this.cbAheadTrigger.ValueMember = "F_DeviceIndex";
                this.cbAheadTrigger.DisplayMember = "F_DeviceName";
                this.cbAheadTrigger.DataSource = dv6;

            }
            this.cbAheadTrigger.Text = "";
        }

        private void cbDeviceIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbDeviceIndex.Text.Trim() =="")
            {
                return;
            }
            DataSet ds1 = dbo.ExceSQL("SELECT F_DeviceIndex,F_EndNode,F_WaitPallet,T_Base_Device.F_DeviceKindIndex,F_DeviceCommandIndex,F_DeviceCommandName FROM T_Base_Device ,T_Base_Device_Command Where T_Base_Device.F_DeviceKindIndex = T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" + Convert.ToInt32(this.cbDeviceIndex.SelectedValue.ToString()) + "");
            DataView dv1 = ds1.Tables[0].DefaultView;
            if (dv1.Count > 0)
            {
                this.cbOrders.ValueMember = "F_DeviceCommandIndex";
                this.cbOrders.DisplayMember = "F_DeviceCommandName";
                this.cbOrders.DataSource = dv1;
                //chbEndNode
                if (dv1[0]["F_EndNode"].ToString() == "1")
                {
                    chbEndNode.Checked = true;
                }
                else
                {
                    chbEndNode.Checked = false;
                }
                //chbWaitPallet
                if (dv1[0]["F_WaitPallet"].ToString() == "1")
                {
                    chbWaitPallet.Checked = true;
                }
                else
                {
                    chbWaitPallet.Checked = false;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int Max= dbo.GetMaxID("F_DetailIndex", "T_Base_Route_Detail");
            
            tbDetailIndex.Text = Max.ToString();
        }

        private void cbRouteKindIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRouteKindIndex.Text.Trim() == "")
            {
                return;
            }
            DataSet ds = dbo.ExceSQL("SELECT F_RouteKindIndex, F_RouteName FROM T_Base_Route_Kind Where F_RouteKindIndex=" + Convert.ToInt32(cbRouteKindIndex.Text.Trim()) + "");
            DataView dv = ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                tbRouteKind.Text = dv[0]["F_RouteName"].ToString();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int Max = dbo.GetMaxID("F_RouteDetailIndex", "T_Base_Route_Detail", "F_RouteKindIndex", this.cbRouteKindIndex.Text.Trim());

            tbRouteDetailIndex.Text = Max.ToString();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (cbOrders.SelectedValue.ToString() == "")
            {
                return;
            }
            else
            {
                string litem;
                litem = cbOrders.Text.Trim() + "=" + cbOrders.SelectedValue.ToString();
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    if (litem == listBox1.Items[i].ToString())
                    {
                        return;
                    }
                }
                this.listBox1.Items.Add(litem );
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
            {
                return;
            }
            this.listBox1.Items.RemoveAt(listBox1.SelectedIndex);
        }

        private void button22_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
            {
                return;
            }
            else
            {
                string item=listBox1.SelectedItem.ToString();
                int idx=listBox1.SelectedIndex - 1;
                if (idx < 0)
                {
                    return;
                }
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                listBox1.Items.Insert(idx, item );
                listBox1.SelectedIndex=idx;
            }
        }

        private void button21_Click(object sender, EventArgs e)
        {
            if ((listBox1.SelectedIndex >= listBox1.Items.Count) || (listBox1.SelectedIndex < 0))
            {
                return;
            }
            else
            {
                string item = listBox1.SelectedItem.ToString();
                int idx = listBox1.SelectedIndex + 1;
                if ((idx >= listBox1.Items.Count) || (idx < 0))
                {
                    return;
                }
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                listBox1.Items.Insert(idx, item);
                listBox1.SelectedIndex = idx;
            }
        }

        private void btNull_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            this.cbDeviceIndex.Text = "";
            this.cbOrders.Text = "";
            this.cbRouteKindIndex.Text = "";
            this.chbEndNode.Checked = false;
            this.chbWaitPallet.Checked = false;
            this.tbDetailIndex.Text = "";
            this.tbRouteDetailIndex.Text = "";
            this.tbRouteKind.Text = "";
        }

        private void button4_Click(object sender, EventArgs e)
        {
           IfUpdate = false;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (Information.IsNumeric(this.tbRouteDetailIndex.Text ) == false)
            {
                MessageBox.Show("一条路径内的详细步骤索引只能输入数字!","误操作提示:",MessageBoxButtons.OK,MessageBoxIcon.Error);
                tbRouteDetailIndex.Focus();
                return;
            }
            if (Information.IsNumeric(this.tbDetailIndex.Text) == false)
            {
                MessageBox.Show("详细路径记录索引只能输入数字!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbDetailIndex.Focus();
                return;
            }
            if (this.cbDeviceIndex.Text.Trim() == "")
            {
                MessageBox.Show("设备索引不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cbDeviceIndex.Focus();
                return;

            }
            if (this.cbRouteKindIndex.Text.Trim() == "")
            {
                MessageBox.Show("路径索引不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cbRouteKindIndex.Focus();
                return;

            }
            if (this.listBox1.Items.Count<=0)
            {
                MessageBox.Show("设备命令集不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                listBox1.Focus();
                return;

            }
            try
            {
                DataSet ds;
                string sql;
                char[] cc = new char[1]{'='};
                string [] litem =new string[listBox1.Items.Count ];
                string orders = "";
                int fdi,rki, rdi, di;
                char  en, wp;
                for (int i = 0; i <  listBox1.Items.Count  ; i++)
                {
                    litem = listBox1.Items[i].ToString().Split(cc);
                    orders  =orders + ";" + litem[1];
                }
                orders = orders.Substring(1);
                fdi = Convert.ToInt32(this.tbDetailIndex.Text);
                rki = Convert.ToInt32(this.cbRouteKindIndex.Text);
                rdi = Convert.ToInt32(this.tbRouteDetailIndex.Text );
                di = Convert.ToInt32(this.cbDeviceIndex.SelectedValue.ToString() );
                if (chbEndNode.Checked == true)
                {
                    en = '1';
                }
                else
                {
                    en = '0';
                }
                if (chbWaitPallet.Checked == true)
                {
                    wp = '1';
                }
                else
                {
                    wp = '0';
                }
                sql = "select * from T_Base_Route_Detail where F_DetailIndex=" + fdi;
                if (dbo.Exists(sql) == false)
                {
                    IfUpdate = false;
                }
                if (IfUpdate == true)
                {
                    
                    sql = "UPDATE T_Base_Route_Detail SET F_RouteKindIndex =" + rki + ", F_RouteDetailIndex =" + rdi + ", F_DeviceIndex =" + di + ", F_Orders ='" + orders + "', F_EndNode ='" + en + "', F_WaitPallet ='" + wp + "' where F_DetailIndex="+ fdi +"";
                    dbo.ExceSQL(sql);
                    MessageBox.Show("每步节点设备和命令信息修改成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    IfUpdate = false;
                }
                else
                {
                    sql = "select * from T_Base_Route_Detail where F_DetailIndex="+fdi;
                    if (dbo.Exists(sql) == true)
                    {
                        MessageBox.Show("每步节点设备和命令信息的详细路径记录索引在数据库已经存在,请不要重复录入数据!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.tbDetailIndex.Focus();
                        return;
                    }
                    
                    dbo.TransBegin();
                    try
                    {
                        if (IFInsert == true)
                        {
                            //首先把本条路经的大于等本行同条路径内的详细步骤索引的纪录全找到(降序)
                            //然后同条路径内的详细步骤索引都加一
                            //最后插入本行纪录的同条路径内的详细步骤索引节点设备和命令数据
                            dbo.ExceSQL("update T_Base_Route_Detail set F_RouteDetailIndex=F_RouteDetailIndex+1 where F_RouteKindIndex=" + rki + " and F_RouteDetailIndex >=" + rdi );
                            button29_Click(sender, e);
                        }
                        sql = "INSERT INTO T_Base_Route_Detail(" +
                            "F_DetailIndex, F_RouteKindIndex, F_RouteDetailIndex, F_DeviceIndex, F_Orders, F_EndNode, F_WaitPallet) VALUES (" +
                            "" + fdi + "," + rki + "," + rdi + "," + di + ",'" + orders + "','" + en + "','" + wp + "')";
                        dbo.ExceSQL(sql);


                        dbo.TransCommit();
                    }
                    catch (Exception ex)
                    {
                        dbo.TransRollback();
                        throw ex;
                    }
                    MessageBox.Show("每步节点设备和命令信息保存成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                sql = CommSql + " and F_DetailIndex=" + fdi;
                ds= dbo.ExceSQL(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btNullQuery_Click(object sender, EventArgs e)
        {
            cbField.Text = "";
            tbContent.Text = "";

        }

        private void btQuery_Click(object sender, EventArgs e)
        {
            try
            {
                string df = "";

                switch (this.cbField.Text)
                {
                    case "详细路径记录索引":
                        df = "F_DetailIndex";
                        break;
                    case "路径索引":
                        df = "T_Base_Route_Detail.F_RouteKindIndex";
                        break;
                    
                    default:
                        return;
                }
                string sql = CommSql+" and  " + df + "=  " + this.tbContent.Text + "  order by T_Base_Route_Detail.F_RouteKindIndex,T_Base_Route_Detail.F_RouteDetailIndex";
                DataSet ds = dbo.ExceSQL(sql);
                
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void button24_Click(object sender, EventArgs e)
        {
            if (listBox3.SelectedIndex < 0)
            {
                return;
            }
            else
            {
                string item = listBox3.SelectedItem.ToString();
                int idx = listBox3.SelectedIndex - 1;
                if (idx < 0)
                {
                    return;
                }
                listBox3.Items.RemoveAt(listBox3.SelectedIndex);
                listBox3.Items.Insert(idx, item);
                listBox3.SelectedIndex = idx;
            }
        }

        private void button23_Click(object sender, EventArgs e)
        {
            if ((listBox3.SelectedIndex >= listBox3.Items.Count) || (listBox3.SelectedIndex < 0))
            {
                return;
            }
            else
            {
                string item = listBox3.SelectedItem.ToString();
                int idx = listBox3.SelectedIndex + 1;
                if ((idx >= listBox3.Items.Count) || (idx < 0))
                {
                    return;
                }
                listBox3.Items.RemoveAt(listBox3.SelectedIndex);
                listBox3.Items.Insert(idx, item);
                listBox3.SelectedIndex = idx;
            }
        }

        private void button26_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex < 0)
            {
                return;
            }
            else
            {
                string item = listBox2.SelectedItem.ToString();
                int idx = listBox2.SelectedIndex - 1;
                if (idx < 0)
                {
                    return;
                }
                listBox2.Items.RemoveAt(listBox2.SelectedIndex);
                listBox2.Items.Insert(idx, item);
                listBox2.SelectedIndex = idx;
            }
        }

        private void button25_Click(object sender, EventArgs e)
        {
            if ((listBox2.SelectedIndex >= listBox2.Items.Count) || (listBox2.SelectedIndex < 0))
            {
                return;
            }
            else
            {
                string item = listBox2.SelectedItem.ToString();
                int idx = listBox2.SelectedIndex + 1;
                if ((idx >= listBox2.Items.Count) || (idx < 0))
                {
                    return;
                }
                listBox2.Items.RemoveAt(listBox2.SelectedIndex);
                listBox2.Items.Insert(idx, item);
                listBox2.SelectedIndex = idx;
            }
        }

        private void button27_Click(object sender, EventArgs e)
        {
            if ((listBox4.SelectedIndex >= listBox4.Items.Count) || (listBox4.SelectedIndex < 0))
            {
                return;
            }
            else
            {
                string item = listBox4.SelectedItem.ToString();
                int idx = listBox4.SelectedIndex + 1;
                if ((idx >= listBox4.Items.Count) || (idx < 0))
                {
                    return;
                }
                listBox4.Items.RemoveAt(listBox4.SelectedIndex);
                listBox4.Items.Insert(idx, item);
                listBox4.SelectedIndex = idx;
            }
        }

        private void button28_Click(object sender, EventArgs e)
        {
            if (listBox4.SelectedIndex < 0)
            {
                return;
            }
            else
            {
                string item = listBox4.SelectedItem.ToString();
                int idx = listBox4.SelectedIndex - 1;
                if (idx < 0)
                {
                    return;
                }
                listBox4.Items.RemoveAt(listBox4.SelectedIndex);
                listBox4.Items.Insert(idx, item);
                listBox4.SelectedIndex = idx;
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            tbDetailIndex1.Text = "";
            cbAheadDetect.Text = "";
            listBox3.Items.Clear();
            rbtw1.Checked = false;
            rbtw2.Checked = false;
        }

       

        private void button18_Click(object sender, EventArgs e)
        {
            tbDetailIndex3.Text = "";
            cbLockedDeviceIndex.Text = "";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            tbDetailIndex2.Text = "";
            cbAssociateDeviceIndex.Text = "";
            cbAssociateDeviceOrder.Text = "";
            listBox2.Items.Clear();
        }

       

        

        private void button14_Click(object sender, EventArgs e)
        {
            if (listBox3.SelectedIndex < 0)
            {
                return;
            }
            this.listBox3.Items.RemoveAt(listBox3.SelectedIndex);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if ((cbAheadDetect.SelectedValue.ToString() == "") || ((rbtw1.Checked==false) &&(rbtw2.Checked==false ) ))
            {
                return;
            }
            else
            {
                string litem;
                string tw;
                if (this.rbtw1.Checked == true)
                {
                    tw = "有探物检测";
                }
                else
                {
                    tw = "无探物检测";
                }
                litem = cbAheadDetect.Text.Trim() + ":" + cbAheadDetect.SelectedValue.ToString()+":" + tw;
                for (int i = 0; i < listBox3.Items.Count; i++)
                {
                    if (listBox3.Items[i].ToString().IndexOf(cbAheadDetect.SelectedValue.ToString())>0)
                    {
                        return;
                    }
                }
                this.listBox3.Items.Add(litem);
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (Information.IsNumeric(tbDetailIndex1.Text.Trim() ) == false)
            {
                MessageBox.Show("详细路径记录索引只能输入数字!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbDetailIndex1.Focus();
                return;
            }
            //if (listBox3.Items.Count <= 0)
            //{
            //    MessageBox.Show("每步节点提前检测光电组不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    listBox3.Focus();
            //    return;
            //}
            string sql;
            int fdi;
            fdi = Convert.ToInt32(tbDetailIndex1.Text.Trim());
            sql = "select * from T_Base_Route_Detail where F_DetailIndex=" + fdi;
            if (dbo.Exists(sql) == false)
            {
                MessageBox.Show("每步节点提前检测光电组的详细路径记录索引在数据库不存在,请先录入每步节点设备和命令信息!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbDetailIndex1.Focus();
                return;
            }
            string gdjc="" ;
            char[] cc = new char[1] { ':' };
            string[] split;
            if (listBox3.Items.Count > 0)
            {
                for (int i = 0; i < listBox3.Items.Count; i++)
                {
                    split = listBox3.Items[i].ToString().Split(cc);
                    if (split[2] == "有探物检测")
                    {
                        gdjc = gdjc + ";" + "-" + split[1];
                    }
                    else
                    {
                        gdjc = gdjc + ";" + split[1];
                    }
                }
                gdjc = gdjc.Substring(1);
            }
            else
            {
                gdjc = null;
            }
            sql = "UPDATE T_Base_Route_Detail SET F_AheadDetect ='" + gdjc + "'  where F_DetailIndex=" + fdi + "";
            dbo.ExceSQL(sql);
            MessageBox.Show("每步节点提前检测光电组信息修改成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            IfUpdate = false;
            sql = CommSql+" and F_DetailIndex=" + fdi;
            DataSet ds = dbo.ExceSQL(sql);

            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void cbAssociateDeviceIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAssociateDeviceIndex.Text.Trim() == "")
            {
                return;
            }
            DataSet ds1 = dbo.ExceSQL("SELECT F_DeviceIndex,T_Base_Device.F_DeviceKindIndex,F_DeviceCommandIndex,F_DeviceCommandName FROM T_Base_Device ,T_Base_Device_Command Where T_Base_Device.F_DeviceKindIndex = T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" + Convert.ToInt32(this.cbAssociateDeviceIndex.SelectedValue.ToString()) + "");
            DataView dv1 = ds1.Tables[0].DefaultView;
            if (dv1.Count > 0)
            {
                this.cbAssociateDeviceOrder.ValueMember = "F_DeviceCommandIndex";
                this.cbAssociateDeviceOrder.DisplayMember = "F_DeviceCommandName";
                this.cbAssociateDeviceOrder.DataSource = dv1;

            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex < 0)
            {
                return;
            }
            this.listBox2.Items.RemoveAt(listBox2.SelectedIndex);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if ((cbAssociateDeviceIndex.SelectedValue.ToString() == "") || (cbAssociateDeviceOrder.SelectedValue.ToString() == ""))
            {
                return;
            }
            else
            {
                string litem;
                litem = cbAssociateDeviceIndex.Text.Trim() + "=" + cbAssociateDeviceIndex.SelectedValue.ToString() + "-" + cbAssociateDeviceOrder.Text.Trim() + "=" + cbAssociateDeviceOrder.SelectedValue.ToString();
                for (int i = 0; i < listBox2.Items.Count; i++)
                {
                    if (litem == listBox2.Items[i].ToString())
                    {
                        return;
                    }
                }
                this.listBox2.Items.Add(litem);
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (Information.IsNumeric(tbDetailIndex2.Text.Trim()) == false)
            {
                MessageBox.Show("详细路径记录索引只能输入数字!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbDetailIndex2.Focus();
                return;
            }
            //if (listBox2.Items.Count <= 0)
            //{
            //    MessageBox.Show("每步节点关联设备和命令组不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    listBox2.Focus();
            //    return;
            //}
            string sql;
            int fdi;
            fdi = Convert.ToInt32(tbDetailIndex2.Text.Trim());
            sql = "select * from T_Base_Route_Detail where F_DetailIndex=" + fdi;
            if (dbo.Exists(sql) == false)
            {
                MessageBox.Show("每步节点关联设备和命令的详细路径记录索引在数据库不存在,请先录入每步节点设备和命令信息!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbDetailIndex2.Focus();
                return;
            }
            string glsbml = "";
            char[] cc = new char[1] { '=' };
            char[] cc1 = new char[1] { '-' };
            string[] split;
            string[] split1;
            if (listBox2.Items.Count > 0)
            {
                for (int i = 0; i < listBox2.Items.Count; i++)
                {
                    split1 = listBox2.Items[i].ToString().Split(cc1);
                    split = split1[0].Split(cc);
                    glsbml = glsbml + ";" + split[1];
                    split = split1[1].Split(cc);
                    glsbml = glsbml + "-" + split[1];

                }
                glsbml = glsbml.Substring(1);
            }
            else
            {
                glsbml = null;
            }
            sql = "UPDATE T_Base_Route_Detail SET F_AssociateDeviceIndex ='" + glsbml + "'  where F_DetailIndex=" + fdi + "";
            dbo.ExceSQL(sql);
            MessageBox.Show("每步节点关联设备和命令信息修改成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            IfUpdate = false;
            sql = CommSql+" and F_DetailIndex=" + fdi;
            DataSet ds = dbo.ExceSQL(sql);

            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void button20_Click(object sender, EventArgs e)
        {
            if (cbLockedDeviceIndex.SelectedValue.ToString() == "")
            {
                return;
            }
            else
            {
                string litem;
                litem = cbLockedDeviceIndex.Text.Trim() + "=" + cbLockedDeviceIndex.SelectedValue.ToString() ;
                for (int i = 0; i < listBox4.Items.Count; i++)
                {
                    if (litem == listBox4.Items[i].ToString())
                    {
                        return;
                    }
                }
                this.listBox4.Items.Add(litem);
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            if (listBox4.SelectedIndex < 0)
            {
                return;
            }
            this.listBox4.Items.RemoveAt(listBox4.SelectedIndex);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (Information.IsNumeric(tbDetailIndex3.Text.Trim()) == false)
            {
                MessageBox.Show("详细路径记录索引只能输入数字!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbDetailIndex3.Focus();
                return;
            }
            //if (listBox4.Items.Count <= 0)
            //{
            //    MessageBox.Show("每步节点运行时锁定设备组不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    listBox4.Focus();
            //    return;
            //}
            string sql;
            int fdi;
            fdi = Convert.ToInt32(tbDetailIndex3.Text.Trim());
            sql = "select * from T_Base_Route_Detail where F_DetailIndex=" + fdi;
            if (dbo.Exists(sql) == false)
            {
                MessageBox.Show("每步节点运行时锁定设备组的详细路径记录索引在数据库不存在,请先录入每步节点设备和命令信息!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbDetailIndex3.Focus();
                return;
            }
            string sdsb = "";
            char[] cc = new char[1] { '=' };
            string[] split;
            if (listBox4.Items.Count > 0)
            {
                for (int i = 0; i < listBox4.Items.Count; i++)
                {
                    split = listBox4.Items[i].ToString().Split(cc);
                    sdsb = sdsb + ";" + split[1];

                }
                sdsb = sdsb.Substring(1);
            }
            else
            {
                sdsb = null;
            }
            sql = "UPDATE T_Base_Route_Detail SET F_LockedDeviceIndex ='" + sdsb + "'  where F_DetailIndex=" + fdi + "";
            dbo.ExceSQL(sql);
            MessageBox.Show("每步节点运行时锁定设备组信息修改成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            IfUpdate = false;
            sql = CommSql+" and F_DetailIndex=" + fdi;
            DataSet ds = dbo.ExceSQL(sql);

            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            IfUpdate = false;
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            
            if (dataGridView1.RowCount <= 0)
            {
                return;
            }
            if (MessageBox.Show("您确认要删除选中行的“详细路径:" + this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "”信息吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            int di = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value);
            dbo.ExceSQL("delete from T_Base_Route_Detail  where F_DetailIndex =" + di);
            button8_Click(sender, e);
            MessageBox.Show("成功删除选中行的“详细路径:" + di + "”信息!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            IfUpdate = true;
            IFInsert = false;
            this.tabControl1.SelectTab("tabPage1");
            tbDetailIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + ""; 
            tbDetailIndex_TextChanged(sender, e);
        }

        private void tbDetailIndex_TextChanged(object sender, EventArgs e)
        {
            char[] cc = new char[1] {';' };
            string[] split;
            string sql;
            if (tbDetailIndex.Text == "") return;
            DataSet ds = dbo.ExceSQL("SELECT * FROM  T_Base_Route_Detail where F_DetailIndex=" + Convert.ToInt32(tbDetailIndex.Text.Trim()));
            DataView dv=ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                //cbRouteKindIndex
                cbRouteKindIndex.Text = dv[0]["F_RouteKindIndex"].ToString();
                cbRouteKindIndex_SelectedIndexChanged(sender, e);
                //tbRouteDetailIndex
                tbRouteDetailIndex.Text = dv[0]["F_RouteDetailIndex"].ToString();
                //cbDeviceIndex
                sql = "SELECT F_DeviceCommandIndex,F_DeviceCommandName,F_DeviceIndex,F_DeviceName FROM T_Base_Device ,T_Base_Device_Command  where  T_Base_Device.F_DeviceKindIndex = T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" + Convert.ToInt32(dv[0]["F_DeviceIndex"]);
                DataSet ds1 = dbo.ExceSQL(sql);
                DataView dv1 = ds1.Tables[0].DefaultView;
                if (dv1.Count <= 0)
                {
                    MessageBox.Show("详细路径表记录的设备索引在设备表不存在!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    cbDeviceIndex.Text = dv1[0]["F_DeviceName"].ToString();
                    cbDeviceIndex_SelectedIndexChanged(sender, e);
                }
                //chbEndNode
                if (dv[0]["F_EndNode"].ToString() == "1")
                {
                    chbEndNode.Checked = true;
                }
                else
                {
                    chbEndNode.Checked = false;
                }
                //chbWaitPallet
                if (dv[0]["F_WaitPallet"].ToString() == "1")
                {
                    chbWaitPallet.Checked = true;
                }
                else
                {
                    chbWaitPallet.Checked = false;
                }
                //listBox1
                listBox1.Items.Clear();
                split = dv[0]["F_Orders"].ToString().Split(cc);
                for (int i = split.GetLowerBound(0); i <= split.GetUpperBound(0); i++)
                {
                    sql = "SELECT F_DeviceCommandIndex,F_DeviceCommandName,F_DeviceIndex,F_DeviceName FROM T_Base_Device,T_Base_Device_Command  where  T_Base_Device.F_DeviceKindIndex = T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" + Convert.ToInt32(dv[0]["F_DeviceIndex"]) + " and F_DeviceCommandIndex=" + Convert.ToInt32(split[i]);
                    DataSet ds2 = dbo.ExceSQL(sql);
                    DataView dv2 = ds2.Tables[0].DefaultView;
                    if (dv2.Count <= 0)
                    {
                        MessageBox.Show("详细路径表记录的设备命令在设备命令表不存在!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        listBox1.Items.Add(dv2[0]["F_DeviceCommandName"].ToString() + "=" + dv2[0]["F_DeviceCommandIndex"].ToString());
                    }
                }
            }
            else
            {
                this.listBox1.Items.Clear();
                this.cbDeviceIndex.Text = "";
                this.cbOrders.Text = "";
                this.cbRouteKindIndex.Text = "";
                this.chbEndNode.Checked = false;
                this.chbWaitPallet.Checked = false;
                this.tbRouteDetailIndex.Text = "";
                this.tbRouteKind.Text = "";
            }
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            IfUpdate = true;
            IFInsert = false;
            this.tabControl1.SelectTab("tabPage2");
            tbDetailIndex1.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            tbDetailIndex1_TextChanged(sender, e);
        }

        private void tbDetailIndex1_TextChanged(object sender, EventArgs e)
        {
            char[] cc = new char[1] { ';' };
            string[] split;
            string sql;
            string fh;
            string tw;
            if (tbDetailIndex1.Text == "") return;
            DataSet ds = dbo.ExceSQL("SELECT * FROM  T_Base_Route_Detail where F_DetailIndex=" + Convert.ToInt32(tbDetailIndex1.Text.Trim()));
            DataView dv = ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                if ((dv[0]["F_AheadDetect"] == DBNull.Value) || (dv[0]["F_AheadDetect"].ToString().Trim() == ""))
                {
                    listBox3.Items.Clear();
                    return;
                }
                //listBox3
                listBox3.Items.Clear();
                split = dv[0]["F_AheadDetect"].ToString().Split(cc);
                for (int i = split.GetLowerBound(0); i <= split.GetUpperBound(0); i++)
                {
                    if (split[i].Substring(0, 1) == "-")
                    {
                        fh = split[i].Substring(1);
                        tw = "有探物检测";
                    }
                    else
                    {
                        fh = split[i];
                        tw = "无探物检测";
                    }
                    sql = "SELECT F_DeviceIndex,F_DeviceName FROM T_Base_Device  where F_DeviceIndex=" + Convert.ToInt32(fh);
                    DataSet ds2 = dbo.ExceSQL(sql);
                    DataView dv2 = ds2.Tables[0].DefaultView;
                    if (dv2.Count <= 0)
                    {
                        MessageBox.Show("详细路径表记录的设备命令在设备命令表不存在!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        listBox3.Items.Add(dv2[0]["F_DeviceName"].ToString() + ":" + dv2[0]["F_DeviceIndex"].ToString() + ":" + tw);
                    }
                }
            }
            else
            {
                this.listBox3.Items.Clear();
                
            }
        }

        private void tbDetailIndex2_TextChanged(object sender, EventArgs e)
        {
            char[] cc = new char[1] { ';' };
            char[] cc1 = new char[1] { '-' };
            string[] split;
            string[] split1;
            string sql;
            
            if (tbDetailIndex2.Text == "") return;
            DataSet ds = dbo.ExceSQL("SELECT * FROM  T_Base_Route_Detail where F_DetailIndex=" + Convert.ToInt32(tbDetailIndex2.Text.Trim()));
            DataView dv = ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                if ((dv[0]["F_AssociateDeviceIndex"] == DBNull.Value)||(dv[0]["F_AssociateDeviceIndex"].ToString().Trim() == ""))
                {
                    listBox2.Items.Clear();
                    return;
                }
                //listBox2
                listBox2.Items.Clear();
                split = dv[0]["F_AssociateDeviceIndex"].ToString().Split(cc);
                for (int i = split.GetLowerBound(0); i <= split.GetUpperBound(0); i++)
                {
                    split1 = split[i].Split(cc1);

                    sql = "SELECT F_DeviceIndex,F_DeviceName,F_DeviceCommandIndex,F_DeviceCommandName FROM T_Base_Device,T_Base_Device_Command Where T_Base_Device.F_DeviceKindIndex = T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" + Convert.ToInt32(split1[0]) + " and F_DeviceCommandIndex=" + Convert.ToInt32(split1[1]);
                    DataSet ds2 = dbo.ExceSQL(sql);
                    DataView dv2 = ds2.Tables[0].DefaultView;
                    if (dv2.Count <= 0)
                    {
                        MessageBox.Show("详细路径表记录的设备和命令在设备和命令表不存在!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        listBox2.Items.Add(dv2[0]["F_DeviceName"].ToString() + "=" + dv2[0]["F_DeviceIndex"].ToString() + "-" + dv2[0]["F_DeviceCommandName"].ToString() + "=" + dv2[0]["F_DeviceCommandIndex"].ToString());
                    }
                }
            }
            else
            {
                this.listBox2.Items.Clear();

            }
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            IfUpdate = true;
            IFInsert = false;
            this.tabControl1.SelectTab("tabPage4");
            tbDetailIndex2.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            tbDetailIndex2_TextChanged(sender, e);
        }

        private void tbDetailIndex3_TextChanged(object sender, EventArgs e)
        {
            char[] cc = new char[1] { ';' };
            
            string[] split;
            
            string sql;

            if (tbDetailIndex3.Text == "") return;
            DataSet ds = dbo.ExceSQL("SELECT * FROM  T_Base_Route_Detail where F_DetailIndex=" + Convert.ToInt32(tbDetailIndex3.Text.Trim()));
            DataView dv = ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                if ((dv[0]["F_LockedDeviceIndex"] == DBNull.Value)||(dv[0]["F_LockedDeviceIndex"].ToString().Trim() == ""))
                {
                    listBox4.Items.Clear();
                    return;
                }
                //listBox4
                listBox4.Items.Clear();
                split = dv[0]["F_LockedDeviceIndex"].ToString().Split(cc);
                for (int i = split.GetLowerBound(0); i <= split.GetUpperBound(0); i++)
                {
                    sql = "SELECT F_DeviceIndex,F_DeviceName FROM T_Base_Device Where  F_DeviceIndex=" + Convert.ToInt32(split[i]);
                    DataSet ds2 = dbo.ExceSQL(sql);
                    DataView dv2 = ds2.Tables[0].DefaultView;
                    if (dv2.Count <= 0)
                    {
                        MessageBox.Show("详细路径表记录的设备在设备表不存在!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        listBox4.Items.Add(dv2[0]["F_DeviceName"].ToString() + "=" + dv2[0]["F_DeviceIndex"].ToString());
                    }
                }
            }
            else
            {
                this.listBox4.Items.Clear();

            }
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            IfUpdate = true;
            IFInsert = false;
            this.tabControl1.SelectTab("tabPage5");
            tbDetailIndex3.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            tbDetailIndex3_TextChanged(sender, e);
        }
        
        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            IFInsert = true;
            IfUpdate = false;

            cbRouteKindIndex.Enabled = false;
            tbRouteDetailIndex.Enabled = false;
            tbRouteKind.Enabled = false;
            button5.Enabled = false;
            button1.Enabled = false;

            this.tabControl1.SelectTab("tabPage1");
            tbDetailIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            tbDetailIndex_TextChanged(sender, e);
            string a, b, c;
            a=cbRouteKindIndex.Text;
            b=tbRouteDetailIndex.Text;
            c=tbRouteKind.Text ;

            button1_Click(sender, e);
            listBox1.Items.Clear();
            cbRouteKindIndex.Text=a  ;
            cbRouteKindIndex_SelectedIndexChanged(sender, e);
            tbRouteDetailIndex.Text=b  ;
            tbRouteKind.Text=c  ;

        }

        private void button29_Click(object sender, EventArgs e)
        {
            IFInsert = false;
            tbDetailIndex.Enabled = true;
            cbRouteKindIndex.Enabled = true;
            tbRouteDetailIndex.Enabled = true;
            tbRouteKind.Enabled = true;
            button5.Enabled = true;
            button1.Enabled = true;
            
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            button29_Click(sender, e);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string sql = CommSql +"  order by T_Base_Route_Detail.F_RouteKindIndex,T_Base_Route_Detail.F_RouteDetailIndex";
            DataSet ds = dbo.ExceSQL(sql);

            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void button33_Click(object sender, EventArgs e)
        {
            if ((cbBlockDevice.SelectedValue.ToString() == "") || (cbBlockDeviceOrder.SelectedValue.ToString() == ""))
            {
                return;
            }
            else
            {
                string litem;
                litem = cbBlockDevice.Text.Trim() + "=" + cbBlockDevice.SelectedValue.ToString() + "-" + cbBlockDeviceOrder.Text.Trim() + "=" + cbBlockDeviceOrder.SelectedValue.ToString();
                for (int i = 0; i < listBox5.Items.Count; i++)
                {
                    if (litem == listBox5.Items[i].ToString())
                    {
                        return;
                    }
                }
                this.listBox5.Items.Add(litem);
            }
        }

        private void button32_Click(object sender, EventArgs e)
        {
            if (listBox5.SelectedIndex < 0)
            {
                return;
            }
            this.listBox5.Items.RemoveAt(listBox5.SelectedIndex);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (listBox5.SelectedIndex < 0)
            {
                return;
            }
            else
            {
                string item = listBox5.SelectedItem.ToString();
                int idx = listBox5.SelectedIndex - 1;
                if (idx < 0)
                {
                    return;
                }
                listBox5.Items.RemoveAt(listBox5.SelectedIndex);
                listBox5.Items.Insert(idx, item);
                listBox5.SelectedIndex = idx;
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if ((listBox5.SelectedIndex >= listBox5.Items.Count) || (listBox5.SelectedIndex < 0))
            {
                return;
            }
            else
            {
                string item = listBox5.SelectedItem.ToString();
                int idx = listBox5.SelectedIndex + 1;
                if ((idx >= listBox5.Items.Count) || (idx < 0))
                {
                    return;
                }
                listBox5.Items.RemoveAt(listBox5.SelectedIndex);
                listBox5.Items.Insert(idx, item);
                listBox5.SelectedIndex = idx;
            }
        }

        private void button31_Click(object sender, EventArgs e)
        {
            tbDetailIndex4.Text = "";
            cbBlockDevice.Text = "";
            cbBlockDeviceOrder.Text = "";
            listBox5.Items.Clear();
        }

        private void button30_Click(object sender, EventArgs e)
        {
            if (Information.IsNumeric(tbDetailIndex4.Text.Trim()) == false)
            {
                MessageBox.Show("详细路径记录索引只能输入数字!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbDetailIndex4.Focus();
                return;
            }
            //if (listBox5.Items.Count <= 0)
            //{
            //    MessageBox.Show("每步节点阻挡设备和命令组不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    listBox5.Focus();
            //    return;
            //}
            string sql;
            int fdi;
            fdi = Convert.ToInt32(tbDetailIndex4.Text.Trim());
            sql = "select * from T_Base_Route_Detail where F_DetailIndex=" + fdi;
            if (dbo.Exists(sql) == false)
            {
                MessageBox.Show("每步节点阻挡设备和命令的详细路径记录索引在数据库不存在,请先录入每步节点设备和命令信息!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbDetailIndex4.Focus();
                return;
            }
            string glsbml = "";
            char[] cc = new char[1] { '=' };
            char[] cc1 = new char[1] { '-' };
            string[] split;
            string[] split1;
            if (listBox5.Items.Count > 0)
            {
                for (int i = 0; i < listBox5.Items.Count; i++)
                {
                    split1 = listBox5.Items[i].ToString().Split(cc1);
                    split = split1[0].Split(cc);
                    glsbml = glsbml + ";" + split[1];
                    split = split1[1].Split(cc);
                    glsbml = glsbml + "-" + split[1];

                }
                glsbml = glsbml.Substring(1);
            }
            else
            {
                glsbml = null;
            }
            sql = "UPDATE T_Base_Route_Detail SET F_BlockDevice ='" + glsbml + "'  where F_DetailIndex=" + fdi + "";
            dbo.ExceSQL(sql);
            MessageBox.Show("每步节点阻挡设备和命令信息修改成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            IfUpdate = false;
            sql = CommSql+" and F_DetailIndex=" + fdi;
            DataSet ds = dbo.ExceSQL(sql);

            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            IfUpdate = true;
            IFInsert = false;
            this.tabControl1.SelectTab("tabPage3");
            tbDetailIndex4.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            tbDetailIndex4_TextChanged(sender, e);
        }

        private void tbDetailIndex4_TextChanged(object sender, EventArgs e)
        {
            char[] cc = new char[1] { ';' };
            char[] cc1 = new char[1] { '-' };
            string[] split;
            string[] split1;
            string sql;

            if (tbDetailIndex4.Text == "") return;
            DataSet ds = dbo.ExceSQL("SELECT * FROM  T_Base_Route_Detail where F_DetailIndex=" + Convert.ToInt32(tbDetailIndex4.Text.Trim()));
            DataView dv = ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                if ((dv[0]["F_BlockDevice"] == DBNull.Value) || (dv[0]["F_BlockDevice"].ToString().Trim() == ""))
                {
                    listBox5.Items.Clear();
                    return;
                }
                //listBox5
                listBox5.Items.Clear();
                split = dv[0]["F_BlockDevice"].ToString().Split(cc);
                for (int i = split.GetLowerBound(0); i <= split.GetUpperBound(0); i++)
                {
                    split1 = split[i].Split(cc1);

                    sql = "SELECT F_DeviceIndex,F_DeviceName,F_DeviceCommandIndex,F_DeviceCommandName FROM T_Base_Device,T_Base_Device_Command Where T_Base_Device.F_DeviceKindIndex = T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" + Convert.ToInt32(split1[0]) + " and F_DeviceCommandIndex=" + Convert.ToInt32(split1[1]);
                    DataSet ds2 = dbo.ExceSQL(sql);
                    DataView dv2 = ds2.Tables[0].DefaultView;
                    if (dv2.Count <= 0)
                    {
                        MessageBox.Show("详细路径表记录的设备和命令在设备和命令表不存在!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        listBox5.Items.Add(dv2[0]["F_DeviceName"].ToString() + "=" + dv2[0]["F_DeviceIndex"].ToString() + "-" + dv2[0]["F_DeviceCommandName"].ToString() + "=" + dv2[0]["F_DeviceCommandIndex"].ToString());
                    }
                }
            }
            else
            {
                this.listBox5.Items.Clear();

            }
        }

        private void cbBlockDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbBlockDevice.Text.Trim() == "")
            {
                return;
            }
            DataSet ds1 = dbo.ExceSQL("SELECT F_DeviceIndex,T_Base_Device.F_DeviceKindIndex,F_DeviceCommandIndex,F_DeviceCommandName FROM T_Base_Device ,T_Base_Device_Command Where T_Base_Device.F_DeviceKindIndex = T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" + Convert.ToInt32(this.cbBlockDevice.SelectedValue.ToString()) + "");
            DataView dv1 = ds1.Tables[0].DefaultView;
            if (dv1.Count > 0)
            {
                this.cbBlockDeviceOrder.ValueMember = "F_DeviceCommandIndex";
                this.cbBlockDeviceOrder.DisplayMember = "F_DeviceCommandName";
                this.cbBlockDeviceOrder.DataSource = dv1;

            }
        }

        private void button37_Click(object sender, EventArgs e)
        {
            tbDetailIndex5.Text = "";
            cbAheadTrigger.Text = "";
            cbAheadTriggerOrder.Text = "";
            listBox6.Items.Clear();
        }

        private void tbDetailIndex5_TextChanged(object sender, EventArgs e)
        {
            char[] cc = new char[1] { ';' };
            char[] cc1 = new char[1] { '-' };
            string[] split;
            string[] split1;
            string sql;

            if (tbDetailIndex5.Text == "") return;
            DataSet ds = dbo.ExceSQL("SELECT * FROM  T_Base_Route_Detail where F_DetailIndex=" + Convert.ToInt32(tbDetailIndex5.Text.Trim()));
            DataView dv = ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                if ((dv[0]["F_AheadTrigger"] == DBNull.Value) || (dv[0]["F_AheadTrigger"].ToString().Trim() == ""))
                {
                    listBox6.Items.Clear();
                    return;
                }
                //listBox6
                listBox6.Items.Clear();
                split = dv[0]["F_AheadTrigger"].ToString().Split(cc);
                for (int i = split.GetLowerBound(0); i <= split.GetUpperBound(0); i++)
                {
                    split1 = split[i].Split(cc1);

                    sql = "SELECT F_DeviceIndex,F_DeviceName,F_DeviceCommandIndex,F_DeviceCommandName FROM T_Base_Device,T_Base_Device_Command Where T_Base_Device.F_DeviceKindIndex = T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" + Convert.ToInt32(split1[0]) + " and F_DeviceCommandIndex=" + Convert.ToInt32(split1[1]);
                    DataSet ds2 = dbo.ExceSQL(sql);
                    DataView dv2 = ds2.Tables[0].DefaultView;
                    if (dv2.Count <= 0)
                    {
                        MessageBox.Show("详细路径表记录的设备和命令在设备和命令表不存在!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        listBox6.Items.Add(dv2[0]["F_DeviceName"].ToString() + "=" + dv2[0]["F_DeviceIndex"].ToString() + "-" + dv2[0]["F_DeviceCommandName"].ToString() + "=" + dv2[0]["F_DeviceCommandIndex"].ToString());
                    }
                }
            }
            else
            {
                this.listBox6.Items.Clear();

            }
        }

        private void button39_Click(object sender, EventArgs e)
        {
            if ((cbAheadTrigger.SelectedValue.ToString() == "") || (cbAheadTriggerOrder.SelectedValue.ToString() == ""))
            {
                return;
            }
            else
            {
                string litem;
                litem = cbAheadTrigger.Text.Trim() + "=" + cbAheadTrigger.SelectedValue.ToString() + "-" + cbAheadTriggerOrder.Text.Trim() + "=" + cbAheadTriggerOrder.SelectedValue.ToString();
                for (int i = 0; i < listBox6.Items.Count; i++)
                {
                    if (litem == listBox6.Items[i].ToString())
                    {
                        return;
                    }
                }
                this.listBox6.Items.Add(litem);
            }
        }

        private void button38_Click(object sender, EventArgs e)
        {
            if (listBox6.SelectedIndex < 0)
            {
                return;
            }
            this.listBox6.Items.RemoveAt(listBox6.SelectedIndex);
        }

        private void button35_Click(object sender, EventArgs e)
        {
            if (listBox6.SelectedIndex < 0)
            {
                return;
            }
            else
            {
                string item = listBox6.SelectedItem.ToString();
                int idx = listBox6.SelectedIndex - 1;
                if (idx < 0)
                {
                    return;
                }
                listBox6.Items.RemoveAt(listBox6.SelectedIndex);
                listBox6.Items.Insert(idx, item);
                listBox6.SelectedIndex = idx;
            }
        }

        private void button34_Click(object sender, EventArgs e)
        {
            if ((listBox6.SelectedIndex >= listBox6.Items.Count) || (listBox6.SelectedIndex < 0))
            {
                return;
            }
            else
            {
                string item = listBox6.SelectedItem.ToString();
                int idx = listBox6.SelectedIndex + 1;
                if ((idx >= listBox6.Items.Count) || (idx < 0))
                {
                    return;
                }
                listBox6.Items.RemoveAt(listBox6.SelectedIndex);
                listBox6.Items.Insert(idx, item);
                listBox6.SelectedIndex = idx;
            }
        }

        private void button36_Click(object sender, EventArgs e)
        {
            if (Information.IsNumeric(tbDetailIndex5.Text.Trim()) == false)
            {
                MessageBox.Show("详细路径记录索引只能输入数字!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbDetailIndex5.Focus();
                return;
            }
            //if (listBox6.Items.Count <= 0)
            //{
            //    MessageBox.Show("提前触发设备和命令组不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    listBox6.Focus();
            //    return;
            //}
            string sql;
            int fdi;
            fdi = Convert.ToInt32(tbDetailIndex5.Text.Trim());
            sql = "select * from T_Base_Route_Detail where F_DetailIndex=" + fdi;
            if (dbo.Exists(sql) == false)
            {
                MessageBox.Show("提前触发设备和命令的详细路径记录索引在数据库不存在,请先录入提前触发设备和命令信息!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbDetailIndex5.Focus();
                return;
            }
            string glsbml = "";
            char[] cc = new char[1] { '=' };
            char[] cc1 = new char[1] { '-' };
            string[] split;
            string[] split1;
            if (listBox6.Items.Count > 0)
            {
                for (int i = 0; i < listBox6.Items.Count; i++)
                {
                    split1 = listBox6.Items[i].ToString().Split(cc1);
                    split = split1[0].Split(cc);
                    glsbml = glsbml + ";" + split[1];
                    split = split1[1].Split(cc);
                    glsbml = glsbml + "-" + split[1];

                }
                glsbml = glsbml.Substring(1);
            }
            else
            {
                glsbml = null;
            }
            sql = "UPDATE T_Base_Route_Detail SET F_AheadTrigger ='" + glsbml + "'  where F_DetailIndex=" + fdi + "";
            dbo.ExceSQL(sql);
            MessageBox.Show("提前触发设备和命令信息修改成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            IfUpdate = false;
            sql = CommSql+" and F_DetailIndex=" + fdi;
            DataSet ds = dbo.ExceSQL(sql);

            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            IfUpdate = true;
            IFInsert = false;
            this.tabControl1.SelectTab("tabPage7");
            tbDetailIndex5.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            tbDetailIndex5_TextChanged(sender, e);
        }

        private void cbAheadTrigger_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAheadTrigger.Text.Trim() == "")
            {
                return;
            }
            DataSet ds1 = dbo.ExceSQL("SELECT F_DeviceIndex,T_Base_Device.F_DeviceKindIndex,F_DeviceCommandIndex,F_DeviceCommandName FROM T_Base_Device ,T_Base_Device_Command Where T_Base_Device.F_DeviceKindIndex = T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" + Convert.ToInt32(this.cbAheadTrigger.SelectedValue.ToString()) + "");
            DataView dv1 = ds1.Tables[0].DefaultView;
            if (dv1.Count > 0)
            {
                this.cbAheadTriggerOrder.ValueMember = "F_DeviceCommandIndex";
                this.cbAheadTriggerOrder.DisplayMember = "F_DeviceCommandName";
                this.cbAheadTriggerOrder.DataSource = dv1;

            }
        }


    }
}