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 AddCustomerServiceing : Form
    {
        RConection r;
        string cc, sp;
        public AddCustomerServiceing(RConection r1)
        {
            r = r1;
            InitializeComponent();
            r.cmd.CommandText = "SELECT * FROM Service";
            r.da.Fill(r.ds, "Services");
            foreach (DataRow row in r.ds.Tables["Services"].Rows)
                comboBox1.Items.Add(row["Name"]);
            comboBox1.SelectedIndex = 0;
            textBox1.SelectAll();
        }

        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 textBox2_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 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_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != 0)
            {
                richTextBox2.Text = comboBox1.Text + "\n";
                richTextBox2.Text += "کد خدمات: " + r.ds.Tables["Services"].Select("Name='" + comboBox1.Text + "'")[0]["Code"].ToString() + "\n";
                richTextBox2.Text += "توضیحات: " + r.ds.Tables["Services"].Select("Name='" + comboBox1.Text + "'")[0]["Expl"].ToString() + "\n";
                richTextBox2.Text += "هزینه: " + r.ds.Tables["Services"].Select("Name='" + comboBox1.Text + "'")[0]["Cost"].ToString() + "\n";
                textBox2.Text = r.ds.Tables["Services"].Select("Name='" + comboBox1.Text + "'")[0]["Code"].ToString();
                sp = r.ds.Tables["Services"].Select("Name='" + comboBox1.Text + "'")[0]["Cost"].ToString();
            }
            else
                richTextBox2.Text = "";
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            this.AcceptButton = button2;
        }

        private void textBox2_Enter(object sender, EventArgs e)
        {
            this.AcceptButton = button3;
        }

        private void textBox3_Enter(object sender, EventArgs e)
        {
            this.AcceptButton = button4;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if (radioButton1.Checked)
                {
                    r.cmd.CommandText = "SELECT * FROM Customer WHERE Code=" + textBox1.Text;
                    r.da.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("کد مسافر " + textBox1.Text + " یافت نشد", "ثبت خدمات برای مسافر", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        richTextBox1.Text = "";
                    }
                    foreach (DataRow row in dt.Rows)
                    {
                        richTextBox1.Text = row["Name"] + " " + row["Family"] + "\n";
                        richTextBox1.Text += "شماره اتاق: " + row["RoomNum"] + "\n";
                        richTextBox1.Text += "کد مسافر: " + row["Code"] + "\n";
                        richTextBox1.Text += "توضیحات: " + row["Expl"] + "\n";
                    }
                    cc = textBox1.Text;
                }
                else if (radioButton2.Checked)
                {
                    r.cmd.CommandText = "SELECT * FROM Customer WHERE RoomNum=" + textBox1.Text;
                    r.da.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("شماره اتاق " + textBox1.Text + " یافت نشد", "ثبت خدمات برای مسافر", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        richTextBox1.Text = "";
                    }
                    foreach (DataRow row in dt.Rows)
                    {
                        richTextBox1.Text = row["Name"] + " " + row["Family"] + "\n";
                        richTextBox1.Text += "شماره اتاق: " + row["RoomNum"] + "\n";
                        richTextBox1.Text += "کد مسافر: " + row["Code"] + "\n";
                        richTextBox1.Text += "توضیحات: " + row["Expl"] + "\n";
                        cc = row["Code"].ToString();
                    }
                }
                textBox1.Focus();
                textBox1.SelectAll();
            }
            catch(SqlException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.ErrorCode);
            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.Source);
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
                label1.Text = "شماره اتاق";
            richTextBox1.Text = "";
            textBox1.Focus();
            textBox1.SelectAll();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
                label1.Text = "کد مسافر";
            richTextBox1.Text = "";
            textBox1.Focus();
            textBox1.SelectAll();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if (r.ds.Tables["Services"].Select("Code=" + textBox2.Text).Length > 0)
                {
                    if (textBox2.Text != "")
                    {
                        richTextBox2.Text = r.ds.Tables["Services"].Select("Code=" + textBox2.Text)[0]["Name"].ToString() + "\n";
                        richTextBox2.Text += "کد خدمات: " + textBox2.Text + "\n";
                        richTextBox2.Text += "توضیحات: " + r.ds.Tables["Services"].Select("Code=" + textBox2.Text)[0]["Expl"].ToString() + "\n";
                        richTextBox2.Text += "هزینه: " + r.ds.Tables["Services"].Select("Code=" + textBox2.Text)[0]["Cost"].ToString() + "\n";
                        sp = r.ds.Tables["Services"].Select("Code=" + textBox2.Text)[0]["Cost"].ToString();
                    }
                }
                else
                {
                    MessageBox.Show("سرویسی با کد " + textBox2.Text + " یافت نشد", "انتخاب سرویس", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    richTextBox2.Text = "";
                }
                textBox2.Focus();
                textBox2.SelectAll();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.ErrorCode);
            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.Source);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                r.cmd.CommandText = "SELECT e.Name as 'e.Name', e.Family as 'e.Family', d.Name as 'd.Name', d.Expl as 'd.Expl' FROM Employee e inner join EmployeeDuty d on (e.DutyCode=d.Code) WHERE e.Code=" + textBox3.Text;
                r.da.Fill(dt);
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("کارمندی با کد " + textBox3.Text + " یافت نشد", "انتخاب کارمند", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    richTextBox3.Text = "";
                }
                else
                {
                    richTextBox3.Text = dt.Rows[0]["e.Name"] + " " + dt.Rows[0]["e.Family"] + "\n" + "عنوان وظیفه: ";
                    richTextBox3.Text += dt.Rows[0]["d.Name"] + "\n" + "توضیحات: ";
                    richTextBox3.Text += dt.Rows[0]["d.Expl"] + "\n";
                }
                textBox3.SelectAll();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.ErrorCode);
            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.Source);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text == "")
                MessageBox.Show("مسافر را انتخاب کنید", "ثبت خدمات برای مسافر", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            else if (richTextBox2.Text == "")
                MessageBox.Show("خدمات را انتخاب کنید", "ثبت خدمات برای مسافر", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            else if (richTextBox3.Text == "")
                MessageBox.Show("کارمند را انتخاب کنید", "ثبت خدمات برای مسافر", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            else
            {
                r.a = false;
                new AddCustomerServiceingSummary(r, cc, textBox2.Text, textBox3.Text, sp, "مشخصات مسافر:\n" + richTextBox1.Text + "\n\n" + "مشخصات خدمات:\n" + richTextBox2.Text + "\n\n" + "مشخصات کارمند:\n" + richTextBox3.Text + "\n\n", textBox4.Text, textBox5.Text, richTextBox4.Text, dateTimePicker1.Value.Date).ShowDialog();
                if (r.a)
                {
                    for (int i = 0; i < this.Controls.Count; i++)
                    {
                        for (int j = 0; j < this.Controls[i].Controls.Count; j++)
                            if (this.Controls[i].Controls[j].GetType() == typeof(TextBox) || this.Controls[i].Controls[j].GetType() == typeof(RichTextBox))
                                this.Controls[i].Controls[j].Text = "";
                    }
                }
            }
        }
    }
}