using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 小型宾馆管理系统
{
    public partial class mainform2 : Form
    {
        public mainform2()
        {
            InitializeComponent();
        }
       public void  showItems1()
       {
           groupBox1.Visible = true;
           dataGridView1.Visible = true;
           comboBox1 .Visible =true ;
           comboBox2.Visible = true;
           button1 .Visible =true ;
           button2.Visible = true;
           textBox1 .Visible =false ;
           textBox2 .Visible =false ;
           comboBox3 .Visible =false ;
           button3 .Visible =false ;
           label1.Visible = false;
           label2.Visible = false;
           label3.Visible = false;
           
       }
        public void showItems2()
        {
            groupBox1.Visible = true;
            dataGridView1.Visible = true;
            comboBox1.Visible = false ;
            comboBox2.Visible = false;
            button1.Visible = false ;
            button2.Visible = false;
            textBox1.Visible = true ;
            textBox2.Visible = true ;
            comboBox3.Visible = true ;
            button3.Visible = true ;
            label1.Visible = true ;
            label2.Visible = true ;
            label3.Visible = true ;
 
        }
        void RefreshData()
        {
            string comStr;
            database db = new database();
            DataSet ds = new DataSet();
            comStr = "select * from tbl_room";
            ds = db.GetDataFromDB(comStr);
            if (ds == null)
            {
                MessageBox.Show("没有任何记录！");
            }
            else
            {
                dataGridView1 .DataSource = ds.Tables[0];
            }
        }
        void combox１ItemUpdate()
        {
            comboBox1.Items.Clear();
              string sqlstr;
            sqlstr = "select roomNo from tbl_room";
            database db = new database();
            DataSet ds = new DataSet();
            ds = db.GetDataFromDB(sqlstr);
       if (ds != null)


                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    comboBox1.Items.Add(ds.Tables[0].Rows[i].ItemArray[0]);
                }
        }


        void combox3ItemUpdate()
        {
            comboBox3.Items.Clear();
         string sqlstr;
            sqlstr = "select roomtypename from tbl_roomtype";
            database db = new database();
            DataSet ds = new DataSet();
            ds = db.GetDataFromDB(sqlstr);
            
            if (ds != null)
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    comboBox3.Items.Add(ds.Tables[0].Rows[i].ItemArray[0]);
                }
        }

        private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showItems1();
            RefreshData();
            button1.Text = "按房间号查询";
            combox１ItemUpdate();
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (textBox2.Enabled)
            {
                int n = this.dataGridView1.CurrentCell.RowIndex;
                textBox1 .Text  = this.dataGridView1[0, n].Value.ToString();
                textBox2 .Text  = this.dataGridView1[2, n].Value.ToString();
                comboBox3 .Text  = this.dataGridView1[1, n].Value.ToString();
            }
        }

        private void mainform2_Load(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            comboBox1.Visible = false;
            comboBox2.Visible = false;
            button1.Visible = false;
            button2.Visible = false;
            textBox1.Visible = false;
            textBox2.Visible = false;
            comboBox3.Visible = false;
            button3.Visible = false;
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            dataGridView1.Visible = false;
            

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            
                if (comboBox1.Text != "")
                {
                    if (button1 .Text  == "按房间号查询")
                    {
                        string comStr;
                        database db = new database();
                        DataSet ds = new DataSet();
                        //comStr = "select * from tbl_room";
                        comStr = "select * from tbl_room where roomno='" + comboBox1.Text.Trim() + " '";

                        ds = db.GetDataFromDB(comStr);
                        if (ds == null)
                        {
                            MessageBox.Show("没有任何记录！");
                        }
                        else
                        {
                            dataGridView1.DataSource = ds.Tables[0];
                           
                        }
                    }
                    else
                    {
                        if (MessageBox.Show("确定要删除该房间信息吗？", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            string sqlStr;
                            sqlStr = "delete from tbl_room where roomno='" +comboBox1 .Text .Trim () + "'";
                            database db = new database();
                            db.UpdataDB(sqlStr);
                            RefreshData();
                        }
 
                    }
                    
                }
                else
                {
                    MessageBox.Show("请选择要查询的房间号！");
                }

            }

        private void button3_Click(object sender, EventArgs e)
        {

            if (textBox1.Text != "" && textBox2.Text != "" && comboBox3.Text != "")
            {
                string x;
                x = "select bed ,price from tbl_roomtype where roomtypename='" + comboBox3.Text.Trim() + "'";
                database db = new database();
                DataSet ds = new DataSet();
                ds = db.GetDataFromDB(x);
                string a, b;
                b = ds.Tables[0].Rows[0].ItemArray[1].ToString();

                a = ds.Tables[0].Rows[0].ItemArray[0].ToString();
                //string a, b, c;
                //a = "1";
                //b = "100";
                //c = "否";
                if (button3.Text == "添加")
                {
                    textBox1.Enabled = true;
                    string sqlStr1;
                    sqlStr1 = "insert into tbl_room values('" + textBox1.Text.Trim() + "','" + comboBox3.Text.Trim() + "','" + textBox2.Text.Trim() + "','" + a.Trim() + "','" + b.Trim() + "'," + false +")";
                    database db1 = new database();
                    bool b1 = db1.UpdataDB(sqlStr1);
                    if (b1)
                    {
                        MessageBox.Show("添加成功！");
                    }

                }


                else
                {
                    string sqlStr2,sqlStr3;
                    database db2 = new database();
                    DataSet ds2 = new DataSet();
                    sqlStr2 = "select inornot from tbl_room where roomNo='"+textBox1 .Text.Trim ()+"'";
                    ds2=db2.GetDataFromDB(sqlStr2 );
                    if(ds2.Tables [0].Rows [0].ItemArray [0].ToString ()=="true")

                    sqlStr3 = "Update tbl_room set roomno='" + textBox1.Text.Trim() + "',roomtypename='" + comboBox3.Text.Trim() + "',roomfloor='" + textBox2.Text.Trim() + "',bed='" + a.ToString() + "',price='" + b.ToString() + "',inornot=" + true + " where roomno='" + textBox1.Text.Trim() + "'";
                    else
                    sqlStr3 = "Update tbl_room set roomno='" + textBox1.Text.Trim() + "',roomtypename='" + comboBox3.Text.Trim() + "',roomfloor='" + textBox2.Text.Trim() + "',bed='" + a.ToString() + "',price='" + b.ToString() + "',inornot=" + false  + " where roomno='" + textBox1.Text.Trim() + "'";

                    db2.UpdataDB(sqlStr3);

                    RefreshData();
                }
            }
            else
            {
                MessageBox.Show("请输入完整信息！");
            }
                   
                   
            
        }

        

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showItems2();
            combox3ItemUpdate();
            RefreshData();
            button3.Text = "添加";
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showItems2();
            textBox1.Enabled = false;
            combox3ItemUpdate();
            RefreshData();
            button3.Text = "修改";

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showItems1();
            RefreshData();
            comboBox2.Visible = false;
            button2.Visible = false;
            button1.Text = "按房间号删除";
            combox１ItemUpdate();
        }

        private void 返回ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainform a = new mainform();
            a.Show();
            this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            string sqlstr = "";
            if (comboBox2.Text == "已入住")
                sqlstr = "select * from tbl_room where inornot=" + true + "";
            if (comboBox2.Text == "未入住")
                sqlstr = "select * from tbl_room where inornot=" + false + "";
            database db = new database();
            DataSet ds = new DataSet();
            ds = db.GetDataFromDB(sqlstr);
            dataGridView1.DataSource = ds.Tables[0];
           
        }

        private void mainform2_FormClosed(object sender, FormClosedEventArgs e)
        {
            mainform a = new mainform();
            a.Show();
            this.Hide();
        }

        
        }

        
    }
