using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Agent_SenderRegistration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblmsg.Text = "Sender not found. Retry with different sender id Or Register Sender";
            txtSenderNo.Text = Convert.ToString(Session["sendernumber"]);
        }
    }
    protected void btnReg_Click(object sender, EventArgs e)
    {
        string Result = "";
        if (txtSenderNo.Text.Trim() != "" || txtSenderName.Text.Trim() != "" || txtAddress.Text.Trim() != "" || txtpincode.Text.Trim() != "")
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.softsolutionzone.in/resources/SKIPDMR/RegisterSender");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = new JavaScriptSerializer().Serialize(new
                {
                    Key = "7ef37a4185480cebda9430277939c47ce93b4183",
                    AgentID = "3890",
                    SenderMobile = txtSenderNo.Text.Trim(),
                    SenderName = txtSenderName.Text.Trim(),
                    Address = txtAddress.Text,
                    Pincode = txtpincode.Text.Trim(),
                    Surname = txtSenderSurname.Text.Trim()
                });
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                Result = streamReader.ReadToEnd();
            }
            if (Result != "")
            {
                JObject jsonResponse = JObject.Parse(Result);
                if (jsonResponse["statuscode"].ToString() == "TXN")
                {
                    string message = "You are successfully registered with us.";
                    string url = "MoneyTransfer.aspx";
                    string script = "window.onload = function(){ alert('";
                    script += message;
                    script += "');";
                    script += "window.location = '";
                    script += url;
                    script += "'; }";
                    ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);

                }
                else
                {
                    lblmsg.Text = "Sender Registration process failed. Please try after some time.";
                }

            }
            else
            {
                lblmsg.Text = "Sender Registration process failed. Please try after some time.";
            }
        }
        else
        {
            lblmsg.Text = "Please enter all mandatory fields.";
        }
    }
}