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

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

        public FrmQuestionAdd()
        {
            InitializeComponent();
        }

        private void FrmQuestionAdd_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";




            CmbAnswer.SelectedIndex = 0;
            


            
        }

        private void CmbGroups_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {

            if (CmbGroups.Text == "")
            {
                Frm_Message Fm = new Frm_Message();
                Fm.lbltext.Text = "گروهی وجود ندارد";
                Fm.ImgClose.Visible = true;
                Fm.ImgYes.Visible = false;
                Fm.ImgNo.Visible = false;

                Fm.ShowDialog();
                return;
            }


            CmbGroups.Enabled = true;
            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;

            TxtQuestionText.Text = "";
            TxtCase1.Text = "";
            TxtCase2.Text = "";
            TxtCase3.Text = "";
            TxtCase4.Text = "";



            CmbGroups.Focus();
            


        }

        private void TxtQuestionText_TextChanged(object sender, EventArgs e)
        {

        }

        private void TxtQuestionText_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) == 13)
            {
             BtnSave_Click (null, null);
            }

        }

        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, "");





                try
                {
                   // SqlConnection con = new SqlConnection("server = .\\SQLEXPRESS;DataBase = DbAzmoon;Integrated Security = True");
                    SqlCommand cmd = new SqlCommand("AddQuestion", 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);

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




                    CmbGroups.Enabled = false;
                    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;






                }


                catch
                {

                    Frm_Message Fm = new Frm_Message();
                    Fm.lbltext.Text = "خطا در اتصال به بانک";
                    Fm.ImgClose.Visible = true;
                    Fm.ImgYes.Visible = false;
                    Fm.ImgNo.Visible = false;

                    Fm.ShowDialog();
 
                }




            }
        }
        private void TxtCase1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) == 13)
            {
                BtnSave_Click(null, null);
            }
        }

        private void TxtCase2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) == 13)
            {
                BtnSave_Click(null, null);
            }
        }

        private void TxtCase3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) == 13)
            {
                BtnSave_Click(null, null);
            }
        }

        private void TxtCase4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) == 13)
            {
                BtnSave_Click(null, null);
            }
        }

        private void CmbAnswer_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) == 13)
            {
                BtnSave_Click(null, null);
            }
        }

        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, "");

            
            CmbGroups.Enabled = false;
                    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;

        }
    }
}
