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 SMSPanel_CustPaybillReq : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string consumerid = ""; string ConsumerMobile = "";
            string stringquery = Convert.ToString(Request.Form["message"]);
            string from = Convert.ToString(Request.Form["number"]);
            string keyword = Convert.ToString(Request.Form["keyword"]);
            InsertSMSLog(from, keyword, stringquery);

            ConsumerMobile = from.Substring(from.Length - Math.Min(10, from.Length));
            string msg = stringquery;
            msg = msg.Replace('+', ' ');
            string[] sms = msg.Split(' ');
            if (sms.Length >= 1)
            {
               consumerid = Convert.ToString(sms[0]);
                string ServiceRequestId = InsertConsumerBillRequest(from,consumerid, stringquery);
                if(ServiceRequestId!="")
                {
                    //SendSMS to Consumer with Service Request ID
                }
            }

        }
    }
    public void InsertSMSLog(string SMSFrom,string Keyword,string Msg)
    {
        //usp_InsertConsumerBillCollectionRequest
        SqlParameter[] warlock ={
                                            new SqlParameter("@SMSFrom",SMSFrom),
                                            new SqlParameter("@Msg",Msg),
                                            new SqlParameter("@Keyword",Keyword)
                                       };
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_InsertSMSLog", warlock);
    }

    public string InsertConsumerBillRequest(string SMSFrom, string ConsumerID, string Msg)
    {
        try
        {
            SqlParameter[] warlock ={
                                            new SqlParameter("@ConsumerID",ConsumerID),
                                            new SqlParameter("@SMSFrom",SMSFrom),
                                            new SqlParameter("@Msg",Msg),
                                            new SqlParameter("@RequestID",SqlDbType.VarChar,50)
                                       };
            warlock[3].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_InsertConsumerBillCollectionRequest", warlock);
            return Convert.ToString(warlock[3].Value);
        }
        catch(Exception ex)
        {
            return "";
        }
    }
}