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 SMSReport : System.Web.UI.Page
{
    string date = DateTime.Now.ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERNAME"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            btndownload.Visible = false;
            lbluserid.Visible = false;
            ddl_partner_id.Visible = false;
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    
    private void BindSmsReport()
    {
        try
        {
            SqlParameter[] getsmsreport = new SqlParameter[]
            {
                //new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                new SqlParameter("@loginid",ddl_partner_id.SelectedValue),
                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;
                btndownload.Visible = false;
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToDateTime(txtFrmDate.Text).Date > Convert.ToDateTime(date))
            {
                lblerror.Text = "From date must be less then current date";
                lblerror.ForeColor = Color.Red;
            }
            else if (Convert.ToDateTime(txtToDate.Text).Date > Convert.ToDateTime(date))
            {
                lblerror.Text = "To Date must be less then current date";
                lblerror.ForeColor = Color.Red;
            }
            else if (Convert.ToDateTime(txtFrmDate.Text).Date > Convert.ToDateTime(txtToDate.Text).Date)
            {
                lblerror.Text = "From date must be less then To date";
                lblerror.ForeColor = Color.Red;
            }
            else
            {
                string datefrom = txtFrmDate.Text.Trim();
                string dateto = txtToDate.Text.Trim();
                string[] datefrom1 = datefrom.Split('/');
                string[] dateto1 = dateto.Split('/');
                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 DDL_UserType_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbluserid.Visible = true;
        ddl_partner_id.Visible = true;
        try
        {
            if (DDL_UserType.SelectedItem.Text.Equals("Distributor"))
            {
                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();
                }
            }
            else
            {
                SqlParameter[] GetAgent = new SqlParameter[]
                {
                    new SqlParameter("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Usertype","Agent"),
                };
                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();
                }
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    protected void GvSMSReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GvSMSReport.PageIndex = e.NewPageIndex;
            BindSmsReport();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    protected void btndownload_Click(object sender, EventArgs e)
    {

    }
}
