using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using RKLib.ExportData;
using System.Web.UI.WebControls;

public partial class AccountSummery : System.Web.UI.Page
{
    decimal BalanceAmount;
    string date = DateTime.Now.ToLongDateString();
    SqlDataReader dr = null;
    DataSet ds = new DataSet();
    DataSet dsMergedContract = new DataSet();
    int loginvariable;
    static string courentlevel;
    string loginid;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERTYPE"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            btndownload.Visible = false;
            if (!IsPostBack)
            {
                lblopeningbalance.Visible = false;
                lbluserid.Visible = false;
                ddl_partner_id.Visible = false;
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (DDL_UserType.SelectedItem.Text.Equals("Self"))
            {
                loginid = Session["LOGINID"].ToString();
                courentlevel = loginid;
                AccountSummary();
            }
            else
            {
                loginid = ddl_partner_id.SelectedValue.ToString();
                courentlevel = loginid;
                AccountSummary();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void btndownload_Click(object sender, EventArgs e)
    {
        DateTime todaysdate = DateTime.Now;
        try
        {            
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@loginid",courentlevel),
                new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                new SqlParameter("@Todate",txtToDate.Text.Trim())
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTIONNew", param);                
            if (ds.Tables[0].Rows.Count != 0)
            {  
                DataTable dtEmployee = ds.Tables["Table"].Copy();
                int[] iColumns = {2, 3,1,11, 7, 9,12,8,6,13 };
                RKLib.ExportData.Export objExport = new
                RKLib.ExportData.Export("Web");
                objExport.ExportDetails(dtEmployee,
                iColumns, Export.ExportFormat.CSV,
                "C:\\AccountSummeryDetailDS.csv");
            }
            else
            {
                lblerror.Visible = true;
                lblerror.Text = "No transaction available";
                lblerror.ForeColor = Color.Red;
                gvAccountReport.Visible = false;
                }            
        }
        catch (Exception ex)
        {
            lblerror.Visible = true;
            lblerror.Text = "There is some technical problem .Please try again";
            lblerror.ForeColor = Color.Red;
            throw (ex);
        }
    }
    protected void gvAccountReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvAccountReport.PageIndex = e.NewPageIndex;        
        AccountSummary();
    }

    protected void DDL_UserType_SelectedIndexChanged(object sender, EventArgs e)
    {

        try
        {
            if (DDL_UserType.SelectedItem.Text.Equals("Self"))
            {
                lbluserid.Visible = false;
                ddl_partner_id.Visible = false;
            }
            else if (DDL_UserType.SelectedItem.Text.Equals("Agent"))
            {
                SqlParameter[] GetAgent = new SqlParameter[]
                {
                    new SqlParameter("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Usertype","Distributor"), 
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetDistributorAgentOfMDS", GetAgent);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ddl_partner_id.DataSource = ds;
                    ddl_partner_id.DataTextField = "USERNAME";
                    ddl_partner_id.DataValueField = "LOGINID";
                    ddl_partner_id.DataBind();
                    lbluserid.Visible = true;
                    ddl_partner_id.Visible = true;
                }
            }
        }
        catch (Exception ex)
        {
            lblerror.Visible = true;
            lblerror.Text = "There is some technical problem .Please try again";
            lblerror.ForeColor = Color.Red;
        }
    }

    private void AccountSummary()
    {
        //string todaysdate = DateTime.Now.ToShortDateString();
        //string[] datetoday = todaysdate.Split('/');
       // string tomonth = Convert.ToString(DateTime.Now.Month);
       // if(tomonth.Lenght<2)
		//	tomonth="0"+tomonth;				 
       // string todate1 = Convert.ToString(DateTime.Now.Day);
       // string year = Convert.ToString(DateTime.Now.Year);
       // string nowdate = (tomonth + "-" + todate1 + "-" + year);
        try
        {    
            if (DDL_UserType.SelectedItem.Text.Equals("Agent"))
            {
                gvAccountReport.Visible = true;
                gvSelf.Visible = false;
                SqlParameter[] param = new SqlParameter[]
				{
					new SqlParameter("@loginid",courentlevel),
					new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
					new SqlParameter("@Todate",txtToDate.Text.Trim())
				};
				DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTIONNew", param);
				if (ds.Tables[0].Rows.Count != 0)
				{
					gvAccountReport.DataSource = ds;
					gvAccountReport.DataBind();
					lblerror.Visible = false;
					btndownload.Visible = true;
					gvAccountReport.Visible = true;
				}
				else
				{
					lblerror.Visible = true;
					lblerror.Text = "No transaction available";
					lblerror.ForeColor = Color.Red;
					gvAccountReport.Visible = false;
					lblopeningbalance.Visible = false;
					lblBalance.Visible = false;
				}        
            }   
			else
            {
                gvAccountReport.Visible = false;
                gvSelf.Visible = true;
                SqlParameter[] param = new SqlParameter[]
				{
					new SqlParameter("@loginid",courentlevel),
					new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
					new SqlParameter("@Todate",txtToDate.Text.Trim())
				};
				DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTIONNewDS_New", param);
				if (ds.Tables[0].Rows.Count != 0)
				{
                    gvSelf.DataSource = ds;
                    gvSelf.DataBind();
					lblerror.Visible = false;
					btndownload.Visible = true;
                    gvSelf.Visible = true;
				}
				else
				{
					lblerror.Visible = true;
					lblerror.Text = "No transaction available";
					lblerror.ForeColor = Color.Red;
                    gvSelf.Visible = false;
					lblopeningbalance.Visible = false;
					lblBalance.Visible = false;
				}        
            }        			
                
        }
        catch (Exception ex)
        {
            lblerror.Visible = true;
            lblerror.Text = "There is some technical problem .Please try again";
            lblerror.ForeColor = Color.Red;
        }
    }
}
