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

namespace HesabdariSubSys
{
    public partial class frmMerchanDiseBuyBook : Form
    {
        public frmMerchanDiseBuyBook()
        {
            InitializeComponent();
        }

        private void UDF_GrdFill()
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.tblMerchanDiseBuyBooks
                        orderby Row.MerchanDiseBuyBookCode descending
                        select new
                        {
                            Row.MerchanDiseBuyBookCode,
                            Row.BuyFactorCode,
                            Row.CheckCode,
                            Row.Debtor,
                            Row.Creditor,
                            Row.ExplNotes

                        };
            Grd_BuyBook.DataSource = Query;
        }

        private void UDF_CmbFill()
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query1 = from Row in db.tblChecks
                         orderby Row.CheckCode descending
                         select new
                         {
                             Row.CheckCode,
                             Row.SerialNo
                         };

            Cmb_CodeCheck.DisplayMember = "SerialNo";
            Cmb_CodeCheck.ValueMember = "CheckCode";

            Cmb_CodeCheck.DataSource = Query1;

        }

        private void UDF_ClearTextBox()
        {
            Txt_BuyFactorCode.Clear();
            Txt_Creditor.Clear();
            Txt_Debtor.Clear();
            Txt_Note.Clear();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            Txt_BuyFactorCode.Text = String.Empty;
            Txt_Creditor.Text = String.Empty;
            Txt_Debtor.Text = String.Empty;
            Txt_Note.Text = String.Empty;

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

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_BuyFactorCode.Text == "" || Txt_Creditor.Text == "" || Txt_Debtor.Text == "" || Txt_Note.Text == "")
            {
                MessageBox.Show("ورود موارد الزامی می باشد");
                return;
            }

            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            tblMerchanDiseBuyBook BuyBook = new tblMerchanDiseBuyBook();

            BuyBook.BuyFactorCode = Txt_BuyFactorCode.Text.Trim();
            BuyBook.CheckCode = Decimal.Parse(Cmb_CodeCheck.SelectedValue.ToString());
            BuyBook.Debtor = Decimal.Parse(Txt_Debtor.Text.Trim());
            BuyBook.Creditor = Decimal.Parse(Txt_Creditor.Text.Trim());
            BuyBook.ExplNotes = Txt_Note.Text.Trim();

            db.tblMerchanDiseBuyBooks.InsertOnSubmit(BuyBook);
            db.SubmitChanges();

            UDF_GrdFill();
            UDF_CmbFill();
            btnAdd.BringToFront();
            Btn_Save.SendToBack();
            UDF_ClearTextBox();
            MessageBox.Show("رکورد جدید درج گردید.");
        }

        private void frmMerchanDiseBuyBook_Load(object sender, EventArgs e)
        {
            UDF_CmbFill();
            UDF_GrdFill();
        }

        private void Grd_BuyBook_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = Grd_BuyBook.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Grd_BuyBook.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++)
                    {
                        Txt_MerchanDiseBuyBookCode.Text = Grd_BuyBook.Rows[int.Parse(Grd_BuyBook.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_BuyFactorCode.Text = Grd_BuyBook.Rows[int.Parse(Grd_BuyBook.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_Debtor.Text = Grd_BuyBook.Rows[int.Parse(Grd_BuyBook.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                        Txt_Creditor.Text = Grd_BuyBook.Rows[int.Parse(Grd_BuyBook.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_Note.Text = Grd_BuyBook.Rows[int.Parse(Grd_BuyBook.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString();

                    }
                    UDF_SelectCmbValue(Decimal.Parse(Txt_MerchanDiseBuyBookCode.Text.Trim()));
                }
            }
        }

        private void UDF_SelectCmbValue(Decimal Dec_CodeBuyBook)
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            tblMerchanDiseBuyBook Buy = db.tblMerchanDiseBuyBooks.Single(n => n.MerchanDiseBuyBookCode == Dec_CodeBuyBook);
            Cmb_CodeCheck.SelectedValue = Buy.CheckCode;
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if ( Txt_BuyFactorCode.Text == "")
            {
                MessageBox.Show("انتخاب فاکتور خرید مورد نظر الزامی است.");

            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
                tblMerchanDiseBuyBook BuyBook = db.tblMerchanDiseBuyBooks.Single(n => n.MerchanDiseBuyBookCode == Decimal.Parse( Txt_MerchanDiseBuyBookCode.Text.Trim()));

                db.tblMerchanDiseBuyBooks.DeleteOnSubmit(BuyBook);
                db.SubmitChanges();

                UDF_ClearTextBox();
                UDF_CmbFill();
                UDF_GrdFill();

            }
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
             if (Txt_MerchanDiseBuyBookCode.Text == "")
            {
                MessageBox.Show("لطفا خرید مورد نظر را انتخاب کنید");
                return;
            }

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
                tblMerchanDiseBuyBook BuyBook = db.tblMerchanDiseBuyBooks.Single(n => n.MerchanDiseBuyBookCode == Decimal.Parse(Txt_MerchanDiseBuyBookCode.Text.Trim()));
                BuyBook.BuyFactorCode = Txt_BuyFactorCode.Text.Trim();
                BuyBook.Debtor = Decimal.Parse(Txt_Debtor.Text.Trim());
                BuyBook.Creditor = Decimal.Parse(Txt_Creditor.Text.Trim());
                BuyBook.CheckCode = Decimal.Parse(Cmb_CodeCheck.SelectedValue.ToString());
                BuyBook.ExplNotes = Txt_Note.Text.Trim();
                db.SubmitChanges();

                UDF_GrdFill();
                UDF_CmbFill();
                UDF_ClearTextBox();
                
            }
        
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Txt_Search.Text == "")
            {
                MessageBox.Show("لطفا کد فاکتور خرید را وارد کنید");
                return;
            }
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.tblMerchanDiseBuyBooks 
                        where Row.BuyFactorCode == Txt_Search.Text.Trim()
                        select new
                        {
                            Row.MerchanDiseBuyBookCode ,
                            Row.BuyFactorCode ,
                            Row.CheckCode ,
                            Row.Debtor,
                            Row.Creditor,
                            Row.ExplNotes 
                        };
            Grd_BuyBook.DataSource = Query;

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

        private void Btn_Refresh_Click(object sender, EventArgs e)
        {

            UDF_GrdFill();
            UDF_CmbFill();
            Btn_Refresh.SendToBack();
            Btn_Search.BringToFront();
        }
    }
}