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