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

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

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

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

        private void btNull_Click(object sender, EventArgs e)
        {
            tbWarehouseIndex.Text = "";
            tbRows.Text = "";
            cbCols.Text = "";
            cbLayers.Text = "";
            tbDescription.Text = "";
        }

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

            if (this.tbWarehouseIndex.Text.Trim() == "")
            {
                MessageBox.Show("�ⷿ���������ǿ�ֵ��", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbWarehouseIndex.Focus();
                return;
            }
            if (Information.IsNumeric(this.tbRows.Text) == false)
            {
                MessageBox.Show("����ֻ�����������ͣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbRows.Focus();
                return;
            }

            if (Information.IsNumeric(this.cbCols.Text) == false)
            {
                MessageBox.Show("����ֻ�����������ͣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbCols.Focus();
                return;
            }
            if (Information.IsNumeric(this.cbLayers.Text) == false)
            {
                MessageBox.Show("����ֻ�����������ͣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbLayers.Focus();
                return;
            }
            if (this.tbDescription.Text.Trim().Length == 0)
            {
                MessageBox.Show("�ⷿ�����������ǿ�ֵ��", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbDescription.Focus();
                return;
            }
            try
            {

                int layers, cols, rows; string whi;
                whi = this.tbWarehouseIndex.Text ;
                rows =Convert.ToInt32( this.tbRows.Text);
                cols = Convert.ToInt32(this.cbCols.Text);
                layers = Convert.ToInt32(this.cbLayers.Text);

                string sql = "SELECT F_WarehouseIndex AS �ⷿ����, F_Rows AS ����, F_Cols AS ����, F_Layers AS ����, F_Description AS �ⷿ���� FROM T_Warehouse where F_WarehouseIndex=" + whi;
                DataSet ds = dbo.ExceSQL(sql);
                if ((ds.Tables[0].DefaultView.Count > 0))
                {
                    if (IfUpdate == false)
                    {
                        MessageBox.Show("�ⷿ���������ݿ��Ѿ����ڣ��벻Ҫ�ظ�¼�����ݣ�", "�������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.tbWarehouseIndex.Focus();
                        return;
                    }

                }
                else
                {
                    IfUpdate = false;
                }
                ds.Clear();
                if (IfUpdate == true)
                {
                    sql = "UPDATE T_Warehouse SET F_Rows =" + rows + ", F_Cols =" + cols + ",F_Layers=" + layers + ",F_Description='" + this.tbDescription.Text + "' where F_WarehouseIndex='" + whi+"'";
                    dbo.ExceSQL(sql);
                    MessageBox.Show("�ⷿ��Ϣ�޸ijɹ���", "������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    IfUpdate = false;
                }
                else
                {

                    sql = "INSERT INTO T_Warehouse(WarehouseIndex,F_Rows,F_Cols,F_Layers,F_Description)VALUES" +
                        " ('" + whi+ "'," + rows+ "," + cols + "," + layers + ",'"+this.tbDescription.Text+"')";
                    dbo.ExceSQL(sql);
                    MessageBox.Show("�ⷿ��Ϣ¼��ɹ���", "������ʾ��", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                sql = sql = "SELECT F_WarehouseIndex AS �ⷿ����, F_Rows AS ����, F_Cols AS ����, F_Layers AS ����, F_Description AS �ⷿ���� FROM T_Warehouse where F_WarehouseIndex=" + whi;
                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_WarehouseIndex";
                }
                else
                {
                    df = "F_Description";
                }
                sql = "SELECT F_WarehouseIndex AS �ⷿ����, F_Rows AS ����,"+
                    " F_Cols AS ����, F_Layers AS ����, F_Description AS �ⷿ����"+
                    " FROM T_Warehouse 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 FrmWareHouseEdit_Load(object sender, EventArgs e)
        {

        }

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

            this.tbWarehouseIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            this.tbRows.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString() + "";
            this.cbCols.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString() + "";
            this.cbLayers.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString()+"";
            this.tbDescription.Text = this.dataGridView1.CurrentRow.Cells[4].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_Warehouse  where F_WarehouseIndex =" + this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
            button2_Click(sender, e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = "SELECT F_WarehouseIndex AS �ⷿ����, F_Rows AS ����,"+
                    " F_Cols AS ����, F_Layers AS ����, F_Description AS �ⷿ���� FROM T_Warehouse ";
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        
    }
}