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

        private void UDF_ClearTextBox()
        {
            Txt_CodeRoom.Clear();
            Txt_Creditor.Clear();
            Txt_Debtor.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.tblInhebitantEndBooks
                        orderby Row.InhebitantEndBookCode descending
                        select new
                        {
                            Row.InhebitantEndBookCode,
                            Row.RoomNumber,
                            Row.CheckCode,
                            Row.Debtor,
                            Row.Creditor
                        };

            Grd_EndBook.DataSource = Query;
        }


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

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

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

            inhab.RoomNumber = Txt_CodeRoom.Text.Trim();
            inhab.CheckCode = Decimal.Parse(Cmb_CodeCheck.SelectedValue.ToString());
            inhab.Debtor = Decimal.Parse(Txt_Debtor.Text.Trim());
            inhab.Creditor = Decimal.Parse(Txt_Creditor.Text.Trim());

            db.tblInhebitantEndBooks.InsertOnSubmit(inhab);
            db.SubmitChanges();

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

        }

        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.tblInhebitantEndBooks
                        where Row.RoomNumber == Txt_Search.Text.Trim()
                        select new
                        {
                            Row.InhebitantEndBookCode,
                            Row.RoomNumber,
                            Row.CheckCode,
                            Row.Debtor,
                            Row.Creditor
                        };
            Grd_EndBook.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();
        }

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

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

                }
            }
        }
        private void UDF_SelectCmbValue(Decimal Dec_EndBookCode)
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            tblInhebitantEndBook inhabitan = db.tblInhebitantEndBooks.Single(n => n.InhebitantEndBookCode == Dec_EndBookCode);
            Cmb_CodeCheck.SelectedValue = inhabitan.CheckCode;
        }

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

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

                db.tblInhebitantEndBooks.DeleteOnSubmit(inhabitan);
                db.SubmitChanges();

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

            }
        }

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

            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش رکورد اطمینان دارید", "اخطار ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
                tblInhebitantEndBook inhabitan = db.tblInhebitantEndBooks.Single(n => n.InhebitantEndBookCode == Decimal.Parse(Txt_CodeEndBook.Text.Trim()));

                inhabitan.RoomNumber=Txt_CodeRoom.Text.Trim();
                inhabitan.CheckCode = Decimal.Parse(Cmb_CodeCheck.SelectedValue.ToString());
                inhabitan.Debtor = Decimal.Parse(Txt_Debtor.Text.Trim());
                inhabitan.Creditor = Decimal.Parse(Txt_Creditor.Text.Trim());
     
                db.SubmitChanges();
                UDF_GrdFill();
                UDF_CmbFill();
                UDF_ClearTextBox();

            }
        }
    }

}