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 RKLib.ExportData;

public partial class Distributor_FundReportAgent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERNAME"] == null)
            {
                Response.Redirect("../Login.aspx");
            }
            if (!IsPostBack)
            {
                getbalancerequestdetail();
            }
            btn_download.Visible = false;
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void getbalancerequestdetail()
    {
        DateTime todaysdate = DateTime.Now;
        try
        {
            if (txt_From.Text == "" && txt_To.Text == "")
            {
                SqlParameter[] getbalancedetail = new SqlParameter[]
                {
                    new SqlParameter ("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Fromdate",todaysdate),
                    new SqlParameter("@Todate",todaysdate),
                    new SqlParameter("@usertype","Agent"),
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetDistributer_Agent_Balance_Request", getbalancedetail);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    GVGetBalReqDetail.DataSource = ds;
                    GVGetBalReqDetail.DataBind();
                    lblerror.Visible = false;
                    GVGetBalReqDetail.Visible = true;
                }
            }
            else
            {
                SqlParameter[] getbalancedetail = new SqlParameter[]
                {
                    new SqlParameter ("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Fromdate",txt_From.Text.Trim()),
                    new SqlParameter("@Todate",txt_To.Text.Trim()),
                    new SqlParameter("@usertype","Agent"),
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetDistributer_Agent_Balance_Request", getbalancedetail);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    GVGetBalReqDetail.DataSource = ds;
                    GVGetBalReqDetail.DataBind();
                    lblerror.Visible = false;
                    btn_download.Visible = true;
                    GVGetBalReqDetail.Visible = true;
                }
                else
                {
                    lblerror.Visible = true;
                    lblerror.Text = "No transaction available";
                    lblerror.ForeColor = Color.Red;
                }
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = "" + ex;
        }
    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToDateTime(txt_From.Text).Date > Convert.ToDateTime(DateTime.Now.ToString()))
            {
                lblerror.Text = "From Date must be less then current Date";
                lblerror.ForeColor = Color.Red;
            }
            else if (Convert.ToDateTime(txt_To.Text).Date > Convert.ToDateTime(DateTime.Now.ToString()))
            {
                lblerror.Text = "To Date must be less then current Date";
                lblerror.ForeColor = Color.Red;
            }
            else if (Convert.ToDateTime(txt_From.Text).Date > Convert.ToDateTime(txt_To.Text).Date)
            {
                lblerror.Text = "From Date must be less then To Date";
                lblerror.ForeColor = Color.Red;
            }
            else
            {
                string datefrom = txt_From.Text.Trim();
                string dateto = txt_To.Text.Trim();
                string[] datefrom1 = datefrom.Split('-');
                string[] dateto1 = dateto.Split('-');
                //string[] datefrom2 = new string[2];
                //string[] dateto2 = new string[2];
                string fromdate = string.Empty;
                string todate = string.Empty;
                fromdate = Convert.ToString(datefrom1.GetValue(2));
                todate = Convert.ToString(dateto1.GetValue(2));

                if (Convert.ToInt32(fromdate) > Convert.ToInt32(todate))
                {
                    lblerror.Text = "You can not Get data of this date";
                    lblerror.ForeColor = Color.Red;

                }
                  //if (Convert.ToInt32(fromdate) < Convert.ToInt32(todate))
                //{
                //    lblerror.Text = "Invalid Date selection";
                //    lblerror.ForeColor = Color.Red;
                //}
                else 
                {
                    getbalancerequestdetail();
                }
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    protected void btn_download_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] getbalancedetail = new SqlParameter[]
                {
                    new SqlParameter ("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Fromdate",txt_From.Text.Trim()),
                    new SqlParameter("@Todate",txt_To.Text.Trim()),
                    new SqlParameter("@usertype","Agent"),
                };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetDistributer_Agent_Balance_Request", getbalancedetail);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GVGetBalReqDetail.DataSource = ds;
                GVGetBalReqDetail.DataBind();
                lblerror.Visible = false;
                DataTable dtEmployee = ds.Tables["Table"].Copy();
                int[] iColumns = { 1, 2, 3, 4, 5, 6, 7,8,9 };
                RKLib.ExportData.Export objExport = new
                RKLib.ExportData.Export("Web");
                objExport.ExportDetails(dtEmployee,
                iColumns, Export.ExportFormat.CSV,
                "C:\\Transactiondetails_otherrecharge.csv");
            }
        }
        catch (Exception ex)
        {
           // lblerror.Text = "" + ex;
            throw (ex);
        }
    }
}
