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 FrmDeviceKindEdit : Form
    {
        bool IfUpdate = false;
        private  bool _isSelected = false;

        public  bool IsSelected
        {
            get { return _isSelected; }
            set {  _isSelected = value; }
        }
        private static FrmDeviceKindEdit _formInstance;

        public static FrmDeviceKindEdit FormInstance
        {
            get 
            {
                if (_formInstance == null) 
                {
                    _formInstance = new FrmDeviceKindEdit();
                    
                }
                return _formInstance;
            }
            set { FrmDeviceKindEdit._formInstance = value; }
        }
        DBOperator dbo = CStaticClass.dbo;
        
        public FrmDeviceKindEdit()
        {
            InitializeComponent();
            _formInstance = this;
            dbo.Open();
        }

        private void FrmDeviceKindEdit_Load(object sender, EventArgs e)
        {
            this.tabControl1.SelectTab("tabPage2");
            if (_isSelected == true)
            {
                btSelectOK.Visible = true;
            }
            else
                btSelectOK.Visible = false;

        }

        

        private void btQuery_Click(object sender, EventArgs e)
        {
            if (tbContent.Text.Trim() == "") return;
            try
            {
                
                string sql = "";
                switch (this.cbField.Text)
                {
                    
                    
                    case "设备类型索引":
                        sql = "SELECT  F_DeviceKindIndex as 设备类型索引,F_DeviceKindName as 设备类型名称 FROM T_Base_Device_Kind  WHERE  F_DeviceKindIndex = '" + this.tbContent.Text + "'";
                        
                        break;
                    case "设备类型名称":
                        sql = "SELECT  F_DeviceKindIndex as 设备类型索引,F_DeviceKindName as 设备类型名称 FROM T_Base_Device_Kind  WHERE  F_DeviceKindName = '" + this.tbContent.Text + "'";
                        break;
                    
                    default:
                        return;
                }
                
                DataSet ds = dbo.ExceSQL(sql);
                //if (ds.Tables[0].DefaultView.Count > 0)
                //{
                    this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                //}
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

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

        }

        private void btNull_Click(object sender, EventArgs e)
        {
            
            this.tbDeviceKindIndex.Text = "";
            this.tbDeviceName.Text = "";
            
        }

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

        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.tbDeviceKindIndex.Text) == false)
            {
                MessageBox.Show("设备类型索引只能是数字类型！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.tbDeviceName.Text.Trim().Length== 0)
            {
                MessageBox.Show("设备类型名称不允许是空值！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            DataSet ds;
            strsql = "SELECT  F_DeviceKindIndex as 设备类型索引,F_DeviceKindName as 设备类型名称 FROM T_Base_Device_Kind  WHERE  F_DeviceKindIndex = '" + this.tbDeviceKindIndex.Text + "'";
            if (dbo.Exists(strsql) == false)
            {
                IfUpdate = false;
            }
            if (IfUpdate == true)
            {
                sql = "update T_Base_Device_Kind  set   F_DeviceKindName=  '" + this.tbDeviceName.Text + "'  where F_DeviceKindIndex=" + Convert.ToInt32(this.tbDeviceKindIndex.Text);
                ds = dbo.ExceSQL(sql);
                MessageBox.Show("设备类型信息修改成功！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                IfUpdate = false;
            }
            else
            {
                strsql = "SELECT  F_DeviceKindIndex as 设备类型索引,F_DeviceKindName as 设备类型名称 FROM T_Base_Device_Kind  WHERE  F_DeviceKindIndex = '" + this.tbDeviceKindIndex.Text + "'";
                ds = dbo.ExceSQL(strsql);
                if(ds.Tables[0].DefaultView.Count>0 )
                {
                    MessageBox.Show("设备类型索引不允许输入重复的值！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                ds.Clear();
                sql = "INSERT INTO T_Base_Device_Kind (F_DeviceKindIndex, F_DeviceKindName)VALUES (" + Convert.ToInt32(this.tbDeviceKindIndex.Text) + ",'" + this.tbDeviceName.Text + "')";
                ds = dbo.ExceSQL(sql);
                MessageBox.Show("设备类型信息录入成功！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            ds.Clear();
            strsql = "SELECT  F_DeviceKindIndex as 设备类型索引,F_DeviceKindName as 设备类型名称 FROM T_Base_Device_Kind  WHERE  F_DeviceKindIndex = '" + this.tbDeviceKindIndex.Text + "'";
            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.tbDeviceKindIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            this.tbDeviceName.Text = this.dataGridView1.CurrentRow.Cells[1].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_Device_Kind where F_DeviceKindIndex =" +Convert.ToInt32( this.dataGridView1.CurrentRow.Cells[0].Value));
            button2_Click(sender, e);
        }

        private void btSelectOK_Click(object sender, EventArgs e)
        {
            
            FrmDeviceEdit.FormInstance.tbDeviceKindIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            FrmDeviceEdit.FormInstance.tbDeviceName.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
            this.Close();
        }

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

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = "SELECT  F_DeviceKindIndex as 设备类型索引,F_DeviceKindName as 设备类型名称 FROM T_Base_Device_Kind";
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                
        }

        
        
    }
}