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;
using System.Data.SqlClient;



namespace HotelProject
{
    public partial class FrmRoomManagement : Form
    {

        SqlConnection con;
        SqlDataAdapter da;
        DataTable dt;
        string Op;

        public FrmRoomManagement()
        {
            InitializeComponent();
        }

        private void FillValues()
        {
            TxtRoomNumber.Text = GrdRooms.CurrentRow.Cells["RoomNum"].Value.ToString();

            for (int i = 0; i < CmbTypes.Items.Count; i++)
            {
                CmbTypes.SelectedIndex = i;

                if (GrdRooms.CurrentRow.Cells["Type"].Value.ToString() == CmbTypes.Text)
                {
                    break;
                }
            }


        }



        private void FrmRoomManagement_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("Server = . ; DataBase = DbHotel ; Integrated Security = True ");
            da = new SqlDataAdapter("SelectAllRooms", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;

            dt = new DataTable();
            da.Fill(dt);
            GrdRooms.DataSource = dt;

            //--------------

           
            SqlDataAdapter da2 = new SqlDataAdapter("SelectRoomTypes", con);
            da2.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataTable dt2 = new DataTable();
            da2.Fill(dt2);
            CmbTypes.DataSource = dt2;
            CmbTypes.DisplayMember = "RoomType";
           


           // FillValues();
        }

        private void GrdRooms_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {





        }

        private void GrdRooms_Click(object sender, EventArgs e)
        {
            FillValues();
        }

        private void GrdRooms_KeyUp(object sender, KeyEventArgs e)
        {
            FillValues();
        }

        private void TxtRoomNum_TextChanged(object sender, EventArgs e)
        {
            NumCorrection.DoCorrection(TxtRoomNum);

            if (!string.IsNullOrWhiteSpace(TxtRoomNum.Text))
            {
                da = new SqlDataAdapter("SearchRoom", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.AddWithValue("@roomnum", TxtRoomNum.Text);
                dt = new DataTable();
                da.Fill(dt);
                GrdRooms.DataSource = dt;
            }
            else
            {
                da = new SqlDataAdapter("SelectAllRooms", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;

                dt = new DataTable();
                da.Fill(dt);
                GrdRooms.DataSource = dt;
            }

        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            Op = "Add";
            BtnAdd.Enabled = false;
            BtnCancel.Enabled = true;
            BtnSave.Enabled = true;
            BtnDelete.Enabled = false;
            BtnEdit.Enabled = false;
            GrdRooms.Enabled = false;
            TxtRoomNum.Enabled = false;
            TxtRoomNumber.Text = "";
            CmbTypes.SelectedIndex = 0;
            TxtRoomNumber.Enabled = true;
            CmbTypes.Enabled = true;

        }

        private void BtnSave_Click(object sender, EventArgs e)
        {

            if (Op == "Add")
            {

                if (String.IsNullOrWhiteSpace(TxtRoomNumber.Text))
                    errorProvider1.SetError(TxtRoomNumber, "شماره اتاق را وارد کنید");
                else
                {
                    errorProvider1.SetError(TxtRoomNumber, "");
                    SqlCommand cmd = new SqlCommand("AddRoom", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@roomnum", TxtRoomNumber.Text);
                    cmd.Parameters.AddWithValue("@roomtype", CmbTypes.Text);
                    cmd.Parameters.AddWithValue("@state", "خالی");


                    cmd.Parameters.Add("@result", SqlDbType.Int);
                    cmd.Parameters["@result"].Direction = ParameterDirection.Output;

                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();

                    int res = Convert.ToInt32(cmd.Parameters["@result"].Value.ToString());

                    if (res == 0)
                    {
                        FrmMessage Fm = new FrmMessage();
                        Fm.LblText.Text = "این شماره اتاق قبلا ثبت شده است";
                        Fm.PicClose.Visible = true;
                        Fm.PicNO.Visible = false;
                        Fm.PicYes.Visible = false;
                        Fm.ShowDialog();
                    }
                    else
                    {
                        FrmMessage Fm = new FrmMessage();
                        Fm.LblText.Text = "اتاق جدید با موفقیت ثبت شد";
                        Fm.PicClose.Visible = true;
                        Fm.PicNO.Visible = false;
                        Fm.PicYes.Visible = false;
                        Fm.ShowDialog();

                        da = new SqlDataAdapter("SelectAllRooms", con);
                        da.SelectCommand.CommandType = CommandType.StoredProcedure;

                        dt = new DataTable();
                        da.Fill(dt);
                        GrdRooms.DataSource = dt;

                        BtnAdd.Enabled = true;
                        BtnCancel.Enabled = false;
                        BtnSave.Enabled = false;
                        BtnDelete.Enabled = true;
                        BtnEdit.Enabled = true;
                        GrdRooms.Enabled = true;
                        TxtRoomNum.Enabled = true;

                        TxtRoomNumber.Enabled = false;
                        CmbTypes.Enabled = false;
                        FillValues();


                    }
                }
            }

            else
            {

                SqlCommand cmd = new SqlCommand("EditRoom", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@roomnum", TxtRoomNumber.Text);
                cmd.Parameters.AddWithValue("@roomtype", CmbTypes.Text);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                FrmMessage Fm = new FrmMessage();
                Fm.LblText.Text = "اتاق مورد نظر با موفقیت ویرایش شد";
                Fm.PicClose.Visible = true;
                Fm.PicNO.Visible = false;
                Fm.PicYes.Visible = false;
                Fm.ShowDialog();

                BtnAdd.Enabled = true;
                BtnCancel.Enabled = false;
                BtnSave.Enabled = false;
                BtnDelete.Enabled = true;
                BtnEdit.Enabled = true;
                GrdRooms.Enabled = true;
                TxtRoomNum.Enabled = true;

                TxtRoomNumber.Enabled = false;
                CmbTypes.Enabled = false;
                FillValues();

                FrmRoomManagement_Load(null, null);


            }



        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            BtnAdd.Enabled = true;
            BtnCancel.Enabled = false;
            BtnSave.Enabled = false;
            BtnDelete.Enabled = true;
            BtnEdit.Enabled = true;
            GrdRooms.Enabled = true;
            TxtRoomNum.Enabled = true;

            TxtRoomNumber.Enabled = false;
            CmbTypes.Enabled = false;
            FillValues();
        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            Op = "Edit";
            BtnAdd.Enabled = false;
            BtnCancel.Enabled = true;
            BtnSave.Enabled = true;
            BtnDelete.Enabled = false;
            BtnEdit.Enabled = false;
            GrdRooms.Enabled = false;
            TxtRoomNum.Enabled = false;         
            TxtRoomNumber.Enabled = false;
            CmbTypes.Enabled = true;
            FillValues();

        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {

            FrmMessage Fm = new FrmMessage();
            Fm.LblText.Text = "آیا از حذف این گزینه اطمینان دارید ؟";
            Fm.PicClose.Visible = false;
            Fm.PicNO.Visible = true;
            Fm.PicYes.Visible = true;
            Fm.ShowDialog();

            if (Fm.Result == false)
            {
                return;
            }




            if (GrdRooms.CurrentRow.Cells[2].Value.ToString() == "خالی")
            {
                SqlCommand cmd = new SqlCommand("DeleteRoom", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@roomnum", TxtRoomNumber.Text);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                
                Fm.LblText.Text = "اتاق مورد نظر حذف شد";
                Fm.PicClose.Visible = true;
                Fm.PicNO.Visible = false;
                Fm.PicYes.Visible = false;
                Fm.ShowDialog();
                FrmRoomManagement_Load(null, null);
            }
            else
            {
                Fm.LblText.Text = "اتاق مورد نظر خالی نیست و نمیتوانید آن را حذف کنید";
                Fm.PicClose.Visible = true;
                Fm.PicNO.Visible = false;
                Fm.PicYes.Visible = false;
                Fm.ShowDialog();
            }




        }

        private void TxtRoomNumber_TextChanged(object sender, EventArgs e)
        {
            NumCorrection.DoCorrection(TxtRoomNumber);
        }
    }

}

