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

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

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

        private void btSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要保存巷道的动画显示信息吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }

            
            if (this.tbLaneDeviceIndex.Text.Trim().Length == 0)
            {
                MessageBox.Show("巷道编号不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbLaneDeviceIndex.Focus();
                return;
            }

            if (this.tbStartCol.Text.Trim().Length == 0)
            {
                MessageBox.Show("起始列不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbStartCol.Focus();
                return;
            }
            if (this.tbEndCol.Text.Trim().Length == 0)
            {
                MessageBox.Show("结束列不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbEndCol.Focus();
                return;
            }
            if (this.tbLeftX.Text.Trim().Length == 0)
            {
                MessageBox.Show("左侧X坐标不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbLeftX.Focus();
                return;
            }
            if (this.tbLeftY.Text.Trim().Length == 0)
            {
                MessageBox.Show("左侧Y坐标不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbLeftY.Focus();
                return;
            }

            if (this.tbTotalWidth.Text.Trim().Length == 0)
            {
                MessageBox.Show("整个巷道图片宽度不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbTotalWidth.Focus();
                return;
            }
            if (this.tbShelfWidth.Text.Trim().Length == 0)
            {
                MessageBox.Show("单元货架图片宽度不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbShelfWidth.Focus();
                return;
            }

            if (this.tbShelfHeight.Text.Trim().Length == 0)
            {
                MessageBox.Show("单元货架图片高度不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbShelfHeight.Focus();
                return;
            }

            try
            {
                int ldi, sc, ec, lx, ly, sw, sh, tw;
                ldi = Convert.ToInt32(tbLaneDeviceIndex.Text);
                sc = Convert.ToInt32(tbStartCol.Text );
                ec = Convert.ToInt32(tbEndCol.Text);
                lx = Convert.ToInt32(tbLeftX.Text);
                ly = Convert.ToInt32(tbLeftY.Text);
                sw = Convert.ToInt32(tbShelfWidth.Text);
                sh = Convert.ToInt32(tbShelfHeight.Text);
                tw = Convert.ToInt32(tbTotalWidth.Text);
                string sql = "SELECT F_LaneDeviceIndex AS 巷道编号, F_StartCol AS 起始列, F_EndCol AS 结束列,"+
                        " F_LeftX AS 左侧X坐标, F_LeftY AS 左侧Y坐标, F_TotalWidth AS 整个巷道图片宽度, "+
                        "F_ShelfWidth AS 单元货架图片宽度, F_ShelfHeight AS 单元货架图片高度 FROM T_Base_LaneInfo "+
                        " Where F_LaneDeviceIndex='" + tbLaneDeviceIndex.Text + "'";
                DataSet ds = dbo.ExceSQL(sql);
                if ((ds.Tables[0].DefaultView.Count > 0))
                {
                    if (IfUpdate == false)
                    {
                        MessageBox.Show("巷道编号在数据库已经存在,请不要重复录入数据!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.tbLaneDeviceIndex.Focus();
                        return;
                    }

                }
                else
                {
                    IfUpdate = false;
                }
                ds.Clear();
                if (IfUpdate == true)
                {
                    sql = "UPDATE T_Base_LaneInfo SET F_StartCol =" + sc + ", F_EndCol =" + ec + ",F_LeftX=" + lx + ",F_LeftY=" + ly + ",F_ShelfWidth=" + sw + ",F_ShelfHeight=" + sh + ",F_TotalWidth=" + tw + " where F_LaneDeviceIndex=" + ldi;
                    dbo.ExceSQL(sql);
                    MessageBox.Show("巷道的动画显示信息修改成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    IfUpdate = false;
                }
                else
                {

                    sql = "INSERT INTO T_Base_LaneInfo(F_LaneDeviceIndex,F_StartCol, F_EndCol,  F_LeftX, F_LeftY,F_ShelfWidth,F_ShelfHeight,F_TotalWidth)VALUES" +
                        " (" + ldi + "," + sc + "," + ec + "," + lx + "," + ly + "," + sw + "," + sh + "," + tw + ")";
                    dbo.ExceSQL(sql);
                    MessageBox.Show("堆垛机设备信息录入成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                sql = "SELECT F_LaneDeviceIndex AS 巷道编号, F_StartCol AS 起始列, F_EndCol AS 结束列," +
                        " F_LeftX AS 左侧X坐标, F_LeftY AS 左侧Y坐标, F_TotalWidth AS 整个巷道图片宽度, " +
                        "F_ShelfWidth AS 单元货架图片宽度, F_ShelfHeight AS 单元货架图片高度 FROM T_Base_LaneInfo " +
                        " Where F_LaneDeviceIndex='" + tbLaneDeviceIndex.Text + "'";
                ds = dbo.ExceSQL(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

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

        private void btNull_Click(object sender, EventArgs e)
        {
            this.tbLaneDeviceIndex.Text = "";
            this.tbLeftX.Text = "";
            this.tbLeftY.Text = "";
            this.tbStartCol.Text = "";
            this.tbEndCol.Text = "";
            this.tbShelfHeight.Text = "";
            this.tbShelfWidth.Text = "";
            this.tbTotalWidth.Text = "";
        }

        private void btQuery_Click(object sender, EventArgs e)
        {
            if (tbContent.Text.Trim() == "") return;
            string sql = "SELECT F_LaneDeviceIndex AS 巷道编号, F_StartCol AS 起始列, F_EndCol AS 结束列,"+
                        " F_LeftX AS 左侧X坐标, F_LeftY AS 左侧Y坐标, F_TotalWidth AS 整个巷道图片宽度, "+
                        "F_ShelfWidth AS 单元货架图片宽度, F_ShelfHeight AS 单元货架图片高度 FROM T_Base_LaneInfo "+
                        " Where F_LaneDeviceIndex='"+ tbContent.Text +"'" ;
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

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

        private void FrmLaneFlashInfo_Load(object sender, EventArgs e)
        {

        }

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

            this.tabControl1.SelectTab("tabPage1");

            this.tbLaneDeviceIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            this.tbStartCol.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString() + "";
            this.tbEndCol.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString() + "";
            this.tbLeftX.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString() + "";
            this.tbLeftY.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString() + "";
            this.tbTotalWidth.Text = this.dataGridView1.CurrentRow.Cells[5].Value.ToString() + "";
            this.tbShelfWidth.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString() + "";
            this.tbShelfHeight.Text = this.dataGridView1.CurrentRow.Cells[7].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_LaneInfo  where F_LaneDeviceIndex =" + Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value));
            button2_Click(sender, e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = "SELECT F_LaneDeviceIndex AS 巷道编号, F_StartCol AS 起始列, F_EndCol AS 结束列," +
                        " F_LeftX AS 左侧X坐标, F_LeftY AS 左侧Y坐标, F_TotalWidth AS 整个巷道图片宽度, " +
                        "F_ShelfWidth AS 单元货架图片宽度, F_ShelfHeight AS 单元货架图片高度 FROM T_Base_LaneInfo ";
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }
    }
}