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 mainform_1 : Form
    {
        void showItems1()
        {
            comboBox1.Visible = true;
            btn111.Visible = true;
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            button2.Visible = false;
            textBox1.Visible = false;
            textBox2.Visible = false;
            textBox3.Visible = false;
            textBox4.Visible = false;
            checkBox1.Visible = false;
            checkBox2.Visible = false;
            checkBox3.Visible = false;
            checkBox4.Visible = false;


        }
        void showItems2()
        {
            comboBox1.Visible = false;
            btn111.Visible = false ;
            label1.Visible = true ;
            label2.Visible = true ;
            label3.Visible = true ;
            label4.Visible = true ;
            button2.Visible = true ;
            textBox4.Visible = true;
            textBox3.Visible = true;
            textBox2.Visible = true;
            textBox1.Visible = true;
            checkBox4.Visible = true;
            checkBox3.Visible = true;
            checkBox2.Visible = true;
            checkBox1.Visible = true;
        }
        void combox1ItemUpdate()
        {

            comboBox1.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++)
                {
                    comboBox1 .Items.Add(ds.Tables[0].Rows[i].ItemArray[0]);
                }
        }
        void RefreshData()
        {
            string comStr;
            database db = new database();
            DataSet ds = new DataSet();
            comStr = "select * from tbl_roomtype";
            ds = db.GetDataFromDB(comStr);
            if (ds == null)
            {
                MessageBox.Show("没有任何记录！");
            }
            else
            {
                dgrView.DataSource = ds.Tables[0];
            }
        }
        

        public mainform_1()
        {
            InitializeComponent();
        }

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

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

        private void btn11_Click(object sender, EventArgs e)
        {
            showItems1();
            combox1ItemUpdate();
            RefreshData();
            btn111.Text = "按客房类型查询";
            groupBox1.Visible = true;
            dgrView.Visible = true;
            //if(dgrView .RowCount>0)
            //{
            //   
            //  for (int i = 0; i < dgrView.RowCount; i++)

            //        comboBox1.Items.Add(dgrView.Rows[i + 1].Cells[1].Value.ToString());
            //}
        }

        private void btn14_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = true;
            dgrView.Visible = true; 
            showItems1();
            RefreshData();
            btn111.Text = "按客房类型删除";
            combox1ItemUpdate();

        }

        private void btn12_Click(object sender, EventArgs e)
        { 
            showItems2();
            button2.Text = "添加";
            textBox1.Enabled = true;
            RefreshData();
            textBox1.Focus();
            groupBox1.Visible = true;
            dgrView.Visible = true;
        }

        private void mainform_1_Load(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            dgrView.Visible = false;
            comboBox1.Visible = false ;
            btn111.Visible = false ;
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            button2.Visible = false;
            textBox1.Visible = false;
            textBox2.Visible = false;
            textBox3.Visible = false;
            textBox4.Visible = false;
            checkBox1.Visible = false;
            checkBox2.Visible = false;
            checkBox3.Visible = false;
            checkBox4.Visible = false;
        }

        private void btn13_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = true;
            dgrView.Visible = true; 
            showItems2();
            button2.Text = "修改";
            textBox1.Enabled = false;
            RefreshData();
        }

        private void btn111_Click(object sender, EventArgs e)
        {
            if (btn111.Text == "按客房类型查询")
            {
                if (comboBox1.Text != "")
                {
                    string comStr;
                    comStr = "select roomtypename,area,bed,price,airconditioning,telephone,tv,bathroom from tbl_roomtype where roomtypename='" + comboBox1.SelectedItem.ToString() + "'";

                    database db = new database();
                    DataSet ds = new DataSet();

                    ds = db.GetDataFromDB(comStr);
                    if (ds == null)
                    {
                        MessageBox.Show("没有任何记录！");
                        RefreshData();
                    }
                    else
                    {
                        dgrView.DataSource = ds.Tables[0];
                    }





                }
                else
                {
                    MessageBox.Show("请选择要查询的类型！");
                }

            }
            else
            {
                try
                {
                    if (comboBox1 .Text !="")
                    {
                        if (MessageBox.Show("确定要删除该房间类型信息吗？", "删除该类型", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            string sqlStr;
                            sqlStr = "delete from tbl_roomtype where roomtypename='" + comboBox1 .Text .Trim () + "'";
                            database db = new database();
                            db.UpdataDB(sqlStr);
                            RefreshData();
                            comboBox1.Items.Remove(textBox1 .Text .Trim ());
                        }


                    }
                    else
                    {
                        MessageBox.Show("请选择要删除的，类型", "提示");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
 
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
            {
                if (button2.Text == "添加")
                {
                    textBox1.Enabled = true;
              int  a=0, h=0, c=0, d=0;
                    if(checkBox3.Checked)
                        a=1;
                    if(checkBox2.Checked)
                        h=1;
                    if(checkBox1.Checked)
                        c=1;
                    if(checkBox4.Checked)
                        d=1;
                        string sqlStr;
                        sqlStr = "insert into tbl_roomtype values ('" + textBox1.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox2.Text.Trim() + "','" + textBox4.Text.Trim() + "','"+a.ToString ()+"','"+h.ToString ()+"','"+c.ToString ()+"','"+d.ToString ()+"')";
                        database db = new database();
                        bool b;
                        b = db.UpdataDB(sqlStr);
                        if (b)
                        {
                            if (MessageBox.Show("添加成功！是否继续添加？", "添加类型", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                            {
                                textBox1.Focus();
                                
                            }
                            comboBox1.Items.Add(textBox1.Text.Trim());
                        }
                        else
                        {
                            return;
                        }
                    RefreshData();

                }



                
                else
                {
                    int a = 0, h = 0, c = 0, d = 0;
                    if (checkBox3.Checked)
                        a = 1;
                    if (checkBox2.Checked)
                        h = 1;
                    if (checkBox1.Checked)
                        c = 1;
                    if (checkBox4.Checked)
                        d = 1;
                    string sqlStr;
                    sqlStr = "Update tbl_roomtype set roomtypename='"+textBox1 .Text .Trim ()+"',area='" +textBox3 .Text .Trim () + "',bed='" +textBox2 .Text .Trim () + "',price='" +textBox4 .Text .Trim () + "',airconditioning='"+a.ToString ()+"',telephone='"+h.ToString()+"',tv='"+c.ToString ()+"',bathroom='"+d.ToString ()+"' where roomtypename='" +textBox1 .Text .Trim () + "'";
                    database db = new database();
                    db.UpdataDB(sqlStr);
                    RefreshData();
 
                }
            }
            else
                    {
                        MessageBox.Show("内容不完全，请重新填写！");
                        textBox1.Focus();
                    }
            }
            catch(Exception ex)
            {
                MessageBox .Show (ex.Message );
            
            }
                }

        private void dgrView_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int n = this.dgrView .CurrentCell.RowIndex;
            textBox1 .Text = this.dgrView[0, n].Value.ToString();
            textBox1.Enabled = false;
            textBox3.Text = this.dgrView[1, n].Value.ToString();
            textBox2.Text = this.dgrView[2, n].Value.ToString();
            textBox4.Text = this.dgrView[3, n].Value.ToString();
        }

        }
            }
    
        

       

