using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Reservtion
{
    public partial class EndInhabitant : Form
    {
        RConection r;
        string cc, rn;
        public EndInhabitant(RConection r1)
        {
            r = r1;
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                r.cmd.CommandText = "select * from Estates";
                r.da.Fill(dt);
                string[] s = new string[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                    s[i] = dt.Rows[i][0].ToString();
                s[0] = new Accept(s).Showf();
                r.cmd.CommandText = "update Customer set RoomNum=0 where RoomNum=" + rn + ";update Room set Estate=N'" + s[0] + "' where Number=" + rn;
                r.conn.Open();
                r.cmd.ExecuteNonQuery();
                r.conn.Close();
                MessageBox.Show("اقامت مسافر پایان یافت", "پایان اقامت", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                richTextBox1.Text = richTextBox2.Text = textBox1.Text = "";
                textBox1.Focus();
            }
            catch(System.Data.SqlClient.SqlException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.ErrorCode);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                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);
                }
                else
                {
                    rn = textBox1.Text;
                    cc = dt.Rows[0]["Code"].ToString();
                    richTextBox1.Text = dt.Rows[0]["Name"] + " " + dt.Rows[0]["Family"] + "\n";
                    richTextBox1.Text += "شماره ملی: " + dt.Rows[0]["ID_Number"] + "\n";
                    richTextBox1.Text += "تاریخ رزرو: " + dt.Rows[0]["ReserveDate"] + "\n";
                    richTextBox1.Text += "در تاریخ " + dt.Rows[0]["PayDate"] + "، " + dt.Rows[0]["PaymentCost"] + " پرداخت شده است.\n";
                    richTextBox1.Text += "تاریخ پایان اقامت: " + DateTime.Now.ToShortDateString() + "\n";
                    richTextBox1.Text += "توضیحات: " + dt.Rows[0]["Expl"] + "\n\n";

                    r.cmd.CommandText = "SELECT * FROM CustomerAfters WHERE CustomerCode=" + cc;
                    r.da.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        richTextBox1.Text += "همراه ندارد";
                    }
                    else
                        foreach (DataRow row in dt.Rows)
                            richTextBox1.Text += row["Name"] + " " + row["Family"] + "\n";
                }
                textBox1.SelectAll();
            }
            catch(System.Data.SqlClient.SqlException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.ErrorCode);
            }

        }

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

        private void button2_Click(object sender, EventArgs e)
        {
            r.cmd.CommandText = "select rc.TotalCost as 'rc.TotalCost', rc.Expl as 'rc.Expl', rc.Date as 'rc.Date', s.Name as 'S.Name', s.Expl as 's.Expl', e.Name as 'e.Name', e.Family as 'e.Family' from RequestService rc inner join [Service] s on (rc.ServiceCode=s.Code) inner join Employee e on(rc.EmployeeCode=e.Code) where rc.CustomerCode=" + cc;
            DataTable dt = new DataTable();
            r.da.Fill(dt);
            if (dt.Rows.Count == 0)
                richTextBox2.Text = "خدماتی ثبت نشده";
            else
            {
                foreach (DataRow row in dt.Rows)
                {
                    richTextBox2.Text += "هزینه خدمات: " + row["rc.TotalCost"] + "\n\n";

                    richTextBox2.Text += "نام خدمات: " + row["s.Name"] + "\n";
                    richTextBox2.Text += "توضیح: " + row["s.Expl"] + "\n\n";

                    richTextBox2.Text += "توضیح خدمات ارائه شده: " + row["rc.Expl"] + "\n";
                    richTextBox2.Text += "تاریخ ارائه خدمات: " + row["rc.Date"] + "\n\n";

                    richTextBox2.Text += "مشخصات کارمند: " + row["e.Name"] + " " + row["e.Family"] + "\n\n\n\n";


                }
            }
        }
    }
}
