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
{
    public partial class frmTahvileKala : Form
    {
        public frmTahvileKala()
        {
            InitializeComponent();
        }

        private void CmbSefareshat()
        {
            FirstClass db;
            DataTable dt;

            db = new FirstClass();
            dt = new DataTable();

            dt = db.dbOut("SELECT     TOP (100) PERCENT SefarshID1, CONVERT(nvarchar(50), SefarshID1) AS SefarshID1Sh FROM    Purchased WHERE     (TahvilID = 0) ORDER BY SefarshID1");
            dt.Rows.Add(new Object[] { 0, "لطفا انتخاب نمائید . . ." });

            Cmb_Sefareshat.DataSource = dt;
            Cmb_Sefareshat.DisplayMember = "SefarshID1Sh";
            Cmb_Sefareshat.ValueMember = "SefarshID1";
            Cmb_Sefareshat.SelectedValue = 0;

            db = new FirstClass();
            dt = new DataTable();

            dt = db.dbOut(@"SELECT     TOP (100) PERCENT TahvilShodegan.TahvilID AS [کد تحویل], TahvilShodegan.SefareshID AS [کد سفارش], Asset.AssetName AS [نام کالا], 
                      AssetUnits.AssetUnit AS [واحد کالا], Sefareshat.Tedad AS [مقدار درخواستی], Categ.Categ AS [طبقه بندی کالا], 
                      TahvilShodegan.TahvilUnit AS [کد واحد درخواست کننده], Units.UnitName AS [نام واحد درخواست کننده], Sefareshat.DateShOfSefaresh1 AS [تاریخ سفارش], 
                      Purchased.PurchShDate AS [تاریخ خرید], TahvilShodegan.TahvilDate AS [تاریخ تحویل], WareHouse.WareHouseName AS [محل نگهداری], 
                      TahvilShodegan.Notes AS ملاحظات  FROM         TahvilShodegan INNER JOIN  Sefareshat ON TahvilShodegan.SefareshID = Sefareshat.SefareshID INNER JOIN
                      Asset ON Sefareshat.AssetID = Asset.AssetID INNER JOIN  AssetUnits ON Asset.AssetUnit = AssetUnits.AssetUnitID INNER JOIN
                      Units ON TahvilShodegan.TahvilUnit = Units.UnitID INNER JOIN  Purchased ON Sefareshat.SefareshID = Purchased.SefarshID1 INNER JOIN
                      WareHouse ON Purchased.WareHouse = WareHouse.WarHouseID INNER JOIN   Categ ON Asset.Categu = Categ.CatID ORDER BY TahvilShodegan.TahvilID DESC");

            dataGridView1.DataSource = dt;
        }

        private void frmTahvileKala_Load(object sender, EventArgs e)
        {
            CmbSefareshat();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                GetSefaresInfo(int.Parse(Cmb_Sefareshat.SelectedValue.ToString()));
            }
            catch
            {

                Cmb_Sefareshat.Enabled = true;
                Btn_GetSefareshDetals.Enabled = true;
                groupBox1.Enabled = false;
                return;
            }
            Cmb_Sefareshat.Enabled = false;
            Btn_GetSefareshDetals.Enabled = false;
            groupBox1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Lsc_AnbarDataContext db = new Lsc_AnbarDataContext();
            TahvilShodegan tblTahvil = new TahvilShodegan();
            
            tblTahvil.SefareshID = int.Parse(Cmb_Sefareshat.SelectedValue.ToString());
            tblTahvil.TahvilUnit = int.Parse(Txt_UnitID.Text.Trim());
            tblTahvil.TahvilDate = tahvilDateTextBox.Text.Trim();
            tblTahvil.Notes = notesTextBox.Text.Trim();

            db.TahvilShodegans.InsertOnSubmit(tblTahvil);
            db.SubmitChanges();


            PurchasedTableAdapter pur = new PurchasedTableAdapter();
            pur.UpdateQuery(true, int.Parse(Cmb_Sefareshat.SelectedValue.ToString()));
            Btn_Insert.Enabled = false;
            Btn_GetSefareshDetals.Enabled = true;
            CmbSefareshat();

            Cmb_Sefareshat.Enabled = true;
            Btn_GetSefareshDetals.Enabled = true;
            groupBox1.Enabled = false;
        }

        private void GetSefaresInfo(int int_SefareshID)
        {
            FirstClass db = new FirstClass();
            DataTable dt = new DataTable();
            dt = db.dbOut(@"SELECT     TOP (100) PERCENT Purchased.PurAssetID AS [کد کالا0], Sefareshat.DateShOfSefaresh1 AS [تاریخ سفارش1], Purchased.SefarshID1 AS [کد سفارش2], 
                      Purchased.PurchShDate AS [تاریخ خرید3], Sefareshat.Tedad AS [تعداد درخواست4], Categ.Categ AS [طبقه بندی 5], Asset.AssetName AS [عنوان کالا 6], 
                      Units.UnitName AS [نام واحد درخواست کننده7], AssetUnits.AssetUnit AS [واحد شمارش8], Purchased.WareHouse AS [کد انبار9], 
                      WareHouse.WareHouseName AS [نام انبار10], Purchased.TahvilID AS [وضعیت تحویل11], Units.UnitID AS [کد واحد درخواست کننده12] FROM         Purchased INNER JOIN
                      Sefareshat ON Purchased.SefarshID1 = Sefareshat.SefareshID INNER JOIN  WareHouse ON Purchased.WareHouse = WareHouse.WarHouseID INNER JOIN
                      Units ON Sefareshat.UnitID = Units.UnitID INNER JOIN  Asset ON Sefareshat.AssetID = Asset.AssetID INNER JOIN
                      Categ ON Asset.Categu = Categ.CatID INNER JOIN  AssetUnits ON Asset.AssetUnit = AssetUnits.AssetUnitID WHERE     (Purchased.SefarshID1 = '" + int_SefareshID + "')");

            Txt_UnitID.Text = dt.Rows[0][12].ToString();
            Txt_UnitName.Text = dt.Rows[0][7].ToString();
            MTxt_DateKharid.Text = dt.Rows[0][3].ToString();
            Txt_AghlamCode.Text = dt.Rows[0][0].ToString();
            Txt_AglamName.Text = dt.Rows[0][6].ToString();
            Txt_VahedeShomaresh.Text = dt.Rows[0][8].ToString();
            Txt_MizaneDarkhast.Text = dt.Rows[0][4].ToString();
            tahvilDateTextBox.Text = db.dtShamsi();

        }

        private void Btn_Cancel_Click(object sender, EventArgs e)
        {
            Cmb_Sefareshat.Enabled = true;
            Btn_GetSefareshDetals.Enabled = true;
            groupBox1.Enabled = false;
        }
    }
}
