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

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

        private void frmRoomCancelReserve_FormClosed(object sender, FormClosedEventArgs e)
        {
            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 = 1) 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());
                    SelectCmbCustomer(int.Parse(Txt_RoomID.Text.Trim()));
                }
            }
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل لغو رزرو اتاق اطمینان دارید", "اخطار لغو رزرو", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                if (Txt_RoomID.Text == "")
                {
                    MessageBox.Show("رکوردی برای ویرایش انتخاب نشده است . . .");
                    return;
                }
                if (int.Parse(Cmb_Customer.SelectedValue.ToString()) == 0 || int.Parse(Cmb_RoomTypes.SelectedValue.ToString()) == 0)
                {
                    MessageBox.Show("رکوردی برای ویرایش انتخاب نشده است . . .");
                    return;
                }
                Class_Db_Connection db;
                db = new Class_Db_Connection();
                db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int.Parse(Txt_RoomID.Text.Trim());
                db.exeSqlCmd(@"DELETE FROM tblRoomReserved  WHERE  (RoomID = @RoomID)");

                db = new Class_Db_Connection();
                db.cmd.Parameters.Add("@ReservedBit", SqlDbType.Bit).Value = Boolean.Parse("false");
                db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int.Parse(Txt_RoomID.Text.Trim());

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

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

        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());
                    SelectCmbCustomer(int.Parse(Txt_RoomID.Text.Trim()));
                }
            }
        }

        private void SelectCmbCustomer(int int_RoomID)
        {
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();
            db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int_RoomID;
            dt = db.dbOut(@"SELECT CustomerID FROM tblRoomReserved WHERE (RoomID = @RoomID)");
            Cmb_Customer.SelectedValue = int.Parse(dt.Rows[0][0].ToString());
        }

    }
}
