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 RoomSearchByProperty : Form
    {
        RConection r;
        public RoomSearchByProperty(RConection r1,int x)
        {
            InitializeComponent();
            r = r1;
            foreach (DataRow row in r.ds.Tables[0].Rows)
                listBox1.Items.Add(row[0].ToString());

            listBox1.SelectedIndex = 0;
        }

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

            listBox1.SelectedIndex = 0;
        }


        private void DataGridViewIntialColumnHeaders()
        {
            dataGridView1.Columns["RoomNum"].HeaderText = "شماره اتاق";
            dataGridView1.Columns["Quantity"].Visible = false;
            dataGridView1.Columns["Number"].Visible = false;
            dataGridView1.Columns["Estate"].HeaderText = "وضعیت";
            dataGridView1.Columns["TotalPrice"].HeaderText = "هزینه";
        }

        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);
            }
        }

        private void listBox2_DoubleClick(object sender, EventArgs e)
        {
            int i;
            for (i = 0; i < listBox3.Items.Count && !listBox3.Items[i].ToString().StartsWith(listBox2.SelectedItem.ToString()); i++) ;
            if (i == listBox3.Items.Count)
                listBox3.Items.Add(listBox1.SelectedItem + ": " + listBox2.SelectedItem);

            else
                MessageBox.Show("این مورد قبلاً انتخاب شده است", "انتخاب ویژگی", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }

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

        private void button3_Click(object sender, EventArgs e)
        {
            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;
                }

                s = "";
                for (i = 0; i < listBox3.Items.Count; i++)
                {
                    s += "(Property=N'" + v[i, 0] + "' AND Item=N'" + v[i, 1] + "')";
                    if (i < listBox3.Items.Count - 1)
                        s += " OR ";
                }

                //dt2 = new DataTable();
                r.cmd.CommandText = "SELECT  RoomNum, Count(RoomNum) as 'Quantity' INTO #Pri FROM RoomPropertis WHERE" + s + " Group By(RoomNum) Order By(Quantity)DESC; select * from #Pri Left outer join Room on #Pri.RoomNum=Room.Number Order by(Quantity)DESC; Drop Table #Pri";
                r.da.Fill(r.ds, "tv");
                //DataRow[] rows = new DataRow[ds.Tables["tv"].Rows.Count];
                //rows = dt2.Select("Quantity=2");
                dataGridView1.DataSource = r.ds.Tables["tv"];
                if (dataGridView1.Columns["RoomNum"].HeaderText != "شماره اتاق")
                    DataGridViewIntialColumnHeaders();
            }
            else
            {
                dataGridView1.Columns.Clear();
                MessageBox.Show("لیست جستجو خالی است", "جستجوی اتاق", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            DataRow[] rows;
            rows = r.ds.Tables[2].Select("RoomNum=" + dataGridView1.CurrentRow.Cells[2].Value.ToString());
            textBox3.Text = "";
            foreach (DataRow row in rows)
                textBox3.Text += row["Property"] + ": " + row["Item"] + "  \n  ";
            r.num = int.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString());
        }

        private void button11_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
