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

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


        public FrmEdit()
        {
            InitializeComponent();
        }

        private void FrmEdit_Load(object sender, EventArgs e)
        {
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("SelectAllGroup", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                GrdGroup.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            this.Height = 371;
            label3.Visible = true;
            TxtGroupName.Visible = true;
            TxtGroupName.Text = "";
            TxtGroupName.Focus();
            BtnCancel.Enabled = true;
            BtnSave.Enabled = true;
            BtnEdit.Enabled = false;
            BtnDelete.Enabled = false;
            TxtGroupName.Text = GrdGroup.CurrentRow.Cells["groupname"].Value.ToString();
            IDGroup = Convert.ToInt32( GrdGroup.CurrentRow.Cells["groupid"].Value);
            


        }

        private void BtnSave_Click(object sender, EventArgs e)
        {


            if (string.IsNullOrWhiteSpace(TxtGroupName.Text))
            {
                errorProvider1.SetError(TxtGroupName, "نام گروه وارد نشده است");
            }
            else
            {
                errorProvider1.SetError(TxtGroupName, "");
            this.Height = 319;
            label3.Visible = false;
            TxtGroupName.Visible = false;
           
           
            BtnCancel.Enabled = false;
            BtnSave.Enabled = false;
            BtnEdit.Enabled = true;
            BtnDelete.Enabled = true;

            SqlCommand cmd = new SqlCommand("EditGroup", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@groupid", IDGroup);
            cmd.Parameters.AddWithValue("@groupname", TxtGroupName.Text.Trim());
            

            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();
            TxtGroupName.Text = "";

            FrmEdit_Load(null, null);


            }


        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Height = 319;
            label3.Visible = false;
            TxtGroupName.Visible = false;
            TxtGroupName.Text = "";

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

        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)
            {
                IDGroup = Convert.ToInt32(GrdGroup.CurrentRow.Cells["groupid"].Value);

                SqlCommand cmd = new SqlCommand("DeleteGroup", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@groupid", IDGroup);


                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                FrmEdit_Load(null, null);

            }

        }

        private void GrdGroup_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
