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.AppRoom
{
    public partial class RoomSearch : Form
    {
        RConection r;
        public RoomSearch(RConection r1)
        {
            InitializeComponent();
            r = r1;
            foreach (DataRow row in r.ds.Tables[0].Rows)
                checkedListBox1.Items.Add(row[0].ToString());

            for (int i = 0; i < checkedListBox1.Items.Count; i++)
                checkedListBox1.SetItemChecked(i, true);
            textBox1.Focus();
        }

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

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text != "")
                {
                    DataRow row;
                    row = r.ds.Tables[3].Select("Number=" + textBox1.Text)[0];
                    textBox2.Text = "وضعیت: " + row["Estate"] + "  هزینه:  " + row["TotalPrice"] + "ريال  \n  ";

                    for (int i = 0; i < checkedListBox1.Items.Count; i++)
                    {

                        if (checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
                        {
                            textBox2.Text += checkedListBox1.Items[i] + ": ";
                            string s = "RoomNum=" + textBox1.Text + " AND Property='" + checkedListBox1.Items[i] + "'";
                            int c = r.ds.Tables[2].Select(s).Length;
                            if (c == 1)
                                textBox2.Text += r.ds.Tables[2].Select(s)[0]["Item"].ToString() + "  \n  ";
                            else
                                textBox2.Text += "ندارد  \n  ";
                        }
                    }
                }
                else
                    MessageBox.Show("فیلد شماره اتاق خالی است", "جستجوی اتاق", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.ErrorCode.ToString());
            }
            catch (SystemException ex)
            {
                if (ex.Message.ToString() == "Index was outside the bounds of the array.")
                    MessageBox.Show("اتاقی به شماره " + textBox1.Text + " یافت نشد", "جستجوی اتاق", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                else
                    MessageBox.Show(ex.Message + "\n" + ex.Source.ToString());
                //textBox2.Text = ex.Message;
            }
            textBox1.SelectAll();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            new Room.EditRoom(r, textBox1.Text).ShowDialog();
            button4.PerformClick();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if (MessageBox.Show("آیا مایلید اتاق را حذف کنید", "حذف اتاق", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    r.cmd.CommandText = "select * from Customer where RoomNum=" + textBox1.Text;
                    r.da.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        if (r.ds.Tables["Room"].Select("Number=" + textBox1.Text).Length > 0)
                        {
                            r.cmd.CommandText = "DELETE from Room WHERE Number=" + textBox1.Text;
                            r.conn.Open();
                            r.cmd.ExecuteNonQuery();
                            r.conn.Close();
                            r.RefreshRC();
                            MessageBox.Show("اتاق حذف شد", "حذف اتاق", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        }
                        else
                            MessageBox.Show("اتاقی به شماره " + textBox1.Text + " یافت نشد", "جستجوی اتاق", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    }
                    else
                        MessageBox.Show("امکان حذف وجود ندارد" + "\n" + "اتاق در حال استفاده می باشد", "حذف اتاق", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.ErrorCode);
                textBox2.Text = ex.ErrorCode.ToString();
            }
            finally
            {
                r.conn.Close();
            }
        }
    }
}
