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

namespace Reservtion.Room
{
    public partial class EditRoom : Form
    {
        RConection r;
        
        public EditRoom(RConection r1, string rn)
        {
            InitializeComponent();
            textBox1.Text = label4.Text = rn;
            r = r1;
            foreach (DataRow row in r.ds.Tables[0].Rows)
                listBox1.Items.Add(row[0].ToString());

            foreach (DataRow row in r.ds.Tables[2].Select("RoomNum=" + rn))
                comboBox1.Items.Add(row["Property"]);
            comboBox1.SelectedIndex = 0;
            button2.PerformClick();
        }

        public EditRoom(RConection r1)
        {
            InitializeComponent();
            r = r1;
            foreach (DataRow row in r.ds.Tables[0].Rows)
                listBox1.Items.Add(row[0].ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("از اطلاعات اتاق برای ثبت اطمینان دارید", "اضافه کردن اتاق جدید", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    if (listBox3.Items.Count > 0)
                    {
                        string[,] v = new string[listBox3.Items.Count, 2];
                        int i = 0;
                        string s = "";
                        for (int j = 0; j < listBox3.Items.Count; j++)
                        {
                            s = listBox3.Items[j].ToString();
                            while (s[i++] != ':') ;
                            v[j, 0] = s.Substring(0, i - 1);
                            v[j, 1] = s.Substring(i + 1);
                            i = 0;
                        }
                        r.conn.Open();
                        for (int j = 0; j < listBox3.Items.Count; j++)
                        {
                            s = "INSERT INTO RoomPropertis (RoomNum,Property,Item) VALUES(";
                            s += label4.Text + ",N'" + v[j, 0] + "',N'" + v[j, 1] + "')";
                            r.cmd.CommandText = s;
                            r.cmd.ExecuteNonQuery();
                        }
                        r.conn.Close();
                    }
                    r.cmd.CommandText = "SELECT * FROM RoomPropertis";
                    r.da.Fill(r.ds, "RoomPropertis");
                    MessageBox.Show("اطلاعات با موفقیت ثبت شد", "ویرایش اتاق", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    listBox3.Items.Clear();
                    textBox1.Focus();
                    textBox1.SelectAll();
                }
            }
            catch //(SqlException ex)
            {
                //MessageBox.Show(ex.Message + "\n" + ex.ErrorCode);
                //textBox2.Text = ex.ErrorCode.ToString();
            }
        }
        

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                comboBox1.Items.Clear();
                comboBox2.Items.Clear();
                comboBox3.Items.Clear();
                label4.Text = "";
                comboBox3.Text = "";
                listBox3.Items.Clear();
                if (textBox1.Text != "")
                {
                    if (r.ds.Tables["Room"].Select("Number=" + textBox1.Text).Length > 0)
                    {
                        foreach (DataRow row in r.ds.Tables[2].Select("RoomNum=" + textBox1.Text))
                            comboBox1.Items.Add(row["Property"]);

                        foreach (DataRow row in r.ds.Tables["Estates"].Rows)
                            comboBox3.Items.Add(row[0]);
                        comboBox3.Text = r.ds.Tables["Room"].Select("Number=" + textBox1.Text)[0]["Estate"].ToString();

                        groupBox3.Enabled = true;
                    }
                    else
                    {
                        groupBox3.Enabled = false;
                        MessageBox.Show("اتاق شماره " + textBox1.Text + " در سیستم ثبت نشده است", "ویرایش اتاق", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    }
                    label4.Text = textBox1.Text;
                    textBox1.SelectAll();
                    comboBox1.SelectedIndex = 0;
                }
            }
            catch
            {
                //textBox2.Text = e.ToString();
            }
            finally
            {
                r.conn.Close();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                comboBox2.Items.Clear();
                foreach (DataRow row in r.ds.Tables[1].Select("Property='" + comboBox1.SelectedItem + "'"))
                    comboBox2.Items.Add(row["Item"]);
                comboBox2.Text = r.ds.Tables[2].Select("RoomNum=" + label4.Text + " AND Property='" + comboBox1.Text + "'")[0]["Item"].ToString();
            }
            catch //(SqlException ex)
            {
                //MessageBox.Show(ex.Message + "\n" + ex.ErrorCode);
            }
            //catch (System.Exception ex)
            //{
            //    //MessageBox.Show(ex.Message + "\n" + ex.Source);
            //}
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                listBox2.Items.Clear();
                foreach (DataRow row in r.ds.Tables[1].Select("Property='" + listBox1.SelectedItem + "'"))
                    listBox2.Items.Add(row["Item"]);
                listBox2.SelectedIndex = 0;
            }
            catch //(SqlException ex)
            {
                //MessageBox.Show(ex.Message + ex.ErrorCode);
            }
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show(ex.Message + ex.Source);
            //    textBox2.Text = ex.Message;
            //}
        }

        private void listBox2_DoubleClick(object sender, EventArgs e)
        {
            int i;
            Boolean d = false;
            for (i = 0; i < listBox3.Items.Count && !listBox3.Items[i].ToString().StartsWith(listBox1.SelectedItem.ToString()); i++) ;
            if (i == listBox3.Items.Count)
                d = true;
            if (d)
            {
                for (i = 0; i < comboBox1.Items.Count && listBox1.SelectedItem.ToString() != comboBox1.Items[i].ToString(); i++) ;
                if (i < comboBox1.Items.Count)
                    d = false;
            }
            

            if(d)
                listBox3.Items.Add(listBox1.SelectedItem + ": " + listBox2.SelectedItem);
            else    
                MessageBox.Show("این مورد قبلاً انتخاب شده است", "انتخاب ویژگی", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            while (listBox3.SelectedIndex >= 0)
                listBox3.Items.Remove(listBox3.SelectedItem);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                r.ds.Tables[2].Select("RoomNum=" + label4.Text + " AND Property='" + comboBox1.Text + "'")[0]["Item"] = comboBox2.Text;
                r.cmd.CommandText = "UpdateItem @RoomNum=" + label4.Text + ",@Property=N'" + comboBox1.Text + "',@Item=N'" + comboBox2.SelectedItem + "'";
                r.conn.Open();
                r.cmd.ExecuteNonQuery();
                r.conn.Close();
            }
            catch //(SqlException ex)
            {
                //MessageBox.Show(ex.Message + "\n" + ex.Number + "/N'" + ex.ErrorCode);
                //textBox2.Text = ex.ErrorCode.ToString();
            }
            //catch (SystemException ex)
            //{
            //    MessageBox.Show(ex.Message + "\n" + ex.Source);
            //    textBox2.Text = ex.Message;
            //}
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                r.cmd.CommandText = "DELETE FROM RoomPropertis WHERE RoomNum=" + label4.Text + " AND Property=N'" + comboBox1.Text + "'";
                r.conn.Open();
                r.cmd.ExecuteNonQuery();
                r.conn.Close();
                r.cmd.CommandText = "SELECT * FROM RoomPropertis";
                r.da.Fill(r.ds, "RoomPropertis");
                comboBox1.Items.Remove(comboBox1.Text);
                //comboBox2.Items.Remove(comboBox2.Text);
                comboBox1.SelectedIndex = 0;
            }
            catch //(SqlException ex)
            {
                //MessageBox.Show(ex.Message + "\n" + ex.Number + "/N'" + ex.ErrorCode);
                //textBox2.Text = ex.ErrorCode.ToString();
            }
            //catch (SystemException ex)
            //{
            //    MessageBox.Show(ex.Message + "\n" + ex.Source);
            //    textBox2.Text = ex.Message;
            //}
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                r.ds.Tables["Room"].Select("Number=" + label4.Text)[0]["Estate"] = comboBox3.Text;
                r.cmd.CommandText = "update Room set Estate=N'" + comboBox3.Text + "'" + " where Number=" + label4.Text;
                r.conn.Open();
                r.cmd.ExecuteNonQuery();
                r.conn.Close();
            }
            catch
            {

            }
            finally
            {
                r.conn.Close();
            }
        }
    }
}