using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
using RKLib.ExportData;

public partial class TransactionReport : System.Web.UI.Page
{
    Allfunctions exporttoexcel = new Allfunctions();
    string date = DateTime.Now.ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERNAME"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            if (!IsPostBack)
            {
                BindDataAccServices();
            }
            btndownload.Visible = false;
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    private void BindDataAccServices()
    {
      string tomonth = Convert.ToString(DateTime.Now.Month);
        if (tomonth.Length < 2)
        {
            tomonth = "0" + tomonth;
        }
        string todate1 = Convert.ToString(DateTime.Now.Day);
        string year1 = Convert.ToString(DateTime.Now.Year);
        string todaysdate = year1 + "-" + tomonth + "-" + todate1;
        try
        {
            if (DDLSelService.SelectedItem.Text.Equals("Select") || txtFrmDate.Text == "" || txtFrmDate.Text == "")
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@ServiceType",DDLSelService.SelectedItem.Text),
                    new SqlParameter("@Fromdate",todaysdate),
                    new SqlParameter("@Todate",todaysdate)
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTION_REPORT", param);
                if (ds.Tables.Count > 0)
                {
                    if (DDLSelService.SelectedItem.Text == "Train")
                    {
                        GvTransactionReport.Visible = false;
                        GridView_Train.Visible = true;
                        gv_DMT.Visible = false;
                        GridView_Train.DataSource = ds;
                        GridView_Train.DataBind();
                        lblerror.Visible = false;
                    }
                    else if (DDLSelService.SelectedItem.Text == "Money Transfer")
                    {
                        GvTransactionReport.Visible = false;
                        GridView_Train.Visible = false;
                        gv_DMT.Visible = true;
                        gv_DMT.DataSource = ds;
                        gv_DMT.DataBind();
                        lblerror.Visible = false;
                    }
                    else
                    {
                        GvTransactionReport.Visible = true;
                        GridView_Train.Visible = false;
                        gv_DMT.Visible = false;
                        GvTransactionReport.DataSource = ds;
                        GvTransactionReport.DataBind();
                        lblerror.Visible = false;
                    }
                }
                else
                {
                    //lblerror.Visible = true;
                    //lblerror.Text = "No transaction available";
                    //lblerror.ForeColor = Color.Red;
                }
            }
            else
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@ServiceType",DDLSelService.SelectedItem.Text),
                    new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTION_REPORT", param);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    ds.Tables[0].Merge(ds.Tables[i]);
                }
                DataTable dt = ds.Tables["Table"].Copy();
                DataView dv = new DataView(dt);
                dv.Sort = "TRANSACTION_DATETIME ASC";
                if (ds.Tables[0].Rows.Count != 0)
                {
                    if (DDLSelService.SelectedItem.Text == "Train")
                    {
                        GvTransactionReport.Visible = false;
                        GridView_Train.Visible = true;
                        gv_DMT.Visible = false;
                        GridView_Train.DataSource = dv;
                        GridView_Train.DataBind();
                        lblerror.Visible = false;
                    }
                    else if (DDLSelService.SelectedItem.Text == "Money Transfer")
                    {
                        GvTransactionReport.Visible = false;
                        GridView_Train.Visible = false;
                        gv_DMT.Visible = true;
                        gv_DMT.DataSource = ds;
                        gv_DMT.DataBind();
                        lblerror.Visible = false;
                    }
                    else
                    {
                        GvTransactionReport.Visible = true;
                        GridView_Train.Visible = false;
                        gv_DMT.Visible = false;
                        GvTransactionReport.DataSource = dv;
                        GvTransactionReport.DataBind();
                        lblerror.Visible = false;
                        btndownload.Visible = true;
                    }
                    //GvTransactionReport.DataSource = dv;
                    //GvTransactionReport.DataBind();
                    //lblerror.Visible = false;
                    //btndownload.Visible =true;
                    //GvTransactionReport.Visible = true;
                }
                else
                {
                    lblerror.Visible = true;
                    lblerror.Text = "No transaction available";
                    lblerror.ForeColor = Color.Red;
                    GvTransactionReport.Visible = false;
                }
            }

        }
        catch (Exception ev)
        {
            lblerror.Text = "erroe=" + ev;
            throw (ev);
        }
    }
    private void gettransaction_details()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@loginid",Session["LOGINID"].ToString()),
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTION", param);
            if (ds.Tables[0].Rows.Count != 0)
            {
                GvTransactionReport.DataSource = ds;
                GvTransactionReport.DataBind();
            }
        }
        catch (Exception ev)
        {
            lblerror.Text = "erroe=" + ev;
            throw (ev);
        }
    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToDateTime(txtFrmDate.Text).Date > Convert.ToDateTime(date))
            {
                lblerror.Text = "From date must be less then current date";
                lblerror.ForeColor = Color.Red;
            }
            else
            {

                BindDataAccServices();

            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    protected void btndownload_Click(object sender, EventArgs e)
    {        
        try
        {
            SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@ServiceType",DDLSelService.SelectedItem.Text),
                    new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTION_REPORT", param);
            if (ds.Tables[0].Rows.Count != 0)
            {
                GvTransactionReport.DataSource = ds;
                GvTransactionReport.DataBind();
                lblerror.Visible = false;
               // Session["GvTransactionReport"] = ds;
                DataTable dtEmployee = ds.Tables["Table"].Copy();
                int[] iColumns = { 1, 2 ,3,4,5, 6,7, 8,};
                RKLib.ExportData.Export objExport = new
                RKLib.ExportData.Export("Web");
                objExport.ExportDetails(dtEmployee,
                iColumns, Export.ExportFormat.CSV,
                "C:\\Transactiondetails_otherrecharge.csv");
            }
            else
            {
                lblerror.Visible = true;
                lblerror.Text = "No transaction available";
                lblerror.ForeColor = Color.Red;
            }
        }
        catch(Exception ev)
        {
            lblerror.Text = "erroe=" + ev;
            throw(ev);
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    protected void GvTransactionReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GvTransactionReport.PageIndex = e.NewPageIndex;
            BindDataAccServices();
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
}
