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 FrmRoomTypes : Form
    {

        SqlConnection con;

        public FrmRoomTypes()
        {
            InitializeComponent();
        }

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

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

            GrdRoomTypes.DataSource = dt;





        }

        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 == true)
           {
               SqlCommand cmd = new SqlCommand("DeleteRoomType",con);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@TypeID", Convert.ToInt32(GrdRoomTypes.CurrentRow.Cells[0].Value.ToString()));

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


               FrmRoomTypes_Load(null, null);

           }

        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(TxtType.Text))
            {
                errorProvider1.SetError(TxtType, "نوع اتاق را وارد کنید");
            }
            else if (String.IsNullOrWhiteSpace(TxtCost.Text))
            {
                errorProvider1.SetError(TxtCost, "کرایه اتاق را وارد کنید");
            }
            else
            {
                errorProvider1.SetError(TxtType, "");
                errorProvider1.SetError(TxtCost, "");

                SqlCommand cmd = new SqlCommand("AddRoomType", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@TypeName", TxtType.Text);
                cmd.Parameters.AddWithValue("@Cost", TxtCost.Text);

                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
                {
                    FrmRoomTypes_Load(null, null);
                    TxtType.Text = "";
                    TxtType.Focus();
                }


            }
        }

        private void rectangleShape1_Click(object sender, EventArgs e)
        {

        }

        private void TxtCost_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) < 57 & Convert.ToInt16(e.KeyChar) > 48 || Convert.ToInt16(e.KeyChar) == 8)
            {
            }
            else
            {
                e.KeyChar = Convert.ToChar(Keys.None);
            }

        }
    }
}
