using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterAdmin_OrgPayTxnDetails : System.Web.UI.Page
{
    decimal ColumnAmount;
    int RowCount;
    decimal ColumnNetAmount;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERTYPE"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            if (!IsPostBack)
            {
                txt_fromdate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                txt_todate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                GetUserDetail();
            }
           
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }
    protected void btn_search_Click(object sender, EventArgs e)
    {
        try
        {
            if (txt_fromdate.Text == "")
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "ShowAlert", "alert('Please Select From Date')", true);
                return;
            }
            else if (txt_todate.Text == "")
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "ShowAlert", "alert('Please Select To Date')", true);
                return;
            }
            else if (drop_level.SelectedValue == "0")
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "ShowAlert", "alert('Please Select User Level')", true);
                return;
            }
            else if (dropusername.SelectedValue == "0")
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "ShowAlert", "alert('Please Select User Name')", true);
                return;
            }
            else
            {
                SqlParameter[] warlock = new SqlParameter[]
                {
                                new SqlParameter("@FromDate",txt_fromdate.Text.Trim()),
                                new SqlParameter("@ToDate",txt_todate.Text.Trim()),
                                new SqlParameter("@Mode",drop_level.SelectedValue),
                                new SqlParameter("@LoginId",dropusername.SelectedItem.Text)
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetConsumerBillPaymentDetils_Admin", warlock);
                Session["dataset"] = ds;
                if (ds.Tables[0].Rows.Count > 0)
                {
                    gridview_completed.DataSource = ds.Tables[0];
                    gridview_completed.DataBind();
                    lbl_totl.Text = "";// Convert.ToDecimal(ds.Tables[1].Rows[0]["TotalAmount"]).ToString("f2");
                }
                else
                {
                    lbl_totl.Text = "";
                    //ScriptManager.RegisterStartupScript(this, GetType(), "ShowAlert", "alert('No Transaction Found.')", true);
                    //return;
                }
            }
        }
        catch (Exception ex)
        {
        }


    }
    public void GetUserDetail()
    {
        string sql = "select distinct LoginId from Login_Detail";
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.Text, sql);
        dropusername.DataTextField = "LoginId";
        dropusername.DataValueField = "LoginId";
        dropusername.DataSource = ds.Tables[0];
        dropusername.DataBind();
        dropusername.Items.Insert(0, new ListItem("All", "All"));
    }
    protected void lnk_download_Rpt_Click(object sender, EventArgs e)
    {
        if (txt_fromdate.Text == "")
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "ShowAlert", "alert('Please Select From Date')", true);
            return;
        }
        else if (txt_todate.Text == "")
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "ShowAlert", "alert('Please Select To Date')", true);
            return;
        }
        else if (drop_level.SelectedValue == "0")
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "ShowAlert", "alert('Please Select User Level')", true);
            return;
        }
        else if (dropusername.SelectedValue == "0")
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "ShowAlert", "alert('Please Select User Name')", true);
            return;
        }
        else
        {
            SqlParameter[] warlock = new SqlParameter[]
                {
                                new SqlParameter("@FromDate",txt_fromdate.Text.Trim()),
                                new SqlParameter("@ToDate",txt_todate.Text.Trim()),
                                new SqlParameter("@Mode",drop_level.SelectedValue),
                                new SqlParameter("@LoginId",dropusername.SelectedItem.Text)
                };
            DataSet ds_excel = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetConsumerBillPaymentDetils_Admin", warlock);


            if (ds_excel.Tables[0].Rows.Count > 0)
            {
                ExportToExcel(ds_excel.Tables[0]);
            }
            else
            {

                ScriptManager.RegisterStartupScript(this, GetType(), "ShowAlert", "alert('No Transaction Found.')", true);
                return;
            }
        }
    }

    public void ExportToExcel(DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            string filename = "DCR.xls";
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            DataGrid dgGrid = new DataGrid();
            dgGrid.DataSource = dt;
            dgGrid.DataBind();

            //Get the HTML for the control.
            dgGrid.RenderControl(hw);
            //Write the HTML back to the browser.
            //Response.ContentType = application/vnd.ms-excel;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
            this.EnableViewState = false;
            Response.Write(tw.ToString());
            Response.End();
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }
}