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

        private void UDF_ClearTextBox()
        {
            MskTxt_Date.Clear();
            Txt_CodeEmploye.Clear();
            Txt_Creditor.Clear();
            Txt_Debtor.Clear();
        }

        private void UDF_GrdFill()
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.tblSalaries 
                        orderby Row.SalaryCode descending
                        select new
                        {
                            Row.SalaryCode ,
                            Row.PayDate ,
                            Row.EmployeeCode,
                            Row.Deptor ,
                            Row.Creditor

                        };
            Grd_SalaryBook.DataSource = Query;
        }

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

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

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

            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            tblSalary sale=new tblSalary();

            sale.PayDate=MskTxt_Date.Text;
            sale.EmployeeCode=Txt_CodeEmploye.Text.Trim();
            sale.Deptor=Decimal.Parse(Txt_Debtor.Text.Trim());
            sale.Creditor=Decimal.Parse(Txt_Creditor.Text.Trim());
            db.tblSalaries.InsertOnSubmit(sale);

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

        private void Grd_SalaryBook_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
            }


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

                db.tblSalaries.DeleteOnSubmit(sale101);
                db.SubmitChanges();

                UDF_ClearTextBox();
                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();
                tblSalary sale = db.tblSalaries.Single(n => n.SalaryCode == Decimal.Parse(Txt_Code.Text.Trim()));
                sale.PayDate = MskTxt_Date.Text;
                sale.EmployeeCode = Txt_CodeEmploye.Text.Trim();
                sale.Deptor = Decimal.Parse(Txt_Debtor.Text.Trim());
                sale.Creditor = Decimal.Parse(Txt_Creditor.Text.Trim());

                db.SubmitChanges();

                UDF_GrdFill();
                UDF_ClearTextBox();
            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Txt_Search.Text == "")
            {
                MessageBox.Show("کد کارمند مورد نظر را انتخاب کنید");
            }
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = from Row in db.tblSalaries 
                        where Row.EmployeeCode == Txt_Search.Text.Trim()
                        select new
                        {
                            Row.SalaryCode ,
                            Row.PayDate,
                            Row.EmployeeCode ,
                            Row.Deptor,
                            Row.Creditor 
                        };
            Grd_SalaryBook.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();
        }

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


        
    


