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

namespace RezerveBeliteHavaPeimaCS
{
    public partial class frmCustumersStatus : Form
    {
        public frmCustumersStatus()
        {
            InitializeComponent();
        }
        /// <summary>
        /// تابع پر کردن کامو باکس مشتری
        /// </summary>
        int int_Custumers = 0;
        private void UDF_FillComCustomer()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblCustumers
                        orderby row.CustomerID descending
                        select new
                        {
                            row.CustomerID,
                            row.NameSname
                        };
            Cmb_CustumersHesabName.DisplayMember = "NameSname";
            Cmb_CustumersHesabName.ValueMember = "CustomerID";
            Cmb_CustumersHesabName.DataSource = Query;
        }
        /// <summary>
        /// تابع به روز رسانی وضعیت مشتریان
        /// </summary>
        private void UDf_RefreshCustumursHesab()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblRezerveTickets
                        orderby row.RezerveID descending
                        select new
                        {
                            row.RezerveID,
                            row.Str_CustomerNameSnameTTTT,
                            row.BianehPardakhti,
                            row.Mandeh
                        };
            Dgv__CustumersHesab.DataSource = Query;
        }
        /// <summary>
        ///تابع نمایش وضعیت مشتریان 
        /// </summary>
        private void UDf_ShowHesab()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblRezerveTickets
                        where (row.RezerverID == int.Parse(Cmb_CustumersHesabName.SelectedValue.ToString().Trim()))
                        orderby row.RezerveID descending
                        select new
                        {  
                            row.RezerveID,
                            row.Str_CustomerNameSnameTTTT,
                            row.BianehPardakhti,
                            row.Mandeh
                        };
            Dgv__CustumersHesab.DataSource = Query;
            Txt_CustumersHesabRezerveId.Text = "";
            Txt_CustumersHesabMablagh.Text = "";
            Txt_CustumersHesabBeyaneh.Text = "";
            Txt_CustumersHesabBedehi.Text = "";
        }
        /// <summary>
        /// محاسبات پرداختی
        /// </summary>
        private void UDF_Mohasebat(Decimal Dec_Pardakht, Decimal Dec_Mandeh, Decimal Dec_Beyaneh)
        {
            if ( Txt_CustumersHesabMablagh.Text == "")//شرط مقایسه خالی بودن تکس باکس
            {
                MessageBox.Show("فیلد های خالی را پر نمایید");
            }
            else
            {
                if (Dec_Pardakht > Dec_Mandeh)//شرط مقایسه مبلغ پرداختی با مانده بدهی
                {
                    MessageBox.Show("مبلغ وارد شده بیشتر از مبلغ بدهی می باشد");
                }
                else
                {
                    Dec_Beyaneh += Dec_Pardakht;
                    Dec_Mandeh -= Dec_Pardakht;
                    LSC_TADataContext db = new LSC_TADataContext();
                    tblRezerveTicket tblRezerve = db.tblRezerveTickets.Single(n => n.RezerveID == int.Parse(Txt_CustumersHesabRezerveId.Text.Trim()));
                    tblRezerve.Mandeh = Double.Parse(Dec_Mandeh.ToString().Trim());
                    tblRezerve.BianehPardakhti = Double.Parse(Dec_Beyaneh.ToString().Trim()) ;
                    db.SubmitChanges();
                    UDf_RefreshCustumursHesab();
                }
            }
        }
        private void frmCusturersStatus_Load(object sender, EventArgs e)
        {
            Pnl_CustumersHesab.Visible = false;
            UDf_RefreshCustumursHesab();
            UDF_FillComCustomer(); 
        }

        int int_ReserveID1 = 0;
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            Int32 selectedCellCount = Dgv__CustumersHesab.GetCellCount(DataGridViewElementStates.Selected);
            int int_Counter = 0;
            if (selectedCellCount > 0)
            {
                if (Dgv__CustumersHesab.AreAllCellsSelected(true))
                {
                    MessageBox.Show("لطفاسلول از سطر مورد نظر راانتخاب نماييد!", "Selected Cells");
                }
                else
                {
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        if (int_Counter == 0)
                        {
                            Txt_CustumersHesabRezerveId.Text = Dgv__CustumersHesab.Rows[int.Parse(Dgv__CustumersHesab.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                            int_ReserveID1 = int.Parse(Dgv__CustumersHesab.Rows[int.Parse(Dgv__CustumersHesab.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString());
                            Cmb_CustumersHesabName.SelectedValue = UDF_GetSharidarID(int_ReserveID1);
                        }
                        int_Counter++;
                        Txt_CustumersHesabRezerveId.Text = int_ReserveID1.ToString();
                        string Str_CmbCustomer = Dgv__CustumersHesab.Rows[int.Parse(Dgv__CustumersHesab.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_CustumersHesabBeyaneh.Text = Dgv__CustumersHesab.Rows[int.Parse(Dgv__CustumersHesab.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_CustumersHesabBedehi.Text = Dgv__CustumersHesab.Rows[int.Parse(Dgv__CustumersHesab.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                    }
                }
            }
        }
        /// <summary>
        /// تابع برگرداندن کد خریدار
        /// </summary>
        private int UDF_GetSharidarID(int int_ReserveID)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblRezerveTicket tbRes = db.tblRezerveTickets.Single(n => n.RezerveID == int_ReserveID);
            return tbRes.RezerverID;
        }
        private void Btn_CustumersHesabLaghv_Click(object sender, EventArgs e)
        {
            try
            {
                Pnl_CustumersHesab.Visible = false;
                Btn_CustumersHesabPardakht.Enabled = true;
                UDf_RefreshCustumursHesab();
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را برطرف نمایید");
            }
        }
        private void Btn_CustumersHesabPardakht_Click(object sender, EventArgs e)
        {
            if (Txt_CustumersHesabMablagh.Text == "")
            {
                MessageBox.Show("فیلد های خالی را پر نمایید");
               
            }
            else
            {
                Pnl_CustumersHesab.Visible = true;
                Btn_CustumersHesabPardakht.Enabled = false;
                int_Custumers = 1;
            }
        }
        private void Btn_CustumersHesabTaeed_Click(object sender, EventArgs e)
        {
            try
            {
                Pnl_CustumersHesab.Visible = false;
                Btn_CustumersHesabPardakht.Enabled = true;
                if (int_Custumers == 1)
                {
                    Decimal Dec_Pardahkt = Decimal.Parse(Txt_CustumersHesabMablagh.Text.ToString().Trim());
                    Decimal Dec_Beyaneh = Decimal.Parse(Txt_CustumersHesabBeyaneh.Text.ToString().Trim());
                    Decimal Dec_Mandeh = Decimal.Parse(Txt_CustumersHesabBedehi.Text.ToString().Trim());
                    UDF_Mohasebat(Dec_Pardahkt, Dec_Mandeh, Dec_Beyaneh);
                    Txt_CustumersHesabBedehi.Text = "";
                    Txt_CustumersHesabBeyaneh.Text = "";
                    Txt_CustumersHesabMablagh.Text = "";
                    Txt_CustumersHesabRezerveId.Text = "";
                }
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را برطرف نمایید");
            }
        }
        private void Btn_CustumersHesabRefresh_Click(object sender, EventArgs e)
        {
            if (Rdo_CustumerHesab2.Checked == true)
            {
                LSC_TADataContext db = new LSC_TADataContext();
                var Query = from row in db.tblRezerveTickets
                            where row.Mandeh != 0
                            orderby row.RezerveID descending
                            select new
                            {
                                row.RezerveID,
                                row.Str_CustomerNameSnameTTTT,
                                row.BianehPardakhti,
                                row.Mandeh
                            };
                Dgv__CustumersHesab.DataSource = Query;
                Rdo_CustumerHesab1.Checked = false;

            }
            else if(Rdo_CustumerHesab1.Checked == true)
            {
                LSC_TADataContext db = new LSC_TADataContext();
                var Query = from row in db.tblRezerveTickets
                            where row.Mandeh == 0
                            orderby row.RezerveID descending
                            select new
                            {
                                row.RezerveID,
                                row.Str_CustomerNameSnameTTTT,
                                row.BianehPardakhti,
                                row.Mandeh
                            };
                Dgv__CustumersHesab.DataSource = Query;
                Rdo_CustumerHesab2.Checked = false;
            }
        }
        private void Txt_CustumersHesabMablagh_KeyPress(object sender, KeyPressEventArgs e)
        {
            int int_Ascii = Convert.ToInt16(e.KeyChar);
            if (int_Ascii >= 48 && int_Ascii <= 57 || int_Ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        private void Btn_HesabView_Click(object sender, EventArgs e)
        {
            UDf_ShowHesab();
            
        }
    }
}