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("堆垛机设备信息修改成功！", "操作提示：", 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;
        }

        
    }
}