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

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

        private void UDF_ClearTextBox()
        {
            Txt_Creditor.Clear();
            MskTxt_DateSabt.Clear();
            Txt_Debtor.Clear();
            Txt_Note.Clear();
        }

        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,
                             Row.CheckNumber,
                             Row.BankName,
                             Row.BankBranck,
                             Row.DateOfCheck,
                             Row.CostOfCheck,
                             Row.PaymentTo,
                             Row.AccountNumber,
                             Row.SignerName,
                             Row.ExplNotes
                         };

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

            Cmb_CodeCheck.DataSource = Query1;

        }

        private void UDF_GrdFill()
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.tblTotalDayBooks
                        orderby Row.RooznamehCode descending
                        select new
                        {
                            Row.RooznamehCode,
                            Row.DateOfSabt,
                            Row.ExplNotes,
                            Row.CheckCOde,
                            Row.Debtor,
                            Row.Creditor
                        };

            Grd_DayBook.DataSource = Query;
        }

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

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_Creditor.Text == "" || MskTxt_DateSabt.Text == "" || Txt_Debtor.Text == "" || Txt_Note.Text == "")
            {
                MessageBox.Show("ورود تاریخ ثبت دفتر ، شرح ، بدهکار وبستانکار الزامی می باشد");
                return;
            }
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            tblTotalDayBook dayBook = new tblTotalDayBook();

            dayBook.DateOfSabt = MskTxt_DateSabt.Text.Trim();
            dayBook.ExplNotes = Txt_Note.Text.Trim();
            dayBook.CheckCOde = Decimal.Parse(Cmb_CodeCheck.SelectedValue.ToString());
            dayBook.Debtor = Decimal.Parse(Txt_Debtor.Text.Trim());
            dayBook.Creditor = Decimal.Parse(Txt_Creditor.Text.Trim());

            db.tblTotalDayBooks.InsertOnSubmit(dayBook);
            db.SubmitChanges();

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

        }

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


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

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

        private void UDF_SelectCmbValue(Decimal Dec_CodeBook)
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            tblTotalDayBook day = db.tblTotalDayBooks.Single(n => n.RooznamehCode == Dec_CodeBook);
            Cmb_CodeCheck.SelectedValue = day.CheckCOde;
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (MskTxt_DateSabt.Text == "")
            {
                MessageBox.Show("انتخاب دفتر روزنامه عمومی مورد نظر الزامی است.");
              
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
                tblTotalDayBook dayBook = db.tblTotalDayBooks.Single(n => n.RooznamehCode == Decimal.Parse(Txt_CodeBook.Text.Trim()));

                db.tblTotalDayBooks.DeleteOnSubmit(dayBook);
                db.SubmitChanges();

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

            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (MskTxt_SearchDate.Text == "")
            {
                MessageBox.Show("تاریخ ثبت دفتر را وارد کنید");
                return;
            }
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.tblTotalDayBooks 
                        where SqlMethods.Like(Row.DateOfSabt , "%" + MskTxt_SearchDate.Text  + "%")
                        select new
                        {
                            Row.RooznamehCode ,
                            Row.DateOfSabt,
                            Row.ExplNotes,
                            Row.CheckCOde,
                            Row.Debtor,
                            Row.Creditor 
                        };
            Grd_DayBook.DataSource = Query;

            if (Query.Count() <= 0)
            {
                MskTxt_SearchDate.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();
        }

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

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
                tblTotalDayBook dayBk = db.tblTotalDayBooks.Single(n => n.RooznamehCode == Decimal.Parse(Txt_CodeBook.Text.Trim()));
                dayBk.DateOfSabt = MskTxt_DateSabt.Text;
                dayBk.ExplNotes = Txt_Note.Text.Trim();
                dayBk.CheckCOde =Decimal.Parse( Cmb_CodeCheck.SelectedValue.ToString());
                dayBk.Debtor = Decimal.Parse(Txt_Debtor.Text.Trim());
                dayBk.Creditor = Decimal.Parse(Txt_Creditor.Text.Trim());
                db.SubmitChanges();

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