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 RezerveBeliteHavaPeimaCS
{
    public partial class frmRptCustomers : Form
    {
        public frmRptCustomers()
        {
            InitializeComponent();
        }
        private void frmRptCustomers_Load(object sender, EventArgs e)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            UDF_FillComCustomer();
            var Query = from row in db.tblCustumers
                        orderby row.CustomerID descending
                        select row;
            XtblCustomerBindingSource.DataSource = Query;
            this.reportViewer1.RefreshReport();
            this.reportViewer1.RefreshReport();
        }
        /// <summary>
        /// تابع پر کردن کامو باکس مشتری
        /// </summary>
        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_FilterCustomer.DisplayMember = "NameSname";
            Cmb_FilterCustomer.ValueMember = "CustomerID";
            Cmb_FilterCustomer.DataSource = Query;
        }
        /// <summary>
        /// تابع فیلتر مشتری
        /// </summary>
        private void UDF_FilterCustomer()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            try
            {
                var Query = from Row in db.tblCustumers
                            where int.Parse(Cmb_FilterCustomer.SelectedValue.ToString()) == Row.CustomerID
                            select Row;
                XtblCustomerBindingSource.DataSource = Query;
            }
            catch
            {
                MessageBox.Show("لطفا فیلدی برای فیلتر کردن انتخاب نمایید");
            }
        }
        /// <summary>
        /// تابع به روزدسانی مشتریان
        /// </summary>
        private void UDf_RefreshCostomer()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblCustumers
                        orderby row.CustomerID descending
                        select new
                        {
                            row.CustomerID,
                            row.NameSname,
                            row.PhoneNum,
                            row.FaxNum,
                            row.Notes
                        };
            XtblCustomerBindingSource.DataSource = Query;
            this.reportViewer1.RefreshReport();
        }   
        private void Btn_FilterCustomer_Click(object sender, EventArgs e)
        {
            UDF_FilterCustomer();
            this.reportViewer1.RefreshReport();
        }
        private void Btn_RefreshCustomer_Click(object sender, EventArgs e)
        {
            try
            {
                UDf_RefreshCostomer();
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را برطرف نمایید");
            }
        }
        private void Cmb_FilterCustomer_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void reportViewer1_Load(object sender, EventArgs e)
        {

        }
    }
}
