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 NewTraveler : Form
    {
        Boolean m = false;
        RConection r;
        int RoomNum;

        TravelerAfters[] NewTravelerAfters = new TravelerAfters[100];
        int o = 0, u = 0;
        public NewTraveler(RConection r1,int RoomNum,string s)
        {
            InitializeComponent();

        }

        public NewTraveler(RConection r1)
        {
            InitializeComponent();
            r = r1;
            r.cmd.CommandText = "SELECT MAX(Code) FROM Customer";
            r.conn.Open();
            object ob = r.cmd.ExecuteScalar();
            if (ob == DBNull.Value)
                label16.Text = "10";
            else
                label16.Text = (Convert.ToInt32(ob) + 1).ToString();

            r.conn.Close();
            DataTable dt = new DataTable();
            r.cmd.CommandText = "SELECT * FROM Organization";
            r.da.Fill(dt);
            foreach (DataRow row in dt.Rows)
                comboBox1.Items.Add(row["Name"].ToString() + "  " + row["Discount%"].ToString() + "%");
            comboBox1.SelectedIndex = 0;
        textBox1.Focus();
        }

        private void NewTraveler_Shown(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int i;
            for (i = 0; i < groupBox1.Controls.Count && groupBox1.Controls[i].Text != ""; i++) ;
            if (i == groupBox1.Controls.Count)
            {
                if (m = !m)
                {
                    button1.Text = "تغییر مشخصات مسافر";
                    groupBox2.Enabled = m;
                    //textBox7.ReadOnly = !m;
                    //textBox8.ReadOnly = !m;
                    //textBox9.ReadOnly = !m;
                    //textBox10.ReadOnly =! m;
                }
                else
                {
                    button1.Text = "ثبت مشخصات مسافر";
                    groupBox6.Enabled = groupBox3.Enabled = groupBox2.Enabled = false;
                    //textBox7.ReadOnly = !m;
                    //textBox8.ReadOnly = !m;
                    //textBox9.ReadOnly = !m;
                    //textBox10.ReadOnly = !m;
                }
                textBox1.ReadOnly = m;
                textBox2.ReadOnly = m;
                textBox3.ReadOnly = m;
                textBox4.ReadOnly = m;
                textBox5.ReadOnly = m;
                textBox6.ReadOnly = m;
                dateTimePicker1.Enabled = !m;
                comboBox1.Enabled = !m;
            }
            else
                MessageBox.Show("اطلاعات کامل نیستند", "ثبت اطلاعات مشتری", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            int i;
            if (o == u)
            {
                for (i = 0; i < groupBox2.Controls.Count && groupBox2.Controls[i].Text != ""; i++) ;
                if (i == groupBox2.Controls.Count)
                {
                    if (NewTravelerAfters[o] != null)
                    {
                        NewTravelerAfters[o].name = textBox7.Text;
                        NewTravelerAfters[o].family = textBox8.Text;
                        NewTravelerAfters[o].id = textBox9.Text;
                        NewTravelerAfters[o].expl = textBox10.Text;
                    }
                    NewTravelerAfters[u++] = new TravelerAfters(textBox7.Text, textBox8.Text, textBox9.Text, textBox10.Text);
                    o++;
                    textBox7.Text = "";
                    textBox8.Text = "";
                    textBox9.Text = "";
                    textBox10.Text = "";
                    button10.Enabled = true;
                    textBox7.Focus();
                }
                else
                    MessageBox.Show("اطلاعات کامل نیستند", "ثبت اطلاعات همراهان مشتری", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
            else
            {
                button10.Enabled = true;
                if (o + 1 == u)
                {
                    if (NewTravelerAfters[o] != null)
                    {
                        NewTravelerAfters[o].name = textBox7.Text;
                        NewTravelerAfters[o].family = textBox8.Text;
                        NewTravelerAfters[o].id = textBox9.Text;
                        NewTravelerAfters[o].expl = textBox10.Text;
                    }
                    textBox7.Text = "";
                    textBox8.Text = "";
                    textBox9.Text = "";
                    textBox10.Text = "";
                    o++;
                    textBox7.Focus();
                }
                else
                {
                    if (o != u)
                    {
                        for (i = 0; i < groupBox2.Controls.Count && groupBox2.Controls[i].Text != ""; i++) ;
                        if (i == groupBox2.Controls.Count)
                        {

                            NewTravelerAfters[o].name = textBox7.Text;
                            NewTravelerAfters[o].family = textBox8.Text;
                            NewTravelerAfters[o].id = textBox9.Text;
                            NewTravelerAfters[o].expl = textBox10.Text;

                            textBox7.Text = NewTravelerAfters[++o].name;
                            textBox8.Text = NewTravelerAfters[o].family;
                            textBox9.Text = NewTravelerAfters[o].id;
                            textBox10.Text = NewTravelerAfters[o].expl;
                        }
                        else
                            MessageBox.Show("اطلاعات کامل نیستند", "ثبت اطلاعات همراهان مشتری", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    }
                    else
                    {
                        textBox7.Text = NewTravelerAfters[++o].name;
                        textBox8.Text = NewTravelerAfters[o].family;
                        textBox9.Text = NewTravelerAfters[o].id;
                        textBox10.Text = NewTravelerAfters[o].expl;
                    }
                }
            }
            textBox7.Focus();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox7.ReadOnly = !textBox7.ReadOnly;
            textBox8.ReadOnly = !textBox8.ReadOnly;
            textBox9.ReadOnly = !textBox9.ReadOnly;
            textBox10.ReadOnly = !textBox10.ReadOnly;

            if (button3.Text == "ثبت مشخصات همراهان")
            {
                button3.Text = "تغییر مشخصات همراهان";
                groupBox3.Enabled = true;
            }
            else
            {
                button3.Text = "ثبت مشخصات همراهان";
                groupBox6.Enabled = groupBox3.Enabled = false;
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            int i;
            if (o != u)
            {
                for (i = 0; i < groupBox2.Controls.Count && groupBox2.Controls[i].Text != ""; i++) ;
                if (i == groupBox2.Controls.Count)
                {

                    NewTravelerAfters[o].name = textBox7.Text;
                    NewTravelerAfters[o].family = textBox8.Text;
                    NewTravelerAfters[o].id = textBox9.Text;
                    NewTravelerAfters[o].expl = textBox10.Text;

                    textBox7.Text = NewTravelerAfters[--o].name;
                    textBox8.Text = NewTravelerAfters[o].family;
                    textBox9.Text = NewTravelerAfters[o].id;
                    textBox10.Text = NewTravelerAfters[o].expl;
                    if (o == 0)
                        button10.Enabled = false;
                }
                else
                    MessageBox.Show("اطلاعات کامل نیستند", "ثبت اطلاعات مشتری", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
            else
            {
                textBox7.Text = NewTravelerAfters[--o].name;
                textBox8.Text = NewTravelerAfters[o].family;
                textBox9.Text = NewTravelerAfters[o].id;
                textBox10.Text = NewTravelerAfters[o].expl;
            }
            textBox7.Focus();
        }

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

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

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

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox11.Text != "")
                {
                    r.cmd.CommandText = "select * from Customer where RoomNum=" + textBox11.Text;
                    r.conn.Open();
                    object ob = r.cmd.ExecuteScalar();
                    r.conn.Close();
                    if (ob == null)
                    {
                        DataRow row;
                        
                        listBox1.Items.Clear();
                        row = r.ds.Tables[3].Select("Number=" + textBox11.Text)[0];
                        listBox1.Items.Add("وضعیت: " + row["Estate"]);
                        listBox1.Items.Add("هزینه: " + row["TotalPrice"] + "ريال");
                        foreach (DataRow row1 in r.ds.Tables["RoomPropertis"].Select("RoomNum=" + textBox11.Text))
                            listBox1.Items.Add(row1["Property"] + ": " + row1["Item"]);
                        RoomNum = int.Parse(textBox11.Text);
                        foreach (DataRow row1 in r.ds.Tables["Estates"].Rows)
                            comboBox2.Items.Add(row1[0]);
                    }
                    else
                        MessageBox.Show("این اتاق قبلاً رزرو شده است", "انتخاب اتاق", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "/n" + ex.ErrorCode);
                textBox11.Text = ex.ErrorCode.ToString();
            }
            catch(SystemException ex)
            {
                if (ex.Message == "Index was outside the bounds of the array.")
                    MessageBox.Show(" اتاق شماره" + textBox11.Text + " در سیستم ثبت نشده است", "انتخاب اتاق", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                else
                {
                    MessageBox.Show(ex.Message + "\n" + ex.Source);
                    textBox11.Text = ex.Message;
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            new AppRoom.RoomSearchByProperty(r, 1).ShowDialog();
            if (r.num != 0)
                textBox11.Text = r.num.ToString();
            r.num = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (o < u)
                {
                    int p = o + 1;
                    while (p <= u)
                        NewTravelerAfters[p - 1] = NewTravelerAfters[p++];
                    u--;

                    textBox7.Text = NewTravelerAfters[o].name;
                    textBox8.Text = NewTravelerAfters[o].family;
                    textBox9.Text = NewTravelerAfters[o].id;
                    textBox10.Text = NewTravelerAfters[o].expl;
                }
            }
            catch
            {
                textBox7.Text = "";
                textBox8.Text = "";
                textBox9.Text = "";
                textBox10.Text = "";
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (RoomNum != 0)
            {
                groupBox4.Enabled = groupBox5.Enabled = !groupBox5.Enabled;
                groupBox6.Enabled = !groupBox6.Enabled;

                if (button7.Text == "ثبت اطلاعات اتاق")
                    button7.Text = "تغییر اطلاعات اتاق";
                else
                    button7.Text = "ثبت اطلاعات اتاق";
            }
            else
                MessageBox.Show("شماره اتاق را انتخاب کنید", "انتخاب اتاق", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            FullInformation fi = new FullInformation(label16.Text, textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, comboBox1.Text, textBox11.Text, NewTravelerAfters, dateTimePicker1.Value);
            new FullInfo(r, fi).ShowDialog();
            if (r.a)
            {
                foreach (Control c in groupBox1.Controls)
                    if (c.GetType() == typeof(TextBox))
                        c.Text = "";
                foreach (Control c in groupBox2.Controls)
                    if (c.GetType() == typeof(TextBox))
                        c.Text = "";
                foreach (Control c in groupBox3.Controls)
                    if (c.GetType() == typeof(TextBox))
                        c.Text = "";
                foreach (Control c in groupBox4.Controls)
                    if (c.GetType() == typeof(TextBox))
                        c.Text = "";
                foreach (Control c in groupBox5.Controls)
                    if (c.GetType() == typeof(TextBox))
                        c.Text = "";
                button1.PerformClick();
            }
            r.a = false;
        }

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

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

            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("از خروج اطمینان دارید"
            this.Close();
        }
    }
}