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

        private void UDF_GrdFill()
        {
            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            var Query = 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 

                        };
            Grd_Check.DataSource = Query;
        }

        private void UDF_ClearTextBox()
        {
            Txt_AccountNumber.Clear();
            Txt_BankName.Clear();
            Txt_CheckNumber.Clear();
            Txt_CodeCheck.Clear();
            Txt_Cost.Clear();
            Txt_Date.Clear();
            Txt_Note.Clear();
            Txt_PaymentTo.Clear();
            Txt_Serial.Clear();
            Txt_Shobe.Clear();
            Txt_SignerName.Clear();
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            Txt_AccountNumber.Text = String.Empty;
            Txt_BankName.Text = String.Empty;
            Txt_CheckNumber.Text = String.Empty;
            Txt_CodeCheck.Text = String.Empty;
            Txt_Cost.Text = String.Empty;
            Txt_Date.Text = String.Empty;
            Txt_Note.Text = String.Empty;
            Txt_PaymentTo.Text = String.Empty;
            Txt_Serial.Text = String.Empty;
            Txt_Shobe.Text = String.Empty;
            Txt_SignerName.Text = String.Empty;

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

        //private void Btn_SaveCheck_Click(object sender, EventArgs e)
        //{
        //    //if (Txt_AccountNumber.Text == "" || Txt_BankName.Text == "" || Txt_CheckNumber.Text == "" || Txt_CodeCheck.Text == "" || Txt_Cost.Text == "" || Txt_Date.Text  == "" || Txt_Note.Text == "" || Txt_PaymentTo.Text == "" || Txt_Serial.Text == "" || Txt_Shobe.Text == "" || Txt_SignerName.Text == "")
        //    //{
        //    //    MessageBox.Show("ورود موارد الزامی می باشد");
        //    //    return;
        //    //}

        //    LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
        //    tblCheck mChec101 = new tblCheck();

        //    MessageBox.Show("ورود موارد الزامی می باشد");
        //    mChec101.SerialNo = Txt_Serial.Text.Trim();
        //    mChec101.CheckNumber = Txt_CheckNumber.Text.Trim();
        //    mChec101.BankName = Txt_BankName.Text.Trim();
        //    mChec101.BankBranck = Txt_Shobe.Text.Trim();
        //    mChec101.DateOfCheck = Txt_Date.Text.Trim();
        //    mChec101.CostOfCheck = Decimal.Parse(Txt_Cost.Text.Trim());
        //    mChec101.PaymentTo = Txt_PaymentTo.Text.Trim();
        //    mChec101.AccountNumber = Txt_AccountNumber.Text.Trim();
        //    mChec101.SignerName = Txt_SignerName.Text.Trim();
        //    mChec101.ExplNotes = Txt_Note.Text.Trim();
        //    MessageBox.Show("ورود موارد الزامی می باشد");

        //    db.tblChecks.InsertOnSubmit(mChec101);
        //    db.SubmitChanges();

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

        //}

        private void Grd_Check_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = Grd_Check. GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Grd_Check.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_CodeCheck.Text =  Grd_Check.Rows[int.Parse( Grd_Check.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_Serial.Text = Grd_Check.Rows[int.Parse(Grd_Check.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_CheckNumber.Text = Grd_Check.Rows[int.Parse(Grd_Check.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_BankName.Text = Grd_Check.Rows[int.Parse(Grd_Check.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                        Txt_Shobe.Text = Grd_Check.Rows[int.Parse(Grd_Check.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_Date.Text = Grd_Check.Rows[int.Parse(Grd_Check.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString();
                        Txt_Cost.Text = Grd_Check.Rows[int.Parse(Grd_Check.SelectedCells[i].RowIndex.ToString())].Cells[6].Value.ToString();
                        Txt_PaymentTo.Text = Grd_Check.Rows[int.Parse(Grd_Check.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString();
                        Txt_AccountNumber.Text = Grd_Check.Rows[int.Parse(Grd_Check.SelectedCells[i].RowIndex.ToString())].Cells[8].Value.ToString();
                        Txt_SignerName.Text = Grd_Check.Rows[int.Parse(Grd_Check.SelectedCells[i].RowIndex.ToString())].Cells[9].Value.ToString();
                        Txt_Note.Text = Grd_Check.Rows[int.Parse(Grd_Check.SelectedCells[i].RowIndex.ToString())].Cells[10].Value.ToString();
                    }
                }
            }
        }

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

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (Txt_Serial.Text == "")
            {
                MessageBox.Show("انتخاب شماره سریال چک الزامی است.");
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
                tblCheck mChec101 = db.tblChecks.Single(n=>n.CheckCode == Decimal.Parse(Txt_CodeCheck.Text.Trim()));

                db.tblChecks.DeleteOnSubmit(mChec101);
                db.SubmitChanges();

                UDF_ClearTextBox();
                UDF_GrdFill();

            }
        }

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

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

                mch101.AccountNumber = Txt_AccountNumber.Text.Trim();
                mch101.BankBranck = Txt_Shobe.Text.Trim();
                mch101.BankName = Txt_BankName.Text.Trim();
                mch101.CheckNumber = Txt_CheckNumber.Text.Trim();
                mch101.CostOfCheck = Decimal.Parse(Txt_Cost.Text.Trim());
                mch101.DateOfCheck = Txt_Date.Text.Trim();
                mch101.PaymentTo = Txt_PaymentTo.Text.Trim();
                mch101.SerialNo = Txt_Serial.Text.Trim();
                mch101.SignerName = Txt_SignerName.Text.Trim();
                mch101.ExplNotes = Txt_Note.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.tblChecks 
                        where Row.SerialNo  == Txt_Search.Text.Trim()
                        select new
                        {
                            Row.CheckCode ,
                            Row.SerialNo ,
                            Row.CheckNumber,
                            Row.BankName,
                            Row.BankBranck,
                            Row.DateOfCheck ,
                            Row.CostOfCheck,
                            Row.PaymentTo,
                            Row.AccountNumber,
                            Row.SignerName,
                            Row.ExplNotes
                        };

            Grd_Check.DataSource = Query;

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

        //private void Btn_RefreshCheck_Click(object sender, EventArgs e)
        //{
        //    UDF_GrdFill();
        //    Btn_Refresh.SendToBack();
        //    Btn_Search.BringToFront();
        //}

        private void button1_Click(object sender, EventArgs e)
        {


        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {

            if (Txt_AccountNumber.Text == "" || Txt_BankName.Text == "" || Txt_CheckNumber.Text == "" || Txt_Cost.Text == "" || Txt_Date.Text == "" || Txt_Note.Text == "" || Txt_PaymentTo.Text == "" || Txt_Serial.Text == "" || Txt_Shobe.Text == "" || Txt_SignerName.Text == "")
            {
                MessageBox.Show("ورود موارد الزامی می باشد");
                return;
            }

            LSC_HesabdariSubSysDataContext db = new LSC_HesabdariSubSysDataContext();
            tblCheck mChec101 = new tblCheck();

            mChec101.SerialNo = Txt_Serial.Text.Trim();
            mChec101.CheckNumber = Txt_CheckNumber.Text.Trim();
            mChec101.BankName = Txt_BankName.Text.Trim();
            mChec101.BankBranck = Txt_Shobe.Text.Trim();
            mChec101.DateOfCheck = Txt_Date.Text.Trim();
            mChec101.CostOfCheck = Decimal.Parse(Txt_Cost.Text.Trim());
            mChec101.PaymentTo = Txt_PaymentTo.Text.Trim();
            mChec101.AccountNumber = Txt_AccountNumber.Text.Trim();
            mChec101.SignerName = Txt_SignerName.Text.Trim();
            mChec101.ExplNotes = Txt_Note.Text.Trim();

            db.tblChecks.InsertOnSubmit(mChec101);
            db.SubmitChanges();

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

        }

        private void Btn_Refresh_Click(object sender, EventArgs e)
        {
            UDF_GrdFill();
            Btn_Refresh.SendToBack();
            Btn_Search.BringToFront();
        }


    }
}
