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

        SqlConnection con;
        String Op = "";




        private void FillChecks()
        {
            TxtUserName.Text = GrdAdmins.CurrentRow.Cells[0].Value.ToString();
            TxtPass.Text = GrdAdmins.CurrentRow.Cells[1].Value.ToString();
            TxtPass2.Text = GrdAdmins.CurrentRow.Cells[1].Value.ToString();
            ChTarifOtaq.Checked = Convert.ToBoolean(GrdAdmins.CurrentRow.Cells[2].Value);
            ChModiriatOtaq.Checked = Convert.ToBoolean(GrdAdmins.CurrentRow.Cells[3].Value);
            ChModiriatKhadamat.Checked = Convert.ToBoolean(GrdAdmins.CurrentRow.Cells[4].Value);
            ChModiriatKarbaran.Checked = Convert.ToBoolean(GrdAdmins.CurrentRow.Cells[5].Value);
            ChPaziresh.Checked = Convert.ToBoolean(GrdAdmins.CurrentRow.Cells[6].Value);
            ChPoshtibanGiri.Checked = Convert.ToBoolean(GrdAdmins.CurrentRow.Cells[7].Value);
            ChGozareshGiri.Checked = Convert.ToBoolean(GrdAdmins.CurrentRow.Cells[8].Value);




        }







        public FrmAdmin()
        {
            InitializeComponent();
        }

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

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

            GrdAdmins.DataSource = dt;

            FillChecks();
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            Op = "Add";
            TxtUserName.Enabled = true;
            TxtUserName.Text = "";
            TxtPass.Enabled = true;
            TxtPass.Text = "";
            TxtPass2.Enabled = true;
            TxtPass2.Text = "";
            TxtPass2.Visible = true;
            GrdAdmins.Enabled = false;
            ChModiriatKarbaran.Enabled = true;
            ChModiriatKarbaran.Checked = true;
            ChModiriatKhadamat.Enabled = true;
            ChModiriatKhadamat.Checked = true;

            ChModiriatOtaq.Enabled = true;
            ChModiriatOtaq.Checked = true;

            ChPaziresh.Enabled = true;
            ChPaziresh.Checked = true;

            ChGozareshGiri.Enabled = true;
            ChGozareshGiri.Checked = true;

            ChPoshtibanGiri.Enabled = true;
            ChPoshtibanGiri.Checked = true;

            ChTarifOtaq.Enabled = true;
            ChTarifOtaq.Checked = true;

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



        }

        private void BtnSave_Click(object sender, EventArgs e)
        {

            if (Op == "Add")
            {


                NumCorrection.DoCorrection(TxtPass);

                if (String.IsNullOrWhiteSpace(TxtUserName.Text))
                {
                    errorProvider1.Clear();
                    errorProvider1.SetError(TxtUserName, "نام کاربر را وارد کنید");
                }
                else if (String.IsNullOrWhiteSpace(TxtPass.Text))
                {
                    errorProvider1.Clear();
                    errorProvider1.SetError(TxtPass, "رمز عبور را وارد کنید");
                }
                else if (TxtPass.Text != TxtPass2.Text)
                {
                    errorProvider1.Clear();
                    errorProvider1.SetError(TxtPass2, "رمز عبور و تکرارش با هم برابر نیستند");
                }

                else
                {
                    errorProvider1.Clear();

                    SqlCommand cmd = new SqlCommand("AddAdmin", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Username", TxtUserName.Text);
                    cmd.Parameters.AddWithValue("@password", TxtPass.Text);
                    cmd.Parameters.AddWithValue("@TarifAnvaOtaq", ChTarifOtaq.Checked);
                    cmd.Parameters.AddWithValue("@ModiriatOtaq", ChModiriatOtaq.Checked);
                    cmd.Parameters.AddWithValue("@ModiriatKhadamat", ChModiriatKhadamat.Checked);
                    cmd.Parameters.AddWithValue("@ModiriatKarbaran", ChModiriatKarbaran.Checked);
                    cmd.Parameters.AddWithValue("@Paziresh", ChPaziresh.Checked);
                    cmd.Parameters.AddWithValue("@PoshtibanGiri", ChPoshtibanGiri.Checked);
                    cmd.Parameters.AddWithValue("@GozareshGiri", ChGozareshGiri.Checked);


                    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();



                    TxtUserName.Enabled = false;
                    TxtPass.Enabled = false;
                    TxtPass2.Enabled = false;
                    TxtPass2.Visible = false;
                    GrdAdmins.Enabled = true;
                    ChModiriatKarbaran.Enabled = false;
                    ChModiriatKhadamat.Enabled = false;
                    ChModiriatOtaq.Enabled = false;
                    ChPaziresh.Enabled = false;
                    ChGozareshGiri.Enabled = false;
                    ChPoshtibanGiri.Enabled = false;
                    ChTarifOtaq.Enabled = false;
                    BtnAdd.Enabled = false;
                    BtnEdit.Enabled = false;
                    BtnSave.Enabled = true;
                    BtnCancel.Enabled = true;
                    BtnDelete.Enabled = true;

                    FrmAdmin_Load(null, null);

                }

            }
            else
            {




                if (String.IsNullOrWhiteSpace(TxtPass.Text))
                {
                    errorProvider1.Clear();
                    errorProvider1.SetError(TxtPass, "رمز عبور را وارد کنید");
                }
                else if (TxtPass.Text != TxtPass2.Text)
                {
                    errorProvider1.Clear();
                    errorProvider1.SetError(TxtPass2, "رمز عبور و تکرارش با هم برابر نیستند");
                }

                else
                {

                    errorProvider1.Clear();
                    NumCorrection.DoCorrection(TxtPass);
                    SqlCommand cmd = new SqlCommand("EditAdmin", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Username", TxtUserName.Text);
                    cmd.Parameters.AddWithValue("@password", TxtPass.Text);
                    cmd.Parameters.AddWithValue("@TarifAnvaOtaq", ChTarifOtaq.Checked);
                    cmd.Parameters.AddWithValue("@ModiriatOtaq", ChModiriatOtaq.Checked);
                    cmd.Parameters.AddWithValue("@ModiriatKhadamat", ChModiriatKhadamat.Checked);
                    cmd.Parameters.AddWithValue("@ModiriatKarbaran", ChModiriatKarbaran.Checked);
                    cmd.Parameters.AddWithValue("@Paziresh", ChPaziresh.Checked);
                    cmd.Parameters.AddWithValue("@PoshtibanGiri", ChPoshtibanGiri.Checked);
                    cmd.Parameters.AddWithValue("@GozareshGiri", ChGozareshGiri.Checked);


                    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();



                    TxtUserName.Enabled = false;
                    TxtPass.Enabled = false;
                    TxtPass2.Enabled = false;
                    TxtPass2.Visible = false;
                    GrdAdmins.Enabled = true;
                    ChModiriatKarbaran.Enabled = false;
                    ChModiriatKhadamat.Enabled = false;
                    ChModiriatOtaq.Enabled = false;
                    ChPaziresh.Enabled = false;
                    ChGozareshGiri.Enabled = false;
                    ChPoshtibanGiri.Enabled = false;
                    ChTarifOtaq.Enabled = false;
                    BtnAdd.Enabled = false;
                    BtnEdit.Enabled = false;
                    BtnSave.Enabled = true;
                    BtnCancel.Enabled = true;
                    BtnDelete.Enabled = true;

                    FrmAdmin_Load(null, null);

                }



            }
        }

        private void GrdAdmins_Click(object sender, EventArgs e)
        {
            FillChecks();
        }

        private void GrdAdmins_KeyUp(object sender, KeyEventArgs e)
        {
            FillChecks();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            TxtUserName.Enabled = false;
            TxtPass.Enabled = false;
            TxtPass2.Enabled = false;
            TxtPass2.Visible = false;
            GrdAdmins.Enabled = true;
            ChModiriatKarbaran.Enabled = false;
            ChModiriatKhadamat.Enabled = false;
            ChModiriatOtaq.Enabled = false;
            ChPaziresh.Enabled = false;
            ChGozareshGiri.Enabled = false;
            ChPoshtibanGiri.Enabled = false;
            ChTarifOtaq.Enabled = false;
            BtnAdd.Enabled = false;
            BtnEdit.Enabled = false;
            BtnSave.Enabled = true;
            BtnCancel.Enabled = true;
            BtnDelete.Enabled = true;

            FrmAdmin_Load(null, null);
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            FrmMessage Fm = new FrmMessage();


            if (GrdAdmins.Rows.Count == 1)
            {
                
                Fm.LblText.Text = "باید حداقل یک کاربر داشته باشید";
                Fm.PicClose.Visible = true;
                Fm.PicNO.Visible = false;
                Fm.PicYes.Visible = false;
                Fm.ShowDialog();
                return;


            }



            
            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("DeleteAdmin", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@username", GrdAdmins.CurrentRow.Cells[0].Value.ToString());

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


                FrmAdmin_Load(null, null);
            }

        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            Op = "Edit";
            TxtUserName.Enabled = false;
            TxtPass.Enabled = true;
            TxtPass2.Enabled = true;
            TxtPass2.Visible = true;
            GrdAdmins.Enabled = false;
            ChModiriatKarbaran.Enabled = true;
            ChModiriatKhadamat.Enabled = true;
            ChModiriatOtaq.Enabled = true;
            ChPaziresh.Enabled = true;
            ChGozareshGiri.Enabled = true;
            ChPoshtibanGiri.Enabled = true;
            ChTarifOtaq.Enabled = true;
            BtnAdd.Enabled = false;
            BtnEdit.Enabled = false;
            BtnSave.Enabled = true;
            BtnCancel.Enabled = true;
            BtnDelete.Enabled = true;

        }
    }
}
