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

namespace WareHouse.frmParvandeh
{
    public partial class Asset101 : Form
    {
        //int int_AllowAdd = 0;

        public Asset101()
        {
            InitializeComponent();
        }

        protected void dbInTotal()
        {
            string sql = @"SELECT     TOP (100) PERCENT Asset.AssetID, Asset.AssetName, AssetUnits.AssetUnit, WareHouse.WareHouseName, Categ.Categ, 
                      Asset.OrderPoint FROM         Asset INNER JOIN    AssetUnits ON Asset.AssetUnit = AssetUnits.AssetUnitID INNER JOIN
                      WareHouse ON Asset.MakanID = WareHouse.WarHouseID INNER JOIN   Categ ON Asset.Categu = Categ.CatID ORDER BY Asset.AssetID DESC";
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            dbconnClass1 mdCon = new dbconnClass1();
            mdCon.dbCon1(sql);
            dataGridView1.DataSource = mdCon.dt;

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            sql = "SELECT CatID, Categ FROM Categ";
            dbconnClass1 cmBo101 = new dbconnClass1();
            cmBo101.dbCon1(sql);
            cmbCateg101.DataSource = cmBo101.dt;
            cmbCateg101.ValueMember = "CatID";
            cmbCateg101.DisplayMember = "Categ";

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            sql = "SELECT WarHouseID, WareHouseName FROM WareHouse";
            dbconnClass1 cmBo102 = new dbconnClass1();
            cmBo102.dbCon1(sql);
            cmbPlace1.DataSource = cmBo102.dt;
            cmbPlace1.ValueMember = "WarHouseID";
            cmbPlace1.DisplayMember = "WareHouseName";
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            sql = "SELECT AssetUnitID, AssetUnit FROM AssetUnits";
            dbconnClass1 cmBo103 = new dbconnClass1();
            cmBo103.dbCon1(sql);
            cmbAssetunit.DataSource = cmBo103.dt;
            cmbAssetunit.ValueMember = "AssetUnitID";
            cmbAssetunit.DisplayMember = "AssetUnit";
        }

        private void Asset101_Load(object sender, EventArgs e)
        {
            dbInTotal();
        }

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

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                DialogResult dr = MessageBox.Show("آیا از حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    FirstClass db = new FirstClass();
                    db.cmd.Parameters.Add("@AssetID", SqlDbType.Int).Value = int.Parse(textBox1.Text.Trim());

                    db.exeCommand("sp_Asset_DeleteRow");
                    dbInTotal();
                    txtAssetname.Clear();
                    orderPointTextBox.Clear();
                    textBox1.Clear();
                    txtAssetname.Clear();
                    orderPointTextBox.Clear();
                    textBox1.Clear();
                    MessageBox.Show("جنس مورد نظر حذف شد");
                }
            }
            else
            {
                MessageBox.Show("کد اقلام وارد نشده");
            }
        }

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

            if (selectedCellCount > 0)
            {
                if (dataGridView1.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 = dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                    }
                    UDF_GetGoodsInfo(int.Parse(Str_Fcode.Trim()));
                }
            }
        }

        private void UDF_GetGoodsInfo(int int_AssetID)
        {
            Lsc_AnbarDataContext db = new Lsc_AnbarDataContext();
            Asset tblAsset = db.Assets.Single(n => n.AssetID == int_AssetID);

            txtAssetname.Text = tblAsset.AssetName;
            cmbAssetunit.SelectedValue = tblAsset.AssetUnit;
            cmbPlace1.SelectedValue = tblAsset.MakanID;
            cmbCateg101.SelectedValue = tblAsset.Categu;
            orderPointTextBox.Text = tblAsset.OrderPoint.ToString();
            textBox1.Text = tblAsset.AssetID.ToString();
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("لطفا کد کالا را وارد نمایید");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                try
                {
                    Lsc_AnbarDataContext db = new Lsc_AnbarDataContext();
                    Asset tblAssetUpdate = db.Assets.Single(n => n.AssetID == int.Parse(textBox1.Text.Trim()));

                    tblAssetUpdate.AssetName = txtAssetname.Text.Trim();
                    tblAssetUpdate.AssetUnit = int.Parse(cmbAssetunit.SelectedValue.ToString());
                    tblAssetUpdate.MakanID = int.Parse(cmbPlace1.SelectedValue.ToString());
                    tblAssetUpdate.Categu = int.Parse(cmbCateg101.SelectedValue.ToString());
                    tblAssetUpdate.OrderPoint = int.Parse(orderPointTextBox.Text.Trim());
                    db.SubmitChanges();
                    dbInTotal();
                    txtAssetname.Clear();
                    orderPointTextBox.Clear();
                    textBox1.Clear();
                }
                catch
                {
                    MessageBox.Show("عملیات انجام نشد");
                }
            }
        }

        private void orderPointTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (txtAssetname.Text != "" && cmbAssetunit.Text != "" && cmbPlace1.Text != "" && cmbCateg101.Text != "")
            {
                if (orderPointTextBox.Text == "")
                {
                    orderPointTextBox.Text = "0";
                }

                FirstClass db = new FirstClass();

                db.cmd.Parameters.Add("@AssetName", SqlDbType.NVarChar).Value = txtAssetname.Text.Trim();
                db.cmd.Parameters.Add("@AssetUnit", SqlDbType.Int).Value = cmbAssetunit.SelectedValue;

                db.cmd.Parameters.Add("@Categu", SqlDbType.Int).Value = cmbCateg101.SelectedValue;
                db.cmd.Parameters.Add("@MakanID", SqlDbType.Int).Value = cmbPlace1.SelectedValue;
                db.cmd.Parameters.Add("@OrderPoint", SqlDbType.Int).Value = int.Parse(orderPointTextBox.Text.Trim());

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

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("فیلتر جستجو مشخص نشده است.");
                return;
            }
            string sql = @"SELECT Asset.AssetID, Asset.AssetName, AssetUnits.AssetUnit, WareHouse.WareHouseName, Categ.Categ, Asset.OrderPoint
                         FROM Asset INNER JOIN 
                         AssetUnits ON Asset.AssetUnit = AssetUnits.AssetUnitID INNER JOIN
                         WareHouse ON Asset.MakanID = WareHouse.WarHouseID INNER JOIN
                         Categ ON Asset.Categu = Categ.CatID WHERE     (Asset.AssetID = '" + int.Parse(textBox1.Text.Trim()) + "')";
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            dbconnClass1 mdCon = new dbconnClass1();
            mdCon.dbCon1(sql);
            dataGridView1.DataSource = mdCon.dt;
            if (mdCon.dt.Rows.Count <= 0)
            {
                textBox1.Text = "";
                Btn_Refresh.BringToFront();
                Btn_Search.SendToBack();
                MessageBox.Show("رکوردی یافته نشد");
            }
        }

        private void Btn_Refresh_Click(object sender, EventArgs e)
        {
            dbInTotal();
            Btn_Refresh.SendToBack();
            Btn_Search.BringToFront();
        }

    }
}