using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBFactory;
using Microsoft.VisualBasic;
namespace SystemConfig
{
    public partial class FrmStackRobotPosition : Form
    {
        bool IfUpdate = false;
        DBOperator dbo = CStaticClass.dbo;
        private static FrmStackRobotPosition _formInstance;

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

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

        private void btBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

        private void btNull_Click(object sender, EventArgs e)
        {
            this.tbStackPositionDeviceIndex.Text = "";
            this.tbDeviceOrder.Text = "";
            this.tbManageID.Text = "";
            this.tbPalletNo.Text = "";
        }

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

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

        }

        private void btQuery_Click(object sender, EventArgs e)
        {

            try
            {
                string df, sql;
                if (this.tbContent.Text.Trim() == "") return;
                if (this.cbField.Text == "码垛位置对应的设备索引")
                {
                    df = "F_StackPositionDeviceIndex";
                }
                else//此位置的码垛命令索引
                {
                    df = "F_DeviceOrder";
                }
                sql = "SELECT F_StackPositionDeviceIndex AS 码垛位置对应的设备索引,F_DeviceOrder AS 此位置的码垛命令索引,F_ManageID AS " +
                        " 锁定此位置的调度任务的FManageID,F_PalletNo AS 此位置上存放的托盘编号,F_GoodsAmount AS 此位置需要码放的货物数量 FROM T_BASE_STACK_ROBOT  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 button2_Click(object sender, EventArgs e)
        {
            string sql;
            sql = "SELECT F_StackPositionDeviceIndex AS 码垛位置对应的设备索引,F_DeviceOrder AS 此位置的码垛命令索引,F_ManageID AS " +
                        " 锁定此位置的调度任务的FManageID,F_PalletNo AS 此位置上存放的托盘编号,F_GoodsAmount AS 此位置需要码放的货物数量 FROM T_BASE_STACK_ROBOT";
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要保存码垛位置信息吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            string sql = "";
            string strsql;
            if (Information.IsNumeric(this.tbStackPositionDeviceIndex.Text) == false)
            {
                MessageBox.Show("码垛位置对应的设备索引只能是数字类型!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (Information.IsNumeric(this.tbDeviceOrder.Text)==false)
            {
                MessageBox.Show("此位置的码垛命令索引只能是数字类型!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (Information.IsNumeric(this.tbGoodsAmount.Text) == false)
            {
                MessageBox.Show("此位置需要码放的货物数量只能是数字类型!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (Information.IsNumeric(this.tbManageID.Text) == false)
            {
                MessageBox.Show("锁定此位置的调度任务的FManageID只能是数字类型!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int _spdi, _do, _mID,_gamount;
            string _pno;
            
            
            _spdi=Convert.ToInt32( this.tbStackPositionDeviceIndex.Text.Trim());
            _do =Convert.ToInt32( this.tbDeviceOrder.Text.Trim());
            if (this.tbManageID.Text.Trim() == "")
            {
                _mID =0;
            }
            else
            {
                _mID = Convert.ToInt32(this.tbManageID.Text.Trim());
            }
            if (this.tbGoodsAmount.Text.Trim() == "")
            {
                _gamount = 0;
            }
            else
            {
                _gamount = Convert.ToInt32(this.tbGoodsAmount.Text.Trim());
            }
            if (this.tbPalletNo.Text.Trim() == "")
            {
                _pno = "-";
            }
            else
            {
                _pno = this.tbPalletNo.Text.Trim();
            }

            DataSet ds;
            strsql = "SELECT F_StackPositionDeviceIndex, F_DeviceOrder, F_ManageID, F_PalletNo FROM T_BASE_STACK_ROBOT WHERE  F_StackPositionDeviceIndex = "+ _spdi;
            if (dbo.Exists(strsql) == false)
            {
                IfUpdate = false;
            }
            if (IfUpdate == true)
            {
                sql = "update T_BASE_STACK_ROBOT  set F_DeviceOrder=" + _do + ",F_ManageID=" + _mID + ",  F_PalletNo=  '" + _pno + "',F_GoodsAmount="+_gamount+"  where F_StackPositionDeviceIndex=" + _spdi;
                ds = dbo.ExceSQL(sql);
                MessageBox.Show("码垛位置信息修改成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                IfUpdate = false;
            }
            else
            {
                strsql = "SELECT F_StackPositionDeviceIndex, F_DeviceOrder, F_ManageID, F_PalletNo FROM T_BASE_STACK_ROBOT WHERE  F_StackPositionDeviceIndex = " + _spdi;
                if (dbo.Exists(strsql) == true)
                {

                    MessageBox.Show("码垛位置对应的设备索引不允许输入重复的值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                sql = "INSERT INTO T_BASE_STACK_ROBOT (F_StackPositionDeviceIndex, F_DeviceOrder,F_ManageID,F_PalletNo,F_GoodsAmount)VALUES (" + _spdi + "," + _do + "," + _mID + ",'" + _pno + "',"+_gamount+")";
                ds = dbo.ExceSQL(sql);
                MessageBox.Show("码垛位置信息录入成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            ds.Clear();
            strsql = "SELECT F_StackPositionDeviceIndex AS 码垛位置对应的设备索引,F_DeviceOrder AS 此位置的码垛命令索引,F_ManageID AS " +
                        " 锁定此位置的调度任务的FManageID,F_PalletNo AS 此位置上存放的托盘编号,F_GoodsAmount AS 此位置需要码放的货物数量 FROM T_BASE_STACK_ROBOT where " +
                        " F_StackPositionDeviceIndex= " + _spdi  ;
            ds = dbo.ExceSQL(strsql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {
             
            if (dataGridView1.RowCount <= 0)
            {
                return;
            }
            this.tabControl1.SelectTab("tabPage1");
            this.tbStackPositionDeviceIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            this.tbDeviceOrder.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString()+"";
            this.tbManageID.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString() + "";
            this.tbPalletNo.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString() + "";
            this.tbGoodsAmount.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_BASE_STACK_ROBOT  where F_StackPositionDeviceIndex =" + Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value));
            button2_Click(sender, e);
        }
    }
}