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.IO;
using System.Drawing;
using RKLib.ExportData;

public partial class Agent_PancardReport : System.Web.UI.Page
{
    decimal BalanceAmount;
    DataTable dtEmployee;
    string date = DateTime.Now.ToLongDateString();
    SqlDataReader dr = null;
    DataSet ds = new DataSet();
    DataSet dsMergedContract = new DataSet();
    int loginvariable;
    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)
            {
                //getopeningbalance();
                //BindDataAccSummery();            
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToDateTime(txtFrmDate.Text).Date > Convert.ToDateTime(date))
            {
                lblerror.Text = "Date must be less then current date";
                lblerror.ForeColor = Color.Red;
            }
            else
            {
                PanCardReport();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }

    private void PanCardReport()
    {
           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 nowdate = year1 + "-" + tomonth + "-" + todate1;
        try
        {
            if (txtFrmDate.Text == "")
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Fromdate",nowdate),
                    new SqlParameter("@Todate",nowdate)
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "up_getPancardDetails", param);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    gvAccountReport.DataSource = ds;
                    gvAccountReport.DataBind();
                    lblerror.Visible = false;
                    btndownload.Visible = true;
                    lblopeningbalance.Visible = false;
                    lblBalance.Visible = false;
                    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
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "up_getPancardDetails", param);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    gvAccountReport.DataSource = ds;
                    gvAccountReport.DataBind();
                    lblerror.Visible = false;
                    btndownload.Visible = true;
                    lblopeningbalance.Visible = false;
                    lblBalance.Visible = false;
                    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;
                }
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }
    protected void btndownload_Click(object sender, EventArgs e)
    {

        DateTime todaysdate = DateTime.Now;
        try
        {
            if (txtFrmDate.Text == "")
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Fromdate",todaysdate),
                    new SqlParameter("@Todate",todaysdate)
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "up_getPancardDetails", param);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    btndownload.Visible = true;
                    DataTable dtEmployee = ds.Tables["Table"].Copy();
                    int[] iColumns = { 0, 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:\\PancardReportAgent.csv");
                }
                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("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "up_getPancardDetails", param);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    btndownload.Visible = true;
                    Session["Dataset"] = ds;
                    DataTable dtEmployee = ds.Tables["Table"].Copy();
                    int[] iColumns = { 0, 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:\\PancardReportAgent.csv");
                }
                else
                {
                    lblerror.Visible = true;
                    lblerror.Text = "No transaction available";
                    lblerror.ForeColor = Color.Red;
                }
            }
        }
        catch (Exception ex)
        {
            CommonFunction.writeToLogFile(Convert.ToString(ex));
            throw (ex);

        }
    }
}
