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



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





        public FrmQuestionEdit()
        {
            InitializeComponent();
        }








        private void FillTextBoxes()
        {


            try
            {

                TxtQuestionText.Text = GrdGroup.CurrentRow.Cells["qtext"].Value.ToString();
                TxtCase1.Text = GrdGroup.CurrentRow.Cells["case1"].Value.ToString();
                TxtCase2.Text = GrdGroup.CurrentRow.Cells["case2"].Value.ToString();
                TxtCase3.Text = GrdGroup.CurrentRow.Cells["case3"].Value.ToString();
                TxtCase4.Text = GrdGroup.CurrentRow.Cells["case4"].Value.ToString();
                CmbAnswer.SelectedIndex = Convert.ToInt32(GrdGroup.CurrentRow.Cells["answer"].Value) - 1;
            }

            catch
            {
                TxtQuestionText.Text = "";
                TxtCase1.Text = "";
                TxtCase2.Text = "";
                TxtCase3.Text = "";
                TxtCase4.Text = "";
                CmbAnswer.SelectedIndex = 0;
            }

            
        }














        private void FrmQuestionEdit_Load(object sender, EventArgs e)
        {


            SqlDataAdapter da = new SqlDataAdapter("SelectAllGroup", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            da.Fill(dt);
            CmbGroups.DataSource = dt;
            CmbGroups.DisplayMember = "GroupName";
            CmbGroups.ValueMember = "GroupID";





            try
            {


                SqlDataAdapter da2 = new SqlDataAdapter("SelectAllQuestion", con);
                da2.SelectCommand.CommandType = CommandType.StoredProcedure;
                da2.SelectCommand.Parameters.AddWithValue("@groupid", ((DataRowView)CmbGroups.SelectedItem)["GroupId"]);
                DataTable dt2 = new DataTable();
                da2.Fill(dt2);
                GrdGroup.DataSource = dt2;
            }
            catch
            {
                //MessageBox.Show(ex.Message);
            }

            
        }

        private void CmbGroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataAdapter da2 = new SqlDataAdapter("SelectAllQuestion", con);
            da2.SelectCommand.CommandType = CommandType.StoredProcedure;
            da2.SelectCommand.Parameters.AddWithValue("@groupid", ((DataRowView)CmbGroups.SelectedItem)["GroupId"]);
            DataTable dt2 = new DataTable();
            da2.Fill(dt2);
            GrdGroup.DataSource = dt2;

            FillTextBoxes();

        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
           
            if (TxtQuestionText.Text== "") 
            {
                return;
            }



            CmbAnswer.Enabled = true;
            TxtCase1.Enabled = true;
            TxtCase2.Enabled = true;
            TxtCase3.Enabled = true;
            TxtCase4.Enabled = true; ;
            TxtQuestionText.Enabled = true;
            BtnEdit.Enabled = false;
            BtnSave.Enabled = true;
            BtnCancel.Enabled = true;
            CmbGroups.Enabled = false;
            BtnDelete.Enabled = false;

           

        }

        private void GrdGroup_RowEnter(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void GrdGroup_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            FillTextBoxes();


        }

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

        private void GrdGroup_MouseClick(object sender, MouseEventArgs e)
        {
            FillTextBoxes();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(CmbGroups.Text))
            {
                errorProvider1.SetError(CmbGroups, "گروه سوالات را انتخاب کنید");
            }

            else if (string.IsNullOrWhiteSpace(TxtQuestionText.Text))
            {
                errorProvider1.SetError(TxtQuestionText, "متن سوال را وارد کنید");
            }


            else if (string.IsNullOrWhiteSpace(TxtCase1.Text))
            {
                errorProvider1.SetError(TxtCase1, "گزینه 1 را وارد کنید");
            }

            else if (string.IsNullOrWhiteSpace(TxtCase2.Text))
            {
                errorProvider1.SetError(TxtCase2, "گزینه 2 را وارد کنید");
            }


            else if (string.IsNullOrWhiteSpace(TxtCase3.Text))
            {
                errorProvider1.SetError(TxtCase3, "گزینه 3 را وارد کنید");
            }



            else if (string.IsNullOrWhiteSpace(TxtCase4.Text))
            {
                errorProvider1.SetError(TxtCase4, "گزینه 4 را وارد کنید");
            }

            else
            {

                errorProvider1.SetError(TxtCase1, "");
                errorProvider1.SetError(TxtCase4, "");
                errorProvider1.SetError(TxtCase3, "");
                errorProvider1.SetError(TxtCase2, "");
                errorProvider1.SetError(TxtQuestionText, "");
                errorProvider1.SetError(CmbGroups, "");


                SqlCommand cmd = new SqlCommand("EditQuestion", con);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@groupid", CmbGroups.SelectedValue);
                cmd.Parameters.AddWithValue("@questiontext", TxtQuestionText.Text);
                cmd.Parameters.AddWithValue("@case1", TxtCase1.Text);
                cmd.Parameters.AddWithValue("@case2", TxtCase2.Text);
                cmd.Parameters.AddWithValue("@case3", TxtCase3.Text);
                cmd.Parameters.AddWithValue("@case4", TxtCase4.Text);
                cmd.Parameters.AddWithValue("@answer", CmbAnswer.Text);
                cmd.Parameters.AddWithValue("@questionid", GrdGroup.CurrentRow.Cells[0].Value.ToString());


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


                BtnCancel.Enabled = false;
                BtnSave.Enabled = false;
                BtnEdit.Enabled = true;
                BtnDelete.Enabled = true;
                CmbAnswer.Enabled = false;
                TxtCase1.Enabled = false;
                TxtCase2.Enabled = false;
                TxtCase3.Enabled = false;
                TxtCase4.Enabled = false; ;
                TxtQuestionText.Enabled = false;
                BtnEdit.Enabled = true;
                BtnSave.Enabled = false;
                BtnCancel.Enabled = false;
                CmbGroups.Enabled = true;



                SqlDataAdapter da2 = new SqlDataAdapter("SelectAllQuestion", con);
                da2.SelectCommand.CommandType = CommandType.StoredProcedure;
                da2.SelectCommand.Parameters.AddWithValue("@groupid", ((DataRowView)CmbGroups.SelectedItem)["GroupId"]);
                DataTable dt2 = new DataTable();
                da2.Fill(dt2);
                GrdGroup.DataSource = dt2;






            }



        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {



            errorProvider1.SetError(TxtCase1, "");
            errorProvider1.SetError(TxtCase4, "");
            errorProvider1.SetError(TxtCase3, "");
            errorProvider1.SetError(TxtCase2, "");
            errorProvider1.SetError(TxtQuestionText, "");
            errorProvider1.SetError(CmbGroups, "");

            BtnCancel.Enabled = false;
            BtnSave.Enabled = false;
            BtnEdit.Enabled = true;
            BtnDelete.Enabled = true;
            CmbAnswer.Enabled = false;
            TxtCase1.Enabled = false;
            TxtCase2.Enabled = false;
            TxtCase3.Enabled = false;
            TxtCase4.Enabled = false; ;
            TxtQuestionText.Enabled = false;
            BtnEdit.Enabled = true;
            BtnSave.Enabled = false;
            BtnCancel.Enabled = false;
            CmbGroups.Enabled = true;
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            if (TxtQuestionText.Text == "")
            {
                return;
            }
            
            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("DeleteQuestion", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@questionid", GrdGroup.CurrentRow.Cells[0].Value.ToString());


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


                SqlDataAdapter da2 = new SqlDataAdapter("SelectAllQuestion", con);
                da2.SelectCommand.CommandType = CommandType.StoredProcedure;
                da2.SelectCommand.Parameters.AddWithValue("@groupid", ((DataRowView)CmbGroups.SelectedItem)["GroupId"]);
                DataTable dt2 = new DataTable();
                da2.Fill(dt2);
                GrdGroup.DataSource = dt2;

                
                FillTextBoxes();


            }

        }

        private void GrdGroup_KeyUp(object sender, KeyEventArgs e)
        {
            FillTextBoxes();
        }
    }
}
