using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ControlSystem
{
    public partial class FrmLaneTest : Form
    {
        private static FrmLaneTest _formInstance;

        public static FrmLaneTest FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmLaneTest();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        CCommonFunction ccf = new CCommonFunction();
        DBFactory.DBOperator dbo = new DBFactory.DBOperator();
        public FrmLaneTest()
        {
            InitializeComponent();
            _formInstance = this;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //button1.Visible = false;
            //button6.Visible = true;
            //CStaticClass.TestLane[0] = Convert.ToInt32( this.tLane1.Text);
            //CStaticClass.TestLaneCell[0] = this.tCell1.Text;
            //string nextcell=GetNextCell(CStaticClass.TestLane[0], CStaticClass.TestLaneCell[0]);
            //string stack = GetStackNo(CStaticClass.TestLane[0]);
            //int AutoManageIdx = ccf.GetTempManageIdx();
            //    //插入临时调度任务T_Manage_Task(监控下的调度任务)：
            ////ccf.RecordMaxTempManageTaskFID(AutoManageIdx);
            //string str = "INSERT INTO T_Manage_Task(FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE," +
            //    "  FSTARTDEVICE, FSTARTCELL, FENDDEVICE, FENDCELL, FLANEWAY, FSTACK)VALUES " +
            //    "(" + AutoManageIdx + ",2,3,'" + CStaticClass.TestLane[0] + "','" + CStaticClass.TestLaneCell[0] +
            //    "','" + CStaticClass.TestLane[0] + "','" + nextcell + "'," + CStaticClass.TestLane[0] + "," + stack + ")";
            //dbo.ExceSQL(str);
            //CStaticClass.StopTestLane = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //button2.Visible = false;
            //button6.Visible = true;
            //CStaticClass.TestLane[1] =Convert.ToInt32( this.tLane2.Text);
            //CStaticClass.TestLaneCell[1] = this.tCell2.Text;

            //string nextcell = GetNextCell(CStaticClass.TestLane[1], CStaticClass.TestLaneCell[1]);
            //string stack = GetStackNo(CStaticClass.TestLane[1]);
            //int AutoManageIdx = ccf.GetTempManageIdx();
            ////插入临时调度任务T_Manage_Task(监控下的调度任务)：
            ////ccf.RecordMaxTempManageTaskFID(AutoManageIdx);
            //dbo.ExceSQL("INSERT INTO T_Manage_Task(FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE, " +
            //    "  FSTARTDEVICE, FSTARTCELL, FENDDEVICE, FENDCELL, FLANEWAY, FSTACK)VALUES " +
            //    "(" + AutoManageIdx + ",2,3,'" + CStaticClass.TestLane[1] + "','" + CStaticClass.TestLaneCell[1] +
            //    "','" + CStaticClass.TestLane[1] + "','" + nextcell + "'," + CStaticClass.TestLane[1] + "," + stack + ")");
            //CStaticClass.StopTestLane = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //button3.Visible = false;
            //button6.Visible = true;
            //CStaticClass.TestLane[2] =Convert.ToInt32( this.tLane3.Text);
            //CStaticClass.TestLaneCell[2] = this.tCell3.Text;
            //string nextcell = GetNextCell(CStaticClass.TestLane[2], CStaticClass.TestLaneCell[2]);
            //string stack = GetStackNo(CStaticClass.TestLane[2]);
            //int AutoManageIdx = ccf.GetTempManageIdx();
            ////插入临时调度任务T_Manage_Task(监控下的调度任务)：
            ////ccf.RecordMaxTempManageTaskFID(AutoManageIdx);
            //dbo.ExceSQL("INSERT INTO T_Manage_Task(FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE, " +
            //    "  FSTARTDEVICE, FSTARTCELL, FENDDEVICE, FENDCELL, FLANEWAY, FSTACK)VALUES " +
            //    "(" + AutoManageIdx + ",2,3,'" + CStaticClass.TestLane[2] + "','" + CStaticClass.TestLaneCell[2] +
            //    "','" + CStaticClass.TestLane[2] + "','" + nextcell + "'," + CStaticClass.TestLane[2] + "," + stack + ")");
            //CStaticClass.StopTestLane = false;
        }
        string GetNextCell(int lane, string cell)
        {
            char[] cc = new char[1] { '-'};
            string[] sp = cell.Split(cc);
            if (sp.Length > 2)
            {
                DataView dv= dbo.ExceSQL("SELECT * FROM ST_CELL WHERE (FLaneWay=" + lane + " and F_Z=" +
                                sp[0] + " and F_X=" + sp[1] + " and F_Y=" + sp[2] + ")").Tables[0].DefaultView ;
                if (dv.Count > 0)
                {
                    int f_xx = (106 + 2) - Convert.ToInt32(sp[1]);
                    int f_yy = (1 + 8) - Convert.ToInt32(sp[2]);
                    int f_zz = 0;
                    if (Convert.ToInt32(sp[0]) % 2 == 0)
                    {
                        f_zz = Convert.ToInt32(sp[0]) - 1;
                    }
                    else
                    {
                        f_zz = Convert.ToInt32(sp[0]) + 1;
                    }
                    if (lane == 18005)
                    {
                        f_xx = (2 + 52) - Convert.ToInt32(sp[1]);
                        f_yy = (1 + 8) - Convert.ToInt32(sp[2]);

                    }
                    f_xx = 54 + Convert.ToInt32(sp[1]);
                    if (f_xx > 107)
                    {
                        f_xx = 107 - f_xx;
                    }
                    f_yy = 5 + Convert.ToInt32(sp[2]);
                    if (f_yy > 8)
                    {
                        f_yy = 8 - f_yy;
                    }
                    DataView dv0 = dbo.ExceSQL("SELECT * FROM ST_CELL WHERE (FLaneWay=" + lane + ") and F_X="+f_xx+" and F_Y= "+f_yy+"and F_Z ="+f_zz).Tables[0].DefaultView ;
                    if (dv0.Count > 0)
                    {
                        return dv0[0]["FCELLCODE"].ToString();
                    }
                    else
                    {
                        return "1-1-1";
                    }
                }
                else
                {
                    return "1-1-1";
                }
            }
            else
            {
                return "1-1-1";
            }
        }
        string GetStackNo(int lane)
        {
            DataView dv = dbo.ExceSQL("SELECT F_StackIndex, F_LaneNo FROM T_Base_StackInfo WHERE F_LaneNo like '%"+lane.ToString()+"%'").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                return dv[0]["F_StackIndex"].ToString();
            }
            else
            {
                //return "2801";
                return "11001";
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            CStaticClass.StopTestLane = true;
            button1.Visible = true;
            button2.Visible = true;
            button3.Visible = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //button4.Visible = false;
            //button6.Visible = true;
            //CStaticClass.TestLane[2] = Convert.ToInt32(this.textBox2.Text);
            //CStaticClass.TestLaneCell[2] = this.textBox1.Text;
            //string nextcell = GetNextCell(CStaticClass.TestLane[2], CStaticClass.TestLaneCell[2]);
            //string stack = GetStackNo(CStaticClass.TestLane[2]);
            //int AutoManageIdx = ccf.GetTempManageIdx();
            ////插入临时调度任务T_Manage_Task(监控下的调度任务)：
            ////ccf.RecordMaxTempManageTaskFID(AutoManageIdx);
            //dbo.ExceSQL("INSERT INTO T_Manage_Task(FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE, " +
            //    "  FSTARTDEVICE, FSTARTCELL, FENDDEVICE, FENDCELL, FLANEWAY, FSTACK)VALUES " +
            //    "(" + AutoManageIdx + ",2,3,'" + CStaticClass.TestLane[2] + "','" + CStaticClass.TestLaneCell[2] +
            //    "','" + CStaticClass.TestLane[2] + "','" + nextcell + "'," + CStaticClass.TestLane[2] + "," + stack + ")");
            //CStaticClass.StopTestLane = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //button5.Visible = false;
            //button6.Visible = true;
            //CStaticClass.TestLane[2] = Convert.ToInt32(this.textBox4.Text);
            //CStaticClass.TestLaneCell[2] = this.textBox3.Text;
            //string nextcell = "10-26-03";
            //string stack = GetStackNo(CStaticClass.TestLane[2]);
            //int AutoManageIdx = ccf.GetTempManageIdx();
            ////插入临时调度任务T_Manage_Task(监控下的调度任务)：
            ////ccf.RecordMaxTempManageTaskFID(AutoManageIdx);
            //dbo.ExceSQL("INSERT INTO T_Manage_Task(FID, F_ManageTaskKindIndex, FCONTROLTASKTYPE, " +
            //    "  FSTARTDEVICE, FSTARTCELL, FENDDEVICE, FENDCELL, FLANEWAY, FSTACK)VALUES " +
            //    "(" + AutoManageIdx + ",2,3,'" + CStaticClass.TestLane[2] + "','" + CStaticClass.TestLaneCell[2] +
            //    "','" + CStaticClass.TestLane[2] + "','" + nextcell + "'," + CStaticClass.TestLane[2] + "," + stack + ")");
            //CStaticClass.StopTestLane = false;
        }

        
    }
}