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

        private void frmRoomTahvil_Load(object sender, EventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - فرم باز شد");
            CmbFill2(); CmbFill();
            if (RdoBtn_Reserved.Checked == true)
            {
                groupBox2.Enabled = true;
                groupBox3.Enabled = false;
            }
            else if (RdoBtn_NotReserved.Checked == true)
            {
                groupBox2.Enabled = false;
                groupBox3.Enabled = true;
            }
        }

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

            dt = db.dbOut(@"SELECT     tblHotelRooms.RoomID, ' کد اتاق: ' + LTRIM(RTRIM(STR(dbo.tblHotelRooms.RoomID)))  + ' - ' + + ' نوع اتاق: ' + dbo.tblRoomTypes.RoomTypeDescription + ' - هزینه هر شب اقامیت: ' + LTRIM(RTRIM(STR(dbo.tblHotelRooms.PriceOfPerNight))) 
                           AS OtagInfo FROM    tblHotelRooms INNER JOIN tblRoomTypes ON tblHotelRooms.RoomTypeID = tblRoomTypes.RoomTypeID WHERE     (tblHotelRooms.ReservedBit = 0) AND (tblHotelRooms.InUseBit = 0)");

            dt.Rows.Add(new Object[] { 0, "انتخاب فرمائید . . ." });

            Cmb_RoomTypes.DataSource = dt;
            Cmb_RoomTypes.DisplayMember = "OtagInfo";
            Cmb_RoomTypes.ValueMember = "RoomID";
            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 frmRoomTahvil_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - فرم بسته شد");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (frm7 == null || frm7.IsDisposed)
            {
                frm7 = new frmRoomCancelReserve();
                frm7.ShowDialog();
            }
            else
            {
                frm7.ShowDialog();
            }
        }

        private void Btn_ThvileOtag_Click(object sender, EventArgs e)
        {
            Class_Db_Connection db;
            DataTable dt;
            Decimal Dec_PricePerNight = 0;
            int int_CustomerID = 0;

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

                db = new Class_Db_Connection();
                db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int.Parse(Txt_ReserveCode.Text.Trim());
                dt = new DataTable();
                dt = db.dbOut(@"SELECT        RoomID, PriceOfPerNight, ReservedBit FROM            tblHotelRooms WHERE (RoomID = @RoomID) AND  (ReservedBit = 1) ORDER BY RoomID DESC");

                if (dt.Rows.Count > 0)
                {
                    Dec_PricePerNight = int.Parse(dt.Rows[0][1].ToString());

                    db = new Class_Db_Connection();
                    db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int.Parse(Txt_ReserveCode.Text.Trim());
                    dt = new DataTable();
                    dt = db.dbOut("SELECT        ReserveID, RoomID, CustomerID  FROM            tblRoomReserved  WHERE        (RoomID = @RoomID)");
                    int_CustomerID = int.Parse(dt.Rows[0][2].ToString());

                    db = new Class_Db_Connection();
                    db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int.Parse(Txt_ReserveCode.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_ReserveCode.Text.Trim());

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

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

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

                    Lsc_LinqToSqlDataContext dbl = new Lsc_LinqToSqlDataContext();
                    tblRoomTahvil tblH = new tblRoomTahvil();
                    tblH.CustomerID = int_CustomerID;
                    tblH.RoomID = int.Parse(Txt_ReserveCode.Text.Trim());
                    tblH.PricePerNight = Dec_PricePerNight;
                    tblH.EnterDateMiladi = DateTime.Now;
                    tblH.EnterDateSh = db.dtShamsi();
                    tblH.TasviehBit = Boolean.Parse("false");

                    dbl.tblRoomTahvils.InsertOnSubmit(tblH);
                    dbl.SubmitChanges();
                    Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - تحویل اتاق انجام شد");
                    MessageBox.Show("تحویل اتاق به مشتری ثبت شد.");
                }
                else
                {
                    MessageBox.Show("کد رزرو صحیح وارد نشده است");
                    return;
                }
            }
        }

        private void Txt_ReserveCode_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 RdoBtn_Reserved_CheckedChanged(object sender, EventArgs e)
        {
            if (RdoBtn_Reserved.Checked == true)
            {
                groupBox2.Enabled = true;
                groupBox3.Enabled = false;
            }
            else if (RdoBtn_NotReserved.Checked == true)
            {
                groupBox2.Enabled = false;
                groupBox3.Enabled = true;
            }
        }

        private void RdoBtn_NotReserved_CheckedChanged(object sender, EventArgs e)
        {
            if (RdoBtn_Reserved.Checked == true)
            {
                groupBox2.Enabled = true;
                groupBox3.Enabled = false;
            }
            else if (RdoBtn_NotReserved.Checked == true)
            {
                groupBox2.Enabled = false;
                groupBox3.Enabled = true;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Class_Db_Connection db;
            DataTable dt;
            Decimal Dec_PricePerNight = 0;

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل تحویل اتاق اطمینان دارید", "اخطار تحویل", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                if (int.Parse(Cmb_Customer.SelectedValue.ToString()) == 0 || int.Parse(Cmb_RoomTypes.SelectedValue.ToString()) == 0)
                {
                    MessageBox.Show("انتخاب همه مقادیر الزامی می باشد . . .");
                    return;
                }
                db = new Class_Db_Connection();
                db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int.Parse(Cmb_RoomTypes.SelectedValue.ToString());
                dt = new DataTable();
                dt = db.dbOut(@"SELECT        RoomID, PriceOfPerNight, ReservedBit FROM            tblHotelRooms WHERE (RoomID = @RoomID) ORDER BY RoomID DESC");
                Dec_PricePerNight = Decimal.Parse(dt.Rows[0][1].ToString());

                db = new Class_Db_Connection();
                db.cmd.Parameters.Add("@InUseBit", SqlDbType.Bit).Value = Boolean.Parse("true");
                db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int.Parse(Cmb_RoomTypes.SelectedValue.ToString());

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

                Lsc_LinqToSqlDataContext dbl = new Lsc_LinqToSqlDataContext();
                tblRoomTahvil tblH = new tblRoomTahvil();
                tblH.CustomerID = int.Parse(Cmb_Customer.SelectedValue.ToString());
                tblH.RoomID = int.Parse(Cmb_RoomTypes.SelectedValue.ToString());
                tblH.PricePerNight = Dec_PricePerNight;
                tblH.EnterDateMiladi = DateTime.Now;
                tblH.EnterDateSh = db.dtShamsi();
                tblH.TasviehBit = Boolean.Parse("false");

                dbl.tblRoomTahvils.InsertOnSubmit(tblH);
                dbl.SubmitChanges();
                Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - تحویل اتاق انجام شد");
                CmbFill(); CmbFill2();
                MessageBox.Show("تحویل اتاق به مشتری ثبت شد.");
            }
        }
    }
}
