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 Agent_SMSReport : System.Web.UI.Page
{
    string date = DateTime.Now.ToShortDateString();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERNAME"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            btndownload.Visible = false;
            if (!IsPostBack)
            {
                BindSmsReport();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    private void BindSmsReport()
    {
        string todaysdate = DateTime.Now.ToShortDateString();
        //string[] datetoday = todaysdate.Split('/');
        //string tomonth = Convert.ToString(datetoday.GetValue(0));
        //string todate1 = Convert.ToString(datetoday.GetValue(1));
        //string year = Convert.ToString(datetoday.GetValue(2));
        //string nowdate = (tomonth + "-" + todate1 + "-" + year);
        try
        {
            if (txtFrmDate.Text == "" || txtFrmDate.Text == "")
            {
                SqlParameter[] getsmsreport = new SqlParameter[]
                {  
                    new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Fromdate",todaysdate),
                    new SqlParameter("@Todate",todaysdate)
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLSMSREPORTFORAGENTPANEL", getsmsreport);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    GvSMSReport.DataSource = ds;
                    GvSMSReport.DataBind();
                    lblerror.Visible = false;
                    btndownload.Visible = true;
                    GvSMSReport.Visible = true;
                }
            }
            else
            {
                SqlParameter[] getsmsreport = 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, "USP_ALLSMSREPORTFORAGENTPANEL", getsmsreport);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    GvSMSReport.DataSource = ds;
                    GvSMSReport.DataBind();
                    lblerror.Visible = false;
                    btndownload.Visible = true;
                }
                else
                {
                    lblerror.Visible = true;
                    lblerror.Text = "No transaction available";
                    lblerror.ForeColor = Color.Red;
                    btndownload.Visible = false;
                }
            }
        }
        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
            {
                string datefrom = txtFrmDate.Text.Trim();
                string dateto = txtFrmDate.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(0));
                todate = Convert.ToString(dateto1.GetValue(0));

                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 if (Convert.ToInt32(fromdate).Equals(Convert.ToInt32(todate)))
                {
                    BindSmsReport();
                }
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
        
    }
    protected void GvSMSReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GvSMSReport.PageIndex = e.NewPageIndex;
        BindSmsReport();
    }
    protected void btndownload_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] getsmsreport = 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, "USP_ALLSMSREPORTFORAGENTPANEL", getsmsreport);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GvSMSReport.DataSource = ds;
                GvSMSReport.DataBind();
                lblerror.Visible = false;
               
                DataTable dtEmployee = ds.Tables["Table"].Copy();
                int[] iColumns = { 1, 2, 3 };
                RKLib.ExportData.Export objExport = new
                RKLib.ExportData.Export("Web");
                objExport.ExportDetails(dtEmployee,
                iColumns, Export.ExportFormat.CSV,
                "C:\\Transactiondetails_otherrecharge.csv");
            }
        }
        catch(Exception ex)
        {
            //Response.Redirect("Index.aspx");
            throw (ex);
        }
    }
}
