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 AddRoom : Form
    {
        RConection r;

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

            comboBox1.Text = comboBox1.Items[0].ToString();
            foreach (DataRow row in r.ds.Tables[4].Rows)
                comboBox1.Items.Add(row[0].ToString());
            label5.Text = r.ds.Tables[5].Rows[0][0].ToString() + "ريال";
        }

        private void listBox5_DoubleClick(object sender, EventArgs e)
        {
            int i;
            for (i = 0; i < listBox6.Items.Count && !listBox6.Items[i].ToString().StartsWith(listBox4.SelectedItem.ToString()); i++) ;
            if (i == listBox6.Items.Count)
            {
                listBox6.Items.Add(listBox4.SelectedItem + ": " + listBox5.SelectedItem);
            }
            else
                MessageBox.Show("این مورد قبلاً انتخاب شده است", "انتخاب ویژگی", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }

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

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

        private void button10_Click(object sender, EventArgs e)
        {
            try
            {
                r.cmd.CommandText = "select max(Number) from Room";
                if (r.conn.State == ConnectionState.Closed)
                    r.conn.Open();
                int NM = ((int)r.cmd.ExecuteScalar()) + 1;
                textBox4.Text = NM.ToString();
                if (r.conn.State == ConnectionState.Open)
                    r.conn.Close();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.ErrorCode.ToString());
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean x = false;
                if (textBox4.Text != "")
                {
                    if (comboBox1.Text != comboBox1.Items[0].ToString())
                    {
                        if (MessageBox.Show("از اطلاعات اتاق برای ثبت اطمینان دارید", "اضافه کردن اتاق جدید", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            x = true;
                            r.cmd.CommandText = "INSERT INTO Room (Number,Estate) VALUES(" + textBox4.Text + ",N'" + comboBox1.Text + "')";
                            r.conn.Open();
                            r.cmd.ExecuteNonQuery();
                            r.conn.Close();
                            if (listBox6.Items.Count > 0)
                            {
                                string[,] v = new string[listBox6.Items.Count, 2];
                                int i = 0;
                                string s = "";
                                for (int j = 0; j < listBox6.Items.Count; j++)
                                {
                                    s = listBox6.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 < listBox6.Items.Count; j++)
                                {
                                    s = "INSERT INTO RoomPropertis (RoomNum,Property,Item) VALUES(";
                                    s += textBox4.Text + ",N'" + v[j, 0] + "',N'" + v[j, 1] + "')";
                                    r.cmd.CommandText = s;
                                    r.cmd.ExecuteNonQuery();
                                }
                                r.conn.Close();
                            }
                            button10.PerformClick();
                            comboBox1.Text = comboBox1.Items[0].ToString();
                            listBox4.SelectedIndex = 0;
                            listBox5.SelectedIndex = 0;
                            listBox6.Items.Clear();
                            textBox4.SelectAll();
                        }
                    }
                    else
                        MessageBox.Show("وضعیت فعلی اتاق را انتخاب کنید", "اضافه کردن اتاق جدید", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
                else
                    MessageBox.Show("فیلد شماره اتاق خالی است", "اضافه کردن اتاق جدید", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);


                if (x && listBox6.Items.Count > 0)
                    MessageBox.Show("اتاق و ویژگی های اتاق ثبت شد", "اضافه کردن اتاق جدید", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                else if (x)
                    MessageBox.Show("اتاق ثبت شد", "اضافه کردن اتاق جدید", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                x = false;
            }
            catch (SqlException ex)
            {
                if (ex.ErrorCode.ToString() == "-2146232060")
                    MessageBox.Show("این اتاق قبلاً اضافه شده است", "افزودن اتاق", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                else
                    MessageBox.Show(ex.Message + "\n" + ex.ErrorCode);

            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.Source);
            }
            finally
            {
                r.conn.Close();
            }
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57 || (int)e.KeyChar == 8))
                e.Handled = true;
        }

        private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void AddRoom_Shown(object sender, EventArgs e)
        {
            button10.PerformClick();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("از خروج از این قسمت اطمینان دارید", "افزودن اتاق جدید", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                this.Close();
        }
    }
}
