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 frmRoomTypes : Form
    {
        public frmRoomTypes()
        {
            InitializeComponent();
        }

        private void frmRoomTypes_Load(object sender, EventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " فرم باز شد ");
            GrdFill();
        }

        private void GrdFill()
        {
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();
            dt = db.dbOut(@"SELECT   Query_RoomTypes.*  FROM    Query_RoomTypes");

            DGrd_RoomTypes.DataSource = dt;

            try
            {
                DGrd_RoomTypes.Columns[0].Width = 120;
                DGrd_RoomTypes.Columns[1].Width = 350;
            }
            catch
            { }

        }

        private void frmRoomTypes_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " فرم بسته شد ");
        }

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

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_RoomType.Text == "")
            {
                MessageBox.Show("ورود نوع اتاق الزامی است");
                return;
            }

            Class_Db_Connection db = new Class_Db_Connection();
            db.cmd.Parameters.Add("@RoomTypeDescription", SqlDbType.NVarChar).Value = Txt_RoomType.Text.Trim();
            //db.exeSqlCmd("INSERT INTO tblRoomTypes (RoomTypeDescription) VALUES (@RoomTypeDescription)");
            db.exeSp("SP_RoomType_InsertCmd");

            btnAdd.BringToFront();
            Btn_Save.SendToBack();
            GrdFill();
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - درج رکورد جدید");
            MessageBox.Show("رکورد جدید ثبت شد");
        }

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

            if (selectedCellCount > 0)
            {
                if (DGrd_RoomTypes.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++)
                    {
                        Txt_RoomType.Text = DGrd_RoomTypes.Rows[int.Parse(DGrd_RoomTypes.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_RoomTYpe_Code.Text = DGrd_RoomTypes.Rows[int.Parse(DGrd_RoomTypes.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();

                    }
                }
            }
        }

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

            if (selectedCellCount > 0)
            {
                if (DGrd_RoomTypes.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++)
                    {
                        Txt_RoomType.Text = DGrd_RoomTypes.Rows[int.Parse(DGrd_RoomTypes.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_RoomTYpe_Code.Text = DGrd_RoomTypes.Rows[int.Parse(DGrd_RoomTypes.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                    }
                }
            }
        }

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

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                if (Txt_RoomType.Text == "")
                {
                    MessageBox.Show("ورود نوع اتاق الزامی است");
                    return;
                }

                Class_Db_Connection db = new Class_Db_Connection();

                db.cmd.Parameters.Add("@RoomTypeID", SqlDbType.Int).Value = int.Parse(Txt_RoomTYpe_Code.Text.Trim());
                db.cmd.Parameters.Add("@RoomTypeDescription", SqlDbType.NVarChar).Value = Txt_RoomType.Text.Trim();

                db.exeSqlCmd(@"UPDATE tblRoomTypes  SET  RoomTypeDescription = @RoomTypeDescription  WHERE  (RoomTypeID = @RoomTypeID) ");
                Txt_RoomType.Text = "";
                Txt_RoomTYpe_Code.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_RoomTYpe_Code.Text == "")
                {
                    MessageBox.Show("رکوردی برای حذف انتخاب نشده است . . .");
                    return;
                }

                try
                {
                    Lsc_LinqToSqlDataContext db = new Lsc_LinqToSqlDataContext();
                    tblRoomType DelRoom = db.tblRoomTypes.Single(n => n.RoomTypeID == int.Parse(Txt_RoomTYpe_Code.Text.Trim()));
                    db.tblRoomTypes.DeleteOnSubmit(DelRoom);
                    db.SubmitChanges();
                    Txt_RoomType.Text = "";
                    Txt_RoomTYpe_Code.Text = "";

                    Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - حذف رکورد");
                    GrdFill();
                    MessageBox.Show("رکورد حذف شد . . .");
                }
                catch
                {
                    Txt_RoomType.Text = "";
                    Txt_RoomTYpe_Code.Text = "";

                    Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - خطای حذف رکورد");
                    GrdFill();
                    MessageBox.Show("رخداد خطا در حذف رکورد . . .");
                }
            }
        }
    }
}
