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 WareHouse.dtsAnbarJadidTableAdapters;

namespace WareHouse.frmParvandeh
{
    public partial class SabteSefareshat : Form
    {

        public SabteSefareshat()
        {
            InitializeComponent();
        }

        public void ddDba()
        {
            FirstClass db = new FirstClass();
            DataTable dt = new DataTable();

            dt = db.dbOut(@"SELECT     TOP (100) PERCENT Sefareshat.SefareshID AS [کد سفارش], Sefareshat.UnitID AS [کد واحد سفارش دهنده], Units.UnitName AS [نام واحد سفارش دهنده], 
                            Sefareshat.AssetID AS [کد کالا], Asset.AssetName AS [نام کالا], AssetUnits.AssetUnit AS [واحد شمارش کالا], Sefareshat.Tedad AS تعداد,   Sefareshat.DateShOfSefaresh1 AS [تاریخ سفارش], Sefareshat.Purch AS [وضعیت خرید]
                            FROM         Sefareshat AS Sefareshat INNER JOIN   Units ON Sefareshat.UnitID = Units.UnitID INNER JOIN  Asset ON Sefareshat.AssetID = Asset.AssetID INNER JOIN   AssetUnits ON Asset.AssetUnit = AssetUnits.AssetUnitID ORDER BY Sefareshat.SefareshID DESC");

            dtgSabtesefareshat.DataSource = dt;
            try
            {
                dtgSabtesefareshat.Columns[1].Width = 150;
                dtgSabtesefareshat.Columns[2].Width = 150;
                dtgSabtesefareshat.Columns[4].Width = 150;
                dtgSabtesefareshat.Columns[5].Width = 150;
            }
            catch { }
        }

        private void SabteSefareshat_Load(object sender, EventArgs e)
        {
            grdFill();
        }

        private void grdFill()
        {
            string sql;
            ddDba();

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

            sql = "SELECT UnitID, UnitName FROM Units";
            dbconnClass1 comBo102 = new dbconnClass1();
            comBo102.dbCon1(sql);
            cmbUnitename.DataSource = comBo102.dt;
            cmbUnitename.ValueMember = "UnitID";
            cmbUnitename.DisplayMember = "UnitName";

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

            sql = "SELECT AssetID, AssetName FROM Asset";
            dbconnClass1 comBo101 = new dbconnClass1();
            comBo101.dbCon1(sql);
            cmbAssetname.DataSource = comBo101.dt;
            cmbAssetname.ValueMember = "AssetID";
            cmbAssetname.DisplayMember = "AssetName";


        }




        private void btnHazfeSefareshat_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("ورود كد سفارش الزامي است");
                return;
            }
            SefareshatTableAdapter tbl = new SefareshatTableAdapter();
            try
            {
                tbl.DeleteSefaresh(int.Parse(textBox1.Text.Trim()));
            }
            catch
            {
                MessageBox.Show("كد وارد شده معتبر نمي باشد");
            }
            grdFill();
        }

        private void tedadTextBox_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 dtgSabtesefareshat_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = dtgSabtesefareshat.GetCellCount(DataGridViewElementStates.Selected);
            String Str_Fcode = String.Empty;
            String Str_VahedCode = String.Empty;
            String Str_GoodsCode = String.Empty;
            String Str_Tedad = String.Empty;
            String Str_Date = String.Empty;

            if (selectedCellCount > 0)
            {
                if (dtgSabtesefareshat.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 = dtgSabtesefareshat.Rows[int.Parse(dtgSabtesefareshat.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Str_VahedCode = dtgSabtesefareshat.Rows[int.Parse(dtgSabtesefareshat.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Str_GoodsCode = dtgSabtesefareshat.Rows[int.Parse(dtgSabtesefareshat.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                        Str_Tedad = dtgSabtesefareshat.Rows[int.Parse(dtgSabtesefareshat.SelectedCells[i].RowIndex.ToString())].Cells[6].Value.ToString();
                        Str_Date = dtgSabtesefareshat.Rows[int.Parse(dtgSabtesefareshat.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString();
                    }
                    textBox1.Text = Str_Fcode;
                    cmbUnitename.SelectedValue = Str_VahedCode.Trim();
                    cmbAssetname.SelectedValue = Str_GoodsCode.Trim();
                    tedadTextBox.Text = Str_Tedad.Trim();
                    MTxt_Date.Text = Str_Date;
                }
            }
        }

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

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (cmbAssetname.Text == "" || cmbUnitename.Text == "" || tedadTextBox.Text == "" || MTxt_Date.Text == "")
            {
                MessageBox.Show("ورود مقادير تمامي فيلدها الزامي مي باشد");
                return;
            }
            int i = 0;
            if (purchCheckBox.Checked)
            {
                i = 1;
            }
            else
            {
                i = 0;
            }
            string sql = "insert into Sefareshat (UnitID, AssetID, Tedad,  Purch, DateShOfSefaresh1)values('" + cmbUnitename.SelectedValue + "' , '" + cmbAssetname.SelectedValue + "' , '" + tedadTextBox.Text.Trim() + "'  , '" + i + "' , '" + MTxt_Date.Text.Trim() + "')";
            dbconnClass1 inSer101 = new dbconnClass1();
            try
            {
                inSer101.dbIns1(sql);
            }
            catch
            {
                MessageBox.Show("مقادير وارد شده معتبر نمي باشند");
            }
            ddDba();

            grdFill();
            btnAdd.BringToFront();
            Btn_Save.SendToBack();
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("رکوردی برای ویرایش انتخاب نشده است");
                return;
            }
            if (cmbAssetname.Text == "" || cmbUnitename.Text == "" || tedadTextBox.Text == "" || MTxt_Date.Text == "")
            {
                MessageBox.Show("ورود مقادير تمامي فيلدها الزامي مي باشد");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                try
                {
                    Lsc_AnbarDataContext db = new Lsc_AnbarDataContext();
                    Sefareshat tblSefareshUpdate = db.Sefareshats.Single(n => n.SefareshID == int.Parse(textBox1.Text.Trim()));

                    tblSefareshUpdate.AssetID = int.Parse(cmbAssetname.SelectedValue.ToString());
                    tblSefareshUpdate.UnitID = int.Parse(cmbUnitename.SelectedValue.ToString());
                    tblSefareshUpdate.DateShOfSefaresh1 = MTxt_Date.Text.Trim();
                    tblSefareshUpdate.Tedad = int.Parse(tedadTextBox.Text.Trim());

                    db.SubmitChanges();

                    ddDba();

                    MTxt_Date.Text = "";
                    tedadTextBox.Text = "";
                    textBox1.Text = "";
                }
                catch { }

            }
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                DialogResult dr = MessageBox.Show("آیا از حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    Lsc_AnbarDataContext db = new Lsc_AnbarDataContext();
                    Sefareshat tblSefareshUpdate = db.Sefareshats.Single(n => n.SefareshID == int.Parse(textBox1.Text.Trim()));
                    db.Sefareshats.DeleteOnSubmit(tblSefareshUpdate);
                    db.SubmitChanges();
                    ddDba();

                    MTxt_Date.Text = "";
                    tedadTextBox.Text = "";
                    textBox1.Text = "";
                }
            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            FirstClass db = new FirstClass();
            DataTable dt = new DataTable();

            try
            {
                dt = db.dbOut(@"SELECT     TOP (100) PERCENT Sefareshat.SefareshID AS [کد سفارش], Sefareshat.UnitID AS [کد واحد سفارش دهنده], Units.UnitName AS [نام واحد سفارش دهنده], 
                            Sefareshat.AssetID AS [کد کالا], Asset.AssetName AS [نام کالا], AssetUnits.AssetUnit AS [واحد شمارش کالا], Sefareshat.Tedad AS تعداد,   Sefareshat.DateShOfSefaresh1 AS [تاریخ سفارش], Sefareshat.Purch AS [وضعیت خرید]
                            FROM  Sefareshat AS Sefareshat INNER JOIN   Units ON Sefareshat.UnitID = Units.UnitID INNER JOIN  Asset ON Sefareshat.AssetID = Asset.AssetID INNER JOIN   AssetUnits ON Asset.AssetUnit = AssetUnits.AssetUnitID WHERE     (Sefareshat.SefareshID = '" + int.Parse(textBox1.Text.Trim()) + "') ORDER BY Sefareshat.SefareshID DESC");
            }
            catch
            {
                textBox1.Text = "";
                Btn_Refresh.BringToFront();
                Btn_Search.SendToBack();
                dtgSabtesefareshat.DataSource = dt;
                MessageBox.Show("رکوردی یافته نشد");
                return;
            }
            dtgSabtesefareshat.DataSource = dt;
            try
            {
                dtgSabtesefareshat.Columns[1].Width = 150;
                dtgSabtesefareshat.Columns[2].Width = 150;
                dtgSabtesefareshat.Columns[4].Width = 150;
                dtgSabtesefareshat.Columns[5].Width = 150;
            }
            catch { }

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

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