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("库房信息修改成功！", "操作提示：", 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;
        }

        
    }
}