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 IncomeReport : System.Web.UI.Page
{
    decimal ColumnAmount;
    decimal ColumnMargin;
    decimal ColumnNetAmount;
    static string OperatorName;
    string date = DateTime.Now.ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERTYPE"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            if (!IsPostBack)
            {
                BindDataAccServices();
            }
            btndownload.Visible = false;
            DivGvTransactionReport.Visible = false;
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    private void BindDataAccServices()
    {
        string  todaysdate = DateTime.Now.ToShortDateString();
        try
        {
            if (ddlSelectService.SelectedItem.Text.Equals("Select") || txtFromDate.Text == "" || txtToDate.Text == "")
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@ServiceType",ddlSelectService.SelectedItem.Text),
                    new SqlParameter("@Fromdate",todaysdate),
                    new SqlParameter("@Todate",todaysdate)
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_SHOWTRANSACTIONSOPERATORWISE", param);
                if (ds.Tables.Count > 0)
                {

                    gvIncomeReport.DataSource = ds;
                    gvIncomeReport.DataBind();
                    lblerror.Visible = false;
                }
                else
                {
                    //lblerror.Visible = false;
                    //lblerror.Text = "No transaction available";
                    //lblerror.ForeColor = Color.Red;
                }
            }
            else
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@ServiceType",ddlSelectService.SelectedItem.Text),
                    new SqlParameter("@Fromdate",txtFromDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_SHOWTRANSACTIONSOPERATORWISE", param);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    gvIncomeReport.Visible = true;
                    gvIncomeReport.DataSource = ds;
                    gvIncomeReport.DataBind();
                    lblerror.Visible = false;
                    btndownload.Visible =true;
                    GvTransactionReport.Visible = false;
                }
                else
                {
                    lblerror.Visible = true;
                    lblerror.Text = "No transaction available";
                    lblerror.ForeColor = Color.Red;
                    gvIncomeReport.Visible = false;
                    GvTransactionReport.Visible = false;
                }
            }

        }
        catch (Exception ex)
        {
            lblerror.Text = "error=" + ex;
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToDateTime(txtFromDate.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(txtFromDate.Text).Date > Convert.ToDateTime(txtToDate.Text).Date)
            {
                lblerror.Text = "From date must be less then To date";
                lblerror.ForeColor = Color.Red;
            }
            else
            {
                string datefrom = txtFromDate.Text.Trim();
                string dateto = txtToDate.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)))
                {
                    BindDataAccServices();

                }
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    protected void gvIncomeReport_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ColumnAmount += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "RequestAmount"));
                ColumnMargin += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Margin"));
                ColumnNetAmount = (ColumnAmount - ColumnMargin);
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "Total Rs:";
                // for the Footer, display the running totals
                e.Row.Cells[4].Text = ColumnAmount.ToString();
                e.Row.Cells[5].Text = ColumnMargin.ToString();
                e.Row.Cells[6].Text = ColumnNetAmount.ToString();
                e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Left;
                e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Left;
                e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Left;
                e.Row.Font.Bold = true;
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = "error=" + ex;
        }
    }
    protected void btnNoOfTxn_Command(object sender, CommandEventArgs e)
    {
        try
        {
            LinkButton btn = (LinkButton)sender;
            GridViewRow rowitem = (GridViewRow)btn.Parent.Parent;
            Label lblOperator;

            lblOperator = (Label)rowitem.FindControl("lblOperator");
            ViewState["lblOperator"] = lblOperator.Text;
            DivGvTransactionReport.Visible = true;
            DIV_gvIncomeReport.Visible = false;
            GvTransactionReport.Visible = true;

            GetDataAccOperators();
            //Response.Write("<script>window.open(ShowResult.aspx?operatorname=" + ViewState["lblOperator"] + "&ToDate=" + txtToDate.Text.Trim() + "&FrmDate=" + txtFromDate.Text.Trim() + "&ServiceName=" + ddlSelectService.SelectedItem.Text + "&Loginid=" + Session["LOGINID"] +");</script>");
            //Response.Write("<script>window.open(Test.aspx);</script>");
            //Response.Write("<script>alert('Data not Enter')</script>");
            //Response.Redirect("ShowResult.aspx?operatorname=" + ViewState["lblOperator"] + "&ToDate=" + txtToDate.Text.Trim() + "&FrmDate=" + txtFromDate.Text.Trim() + "&ServiceName=" + ddlSelectService.SelectedItem.Text + "&Loginid=" + Session["LOGINID"] + "");
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
                
    }
    private void GetDataAccOperators()
    {
        try
        {
            SqlParameter[] getdataaccessopertor = new SqlParameter[]
            {
                new SqlParameter("@operator",ViewState["lblOperator"]),
                new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                new SqlParameter("@ServiceType",ddlSelectService.SelectedItem.Text),
                new SqlParameter("@Fromdate",txtFromDate.Text.Trim()),
                new SqlParameter("@Todate",txtToDate.Text.Trim())

            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTION_REPORT_OPERATORWISE", getdataaccessopertor);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GvTransactionReport.DataSource = ds;
                GvTransactionReport.DataBind();
                lblerror.Visible = false;
            }
            else
            {
                lblerror.Visible = true;
                lblerror.Text = "No transaction available";
                lblerror.ForeColor = Color.Red;
            }
        }
        catch (Exception ev)
        {
            lblerror.Text =""+ ev;
        }
    }
    protected void btndownload_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@ServiceType",ddlSelectService.SelectedItem.Text),
                    new SqlParameter("@Fromdate",txtFromDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_SHOWTRANSACTIONSOPERATORWISE", param);
            if (ds.Tables[0].Rows.Count != 0)
            {
                gvIncomeReport.Visible = true;
                gvIncomeReport.DataSource = ds;
                gvIncomeReport.DataBind();
                lblerror.Visible = false;
                Session["gvIncomeReport"] = ds;
                DataTable dtEmployee = ds.Tables["Table"].Copy();
                int[] iColumns = { 0, 1, 2, 3, 4, 5, };
                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;
                gvIncomeReport.Visible = false;
            }
        }
        catch (Exception ev)
        {
            lblerror.Text = "error=" + ev;
            throw(ev);
        }
    }

    
}
