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
{
    public partial class RoomPropertis : Form
    {
        RConection r;
        DataSet ds = new DataSet();
        Boolean Add = false;
        EProperty ep;
        Boolean x = true;
        Boolean fromList2 = false;
        int list2 = 0;

        public RoomPropertis(RConection r1)
        {
            r = r1;
            InitializeComponent();
            textBox1.Tag = true;
            textBox4.Tag = true;
            textBox5.Tag = true;
            textBox6.Tag = true;
        }

        private void EmptyText()
        {
            textBox1.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
        }

        private void SelectLtext()
        {
            textBox1.SelectAll();
            textBox4.SelectAll();
            textBox5.SelectAll();
            textBox6.SelectAll();
        }

        private int ExecuteCommand(string C)
        {
            if (C != "")
            {
                try
                {
                    r.cmd.CommandText = C;
                    r.conn.Open();
                    r.cmd.ExecuteNonQuery();
                    r.conn.Close();
                    SelectLtext();
                    return 0;
                }
                catch (SqlException ex)
                {
                    if(ex.ErrorCode.ToString()=="-2146232060")
                        MessageBox.Show("این مورد قبلا اضافه شده است","افضودن ویژگی",MessageBoxButtons.OK,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1);
                    SelectLtext();
                    return (int)ex.ErrorCode;
                }
                finally
                {
                    r.conn.Close();
                }
            }
            SelectLtext();
            return 0;
        }

        private void Rrefresh()
        {
            try
            {
                ds.Clear();
                r.cmd.CommandText = "select * from dbo.Property;select * from dbo.PropertyItems";
                r.da.Fill(ds, "T");
                foreach (DataRow row in ds.Tables["T"].Rows)
                    listBox1.Items.Add(row["Property"]);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + ex.ErrorCode.ToString());
            }
        }

        private void RoomPropertis_Shown(object sender, EventArgs e)
        {
            Rrefresh();
            textBox5.Enabled = true;

            listBox1.SelectedIndex = 0;
            list2 = listBox2.SelectedIndex = 0;
            //listBox1.DataSource = ds.Tables["T"];
            //listBox1.DisplayMember = "Property";

            //listBox1.SelectedItem = 0;

            //r.cmd.CommandText = "select * from dbo.PropertyItems";
            //r.da.Fill(dt2);
            //SqlDataReader rdr = r.cmd.ExecuteReader();
            //while (rdr.Read())
            //{
            //    listBox1.Items.Add(rdr["Property"].ToString());

            //}
            //r.conn.Close();

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (button1.Text == "ویژگی جدید" && x)
            {
                label12.Text = ds.Tables["T"].Select("[Property]='" + listBox1.SelectedItem.ToString() + "'")[0]["Expl"].ToString();
                listBox2.Items.Clear();
                foreach (DataRow row in ds.Tables["T1"].Select(("[Property]='" + listBox1.SelectedItem.ToString() + "'")))
                {
                    listBox2.Items.Add(row["Item"].ToString());
                }
                list2 = listBox2.SelectedIndex = 0;
            }

            if (!Add && panel1.Enabled)
            {
                textBox1.Text = listBox1.SelectedItem.ToString();
                textBox6.Text = ds.Tables["T"].Select("[Property]='" + listBox1.SelectedItem.ToString() + "'")[0]["Expl"].ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void RoomPropertis_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (button1.Text == "ویژگی جدید")
            {
                panel1.Enabled = true;
                Add = true;
                button1.Text = "اتمام";
                label10.Text = "";
                label7.Text = "";
            }
            else
            {
                button1.Text = "ویژگی جدید";
                Add = false;
                panel1.Enabled = false;
                EmptyText();
                listBox1.Items.Clear();
                Rrefresh();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string Command = "";
            if (Add)//اضافه کردن
            {
                if (textBox1.Text == "" || textBox5.Text == "")//ویژگی یا هزینه خالی بود
                    MessageBox.Show("!!!فیلد ویژگی یا هزینه خالی است", "اضافه کردن ویژگی جدید", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                else                                         //ویژگی یا هزینه پر بود
                {

                    if (textBox1.Enabled && MessageBox.Show(textBox1.Text +":نام ویژگی" + "\n" + "موارد فوق را تایید می کنید؟", "اضافه کردن ویژگی جدید", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes || textBox1.Enabled == false)
                    {
                        if (textBox4.Text == "" || textBox4.Text == textBox1.Text)//بدون آیتم
                        {
                            Command = "INSERT into dbo.Property ([Property],Expl) values (N'" + textBox1.Text + "',N'" + textBox6.Text + "')";

                            if (ExecuteCommand(Command) == 0)
                            {
                                listBox1.Items.Clear();
                                listBox2.Items.Clear();
                                listBox1.Items.Add(textBox1.Text);
                                Command = "INSERT into dbo.PropertyItems ([Property],Item,Price) values(N'" + textBox1.Text + "',N'" + textBox1.Text + "'," + textBox5.Text + ")";
                                ExecuteCommand(Command);
                                listBox2.Items.Add(textBox4.Text);
                            }
                            else
                            {
                                Command = "";
                                textBox1.Enabled = textBox6.Enabled = true;
                            }

                            EmptyText();
                            button1.PerformClick();
                        }
                        else                                                     //با آیتم textBox4
                        {
                            if (textBox1.Enabled)
                            {
                                Command = "INSERT into dbo.Property ([Property],Expl) values (N'" + textBox1.Text + "',N'" + textBox6.Text + "')";

                                if (ExecuteCommand(Command) == 0)
                                {
                                    listBox1.Items.Clear();
                                    listBox2.Items.Clear();
                                    listBox1.Items.Add(textBox1.Text);
                                    Command = "INSERT into dbo.PropertyItems ([Property],Item,Price) values(N'" + textBox1.Text + "',N'" + textBox4.Text + "'," + textBox5.Text + ")";
                                    if (ExecuteCommand(Command) == 0)
                                    {
                                        listBox2.Items.Add(textBox4.Text);
                                        textBox1.Enabled = textBox6.Enabled = false;
                                    }
                                }
                                else
                                {
                                    Command = "";
                                    EmptyText();
                                }
                            }
                            else
                            {
                                if (textBox4.Text == "")
                                    MessageBox.Show("!!!فیلد آیتم خالی است", "افضودن آیتم به ویژگی", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                                else
                                {
                                    Command = "INSERT into dbo.PropertyItems ([Property],Item,Price) values(N'" + textBox1.Text + "',N'" + textBox4.Text + "'," + textBox5.Text + ")";
                                    if (ExecuteCommand(Command) == 0)
                                        listBox2.Items.Add(textBox4.Text);
                                }
                            }
                        }

                        if (textBox1.Enabled)
                        {
                            textBox1.Focus();
                        }
                        else
                        {
                            textBox4.Focus();
                        }
                    }
                    else
                    {
                        textBox1.Focus();
                    }
                }
            }
            else                          //Add==False;
            {
                if (textBox1.Text != ep.Pro || textBox4.Text != ep.Item || textBox5.Text != ep.Price || textBox6.Text != ep.Expl)
                {
                    if (textBox1.Text == listBox1.SelectedItem.ToString())
                    {
                        r.cmd.CommandText = "Update dbo.Property SET Expl=N'" + textBox6.Text + "' WHERE [Property]=N'" + listBox1.SelectedItem.ToString() + "';Update dbo.PropertyItems SET Item=N'" + textBox4.Text + "',Price=" + textBox5.Text + " WHERE [Property]=N'" + listBox1.SelectedItem.ToString() + "' AND Item=N'" + ep.Item + "';";
                        x = false;
                    }
                    else
                        r.cmd.CommandText = "insert into dbo.Property ([Property],Expl) values(N'" + textBox1.Text + "',N'" + textBox6.Text + "'); update dbo.PropertyItems SET [Property]=N'" + textBox1.Text + "' WHERE [Property]='" + listBox1.SelectedItem.ToString() + "'; Update dbo.PropertyItems SET Item=N'" + textBox4.Text + "',Price=" + textBox5.Text + " WHERE [Property]=N'" + textBox1.Text + "' AND Item=N'" + listBox2.SelectedItem.ToString() + "'; delete FROM dbo.Property WHERE [Property]=N'" + listBox1.SelectedItem.ToString() + "';";

                    r.conn.Open();
                    r.cmd.ExecuteNonQuery();
                    r.conn.Close();
                    r.cmd.CommandText = "";

                    listBox1.Items.Clear();
                    Rrefresh();

                    
                    listBox1.SelectedItem = textBox1.Text;
                    if (fromList2)
                    {

                    }
                    else
                    {
                        listBox2.SelectedItem = textBox4.Text;
                        textBox1.SelectAll();
                    }
                    x = true;
                }
                else
                {
                    textBox1.SelectAll();
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            EmptyText();
            textBox1.Enabled = textBox6.Enabled = true;
            textBox1.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "ویرایش ویژگی")
            {
                Add = false;
                button2.Text = "اتمام ویرایش";
                panel1.Enabled = true;
                textBox1.Text = listBox1.SelectedItem.ToString();
                textBox4.Text = listBox2.SelectedItem.ToString();
                textBox5.Text = label10.Text;
                textBox6.Text = label12.Text;
                ep = new EProperty(listBox1.SelectedItem.ToString(), listBox2.SelectedItem.ToString(), label10.Text, label12.Text);
            }
            else
            {
                EmptyText();
                listBox1.Items.Clear();
                Rrefresh();
                panel1.Enabled = false;
                button2.Text = "ویرایش ویژگی";
            }
            //EProperty ep = new EProperty(listBox1.SelectedItem.ToString(), listBox2.SelectedItem.ToString(), label10.Text, label12.Text);
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            textBox1.SelectAll();
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            if ((Boolean)textBox1.Tag && textBox1.Text.Length != 0)
            {
                textBox1.SelectAll();
                textBox1.Tag = false;
            }
        }

        private void textBox4_Enter(object sender, EventArgs e)
        {
            textBox4.SelectAll();
        }

        private void textBox4_Click(object sender, EventArgs e)
        {
            if ((Boolean)textBox4.Tag && textBox4.Text.Length != 0)
            {
                textBox4.SelectAll();
                textBox4.Tag = false;
            }
        }

        private void textBox6_Click(object sender, EventArgs e)
        {
            if ((Boolean)textBox6.Tag && textBox6.Text.Length != 0)
            {
                textBox6.SelectAll();
                textBox6.Tag = false;
            }
        }

        private void textBox6_Enter(object sender, EventArgs e)
        {
            textBox6.SelectAll();
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            list2 = listBox2.SelectedIndex;
            if (!Add)
            {
                fromList2 = true;
                button4.PerformClick();
            }
            DataRow row = ds.Tables["T1"].Select("[Property]='" + listBox1.SelectedItem.ToString() + "' AND Item='" + listBox2.SelectedItem.ToString() + "'")[0];
            label10.Text = row["Price"].ToString();
            label7.Text = row["EditTime"].ToString();


            if (!Add && panel1.Enabled)
            {
                textBox4.Text = listBox2.SelectedItem.ToString();
                textBox5.Text = row["Price"].ToString();
                ep.Rep(listBox2.SelectedItem.ToString(), row["Price"].ToString());
            }
        }

        private void textBox5_Enter(object sender, EventArgs e)
        {
            textBox5.SelectAll();
        }

        private void textBox5_Click(object sender, EventArgs e)
        {
            if ((Boolean)textBox5.Tag && textBox5.Text.Length != 0)
            {
                textBox5.SelectAll();
                textBox5.Tag = false;
            }
        }
    }
}
