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

namespace SystemConfig
{
    public partial class FrmAGVGateEdit : Form
    {
        bool IfUpdate = false;
        DBOperator dbo =CStaticClass.dbo;
        private static FrmAGVGateEdit _formInstance;

        public static FrmAGVGateEdit FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmAGVGateEdit();

                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmAGVGateEdit()
        {
            InitializeComponent();
            _formInstance = this;
        }

        private void btLaneGateDeviceIndex_Click(object sender, EventArgs e)
        {
            FrmDeviceEdit.FormInstance.LaneGateDeviceIndex = true;
            FrmDeviceEdit.FormInstance.LaneIndex = false;
            FrmDeviceEdit.FormInstance.IsSelected = true;
            FrmDeviceEdit.FormInstance.ShowDialog();
        }

        private void btLaneIndex_Click(object sender, EventArgs e)
        {
            FrmDeviceEdit.FormInstance.LaneIndex = true;
            FrmDeviceEdit.FormInstance.LaneGateDeviceIndex = false;
            FrmDeviceEdit.FormInstance.IsSelected = true;
            FrmDeviceEdit.FormInstance.ShowDialog();
        }

        private void btNull_Click(object sender, EventArgs e)
        {
            this.tbAGVGateDeviceIndex.Text = "";
            this.tbChannelsIndex.Text = "";
            this.tbTop.Text = "";
            this.tbLeft.Text = "";
            this.tbAddress.Text = "";
            this.chbInput.Checked = false;
            this.chbOutput.Checked = false;

        }

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

        private void btSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("��ȷ��Ҫ������ǣ�վ����Ϣ��", "������ʾ��", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }

            if (Information.IsNumeric(this.tbAGVGateDeviceIndex.Text) == false)
            {
                MessageBox.Show("���ǣ�վ�㴦��Ӧ���豸����ֻ�����������ͣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbAGVGateDeviceIndex.Focus();
                return;
            }
            if (Information.IsNumeric(this.tbPCS.Text) == false)
            {
                MessageBox.Show("PCSվ��ֻ�����������ͣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbPCS.Focus();
                return;
            }
            if (Information.IsNumeric(this.tbChannelsIndex.Text) == false)
            {
                MessageBox.Show("���ǣ�ͨ��ֻ�����������ͣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbChannelsIndex.Focus();
                return;
            }
            if (Information.IsNumeric(this.tbTop.Text) == false)
            {
                MessageBox.Show("���ǣ�վ��Y����ֻ�����������ͣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbTop.Focus();
                return;
            }
            if (Information.IsNumeric(this.tbLeft.Text) == false)
            {
                MessageBox.Show("���ǣ�վ��X����ֻ�����������ͣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbLeft.Focus();
                return;
            }
            //if (Information.IsNumeric(this.tbChanel.Text) == false)
            //{
            //    MessageBox.Show("���ǣ�ͨ����ֻ�����������ͣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    this.tbChanel.Focus();
            //    return;
            //}
            //if (Information.IsNumeric(this.tblayer.Text) == false)
            //{
            //    MessageBox.Show("���ǣֲ��ַֻ�����������ͣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    this.tblayer.Focus();
            //    return;
            //}
            if ((this.chbInput.Checked == false) && (this.chbOutput.Checked == false))
            {
                MessageBox.Show("����ѡ���dz����ڡ��͡�������ڡ���", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {//tbChanel.Text.Trim()+";"+tbAddress.Text.Trim()+";"+tblayer.Text.Trim()
                string addr = tbAddress.Text.Trim();
                char inp, outp;
                if (this.chbInput.Checked == true)
                {
                    inp = '1';
                }
                else
                {
                    inp = '0';
                }
                if (this.chbOutput.Checked == true)
                {
                    outp = '1';
                }
                else
                {
                    outp = '0';
                }
                string sql = "SELECT F_AGVGateDeviceIndex FROM T_Base_Agv_Gate where F_AGVGateDeviceIndex=" + Convert.ToInt32(this.tbAGVGateDeviceIndex.Text);
                DataSet ds = dbo.ExceSQL(sql);
                if ((ds.Tables[0].DefaultView.Count > 0))
                {
                    if (IfUpdate == false)
                    {
                        MessageBox.Show("���ǣ�վ�㴦��Ӧ���豸���������ݿ��Ѿ����ڣ��벻Ҫ�ظ�¼�����ݣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.tbAGVGateDeviceIndex.Focus();
                        return;
                    }
                    
                }
                else
                {
                    IfUpdate = false;
                }
                ds.Clear();
                if (IfUpdate == true)
                {
                    sql = "UPDATE T_Base_AGV_Gate SET F_ChannelsIndex =" + Convert.ToInt32(this.tbChannelsIndex.Text)
                        + ", F_Address ='" + addr + "',F_Top=" + tbTop.Text + ",F_Left=" + tbLeft.Text + ", F_Input =" + inp +
                        ", F_Output =" + outp + ",f_pcssite ="+ tbPCS.Text +" where F_AGVGateDeviceIndex=" + Convert.ToInt32(this.tbAGVGateDeviceIndex.Text);
                    dbo.ExceSQL(sql);
                    MessageBox.Show("���ǣ�վ����Ϣ�޸ijɹ���", "������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    IfUpdate = false;
                }
                else
                {

                    sql = "INSERT INTO T_Base_AGV_Gate(F_AGVGateDeviceIndex, F_ChannelsIndex, F_Address, F_Input, F_Output,F_Top,F_Left,F_PCSSite)VALUES" +
                        " (" + Convert.ToInt32(this.tbAGVGateDeviceIndex.Text) + "," + Convert.ToInt32(this.tbChannelsIndex.Text)
                        + ",'" + addr + "'," + inp + "," + outp + "," + tbTop.Text + "," + tbLeft.Text + "," + tbPCS.Text + ")";
                    dbo.ExceSQL(sql);
                    MessageBox.Show("���ǣ�վ����Ϣ¼��ɹ���", "������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                sql = "SELECT F_AGVGateDeviceIndex AS AGVվ�㴦��Ӧ���豸����,f_devicename as �豸����,F_ChannelsIndex AS AGVͨ������,F_Address AS " +
                        " AGVվ���ַ,F_Input AS �������,F_Output AS �dz�����,F_Top as վ��Y����,F_Left as վ��X����,f_pcssite as PCSվ��λ�� FROM T_Base_AGV_Gate,t_base_device " +
                        " where f_deviceindex = f_agvgatedeviceindex and " +
                    " F_AGVGateDeviceIndex=" + Convert.ToInt32(this.tbAGVGateDeviceIndex.Text);
                ds = dbo.ExceSQL(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView ;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

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

        private void btQuery_Click(object sender, EventArgs e)
        {
            try
            {
                string df, sql;
                if (this.tbContent.Text.Trim() == "") return;
                if (this.cbField.Text == "AGVͨ������")
                {
                    df = "F_ChannelsIndex";
                }
                else
                {
                    df = "F_AGVGateDeviceIndex";
                }
                sql = "SELECT F_AGVGateDeviceIndex AS AGVվ�㴦��Ӧ���豸����,f_devicename as �豸����,F_ChannelsIndex AS AGVͨ������,F_Address AS " +
                        " AGVվ���ַ,F_Input AS �������,F_Output AS �dz�����,F_Top as վ��Y����,F_Left as վ��X����,f_pcssite as PCSվ��λ�� FROM T_Base_AGV_Gate,t_base_device " +
                        " where f_deviceindex = f_agvgatedeviceindex and " +
                        df + "= " + Convert.ToInt32(this.tbContent.Text);
                DataSet ds = dbo.ExceSQL(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount <= 0)
            {
                return;
            }
            char[] cc = new char[1] { ';' };
            string[] spl;
            spl = this.dataGridView1.CurrentRow.Cells[0].Value.ToString().Split(cc);
            this.tabControl1.SelectTab("tabPage1");

            this.tbAGVGateDeviceIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            this.tbChannelsIndex.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString() + "";
            this.tbAddress.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
            //this.tbChanel.Text = addr[0];
            //this.tbAddress.Text = addr[1];
            //this.tblayer.Text = addr[2];
            if (this.dataGridView1.CurrentRow.Cells[4].Value.ToString() == "1")
            {
                this.chbInput.Checked = true;
            }
            else
            {
                this.chbInput.Checked = false;
            }
            if (this.dataGridView1.CurrentRow.Cells[5].Value.ToString() == "1")
            {
                this.chbOutput.Checked = true;
            }
            else
            {
                this.chbOutput.Checked = false;
            }
            this.tbTop.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString() + "";
            this.tbLeft.Text = this.dataGridView1.CurrentRow.Cells[7].Value.ToString() + "";
            this .tbPCS.Text = this.dataGridView1.CurrentRow.Cells[8].Value.ToString() + "";
            IfUpdate = true;
        }

        private void tsmDel_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;
            }
            dbo.ExceSQL("delete from T_Base_Agv_Gate  where F_AGVGateDeviceIndex =" + Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value));
            button2_Click(sender, e);
        }

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

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = "SELECT F_AGVGateDeviceIndex AS AGVվ�㴦��Ӧ���豸����,f_devicename as �豸����,F_ChannelsIndex AS AGVͨ������,F_Address AS " +
                        " AGVվ���ַ,F_Input AS �������,F_Output AS �dz�����,F_Top as վ��Y����,F_Left as վ��X����,f_pcssite as PCSվ��λ�� FROM T_Base_AGV_Gate,t_base_device where f_deviceindex = f_agvgatedeviceindex";
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        

        

        
        
    }
}