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

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

        private void GrdFill()
        {
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();
            if (int.Parse(Class_Global_Vars.ThisLogedUserTypeID) == 1)
            {
                dt = db.dbOut(@"SELECT Query_HotelUsersADMIN.* FROM Query_HotelUsersADMIN");
            }
            else if (int.Parse(Class_Global_Vars.ThisLogedUserTypeID) == 2)
            {
                 dt = db.dbOut(@"SELECT     tblUsers.RowID AS [کد کاربری], tblUsers.UsrName AS [نام کاربری], tblUsers.PsWord AS [کلمه عبور], tblUserTypes.UserTypeDescr AS [نوع کاربر], 
                      tblUsers.UsrType AS [کد نوع کاربر], tblUsers.NameOfUser AS نام, tblUsers.SnameOfUser AS [نام خانوادگی], tblUsers.PhoneNumber AS تلفن, 
                      tblUsers.MobilePhones AS [تلفن همراه], tblUsers.UserNotes AS ملاحظات FROM         tblUsers INNER JOIN tblUserTypes ON tblUsers.UsrType = tblUserTypes.UserTypeID WHERE  (dbo.tblUsers.UsrType > 2) OR (dbo.tblUsers.RowID = '" + int.Parse(Class_Global_Vars.ThisLogedUserID) + "') ORDER BY tblUsers.RowID DESC");
            }
            DGrd_HotelUsers.DataSource = dt;
        }

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

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

            Cmb_UserTypes.DataSource = dt;
            Cmb_UserTypes.DisplayMember = "UserTypeDescr";
            Cmb_UserTypes.ValueMember = "UserTypeID";
            Cmb_UserTypes.SelectedValue = 0;


        }

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

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Cmb_UserTypes.SelectedValue = 0;
            Txt_Mobile.Text = "";
            Txt_Name.Text = "";
            Txt_Notes.Text = "";
            Txt_Password.Text = "";
            Txt_Phone.Text = "";
            Txt_Sname.Text = "";
            Txt_UserName.Text = "";
            btnAdd.SendToBack();
            Btn_Save.BringToFront();
            MessageBox.Show("لطفا اطلاعات را وارد نمایید");
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (int.Parse(Cmb_UserTypes.SelectedValue.ToString()) == 0)
            {
                MessageBox.Show("انتخاب نوع کاربر الزامی می باشد . . .");
                return;
            }
            if (Txt_UserName.Text == "" || Txt_Password.Text == "" || Txt_Name.Text == "" || Txt_Sname.Text == "" || Txt_Mobile.Text == "")
            {
                MessageBox.Show("ورود مقادیر فیلدهای آبی رنگ الزامی می باشد . . .");
                return;
            }
            Class_Db_Connection db = new Class_Db_Connection();
            db.cmd.Parameters.Add("@UsrName", SqlDbType.NVarChar).Value = Txt_UserName.Text.Trim();
            db.cmd.Parameters.Add("@PsWord", SqlDbType.NVarChar).Value = Txt_Password.Text.Trim();
            db.cmd.Parameters.Add("@UsrType", SqlDbType.Int).Value = int.Parse(Cmb_UserTypes.SelectedValue.ToString());
            db.cmd.Parameters.Add("@NameOfUser", SqlDbType.NVarChar).Value = Txt_Name.Text.Trim();
            db.cmd.Parameters.Add("@SnameOfUser", SqlDbType.NVarChar).Value = Txt_Sname.Text.Trim();
            db.cmd.Parameters.Add("@PhoneNumber", SqlDbType.NVarChar).Value = Txt_Phone.Text.Trim();
            db.cmd.Parameters.Add("@MobilePhones", SqlDbType.NVarChar).Value = Txt_Mobile.Text.Trim();
            db.cmd.Parameters.Add("@DateOfRegister", SqlDbType.DateTime).Value = DateTime.Now;
            db.cmd.Parameters.Add("@UserNotes", SqlDbType.NVarChar).Value = Txt_Notes.Text.Trim();

            db.exeSqlCmd(@"INSERT INTO tblUsers (UsrName, PsWord, UsrType, NameOfUser, SnameOfUser, PhoneNumber, MobilePhones, DateOfRegister, UserNotes)
                           VALUES (@UsrName,@PsWord,@UsrType,@NameOfUser,@SnameOfUser,@PhoneNumber,@MobilePhones,@DateOfRegister,@UserNotes)");
            btnAdd.BringToFront();
            Btn_Save.SendToBack();
            Cmb_UserTypes.SelectedValue = 0;
            Txt_Mobile.Text = "";
            Txt_Name.Text = "";
            Txt_Notes.Text = "";
            Txt_Password.Text = "";
            Txt_Phone.Text = "";
            Txt_Sname.Text = "";
            Txt_UserName.Text = "";

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


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

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                if (int.Parse(Cmb_UserTypes.SelectedValue.ToString()) == 0)
                {
                    MessageBox.Show("انتخاب نوع کاربر الزامی می باشد . . .");
                    return;
                }
                if (Txt_UserName.Text == "" || Txt_Password.Text == "" || Txt_Name.Text == "" || Txt_Sname.Text == "" || Txt_Mobile.Text == "")
                {
                    MessageBox.Show("ورود مقادیر فیلدهای آبی رنگ الزامی می باشد . . .");
                    return;
                }

                Class_Db_Connection db = new Class_Db_Connection();
                db.cmd.Parameters.Add("@RowID", SqlDbType.Int).Value = int.Parse(Txt_Code_Karbari.Text.Trim());
                db.cmd.Parameters.Add("@UsrName", SqlDbType.NVarChar).Value = Txt_UserName.Text.Trim();
                db.cmd.Parameters.Add("@PsWord", SqlDbType.NVarChar).Value = Txt_Password.Text.Trim();
                db.cmd.Parameters.Add("@UsrType", SqlDbType.Int).Value = int.Parse(Cmb_UserTypes.SelectedValue.ToString());
                db.cmd.Parameters.Add("@NameOfUser", SqlDbType.NVarChar).Value = Txt_Name.Text.Trim();
                db.cmd.Parameters.Add("@SnameOfUser", SqlDbType.NVarChar).Value = Txt_Sname.Text.Trim();
                db.cmd.Parameters.Add("@PhoneNumber", SqlDbType.NVarChar).Value = Txt_Phone.Text.Trim();
                db.cmd.Parameters.Add("@MobilePhones", SqlDbType.NVarChar).Value = Txt_Mobile.Text.Trim();
                db.cmd.Parameters.Add("@UserNotes", SqlDbType.NVarChar).Value = Txt_Notes.Text.Trim();

                db.exeSqlCmd(@"UPDATE  tblUsers SET UsrName = @UsrName, PsWord = @PsWord, UsrType = @UsrType, NameOfUser = @NameOfUser, SnameOfUser = @SnameOfUser, 
                               PhoneNumber = @PhoneNumber, MobilePhones = @MobilePhones,  UserNotes = @UserNotes WHERE  (RowID = @RowID)");

                Cmb_UserTypes.SelectedValue = 0;
                Txt_Mobile.Text = "";
                Txt_Name.Text = "";
                Txt_Notes.Text = "";
                Txt_Password.Text = "";
                Txt_Phone.Text = "";
                Txt_Sname.Text = "";
                Txt_UserName.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_Code_Karbari.Text == "")
                {
                    MessageBox.Show("رکوردی برای حذف انتخاب نشده است . . .");
                    return;
                }
                try
                {
                    Lsc_LinqToSqlDataContext db = new Lsc_LinqToSqlDataContext();
                    tblUser DelUsr = db.tblUsers.Single(n => n.RowID == int.Parse(Txt_Code_Karbari.Text.Trim()));
                    db.tblUsers.DeleteOnSubmit(DelUsr);
                    db.SubmitChanges();

                    Cmb_UserTypes.SelectedValue = 0;
                    Txt_Mobile.Text = "";
                    Txt_Name.Text = "";
                    Txt_Notes.Text = "";
                    Txt_Password.Text = "";
                    Txt_Phone.Text = "";
                    Txt_Sname.Text = "";
                    Txt_UserName.Text = "";

                    Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - حذف رکورد");
                    GrdFill();
                    MessageBox.Show("رکورد حذف شد");
                }
                catch
                {
                    Cmb_UserTypes.SelectedValue = 0;
                    Txt_Mobile.Text = "";
                    Txt_Name.Text = "";
                    Txt_Notes.Text = "";
                    Txt_Password.Text = "";
                    Txt_Phone.Text = "";
                    Txt_Sname.Text = "";
                    Txt_UserName.Text = "";

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

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

            if (selectedCellCount > 0)
            {
                if (DGrd_HotelUsers.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++)
                    {
                        Cmb_UserTypes.SelectedValue = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_Code_Karbari.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_UserName.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_Password.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_Name.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString();
                        Txt_Sname.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[6].Value.ToString();
                        Txt_Phone.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString();
                        Txt_Mobile.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[8].Value.ToString();
                        Txt_Notes.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[9].Value.ToString();
                    }
                }
            }
        }


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

            if (selectedCellCount > 0)
            {
                if (DGrd_HotelUsers.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++)
                    {
                        Cmb_UserTypes.SelectedValue = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_Code_Karbari.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_UserName.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_Password.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_Name.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString();
                        Txt_Sname.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[6].Value.ToString();
                        Txt_Phone.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString();
                        Txt_Mobile.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[8].Value.ToString();
                        Txt_Notes.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[9].Value.ToString();
                    }
                }
            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Txt_UserName.Text == "")
            {
                MessageBox.Show("نام کاربری برای جستجو وارد نشده است . . .");
                return;
            }
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();
            if (int.Parse(Class_Global_Vars.ThisLogedUserTypeID) == 1)
            {
                dt = db.dbOut(@"SELECT     tblUsers.RowID AS [کد کاربری], tblUsers.UsrName AS [نام کاربری], tblUsers.PsWord AS [کلمه عبور], tblUserTypes.UserTypeDescr AS [نوع کاربر], 
                      tblUsers.UsrType AS [کد نوع کاربر], tblUsers.NameOfUser AS نام, tblUsers.SnameOfUser AS [نام خانوادگی], tblUsers.PhoneNumber AS تلفن, 
                      tblUsers.MobilePhones AS [تلفن همراه], tblUsers.UserNotes AS ملاحظات FROM         tblUsers INNER JOIN tblUserTypes ON tblUsers.UsrType = tblUserTypes.UserTypeID WHERE  (dbo.tblUsers.UsrName = N'" + Txt_UserName.Text.Trim() + "') ORDER BY tblUsers.RowID DESC");
            }
            else if (int.Parse(Class_Global_Vars.ThisLogedUserTypeID) == 2)
            {
                dt = db.dbOut(@"SELECT     tblUsers.RowID AS [کد کاربری], tblUsers.UsrName AS [نام کاربری], tblUsers.PsWord AS [کلمه عبور], tblUserTypes.UserTypeDescr AS [نوع کاربر], 
                      tblUsers.UsrType AS [کد نوع کاربر], tblUsers.NameOfUser AS نام, tblUsers.SnameOfUser AS [نام خانوادگی], tblUsers.PhoneNumber AS تلفن, 
                      tblUsers.MobilePhones AS [تلفن همراه], tblUsers.UserNotes AS ملاحظات FROM         tblUsers INNER JOIN tblUserTypes ON tblUsers.UsrType = tblUserTypes.UserTypeID WHERE  (dbo.tblUsers.UsrType > 2) AND (dbo.tblUsers.UsrName = '" + Txt_UserName.Text.Trim() + "') ORDER BY tblUsers.RowID DESC");
            }
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - جستجو");
            DGrd_HotelUsers.DataSource = dt;
            if (dt.Rows.Count <= 0)
            {
                Cmb_UserTypes.SelectedValue = 0;
                Txt_Mobile.Text = "";
                Txt_Name.Text = "";
                Txt_Notes.Text = "";
                Txt_Password.Text = "";
                Txt_Phone.Text = "";
                Txt_Sname.Text = "";
                Txt_UserName.Text = "";
                Btn_Refresh.BringToFront();
                Btn_Search.SendToBack();
                MessageBox.Show("رکوردی یافته نشد");
            }
        }

        private void Btn_Refresh_Click(object sender, EventArgs e)
        {
            Btn_Refresh.SendToBack();
            Btn_Search.BringToFront();

            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();
            if (int.Parse(Class_Global_Vars.ThisLogedUserTypeID) == 1)
            {
                dt = db.dbOut(@"SELECT     tblUsers.RowID AS [کد کاربری], tblUsers.UsrName AS [نام کاربری], tblUsers.PsWord AS [کلمه عبور], tblUserTypes.UserTypeDescr AS [نوع کاربر], 
                      tblUsers.UsrType AS [کد نوع کاربر], tblUsers.NameOfUser AS نام, tblUsers.SnameOfUser AS [نام خانوادگی], tblUsers.PhoneNumber AS تلفن, 
                      tblUsers.MobilePhones AS [تلفن همراه], tblUsers.UserNotes AS ملاحظات FROM         tblUsers INNER JOIN tblUserTypes ON tblUsers.UsrType = tblUserTypes.UserTypeID ORDER BY tblUsers.RowID DESC");
            }
            else if (int.Parse(Class_Global_Vars.ThisLogedUserTypeID) == 2)
            {
                dt = db.dbOut(@"SELECT     tblUsers.RowID AS [کد کاربری], tblUsers.UsrName AS [نام کاربری], tblUsers.PsWord AS [کلمه عبور], tblUserTypes.UserTypeDescr AS [نوع کاربر], 
                      tblUsers.UsrType AS [کد نوع کاربر], tblUsers.NameOfUser AS نام, tblUsers.SnameOfUser AS [نام خانوادگی], tblUsers.PhoneNumber AS تلفن, 
                      tblUsers.MobilePhones AS [تلفن همراه], tblUsers.UserNotes AS ملاحظات FROM         tblUsers INNER JOIN tblUserTypes ON tblUsers.UsrType = tblUserTypes.UserTypeID WHERE  (dbo.tblUsers.UsrType > 2) OR (dbo.tblUsers.RowID = '" + int.Parse(Class_Global_Vars.ThisLogedUserID) + "') ORDER BY tblUsers.RowID DESC");
            }
            DGrd_HotelUsers.DataSource = dt;

        }

    }
}
