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

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

        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 WHERE     (tblHotelRooms.ReservedBit = 0) AND (tblHotelRooms.InUseBit = 0) ORDER BY RoomID DESC");
            DGrd_RoomState.DataSource = dt;
            Txt_Operator.Text = Class_Global_Vars.ThisLogedUserName;
            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 CmbFill2()
        {
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();

            dt = db.dbOut("SELECT     TOP (100) PERCENT RowID, ' نام کاربری:  ' + UsrName + '  - نام: ' + NameOfUser + ' ' + SnameOfUser AS NameSname   FROM      tblUsers  WHERE     (UsrType = 3)  ORDER BY RowID DESC");
            dt.Rows.Add(new Object[] { 0, "انتخاب فرمائید . . ." });

            Cmb_Customer.DataSource = dt;
            Cmb_Customer.DisplayMember = "NameSname";
            Cmb_Customer.ValueMember = "RowID";
            Cmb_Customer.SelectedValue = 0;
        }

        private void DGrd_RoomState_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_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_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 btnAdd_Click(object sender, EventArgs e)
        {
            Cmb_Customer.SelectedValue = 0;
            btnAdd.SendToBack();
            Btn_Save.BringToFront();
            MessageBox.Show("لطفا اطلاعات را وارد نمایید");
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل رزرو اتاق اطمینان دارید", "اخطار رزرو", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                Class_Db_Connection db;
                db = new Class_Db_Connection();
                if (int.Parse(Cmb_Customer.SelectedValue.ToString()) == 0 || int.Parse(Cmb_RoomTypes.SelectedValue.ToString()) == 0)
                {
                    MessageBox.Show("انتخاب مشتری  و   نوع اتاق الزامی می باشد . . .");
                    return;
                }
                if (Txt_RoomID.Text == "")
                {
                    MessageBox.Show("اتاقی برای رزرو انتخاب نشده است . . .");
                    return;
                }
                db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int.Parse(Txt_RoomID.Text.Trim());
                db.cmd.Parameters.Add("@CustomerID", SqlDbType.Int).Value = int.Parse(Cmb_Customer.SelectedValue.ToString());
                db.cmd.Parameters.Add("@OperatorID", SqlDbType.Int).Value = int.Parse(Class_Global_Vars.ThisLogedUserID.ToString());
                db.cmd.Parameters.Add("@ReserveDate", SqlDbType.NVarChar).Value = db.dtShamsi();
                // ثبت رزرو در جدول رزروها
                db.exeSqlCmd(@"INSERT INTO tblRoomReserved (RoomID, CustomerID, OperatorID, ReserveDate) VALUES  (@RoomID,@CustomerID,@OperatorID,@ReserveDate)");

                // رزرو اتاق در جدول اتاقها
                db = new Class_Db_Connection();
                db.cmd.Parameters.Add("@ReservedBit", SqlDbType.Bit).Value = Boolean.Parse("true");
                db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int.Parse(Txt_RoomID.Text.Trim());

                db.exeSqlCmd(@"UPDATE  tblHotelRooms  SET ReservedBit = @ReservedBit  WHERE (RoomID = @RoomID)");

                Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - رزرو جدید");
                GrdFill();
                btnAdd.BringToFront();
                Btn_Save.SendToBack();
                MessageBox.Show("رزرو جدید انجام شد . . . ");
            }
            else
            {
                Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - لغو رزرو");
                GrdFill();
                btnAdd.BringToFront();
                Btn_Save.SendToBack();
                MessageBox.Show("رزرو لغو شد . . . ");
            }
        }

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