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

namespace WareHouse.frmTaghiiratsystem
{
    public partial class frmAnbarejadid : 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 WarHouseID, WareHouseName FROM WareHouse order by WarHouseID DESC";
                con.Open();
                da.Fill(dtb);
                dtgWarename.DataSource = dtb;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

        public frmAnbarejadid()
        {
            InitializeComponent();
        }

        private void frmAnbarejadid_Load(object sender, EventArgs e)
        {
            dbConnection();
            clasFolder.clasFarsi farMe1 = new clasFolder.clasFarsi();
            farMe1.farFunction();  
        }


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

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (txtWarname.Text != "")
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = Properties.Settings.Default.DBWare_mdbConnectionString.ToString();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = "insert into WareHouse (WareHouseName)values('" + txtWarname.Text.Trim() + "')";
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                dbConnection();
                txtWarname.Clear();
                dbConnection();
                txtWarname.Clear();

                dbConnection();

                btnAdd.BringToFront();
                Btn_Save.SendToBack();
            }
            else
            {
                MessageBox.Show("لطفا اطلاعات را وارد نمایید");
            }
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (Txt_WarCode.Text == "")
            {
                MessageBox.Show("واحدی برای ویرایش انتخاب نشده است");
                return;
            }
            if (txtWarname.Text == "")
            {
                MessageBox.Show("لطفا عنوان واحد را وارد نمایید");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                Lsc_AnbarDataContext db = new Lsc_AnbarDataContext();
                WareHouse101 EdtUnit = db.WareHouse101s.Single(n => n.WarHouseID == int.Parse(Txt_WarCode.Text.Trim()));
                EdtUnit.WareHouseName = txtWarname.Text.Trim();
                db.SubmitChanges();
                dbConnection();
                Txt_WarCode.Clear();
                txtWarname.Clear();
            }
        }

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

            if (selectedCellCount > 0)
            {
                if (dtgWarename.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 = dtgWarename.Rows[int.Parse(dtgWarename.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Str_FStr = dtgWarename.Rows[int.Parse(dtgWarename.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                    }
                    Txt_WarCode.Text = Str_Fcode;
                    txtWarname.Text = Str_FStr;

                }
            }
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (Txt_WarCode.Text == "")
            {
                MessageBox.Show("رکوردی برای حذف انتخاب نشده است");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                Lsc_AnbarDataContext db = new Lsc_AnbarDataContext();
                try
                {
                    WareHouse101 DelUnit = db.WareHouse101s.Single(n => n.WarHouseID == int.Parse(Txt_WarCode.Text.Trim()));
                    db.WareHouse101s.DeleteOnSubmit(DelUnit);
                    db.SubmitChanges();
                    dbConnection();
                    Txt_WarCode.Clear();
                    txtWarname.Clear();
                    MessageBox.Show("رکورد مورد نظر حذف گردید");
                }
                catch
                {
                    MessageBox.Show("واحدی برای حذف انتخاب نشده است");
                    return;
                }

            }
        }
    }
}