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

        SqlConnection con;
        String Op = "";




        private void Filltext()
        {
            TxtName.Text = GrdServices.CurrentRow.Cells[1].Value.ToString();
            TxtCost.Text = GrdServices.CurrentRow.Cells[2].Value.ToString();



        }










        public FrmServices()
        {
            InitializeComponent();
        }

        private void rectangleShape1_Click(object sender, EventArgs e)
        {

        }

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

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

            GrdServices.DataSource = dt;


            Filltext();





        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            Op = "Add";
            BtnAdd.Enabled = false;
            BtnSave.Enabled = true;
            BtnCancel.Enabled = true;
            BtnEdit.Enabled = false;
            BtnDelete.Enabled = false;
            GrdServices.Enabled = false;
            TxtCost.Enabled = true;
            TxtName.Enabled = true;
            TxtCost.Text = "";
            TxtName.Text = "";
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (Op == "Add")
            {


                if (String.IsNullOrWhiteSpace(TxtName.Text))
                {
                    errorProvider1.SetError(TxtName, "نام سرویس را وارد کنید");
                }
                else if (String.IsNullOrWhiteSpace(TxtCost.Text))
                {
                    errorProvider1.SetError(TxtCost, "قیمت واحد سرویس را وارد کنید");
                }
                else
                {
                    errorProvider1.SetError(TxtName, "");
                    errorProvider1.SetError(TxtCost, "");

                    SqlCommand cmd = new SqlCommand("AddService", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@servicename", TxtName.Text);
                    cmd.Parameters.AddWithValue("@servicecost", TxtCost.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();

                    FrmServices_Load(null, null);

                    BtnAdd.Enabled = true;
                    BtnSave.Enabled = false;
                    BtnCancel.Enabled = false;
                    BtnEdit.Enabled = true;
                    BtnDelete.Enabled = true;
                    GrdServices.Enabled = true;
                    TxtCost.Enabled = false;
                    TxtName.Enabled = false;



                }

            }
            else
            {

                if (String.IsNullOrWhiteSpace(TxtName.Text))
                {
                    errorProvider1.SetError(TxtName, "نام سرویس را وارد کنید");
                }
                else if (String.IsNullOrWhiteSpace(TxtCost.Text))
                {
                    errorProvider1.SetError(TxtCost, "قیمت واحد سرویس را وارد کنید");
                }
                else
                {
                    errorProvider1.SetError(TxtName, "");
                    errorProvider1.SetError(TxtCost, "");

                    SqlCommand cmd = new SqlCommand("EditService", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@servicename", TxtName.Text);
                    cmd.Parameters.AddWithValue("@servicecost", TxtCost.Text);
                    cmd.Parameters.AddWithValue("@id",GrdServices.CurrentRow.Cells[0].Value.ToString());
                    
                    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();

                    FrmServices_Load(null, null);

                    BtnAdd.Enabled = true;
                    BtnSave.Enabled = false;
                    BtnCancel.Enabled = false;
                    BtnEdit.Enabled = true;
                    BtnDelete.Enabled = true;
                    GrdServices.Enabled = true;
                    TxtCost.Enabled = false;
                    TxtName.Enabled = false;



                }

            }
        }

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

            Filltext();
        }

        private void GrdServices_Click(object sender, EventArgs e)
        {
            Filltext();

        }

        private void GrdServices_KeyUp(object sender, KeyEventArgs e)
        {
            Filltext();
        }

        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("DeleteService", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", Convert.ToInt32(GrdServices.CurrentRow.Cells[0].Value.ToString()));

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


                FrmServices_Load(null, null);


            }
        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            Op = "Edit";
            BtnAdd.Enabled = false;
            BtnSave.Enabled = true;
            BtnCancel.Enabled = true;
            BtnEdit.Enabled = false;
            BtnDelete.Enabled = false;
            GrdServices.Enabled = false;
            TxtCost.Enabled = true;
            TxtName.Enabled = true;
        }

        private void TxtCost_TextChanged(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);
            }
        }
    }
}
