using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AzmoonGir
{
    public partial class FrmAdmin : Form
    {

        SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\DbAzmoon.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
        string whatstate = "";


        public FrmAdmin()
        {
            InitializeComponent();
        }





        private void FillChecks()
    {


     

      try
      {

            TxtUserName.Text = Grd.CurrentRow.Cells["UserName"].Value.ToString();
            TxtPass1.Text = Grd.CurrentRow.Cells["Password"].Value.ToString();

            ChQuestionAdd.Checked = Convert.ToBoolean(Grd.CurrentRow.Cells["QuestionAdd"].Value);
      
            ChGroupAdd.Checked = Convert.ToBoolean(Grd.CurrentRow.Cells["GroupAdd"].Value);
   
            ChQuestionEdit.Checked = Convert.ToBoolean(Grd.CurrentRow.Cells["QuestionEdit"].Value);

            ChGroupEdit.Checked = Convert.ToBoolean(Grd.CurrentRow.Cells["GroupEdit"].Value);
            
            ChAzmoon.Checked = Convert.ToBoolean(Grd.CurrentRow.Cells["Azmoon"].Value);
           
            ChManagement.Checked = Convert.ToBoolean(Grd.CurrentRow.Cells["Management"].Value);

            ChReport.Checked = Convert.ToBoolean(Grd.CurrentRow.Cells["Report"].Value);

            ChBackup.Checked = Convert.ToBoolean(Grd.CurrentRow.Cells["Backup"].Value);





        }

        catch
        {

            TxtUserName.Text = "";
            TxtPass1.Text = "";

            ChQuestionAdd.Checked = false;

            ChGroupAdd.Checked = false;

            ChQuestionEdit.Checked = false;

            ChGroupEdit.Checked = false;

            ChAzmoon.Checked = false;

            ChManagement.Checked = false;

            ChReport.Checked = false;

            ChBackup.Checked = false;

       }









    }




        private void FrmAdmin_Load(object sender, EventArgs e)
        {

        }

        private void FrmAdmin_Load_1(object sender, EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter("SelectAllAdmins", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            da.Fill(dt);
            Grd.DataSource = dt;
        //    MessageBox.Show(dt.Rows[0][5].ToString());



            FillChecks();








        }

        private void Grd_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            FillChecks();

        }

        private void Grd_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            FillChecks();
        }

        private void Grd_MouseClick(object sender, MouseEventArgs e)
        {
            FillChecks();
        }

        private void Grd_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {




            if (whatstate == "Add")
            {
                

                errorProvider1.SetError(TxtPass2, "");
                errorProvider1.SetError(TxtPass1, "");
                errorProvider1.SetError(TxtUserName, "");
                



                if (TxtPass1.Text != TxtPass2.Text)
                {
                    errorProvider1.SetError(TxtPass2, "رمز عبور وارد شده مطابقت ندارد");
                    return;
                }

                if (string.IsNullOrWhiteSpace(TxtUserName.Text))
                {
                    errorProvider1.SetError(TxtUserName, "نام کاربر وارد نشده است");
                    return;
                }

                if (string.IsNullOrWhiteSpace(TxtPass1.Text))
                {
                    errorProvider1.SetError(TxtPass1, "رمز عبور وارد نشده است");
                    return;
                }

               


                errorProvider1.SetError(TxtPass2, "");
                errorProvider1.SetError(TxtPass1, "");
                errorProvider1.SetError(TxtUserName, "");
                

                //SqlConnection con = new SqlConnection("server = .\\SQLEXPRESS;DataBase = DbAzmoon;Integrated Security = True");
                SqlCommand cmd = new SqlCommand("AddAdmin", con);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@username", TxtUserName.Text);
                cmd.Parameters.AddWithValue("@addquestion", ChQuestionAdd.Checked);
                cmd.Parameters.AddWithValue("@addgroup", ChGroupAdd.Checked);
                cmd.Parameters.AddWithValue("@editquestion", ChQuestionEdit.Checked);
                cmd.Parameters.AddWithValue("@editgroup", ChGroupEdit.Checked);
                cmd.Parameters.AddWithValue("@management", ChManagement.Checked);
                cmd.Parameters.AddWithValue("@azmoon", ChAzmoon.Checked);
                cmd.Parameters.AddWithValue("@backup", ChBackup.Checked);
                cmd.Parameters.AddWithValue("@report", ChReport.Checked);
                cmd.Parameters.AddWithValue("@password", TxtPass1.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());


                Frm_Message Fm = new Frm_Message();
                if (res == 1)
                {
                    Fm.lbltext.Text = "این نام کاربری قبلا ثبت شده است";
                    Fm.ImgClose.Visible = true;
                    Fm.ImgYes.Visible = false;
                    Fm.ImgNo.Visible = false;

                    Fm.ShowDialog();
                }
                else
                {
                    Fm.lbltext.Text = "کاربر با موفقیت ثبت شد";
                    Fm.ImgClose.Visible = true;
                    Fm.ImgYes.Visible = false;
                    Fm.ImgNo.Visible = false;

                    Fm.ShowDialog();

                    TxtPass1.Enabled = false;
                    TxtPass1.Text = "";
                    TxtPass2.Text = "";
                    TxtPass2.Enabled = false;
                    TxtPass2.Visible = false;
                    label2.Visible = false;
                    TxtUserName.Text = "";
                    TxtUserName.Enabled = false;
                    ChQuestionAdd.Enabled = false;
                    ChQuestionAdd.Checked = false;
                    ChQuestionEdit.Enabled = false;
                    ChQuestionEdit.Checked = false;
                    ChGroupAdd.Enabled = false;
                    ChGroupAdd.Checked = false;
                    ChGroupEdit.Enabled = false;
                    ChGroupEdit.Checked = false;
                    ChAzmoon.Enabled = false;
                    ChAzmoon.Checked = false;
                    ChManagement.Enabled = false;
                    ChManagement.Checked = false;
                    ChReport.Enabled = false;
                    ChReport.Checked = false;
                    ChBackup.Enabled = false;
                    ChBackup.Checked = false;
                    btnAdd.Enabled = true;
                    BtnEdit.Enabled = true;
                    BtnDelete.Enabled = true;
                    BtnSave.Enabled = false;
                    BtnCancel.Enabled = false;
                    Grd.Enabled = true;
                    
                    FrmAdmin_Load_1(null, null);
                    

                }

            }

            else
            {


                errorProvider1.SetError(TxtPass2, "");
                errorProvider1.SetError(TxtPass1, "");
                errorProvider1.SetError(TxtUserName, "");




                if (TxtPass1.Text != TxtPass2.Text)
                {
                    errorProvider1.SetError(TxtPass2, "رمز عبور وارد شده مطابقت ندارد");
                    return;
                }

               

                if (string.IsNullOrWhiteSpace(TxtPass1.Text))
                {
                    errorProvider1.SetError(TxtPass1, "رمز عبور وارد نشده است");
                    return;
                }




                errorProvider1.SetError(TxtPass2, "");
                errorProvider1.SetError(TxtPass1, "");
               


                //SqlConnection con = new SqlConnection("server = .\\SQLEXPRESS;DataBase = DbAzmoon;Integrated Security = True");
                SqlCommand cmd = new SqlCommand("EditAdmin", con);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@username", TxtUserName.Text);
                cmd.Parameters.AddWithValue("@addquestion", ChQuestionAdd.Checked);
                cmd.Parameters.AddWithValue("@addgroup", ChGroupAdd.Checked);
                cmd.Parameters.AddWithValue("@editquestion", ChQuestionEdit.Checked);
                cmd.Parameters.AddWithValue("@editgroup", ChGroupEdit.Checked);
                cmd.Parameters.AddWithValue("@management", ChManagement.Checked);
                cmd.Parameters.AddWithValue("@azmoon", ChAzmoon.Checked);
                cmd.Parameters.AddWithValue("@backup", ChBackup.Checked);
                cmd.Parameters.AddWithValue("@report", ChReport.Checked);
                cmd.Parameters.AddWithValue("@password", TxtPass1.Text);
                
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

            
                Frm_Message Fm = new Frm_Message();
            
                    Fm.lbltext.Text = "کاربر با موفقیت ویرایش شد";
                    Fm.ImgClose.Visible = true;
                    Fm.ImgYes.Visible = false;
                    Fm.ImgNo.Visible = false;

                    Fm.ShowDialog();

                    TxtPass1.Enabled = false;
                    TxtPass1.Text = "";
                    TxtPass2.Text = "";
                    TxtPass2.Enabled = false;
                    TxtPass2.Visible = false;
                    label2.Visible = false;
                    TxtUserName.Text = "";
                    TxtUserName.Enabled = false;
                    ChQuestionAdd.Enabled = false;
                    ChQuestionAdd.Checked = false;
                    ChQuestionEdit.Enabled = false;
                    ChQuestionEdit.Checked = false;
                    ChGroupAdd.Enabled = false;
                    ChGroupAdd.Checked = false;
                    ChGroupEdit.Enabled = false;
                    ChGroupEdit.Checked = false;
                    ChAzmoon.Enabled = false;
                    ChAzmoon.Checked = false;
                    ChManagement.Enabled = false;
                    ChManagement.Checked = false;
                    ChReport.Enabled = false;
                    ChReport.Checked = false;
                    ChBackup.Enabled = false;
                    ChBackup.Checked = false;
                    btnAdd.Enabled = true;
                    BtnEdit.Enabled = true;
                    BtnDelete.Enabled = true;
                    BtnSave.Enabled = false;
                    BtnCancel.Enabled = false;
                    Grd.Enabled = true;

                    FrmAdmin_Load_1(null, null);


                }



            }

    










        

        private void btnAdd_Click(object sender, EventArgs e)
        {
            TxtPass1.Enabled = true;
            TxtPass1.Text = "";
            TxtPass2.Text = "";
            TxtPass2.Enabled = true;
            TxtPass2.Visible = true;
            label2.Visible = true;
            TxtUserName.Text = "";
            TxtUserName.Enabled = true;
            ChQuestionAdd.Enabled = true;
            ChQuestionAdd.Checked = true;
            ChQuestionEdit.Enabled = true;
            ChQuestionEdit.Checked = true;
            ChGroupAdd.Enabled = true;
            ChGroupAdd.Checked = true;
            ChGroupEdit.Enabled = true;
            ChGroupEdit.Checked = true;
            ChAzmoon.Enabled = true;
            ChAzmoon.Checked = true;
            ChManagement.Enabled = true;
            ChManagement.Checked = true;
            ChReport.Enabled = true;
            ChReport.Checked = true;
            ChBackup.Enabled = true;
            ChBackup.Checked = true;
            btnAdd.Enabled = false;
            BtnEdit.Enabled = false;
            BtnDelete.Enabled = false;
            BtnSave.Enabled = true;
            BtnCancel.Enabled = true;
            Grd.Enabled = false;

            whatstate = "Add";


            TxtUserName.Focus();


        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
           

            TxtPass1.Enabled = false;
            TxtPass1.Text = "";
            TxtPass2.Text = "";
            TxtPass2.Enabled = false;
            TxtPass2.Visible = false;
            label2.Visible = false;
            TxtUserName.Text = "";
            TxtUserName.Enabled = false;
            ChQuestionAdd.Enabled = false;
            ChQuestionAdd.Checked = false;
            ChQuestionEdit.Enabled = false;
            ChQuestionEdit.Checked = false;
            ChGroupAdd.Enabled = false;
            ChGroupAdd.Checked = false;
            ChGroupEdit.Enabled = false;
            ChGroupEdit.Checked = false;
            ChAzmoon.Enabled = false;
            ChAzmoon.Checked = false;
            ChManagement.Enabled = false;
            ChManagement.Checked = false;
            ChReport.Enabled = false;
            ChReport.Checked = false;
            ChBackup.Enabled = false;
            ChBackup.Checked = false;
            btnAdd.Enabled = true;
            BtnEdit.Enabled = true;
            BtnDelete.Enabled = true;
            BtnSave.Enabled = false;
            BtnCancel.Enabled = false;
            Grd.Enabled = true;
            FillChecks();
            FrmAdmin_Load_1(null, null);


        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            TxtPass1.Enabled = true;

            TxtPass2.Text = TxtPass1.Text;
            TxtPass2.Enabled = true;
            TxtPass2.Visible = true;
            label2.Visible = true;
           
            TxtUserName.Enabled = false;
            ChQuestionAdd.Enabled = true;
            
            ChQuestionEdit.Enabled = true;
           
            ChGroupAdd.Enabled = true;
            
            ChGroupEdit.Enabled = true;
           
            ChAzmoon.Enabled = true;
         
            ChManagement.Enabled = true;
          
            ChReport.Enabled = true;
          
            ChBackup.Enabled = true;
         
            btnAdd.Enabled = false;
            BtnEdit.Enabled = false;
            BtnDelete.Enabled = false;
            BtnSave.Enabled = true;
            BtnCancel.Enabled = true;
            Grd.Enabled = false;

            whatstate = "Edit";


            TxtUserName.Focus();

        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            Frm_Message Fm = new Frm_Message();
            Fm.lbltext.Text = "آیا از حذف کاربر جاری اطمینان دارید؟";
            Fm.ImgClose.Visible = false;
            Fm.ImgYes.Visible = true;
            Fm.ImgNo.Visible = true;
            Fm.ShowDialog();


            if (Fm.resualt == true)
            {

                SqlCommand cmd = new SqlCommand("DeleteAdmin", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@username", TxtUserName.Text);


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

                FrmAdmin_Load_1(null, null);
                

            }
        }

        private void Grd_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void BtnCancel_Click_1(object sender, EventArgs e)
        {

            TxtPass1.Enabled = false;
            TxtPass1.Text = "";
            TxtPass2.Text = "";
            TxtPass2.Enabled = false;
            TxtPass2.Visible = false;
            label2.Visible = false;
            TxtUserName.Text = "";
            TxtUserName.Enabled = false;
            ChQuestionAdd.Enabled = false;
            ChQuestionAdd.Checked = false;
            ChQuestionEdit.Enabled = false;
            ChQuestionEdit.Checked = false;
            ChGroupAdd.Enabled = false;
            ChGroupAdd.Checked = false;
            ChGroupEdit.Enabled = false;
            ChGroupEdit.Checked = false;
            ChAzmoon.Enabled = false;
            ChAzmoon.Checked = false;
            ChManagement.Enabled = false;
            ChManagement.Checked = false;
            ChReport.Enabled = false;
            ChReport.Checked = false;
            ChBackup.Enabled = false;
            ChBackup.Checked = false;
            btnAdd.Enabled = true;
            BtnEdit.Enabled = true;
            BtnDelete.Enabled = true;
            BtnSave.Enabled = false;
            BtnCancel.Enabled = false;
            Grd.Enabled = true;

            FrmAdmin_Load_1(null, null);
        }

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

}
