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 MainTemplate
{
    public partial class frmRooms : Form
    {
        public frmRooms()
        {
            InitializeComponent();
        }

        private void frmRooms_Load(object sender, EventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " is Opened.");
            CmbFill();
            GrdFill();
        }

        private void GrdFill()
        {
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();

            /*dt = db.dbOut(@"SELECT     TOP (100) PERCENT tblHotelRooms.RoomID AS [کد اتاق], tblHotelRooms.RoomTypeID AS [کد نوع اتاق], tblRoomTypes.RoomTypeDescription AS [نوع اتاق], 
                      tblHotelRooms.BedQuantity AS [تعداد تخت], tblHotelRooms.PriceOfPerNight AS [مبلغ هر شب اقامت], 
                      CASE tblHotelRooms.ReservedBit WHEN 0 THEN 'اتاق رزرو نشده است' WHEN 1 THEN 'اتاق رزرو شده است' END AS [وضعیت رزرو], 
                      CASE tblHotelRooms.InUseBit WHEN 0 THEN 'اتاق آزاد است' WHEN 1 THEN 'اتاق تحویل مشتری شده است' END AS [وضعیت تحویل اتاق], 
                      tblHotelRooms.NotesBen AS ملاحظات  FROM         tblHotelRooms INNER JOIN  tblRoomTypes ON tblHotelRooms.RoomTypeID = tblRoomTypes.RoomTypeID ORDER BY RoomID DESC");*/

            dt = db.dbOut(@"SELECT   Query_HotelRooms.* FROM Query_HotelRooms");

            DGrd_RoomState.DataSource = dt;
            try
            {
                DGrd_RoomState.Columns[4].Width = 150;
                DGrd_RoomState.Columns[5].Width = 150;
                DGrd_RoomState.Columns[6].Width = 150;
                DGrd_RoomState.Columns[7].Width = 350;
            }
            catch
            { }
        }

        private void CmbFill()
        {
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();

            dt = db.dbOut("SELECT  RoomTypeID, RoomTypeDescription  FROM   tblRoomTypes ORDER BY RoomTypeID");
            dt.Rows.Add(new Object[] { 0, "انتخاب فرمائید . . ." });

            Cmb_RoomTypes.DataSource = dt;
            Cmb_RoomTypes.DisplayMember = "RoomTypeDescription";
            Cmb_RoomTypes.ValueMember = "RoomTypeID";
            Cmb_RoomTypes.SelectedValue = 0;


        }

        private void frmRooms_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " is Closed.");
        }

        private void Txt_BedQuantity_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Txt_NightPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Cmb_RoomTypes.SelectedValue = 0;
            Txt_BedQuantity.Text = "";
            Txt_NightPrice.Text = "";
            Txt_Notes.Text = "";
            btnAdd.SendToBack();
            Btn_Save.BringToFront();
            MessageBox.Show("لطفا اطلاعات را وارد نمایید");
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (int.Parse(Cmb_RoomTypes.SelectedValue.ToString()) == 0)
            {
                MessageBox.Show("نوع اتاق مشخص نشده است.");
                return;
            }
            if (Txt_BedQuantity.Text == "" || Txt_NightPrice.Text == "")
            {
                MessageBox.Show("ورود اطلاعات فیلدهای سطر اول الزامی می باشد.");
                return;
            }
            Class_Db_Connection db = new Class_Db_Connection();
            db.cmd.Parameters.Add("@RoomTypeID" , SqlDbType.Int).Value = int.Parse(Cmb_RoomTypes.SelectedValue.ToString());
            db.cmd.Parameters.Add("BedQuantity", SqlDbType.Int).Value = int.Parse(Txt_BedQuantity.Text.Trim());
            db.cmd.Parameters.Add("@PriceOfPerNight", SqlDbType.Decimal).Value = Decimal.Parse(Txt_NightPrice.Text.Trim());
            db.cmd.Parameters.Add("@ReservedBit", SqlDbType.Bit).Value = Boolean.Parse("false");
            db.cmd.Parameters.Add("@InUseBit", SqlDbType.Bit).Value = Boolean.Parse("false"); ;
            db.cmd.Parameters.Add("@OutOfOrderBit", SqlDbType.Bit).Value = Boolean.Parse("false"); ;
            db.cmd.Parameters.Add("@NotesBen", SqlDbType.NVarChar).Value = Txt_Notes.Text.Trim();

            db.exeSqlCmd(@"INSERT INTO tblHotelRooms (RoomTypeID, BedQuantity, PriceOfPerNight, ReservedBit, InUseBit, OutOfOrderBit, NotesBen)
                           VALUES  (@RoomTypeID,@BedQuantity,@PriceOfPerNight,@ReservedBit,@InUseBit,@OutOfOrderBit,@NotesBen)");
            btnAdd.BringToFront();
            Btn_Save.SendToBack();
            Cmb_RoomTypes.SelectedValue = 0;
            Txt_BedQuantity.Text = "";
            Txt_NightPrice.Text = "";
            Txt_Notes.Text = "";
            GrdFill();
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - درج رکورد جدید");
            MessageBox.Show("رکورد جدید ثبت شد");
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (Txt_RoomID.Text == "")
            {
                MessageBox.Show("رکوردی برای ویرایش انتخاب نشده است . . .");
                return;
            }

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                if (int.Parse(Cmb_RoomTypes.SelectedValue.ToString()) == 0)
                {
                    MessageBox.Show("انتخاب نوع اتاق الزامی می باشد . . . ");
                    return;
                }
                if (Txt_BedQuantity.Text == "" || Txt_NightPrice.Text == "")
                {
                    MessageBox.Show("ورود تعداد تخت و مبلغ قیمت هر شب الزامی می باشد . . .");
                    return;
                }

                Class_Db_Connection db = new Class_Db_Connection();
                db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int.Parse(Txt_RoomID.Text.Trim());
                db.cmd.Parameters.Add("@RoomTypeID", SqlDbType.Int).Value = int.Parse(Cmb_RoomTypes.SelectedValue.ToString());
                db.cmd.Parameters.Add("@BedQuantity", SqlDbType.Int).Value = int.Parse(Txt_BedQuantity.Text.Trim());
                db.cmd.Parameters.Add("@PriceOfPerNight", SqlDbType.Decimal).Value = Decimal.Parse(Txt_NightPrice.Text.Trim());
                db.cmd.Parameters.Add("@NotesBen", SqlDbType.NVarChar).Value = Txt_Notes.Text.Trim();

                db.exeSqlCmd(@"UPDATE  tblHotelRooms SET   RoomTypeID = @RoomTypeID, BedQuantity = @BedQuantity, PriceOfPerNight = @PriceOfPerNight, NotesBen = @NotesBen WHERE (RoomID = @RoomID)");

                Cmb_RoomTypes.SelectedValue = 0;
                Txt_BedQuantity.Text = "";
                Txt_NightPrice.Text = "";
                Txt_Notes.Text = "";
                Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - ویرایش رکورد");
                GrdFill();
                MessageBox.Show("رکورد مورد نظر ویرایش گردید . . .");

            }

        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                if (Txt_RoomID.Text == "")
                {
                    MessageBox.Show("رکوردی برای حذف انتخاب نشده است . . .");
                    return;
                }
                try
                {
                    Lsc_LinqToSqlDataContext db = new Lsc_LinqToSqlDataContext();
                    tblHotelRoom DelRoom = db.tblHotelRooms.Single(n => n.RoomID == int.Parse(Txt_RoomID.Text.Trim()));
                    db.tblHotelRooms.DeleteOnSubmit(DelRoom);
                    db.SubmitChanges();
                    Cmb_RoomTypes.SelectedValue = 0;
                    Txt_BedQuantity.Text = "";
                    Txt_NightPrice.Text = "";
                    Txt_Notes.Text = "";
                    GrdFill();
                    Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + "  - حذف رکورد");
                    MessageBox.Show("رکورد مورد نظر حذف گردید . . .");
                }
                catch
                {
                    MessageBox.Show("رکوردی برای حذف انتخاب نشده است . . .");
                    return;
                }
            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Txt_Searc.Text == "")
            {
                MessageBox.Show("کد اتاق برای جستجو وارد نشده است . . .");
                return;
            }

            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();

            dt = db.dbOut(@"SELECT     TOP (100) PERCENT tblHotelRooms.RoomID AS [کد اتاق], tblHotelRooms.RoomTypeID AS [کد نوع اتاق], tblRoomTypes.RoomTypeDescription AS [نوع اتاق], 
                      tblHotelRooms.BedQuantity AS [تعداد تخت], tblHotelRooms.PriceOfPerNight AS [مبلغ هر شب اقامت], 
                      CASE tblHotelRooms.ReservedBit WHEN 0 THEN 'اتاق رزرو نشده است' WHEN 1 THEN 'اتاق رزرو شده است' END AS [وضعیت رزرو], 
                      CASE tblHotelRooms.InUseBit WHEN 0 THEN 'اتاق آزاد است' WHEN 1 THEN 'اتاق تحویل مشتری شده است' END AS [وضعیت تحویل اتاق], 
                      tblHotelRooms.NotesBen AS ملاحظات  FROM         tblHotelRooms INNER JOIN  tblRoomTypes ON tblHotelRooms.RoomTypeID = tblRoomTypes.RoomTypeID  WHERE     (dbo.tblHotelRooms.RoomID = '" + int.Parse(Txt_Searc.Text.Trim()) + "')  ORDER BY RoomID DESC");
            DGrd_RoomState.DataSource = dt;
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - جستجو");
            if (dt.Rows.Count <= 0)
            {
                Txt_Searc.Text = "";
                Btn_Refresh.BringToFront();
                Btn_Search.SendToBack();
                MessageBox.Show("رکوردی یافته نشد");
            }

            try
            {
                DGrd_RoomState.Columns[4].Width = 150;
                DGrd_RoomState.Columns[5].Width = 150;
                DGrd_RoomState.Columns[6].Width = 150;
                DGrd_RoomState.Columns[7].Width = 350;
            }
            catch
            { }
        }

        private void Txt_Searc_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Btn_Refresh_Click(object sender, EventArgs e)
        {
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();

            Btn_Refresh.SendToBack();
            Btn_Search.BringToFront();

            dt = db.dbOut(@"SELECT     TOP (100) PERCENT tblHotelRooms.RoomID AS [کد اتاق], tblHotelRooms.RoomTypeID AS [کد نوع اتاق], tblRoomTypes.RoomTypeDescription AS [نوع اتاق], 
                      tblHotelRooms.BedQuantity AS [تعداد تخت], tblHotelRooms.PriceOfPerNight AS [مبلغ هر شب اقامت], 
                      CASE tblHotelRooms.ReservedBit WHEN 0 THEN 'اتاق رزرو نشده است' WHEN 1 THEN 'اتاق رزرو شده است' END AS [وضعیت رزرو], 
                      CASE tblHotelRooms.InUseBit WHEN 0 THEN 'اتاق آزاد است' WHEN 1 THEN 'اتاق تحویل مشتری شده است' END AS [وضعیت تحویل اتاق], 
                      tblHotelRooms.NotesBen AS ملاحظات  FROM         tblHotelRooms INNER JOIN  tblRoomTypes ON tblHotelRooms.RoomTypeID = tblRoomTypes.RoomTypeID ORDER BY RoomID DESC");
            DGrd_RoomState.DataSource = dt;
            try
            {
                DGrd_RoomState.Columns[4].Width = 150;
                DGrd_RoomState.Columns[5].Width = 150;
                DGrd_RoomState.Columns[6].Width = 150;
                DGrd_RoomState.Columns[7].Width = 350;
            }
            catch
            { }

        }


        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = DGrd_RoomState.GetCellCount(DataGridViewElementStates.Selected);
            String Str_Fcode = String.Empty;

            if (selectedCellCount > 0)
            {
                if (DGrd_RoomState.AreAllCellsSelected(true))
                {
                    MessageBox.Show("All cells are selected", "Selected Cells");
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();

                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Str_Fcode = DGrd_RoomState.Rows[int.Parse(DGrd_RoomState.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_BedQuantity.Text = DGrd_RoomState.Rows[int.Parse(DGrd_RoomState.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                        Txt_NightPrice.Text = DGrd_RoomState.Rows[int.Parse(DGrd_RoomState.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_Notes.Text = DGrd_RoomState.Rows[int.Parse(DGrd_RoomState.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString();
                        Txt_RoomID.Text = DGrd_RoomState.Rows[int.Parse(DGrd_RoomState.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                    }
                    Cmb_RoomTypes.SelectedValue = int.Parse(Str_Fcode.Trim());
                }
            }
        }

        private void DGrd_RoomState_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = DGrd_RoomState.GetCellCount(DataGridViewElementStates.Selected);
            String Str_Fcode = String.Empty;

            if (selectedCellCount > 0)
            {
                if (DGrd_RoomState.AreAllCellsSelected(true))
                {
                    MessageBox.Show("All cells are selected", "Selected Cells");
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();

                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Str_Fcode = DGrd_RoomState.Rows[int.Parse(DGrd_RoomState.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_BedQuantity.Text = DGrd_RoomState.Rows[int.Parse(DGrd_RoomState.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                        Txt_NightPrice.Text = DGrd_RoomState.Rows[int.Parse(DGrd_RoomState.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_Notes.Text = DGrd_RoomState.Rows[int.Parse(DGrd_RoomState.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString();
                        Txt_RoomID.Text = DGrd_RoomState.Rows[int.Parse(DGrd_RoomState.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                    }
                    Cmb_RoomTypes.SelectedValue = int.Parse(Str_Fcode.Trim());
                }
            }
        }
    }
}
