using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBFactory;
namespace SystemConfig
{
    public partial class FrmPCSLocation : Form
    {
        DBOperator dboM = new DBOperator("ManConnString", "ManDBFactory");
        private static FrmPCSLocation _formInstance;
        string CommomSql = "select location as PCS货架编号,areano 货区编号,agvloc as PCS站地址 from locationtab  ";
        public static FrmPCSLocation FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmPCSLocation();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmPCSLocation()
        {
            InitializeComponent();
            _formInstance = this;
        }

        private void btNull_Click(object sender, EventArgs e)
        {
            this.tbAreano.Text = "";
            this.tbLocation.Text = "";
            this.nudAgvloc.Value = 0;
        }

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

        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataView dv = dboM.ExceSQL(CommomSql ).Tables[0].DefaultView;
            dataGridView1.DataSource = dv;
        }

        private void btQuery_Click(object sender, EventArgs e)
        {
            try
            {
                string df, sql;
                if ((this.tbContent.Text.Trim() == "") ||(this.cbField.Text.Trim()=="")) return;
                if (this.cbField.Text == "PCS货架编号")
                {
                    df = "location";
                }
                else if (this.cbField.Text == "货区编号")
                {
                    df = "areano";
                }
                else//
                {
                    df = "agvloc";
                }
                sql = CommomSql + " where " + df + "= '" + this.tbContent.Text + "'";
                DataSet ds = dboM.ExceSQL(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要修改选中货架编号PCS站地址信息吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }


            if (this.tbLocation.Text.Trim().Length == 0)
            {
                MessageBox.Show("PCS货架编号不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbLocation.Focus();
                return;
            }
            string sql = "";
            try
            {
                sql = "UPDATE locationtab t SET agvloc = " + nudAgvloc.Value + " where t.location= '" + this.tbLocation.Text + "'";
                dboM.ExceSQL(sql);
                MessageBox.Show("PCS货架站地址修改成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                sql = CommomSql + " where location= '" + this.tbLocation.Text + "'";
                DataSet ds = dboM.ExceSQL(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

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

            this.tabControl1.SelectTab("tabPage1");

            this.tbLocation.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            this.tbAreano.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString() + "";
            this.nudAgvloc.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString() + "";
        }
    }
}