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

namespace WareHouse
{
    public partial class frmCategoory : Form
    {
        public void dbConnection()
        {
            SqlConnection con = new SqlConnection();
            try
            {

                con.ConnectionString = Properties.Settings.Default.DBWare_mdbConnectionString.ToString();
                SqlCommand cmd = new SqlCommand();
                DataTable dtb = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                cmd.Connection = con;
                cmd.CommandText = @"SELECT CatID, Categ FROM Categ order by CatID desc";
                con.Open();
                da.Fill(dtb);
                dgrdCateg.DataSource = dtb;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

        public frmCategoory()
        {
            InitializeComponent();
        }

        private void frmCategoory_Load(object sender, EventArgs e)
        {
            dbConnection();
        }



        private void btnDel_Click(object sender, EventArgs e)
        {
            mydb d1 = new mydb();
            d1.exeFun("delete from Categ where catid='" + txtId.Text + "' ");
            dbConnection();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            txtCateg.Text = String.Empty;
            btnAdd.SendToBack();
            Btn_Save.BringToFront();
            MessageBox.Show("لطفا اطلاعات را وارد نمایید");
        }

        private void dgrdCateg_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = dgrdCateg.GetCellCount(DataGridViewElementStates.Selected);
            String Str_Fcode = String.Empty;
            String Str_FStr = String.Empty;

            if (selectedCellCount > 0)
            {
                if (dgrdCateg.AreAllCellsSelected(true))
                {
                    MessageBox.Show("All cells are selected", "Selected Cells");
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();

                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Str_Fcode = dgrdCateg.Rows[int.Parse(dgrdCateg.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Str_FStr = dgrdCateg.Rows[int.Parse(dgrdCateg.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                    }
                    txtId.Text = Str_Fcode;
                    txtEdit.Text = Str_Fcode;
                    txtCateg.Text = Str_FStr;

                }
            }
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (txtCateg.Text == "")
            {
                MessageBox.Show("کد طبقه بندی وارد نشده است");
                return;
            }
            SqlConnection con = new SqlConnection();
            con.ConnectionString = Properties.Settings.Default.DBWare_mdbConnectionString.ToString();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "insert into Categ (Categ)values('" + txtCateg.Text + "')";
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            dbConnection();
            txtCateg.Clear();
            dbConnection();
            txtCateg.Clear(); 
            dbConnection();
            btnAdd.BringToFront();
            Btn_Save.SendToBack();
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (txtEdit.Text == "")
            {
                MessageBox.Show("رکوردی برای ویرایش انتخاب نشده است");
                return;
            }
            if (txtCateg.Text == "")
            {
                MessageBox.Show("لطفا عنوان طبقه بندی را وارد نمایید");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                Lsc_AnbarDataContext db = new Lsc_AnbarDataContext();
                Categ EdtCateg = db.Categs.Single(n => n.CatID == int.Parse(txtEdit.Text.Trim()));
                EdtCateg.Categ1 = txtCateg.Text.Trim();
                db.SubmitChanges();
                dbConnection();
                txtCateg.Clear();
                txtEdit.Clear();
                txtId.Clear();
            }
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (txtEdit.Text == "")
            {
                MessageBox.Show("رکوردی برای حذف انتخاب نشده است");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                try
                {
                    Lsc_AnbarDataContext db = new Lsc_AnbarDataContext();
                    Categ EdtCateg = db.Categs.Single(n => n.CatID == int.Parse(txtEdit.Text.Trim()));
                    db.Categs.DeleteOnSubmit(EdtCateg);

                    db.SubmitChanges();
                    dbConnection();
                    txtCateg.Clear();
                    txtEdit.Clear();
                    txtId.Clear();
                }
                catch
                {
                    MessageBox.Show("رکوردی برای حذف انتخاب نشده است");
                }
            }
        }
    }
}