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

        public static FrmDeviceCommand FormInstance
        {
            get 
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmDeviceCommand();
                }
                return FrmDeviceCommand._formInstance;
            }
            set { FrmDeviceCommand._formInstance = value; }
        }

        public FrmDeviceCommand()
        {
            InitializeComponent();
            _formInstance = this;
        }

        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.cbDeviceKindIndex.Text) == false)
            {
                MessageBox.Show("É豸ÀàÐÍË÷ÒýÖ»ÄÜÊÇÊý×ÖÀàÐÍ£¡", "Îó²Ù×÷Ìáʾ£º", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (Information.IsNumeric(this.tbDeviceCommandIndex.Text) == false)
            {
                MessageBox.Show("É豸ÃüÁîË÷ÒýÖ»ÄÜÊÇÊý×ÖÀàÐÍ£¡", "Îó²Ù×÷Ìáʾ£º", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.tbDeviceCommandName.Text.Trim().Length == 0)
            {
                MessageBox.Show("É豸ÃüÁîÃû³Æ²»ÔÊÐíÊÇ¿ÕÖµ£¡", "Îó²Ù×÷Ìáʾ£º", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            DataSet ds;
            strsql = "SELECT F_DeviceKindIndex, F_DeviceCommandIndex, F_DeviceCommandName FROM T_Base_Device_Command WHERE  F_DeviceKindIndex =  " +Convert.ToInt32( this.cbDeviceKindIndex.Text.Trim()) + "  and F_DeviceCommandIndex= "+Convert.ToInt32( this.tbDeviceCommandIndex.Text.Trim()) ;
            if (dbo.Exists(strsql) == false)
            {
                IfUpdate = false;
            }
            if (IfUpdate == true)
            {
                sql = "update T_Base_Device_Command set F_DeviceCommandName=  '" + this.tbDeviceCommandName.Text + "'  where F_DeviceKindIndex= " +Convert.ToInt32( this.cbDeviceKindIndex.Text.Trim()) + "  and F_DeviceCommandIndex=" +Convert.ToInt32( this.tbDeviceCommandIndex.Text.Trim()) ;
                ds = dbo.ExceSQL(sql);
                MessageBox.Show("É豸ÀàÐÍÐÅÏ¢Ð޸ijɹ¦£¡", "²Ù×÷Ìáʾ£º", MessageBoxButtons.OK, MessageBoxIcon.Information);
                IfUpdate = false;
            }
            else
            {
                strsql = "SELECT F_DeviceKindIndex, F_DeviceCommandIndex, F_DeviceCommandName FROM T_Base_Device_Command WHERE  F_DeviceKindIndex =  " +Convert.ToInt32( this.cbDeviceKindIndex.Text.Trim()) + "  and F_DeviceCommandIndex=" +Convert.ToInt32( this.tbDeviceCommandIndex.Text.Trim()) + "";
                if (dbo.Exists(strsql) == true)
                {
                
                    MessageBox.Show("É豸ÀàÐÍË÷ÒýºÍÃüÁîË÷Òý²»ÔÊÐíÊäÈëÖØ¸´µÄÖµ£¡", "Îó²Ù×÷Ìáʾ£º", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                sql = "INSERT INTO T_Base_Device_Command (F_DeviceKindIndex, F_DeviceCommandIndex,F_DeviceCommandName)VALUES (" + Convert.ToInt32(this.cbDeviceKindIndex.Text) + "," + Convert.ToInt32(this.tbDeviceCommandIndex.Text) + ",'" + this.tbDeviceCommandName.Text + "')";
                ds = dbo.ExceSQL(sql);
                MessageBox.Show("É豸ÀàÐÍÐÅϢ¼Èë³É¹¦£¡", "²Ù×÷Ìáʾ£º", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            ds.Clear();
            strsql = "SELECT F_DeviceKindIndex AS É豸ÀàÐÍË÷Òý,F_DeviceCommandIndex AS É豸ÃüÁîË÷Òý,F_DeviceCommandName AS " +
                        " É豸ÃüÁîÃû³Æ FROM T_Base_Device_Command  where " +
                        " F_DeviceCommandIndex= " + Convert.ToInt32(this.tbDeviceCommandIndex.Text) + " and F_DeviceKindIndex = " +Convert.ToInt32( this.cbDeviceKindIndex.Text) + "";
            ds = dbo.ExceSQL(strsql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void btQuery_Click(object sender, EventArgs e)
        {
            //É豸ÀàÐÍË÷Òý
            //É豸ÃüÁîË÷Òý
            try
            {
                string df, sql;
                if (this.tbContent.Text.Trim() == "") return;
                if (this.cbField.Text == "É豸ÀàÐÍË÷Òý")
                {
                    df = "F_DeviceKindIndex";
                }
                else
                {
                    df = "F_DeviceCommandIndex";
                }
                sql = "SELECT F_DeviceKindIndex AS É豸ÀàÐÍË÷Òý,F_DeviceCommandIndex AS É豸ÃüÁîË÷Òý,F_DeviceCommandName AS " +
                        " É豸ÃüÁîÃû³Æ FROM T_Base_Device_Command  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)
        {
            cbField.Text = "";
            tbContent.Text = "";

        }

        private void btNull_Click(object sender, EventArgs e)
        {
            this.cbDeviceKindIndex.Text = "";
            this.textBox3.Text = "";
            this.tbDeviceCommandIndex.Text = "";
            this.tbDeviceCommandName.Text = "";
        }

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

        private void FrmDeviceCommand_Load(object sender, EventArgs e)
        {
            DataSet ds = dbo.ExceSQL("SELECT F_DeviceKindIndex, F_DeviceKindName FROM T_Base_Device_Kind");
            DataView dv = ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                this.cbDeviceKindIndex.ValueMember = "F_DeviceKindIndex";
                this.cbDeviceKindIndex.DisplayMember = "F_DeviceKindIndex";
                this.cbDeviceKindIndex.DataSource = dv;
            }
        }

        private void cbDeviceKindIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataSet ds = dbo.ExceSQL("SELECT F_DeviceKindIndex, F_DeviceKindName FROM T_Base_Device_Kind Where F_DeviceKindIndex='" +  cbDeviceKindIndex.Text.Trim()+"'");
            DataView dv = ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                textBox3.Text = dv[0]["F_DeviceKindName"].ToString();
            }
        }

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

            this.cbDeviceKindIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            cbDeviceKindIndex_SelectedIndexChanged(sender, e);
            this.tbDeviceCommandIndex.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString() + "";
            this.tbDeviceCommandName.Text = this.dataGridView1.CurrentRow.Cells[2].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() + "¡±£­£­É豸ÃüÁîË÷Òý:" + this.dataGridView1.CurrentRow.Cells[1].Value.ToString() + "ÐÅÏ¢Âð£¿", "²Ù×÷Ìáʾ£º", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            dbo.ExceSQL("delete from T_Base_Device_Kind  where F_DeviceKindIndex =" + Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value) + " and F_DeviceCommandIndex="+ Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[1].Value));
            button2_Click(sender, e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = "SELECT F_DeviceKindIndex AS É豸ÀàÐÍË÷Òý,F_DeviceCommandIndex AS É豸ÃüÁîË÷Òý,F_DeviceCommandName AS " +
                       " É豸ÃüÁîÃû³Æ FROM T_Base_Device_Command";
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }
    }
}