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 FrmStackInfoEdit : Form
    {
        bool IfUpdate = false;
        DBOperator dbo = CStaticClass.dbo;
        private static FrmStackInfoEdit _formInstance;

        public static FrmStackInfoEdit FormInstance
        {
            get 
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmStackInfoEdit();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }

        public FrmStackInfoEdit()
        {
            InitializeComponent();
            _formInstance = this;
        }

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

        private void btNull_Click(object sender, EventArgs e)
        {
            tbStackIndex.Text = "";
            tbLaneNo.Text = "";
            cbForkAmount.Text = "";
            cbReach.Text = "";
        }

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

            if (Information.IsNumeric(this.tbStackIndex.Text) == false)
            {
                MessageBox.Show("�Ѷ���豸����ֻ�����������ͣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbStackIndex.Focus();
                return;
            }
            if (this.tbLaneNo.Text.Trim().Length == 0)
            {
                MessageBox.Show("�����Ų����������ֵ��", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbLaneNo.Focus();
                return;
            }

            if (this.cbForkAmount.Text.Trim().Length == 0)
            {
                MessageBox.Show("�������Ͳ����������ֵ��", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbForkAmount.Focus();
                return;
            }
            if (this.cbReach.Text.Trim().Length == 0)
            {
                MessageBox.Show("����������Ͳ����������ֵ��", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbReach.Focus();
                return;
            }
            
            try
            {
                string  ln;
                int si,reach, fork;
                si = Convert.ToInt32(this.tbStackIndex.Text );
                ln = this.tbLaneNo.Text;
                //����
                //˫��
                if (this.cbForkAmount.Text == "����")
                {
                    fork = 1;
                }
                else
                {
                    fork = 2;
                }
                //������
                //˫����
                if (this.cbReach.Text == "������")
                {
                    reach=1;
                }
                else
                {
                    reach=2;
                }
                string sql = "SELECT F_StackIndex AS �Ѷ���豸����,F_LaneNo AS ������,F_ForkAmount AS " +
                        " ��������,F_Reach AS ����������� FROM T_Base_StackInfo where F_StackIndex=" +si;
                DataSet ds = dbo.ExceSQL(sql);
                if ((ds.Tables[0].DefaultView.Count > 0))
                {
                    if (IfUpdate == false)
                    {
                        MessageBox.Show("�Ѷ���豸���������ݿ��Ѿ����ڣ��벻Ҫ�ظ�¼�����ݣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.tbStackIndex.Focus();
                        return;
                    }

                }
                else
                {
                    IfUpdate = false;
                }
                ds.Clear();
                if (IfUpdate == true)
                {
                    sql = "UPDATE T_Base_StackInfo SET F_LaneNo ='" + ln + "', F_ForkAmount =" + fork + ",F_Reach=" + reach + " where F_StackIndex=" + si;
                    dbo.ExceSQL(sql);
                    MessageBox.Show("�Ѷ���豸��Ϣ�޸ijɹ���", "������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    IfUpdate = false;
                }
                else
                {

                    sql = "INSERT INTO T_Base_StackInfo(F_StackIndex, F_LaneNo,  F_ForkAmount, F_Reach)VALUES" +
                        " (" + si+ ",'" + ln+ "'," + fork + "," + reach + ")";
                    dbo.ExceSQL(sql);
                    MessageBox.Show("�Ѷ���豸��Ϣ¼��ɹ���", "������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                sql = "SELECT F_StackIndex AS �Ѷ���豸����,F_LaneNo AS ������,F_ForkAmount AS " +
                        " ��������,F_Reach AS ����������� FROM T_Base_StackInfo where F_StackIndex=" + si;
                ds = dbo.ExceSQL(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btQuery_Click(object sender, EventArgs e)
        {
            
            try
            {
                string df, sql;
                if (this.tbContent.Text.Trim() == "") return;
                if (this.cbField.Text == "�Ѷ���豸����")
                {
                    df = "F_StackIndex";
                }
                else//������
                {
                    df = "F_LaneNo";
                }
                sql = "SELECT F_StackIndex AS �Ѷ���豸����,F_LaneNo AS ������,F_ForkAmount AS " +
                        " ��������,F_Reach AS ����������� FROM T_Base_StackInfo where " +
                        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 btNullQuery_Click(object sender, EventArgs e)
        {
            this.cbField.Text = "";
            this.tbContent.Text = "";

        }

        private void FrmStackInfoEdit_Load(object sender, EventArgs e)
        {

        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount <= 0)
            {
                return;
            }
            
            this.tabControl1.SelectTab("tabPage1");

            this.tbStackIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            this.tbLaneNo.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString() + "";
            //����
            //˫��
            if (this.dataGridView1.CurrentRow.Cells[2].Value.ToString() + "" == "1")
            {
                this.cbForkAmount.Text = "����";
            }
            else
            {
                this.cbForkAmount.Text = "˫��";
            }
            //������
            //˫����
            if (this.dataGridView1.CurrentRow.Cells[3].Value.ToString() + "" == "1")
            {
                this.cbReach.Text = "������";
            }
            else
            {
                this.cbReach.Text = "˫����";
            }
            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_StackInfo  where F_StackIndex =" + Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value));
            button2_Click(sender, e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = "SELECT F_StackIndex AS �Ѷ���豸����,F_LaneNo AS ������,F_ForkAmount AS " +
                        " ��������,F_Reach AS ����������� FROM T_Base_StackInfo";
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        
    }
}