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.IO;

public partial class MyReports : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            gridview_userreport.Attributes.Add("bordercolor", "#e3e3e3");
            if (!IsPostBack)
            {
                txt_from.Text = DateTime.Now.AddDays(-1).ToString("dd/MM/yyyy");
                txt_to.Text = DateTime.Now.ToString("dd/MM/yyyy");
                txt_from.Attributes.Add("readonly", "readonly");
                txt_to.Attributes.Add("readonly", "readonly");
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }

    DataSet ds;
    public void bind()
    {
        DateTime from = new DateTime(Convert.ToInt16(txt_from.Text.Split('/')[2]), Convert.ToInt16(txt_from.Text.Split('/')[1]), Convert.ToInt16(txt_from.Text.Split('/')[0]));
        DateTime to = new DateTime(Convert.ToInt16(txt_to.Text.Split('/')[2]), Convert.ToInt16(txt_to.Text.Split('/')[1]), Convert.ToInt16(txt_to.Text.Split('/')[0]));
        if (to > Convert.ToDateTime(DateTime.Now.ToShortDateString()))
        {
            RegisterClientScriptBlock("alert", "<script>alert('To date can not be later than current date.');</script>");
        }
        else
        {

            if (from <= to)
            {
                SqlParameter[] param = new SqlParameter[]
         {
             new SqlParameter("@from",from),
             new SqlParameter("@to",to),
            
             new SqlParameter("@userid",  Convert.ToInt32(Session["LOGINID"]))
         
         };
                ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "transaction_report_User", param);
                if (ds.Tables.Count > 0)
                {
                    gridview_userreport.DataSource = ds.Tables[0];
                    gridview_userreport.DataBind();

                }
                else
                {
                    RegisterClientScriptBlock("alert", "<script>alert('Technical error occured.Please try again.');</script>");
                }
            }
            else
            {
                RegisterClientScriptBlock("alert", "<script>alert('Form date can be  later than TO date.');</script>");
            }
        }
    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        try
        {
            bind();
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    protected void grdview_service_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
   
       
    }
 

    protected void grdview_service_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName != "Page")
        {

            

            string data=Convert.ToString( e.CommandArgument);
            string partnerrefid = data.Split(',')[0];

            string LaviFlightType = data.Split(',')[1];

            Session["partnerrefidtrip"] = partnerrefid;
            if (LaviFlightType == "DOM")
            {

                Response.Redirect("Transaction.aspx", false);
             }
            else if(LaviFlightType=="INT")
            {
                Response.Redirect("InternationalTransaction.aspx", false);
             }



        }
    }
    


    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }

    protected void gridview_userreport_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    }

    protected void gridview_userreport_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gridview_userreport.PageIndex = e.NewPageIndex;
            bind();
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    protected void gridview_userreport_RowCreated(object sender, GridViewRowEventArgs e)
    {

        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
              
               // e.Row.Cells[3].Attributes.Add("class", "text");
                //________
                string vvv = ((Label)e.Row.FindControl("lbloperator")).Text;
                int i = e.Row.RowIndex + (gridview_userreport.PageSize * (gridview_userreport.PageIndex));
                string service = Convert.ToString(ds.Tables[0].Rows[i]["ServicePrefix"]);
                string Origin = Convert.ToString(ds.Tables[0].Rows[i]["Origin"]);
                string Destination = Convert.ToString(ds.Tables[0].Rows[i]["Destination"]);
                string travelMode = Convert.ToString(ds.Tables[0].Rows[i]["travel_Mode"]);
                string paymentmethod=Convert.ToString(ds.Tables[0].Rows[i]["PaymentMethod"]);
                      string Passname="";

                      string s = Convert.ToString(ds.Tables[0].Rows[i]["count"]);
                      if (s != "")
                      {
                          if (Convert.ToInt32(ds.Tables[0].Rows[i]["count"]) > 1)
                          {

                              Passname = Convert.ToString(ds.Tables[0].Rows[i]["AdultTitle"]) + " " + Convert.ToString(ds.Tables[0].Rows[i]["AdultFirstName"])
                                  + " " + Convert.ToString(ds.Tables[0].Rows[i]["AdultLastName"]) + " + " + Convert.ToString(Convert.ToInt32(ds.Tables[0].Rows[i]["count"]) - 1);
                          }
                          else
                          {
                              Passname = Convert.ToString(ds.Tables[0].Rows[i]["AdultTitle"]) + " " + Convert.ToString(ds.Tables[0].Rows[i]["AdultFirstName"])
                            + " " + Convert.ToString(ds.Tables[0].Rows[i]["AdultLastName"]);
                          }
                      }


                string BookingStatus = Convert.ToString(ds.Tables[0].Rows[i]["TicketBooking_status"]);

                

                if (service == "INAIR_BOOKING" || service == "DOMBOOKING")
                {

                    ((Label)e.Row.FindControl("lbloperator")).Text = Origin + "-" + Destination + "(" + travelMode + ")";
                

                    if (BookingStatus == "SUCCESS" || BookingStatus == "Pending")
                    {
                        ((Label)e.Row.FindControl("lblservice")).Text = "Booking";
                
                      ((Label)e.Row.FindControl("lbltxnstatus")).Text = BookingStatus;
                      ((Label)e.Row.FindControl("lbl_passenger")).Text = Passname;
                      if (BookingStatus == "SUCCESS")
                      {
                          ((Label)e.Row.FindControl("lbltxnstatus")).Text = "Success";  }
                          ((LinkButton)e.Row.FindControl("lblpartnerRefId")).Visible = true;
                          ((Label)e.Row.FindControl("lbl_partnr")).Visible = false;

                    
                         
                        if(paymentmethod=="PG")
                        {
                            ((Label)e.Row.FindControl("lbl_Action")).Text = "--";
                        }
                        else
                        {
                            ((Label)e.Row.FindControl("lbl_Action")).Text = "Debit";
                        }
                    }
                    else if (BookingStatus == "Cancelled")
                    {
                        ((Label)e.Row.FindControl("lblservice")).Text = "Refund";
                        ((Label)e.Row.FindControl("lbl_passenger")).Text = "E - Wallet";
                       ((Label)e.Row.FindControl("lbltxnstatus")).Text = "Error";
                       ((Label)e.Row.FindControl("lbl_Action")).Text = "Credit";

                       ((LinkButton)e.Row.FindControl("lblpartnerRefId")).Visible = false;
                       ((Label)e.Row.FindControl("lbl_partnr")).Visible = true;

                    }
                }
                else if (service == "AIRCANCEL") 
                {
                    ((Label)e.Row.FindControl("lblservice")).Text = "Cancellation";
                    ((Label)e.Row.FindControl("lbloperator")).Text = "Cancellation in Wallet";
                    ((Label)e.Row.FindControl("lbl_passenger")).Text = Passname;
                    ((Label)e.Row.FindControl("lbltxnstatus")).Text = "Success";
                    ((Label)e.Row.FindControl("lbl_Action")).Text = "Credit";

                    ((LinkButton)e.Row.FindControl("lblpartnerRefId")).Visible = true;
                    ((Label)e.Row.FindControl("lbl_partnr")).Visible = false;

                }
                else if (service == "WALLETRECH")
                {
                    ((Label)e.Row.FindControl("lblservice")).Text = "Wallet Top Up";
                    ((Label)e.Row.FindControl("lbloperator")).Text = "Credit in Wallet";
                    ((Label)e.Row.FindControl("lbl_passenger")).Text = " --";
                    ((Label)e.Row.FindControl("lbltxnstatus")).Text = "Success";
                    ((Label)e.Row.FindControl("lbl_Action")).Text = "Credit";
                    ((LinkButton)e.Row.FindControl("lblpartnerRefId")).Visible = false;
                    ((Label)e.Row.FindControl("lbl_partnr")).Visible = true;

                }
            }
        }
        catch (Exception ex) {
            throw (ex);
        }
    }
    protected void btn_dwnlod_Click(object sender, EventArgs e)
    {
        try
        {
            gridview_userreport.AllowPaging = false;

            bind();
            if (gridview_userreport.Rows.Count > 0)
            {
                string datestyle = @"<style> .nf{mso-number-format:\@;}</style>";
                foreach (GridViewRow oItem in gridview_userreport.Rows)
                {
                    oItem.Cells[3].Attributes.Add("class", "nf");
                }
                gridview_userreport.Columns[9].Visible = true;
                gridview_userreport.Columns[10].Visible = true;

                StringWriter tw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(tw);
                Response.Clear();
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader(
                    "content-disposition",
                    "attachment;filename=TransationReport.xls");
                gridview_userreport.RenderControl(hw);
                Response.Write(datestyle);
                Response.Write(tw.ToString());
                Response.End();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
        }
    
}
