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

        private void UDF_GrdFill()
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.tblPensionBooks 
                        orderby Row.PeinsionBookCode  descending
                        select new
                        {
                            Row.PeinsionBookCode ,
                            Row.RoomNumber ,
                            Row.CustomerCode ,
                            Row.RequestServideDescription 

                        };
            Grd_PensionBook.DataSource = Query;
        }

        private void UDF_ClearTextBox()
        {
            Txt_CodeCustomer.Clear();
            Txt_CodeRequest.Clear();
            Txt_RoomNumber.Clear();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            Txt_CodeCustomer.Text = String.Empty;
            Txt_CodeRequest.Text = String.Empty;
            Txt_RoomNumber.Text = String.Empty;

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

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_CodeCustomer.Text == "" || Txt_CodeRequest.Text == "" || Txt_RoomNumber.Text == "" )
            {
                MessageBox.Show("ورود شماره اتاق ، کد مشتری و خدمات درخواست شده الزامی می باشد");
                return;
            }

            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            tblPensionBook pansion = new tblPensionBook();
            pansion.CustomerCode = Txt_CodeCustomer.Text.Trim();
            pansion.RoomNumber = Txt_RoomNumber.Text.Trim();
            pansion.RequestServideDescription = Txt_CodeRequest.Text.Trim();

            db.tblPensionBooks.InsertOnSubmit(pansion);
            db.SubmitChanges();

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

        private void Grd_PensionBook_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = Grd_PensionBook.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Grd_PensionBook.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_Code.Text = Grd_PensionBook.Rows[int.Parse(Grd_PensionBook.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_RoomNumber.Text = Grd_PensionBook.Rows[int.Parse(Grd_PensionBook.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_CodeCustomer.Text = Grd_PensionBook.Rows[int.Parse(Grd_PensionBook.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_CodeRequest.Text = Grd_PensionBook.Rows[int.Parse(Grd_PensionBook.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                    }
                }
            }
        }

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

                db.tblPensionBooks.DeleteOnSubmit(pansion);
                db.SubmitChanges();

                UDF_ClearTextBox(); 
                UDF_GrdFill();

            }
        }

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

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

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

                pansion.CustomerCode = Txt_CodeCustomer.Text.Trim();
                pansion.RoomNumber = Txt_RoomNumber.Text.Trim();
                pansion.RequestServideDescription = Txt_CodeRequest.Text.Trim();
                db.SubmitChanges();

                UDF_GrdFill();
                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.tblPensionBooks 
                        where Row.CustomerCode == Txt_Search.Text.Trim()
                        select new
                        {
                            Row.PeinsionBookCode ,
                            Row.RoomNumber ,
                            Row.CustomerCode ,
                            Row.RequestServideDescription 
                        };
            Grd_PensionBook.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();
            Btn_Refresh.SendToBack();
            Btn_Search.BringToFront();
        }
    }
}
