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_SMSReport : System.Web.UI.Page
{
    string date = DateTime.Now.ToString();
    static string courentlevel;
    string loginid;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERNAME"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            //if (!IsPostBack)
            //{
            //    getagentusername();
            //}
            lbluserid.Visible = false;
            ddl_partner_id.Visible = false;
            btndownload.Visible = false;
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    //private void getagentusername()
    //{
    //    SqlParameter[] GetAgent = new SqlParameter[]
    //    {
    //        new SqlParameter("@Loginid",Session["LOGINID"].ToString()),
    //    };
    //    DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetDistributerAgent", GetAgent);
    //    if (ds.Tables[0].Rows.Count > 0)
    //    {
    //        DDLSelUser.DataSource = ds;
    //        DDLSelUser.DataTextField = "USERNAME";
    //        DDLSelUser.DataValueField = "USERNAME";
    //        DDLSelUser.DataBind();
    //    }
    //}
    private void BindSmsReport()
    {
        try
        {
            SqlParameter[] getsmsreport = new SqlParameter[]
            {
                new SqlParameter("@loginid",courentlevel),
                //new SqlParameter("@USERNAME",DDLSelUser.SelectedItem.Text),
                new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                new SqlParameter("@Todate",txtToDate.Text.Trim())
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLSMSREPORT", getsmsreport);
            if (ds.Tables[0].Rows .Count > 0)
            {
                GvSMSReport.DataSource = ds;
                GvSMSReport.DataBind();
                lblerror.Visible = false;
                btndownload.Visible = true;
                GvSMSReport.Visible = true;
            }
            else
            {
                lblerror.Visible = true;
                lblerror.Text = "No transaction available";
                lblerror.ForeColor = Color.Red;
                GvSMSReport.Visible = false;
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = "There is some technical error,Please try after some time";
            lblerror.ForeColor = Color.Red;
            lblerror.Visible = true;
        }
    }
    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
            {

                if (DDL_UserType.SelectedItem.Text.Equals("Self"))
                {
                    loginid = Session["LOGINID"].ToString();
                    courentlevel = loginid;
                    BindSmsReport();
                }
                else
                {
                    loginid = ddl_partner_id.SelectedValue.ToString();
                    courentlevel = loginid;
                    BindSmsReport();
                }
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
        
    }
    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)
        {
            throw (ex);
        }
    }
    protected void btndownload_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] getsmsreport = new SqlParameter[]
            {
                new SqlParameter("@loginid",courentlevel),
                //new SqlParameter("@USERNAME",DDLSelUser.SelectedItem.Text),
                new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                new SqlParameter("@Todate",txtToDate.Text.Trim())
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLSMSREPORT", getsmsreport);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GvSMSReport.DataSource = ds;
                GvSMSReport.DataBind();
                lblerror.Visible = false;

                DataTable dtEmployee = ds.Tables["Table"].Copy();
                int[] iColumns = { 0,1 };
                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 ex)
        {
            throw (ex);   
        }
    }
    protected void GvSMSReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            BindSmsReport();
            GvSMSReport.PageIndex = e.NewPageIndex;
            GvSMSReport.DataBind();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  

    }
}
