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 MainTemplate.RptFolder
{
    public partial class frmRepLogs : Form
    {
        public frmRepLogs()
        {
            InitializeComponent();
        }

        private void frmRepLogs_Load(object sender, EventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " is Opened.");
            Lsc_LinqToSqlDataContext db = new Lsc_LinqToSqlDataContext();
            var Query = from tbl1 in db.tblLogs
                        orderby tbl1.RowLogID descending
                        select new
                        {
                            tbl1.RowLogID , 
                            tbl1.LogDescription,
                            tbl1.UserID
                        };

            tblUserBindingSource.DataSource = Query;

            reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent;
            reportViewer1.ZoomPercent = 25;

            this.reportViewer1.RefreshReport();
            CmbFill();
        }

        private void Btn_View_Click(object sender, EventArgs e)
        {

            if (int.Parse(Cmb_Users.SelectedValue.ToString()) == 0)
            {
                Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " مشاهده گزارش رخدادها");
                Lsc_LinqToSqlDataContext db = new Lsc_LinqToSqlDataContext();
                var Query = from tbl1 in db.tblLogs
                            orderby tbl1.RowLogID descending
                            select new
                            {
                                tbl1.RowLogID,
                                tbl1.LogDescription,
                                tbl1.UserID
                            };

                tblUserBindingSource.DataSource = Query;

                reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
                reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent;
                reportViewer1.ZoomPercent = 25;
                this.reportViewer1.RefreshReport();
            }
            else
            {
                Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " مشاهده گزارش رخدادها");
                Lsc_LinqToSqlDataContext db = new Lsc_LinqToSqlDataContext();
                var Query = from tbl1 in db.tblLogs
                            where tbl1.UserID == int.Parse(Cmb_Users.SelectedValue.ToString())
                            orderby tbl1.RowLogID descending
                            select new
                            {
                                tbl1.RowLogID,
                                tbl1.LogDescription,
                                tbl1.UserID
                            };

                tblUserBindingSource.DataSource = Query;

                reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
                reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent;
                reportViewer1.ZoomPercent = 25;

                this.reportViewer1.RefreshReport();

            }
        }

        private void CmbFill()
        {
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();

            dt = db.dbOut(@"SELECT        tblUsers.RowID,  'نام کاربری: ' + tblUsers.UsrName + '-  نام: ' + tblUsers.NameOfUser + '  ' + tblUsers.SnameOfUser + ' - نوع کاربر:  ' + tblUserTypes.UserTypeDescr AS Expr1
                            FROM            tblUsers INNER JOIN  tblUserTypes ON tblUsers.UsrType = tblUserTypes.UserTypeID");

            Cmb_Users.ValueMember = "RowID";
            Cmb_Users.DisplayMember = "Expr1";
            dt.Rows.Add(new Object[] { 0, "نمایش همه . . . " });
            Cmb_Users.DataSource = dt;
            try
            {
                Cmb_Users.SelectedValue = 0;
            }
            catch { }
        }

        private void frmRepLogs_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " is Closed");
        }
    }
}
