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 CofeeShop
{
    public partial class frmRptLogs : Form
    {
        public frmRptLogs()
        {
            InitializeComponent();
        }

        private void frmRptLogs_Load(object sender, EventArgs e)
        {
            Class_tblLogs LogReg = new Class_tblLogs(this.Text + " فعال گردید.");
            LogReg.UDF_SaveLogToDb();
            reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent;
            reportViewer1.ZoomPercent = 50;

            LSC_CofeeShopDataContext db = new LSC_CofeeShopDataContext();
            var Query = from Row in db.tblLogs
                        orderby Row.LogID descending
                        select new
                        {
                            Row.LogID , Row.LogDescription
                        };

            lSCRestuRantDataContextBindingSource.DataSource = Query;
            this.reportViewer1.RefreshReport();
            UDF_GetUsersName();
        }

        private void UDF_GetUsersName()
        {
            DataTable Dt_UsersList = new DataTable();
            Dt_UsersList.Columns.Add("RowID", typeof(int));
            Dt_UsersList.Columns.Add("UserName", typeof(String));
            int int_RowCnt = 0;
            Dt_UsersList.Rows.Add(new Object[] { int_RowCnt, "انتخاب فرمائید . . ." });
            int_RowCnt++;

            LSC_CofeeShopDataContext db = new LSC_CofeeShopDataContext();
            var wordGroups = from w in db.tblLogs
                             group w by w.UsersName into g
                             select new { FirstLetter = g.Key.ToString(), Words = g };
            foreach (var g in wordGroups)
            {
                Dt_UsersList.Rows.Add(new Object[] { int_RowCnt, g.FirstLetter });
                int_RowCnt++;
            }
            Cmb_Users.DisplayMember = "UserName";
            Cmb_Users.ValueMember = "RowID";
            Cmb_Users.DataSource = Dt_UsersList;

        }

        private void frmRptLogs_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class_tblLogs LogReg = new Class_tblLogs(this.Text + " بسته شد.");
            LogReg.UDF_SaveLogToDb();
        }

        

        private void Btn_Show_Click(object sender, EventArgs e)
        {
            reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent;
            reportViewer1.ZoomPercent = 50;

            LSC_CofeeShopDataContext db = new LSC_CofeeShopDataContext();
            var Query = from Row in db.tblLogs
                        where Row.UsersName.Trim() == Cmb_Users.Text.Trim()
                        orderby Row.LogID descending
                        select new
                        {
                            Row.LogID,
                            Row.LogDescription
                        };

            lSCRestuRantDataContextBindingSource.DataSource = Query;
            this.reportViewer1.RefreshReport();
        }
    }
}
