using System;
using System.Data;
using System.Configuration;
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.Net;
using System.IO;

/// <summary>
/// Summary description for CheckAgentbalance
/// </summary>

public class B2B
{
    public B2B()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public string CheckAgentBalance(string AgentId,string HURL)
    { 
        SqlDataReader dr1;
        try
        {
            SqlParameter[] puneet = new SqlParameter[]
{
new SqlParameter("@agent_id",AgentId)
};
            dr1 = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "getagentamount_Warlock", puneet);
            if (dr1.Read())
            {              
                   return Convert.ToString(Convert.ToDouble(dr1["availabaleamount"]).ToString("f2"));
            }
            else
                return "0";
        }
        catch (Exception ex)
        {
            return "0";
        }
    }

public string CheckAgentcutoffbal(string AgentId,string HURL)
    {
        SqlDataReader dr2;
        try
        {
            SqlParameter[] puneet1 = new SqlParameter[]
{
new SqlParameter("@agent_id",AgentId)
};
            dr2 = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "getagentcutoff_bal", puneet1);
            if (dr2.Read())
            {
                return Convert.ToString(Convert.ToDouble(dr2["cutoff_amount"]).ToString("f2"));
            }
            else
                return "0";
        }
        catch (Exception ex)
        {
            return "0";
        }
    }

    public int Debit_Agent_Amount_Insertin_ATT(string agentid, decimal requestamt,decimal deducamt,decimal agentcommission, string txnid, string dsid, string Ip, string service, string HURL)
    {
        SqlParameter[] p = new SqlParameter[]
        {
            new SqlParameter("@agent_id",agentid),
             new SqlParameter("@requestedamt",requestamt),
              new SqlParameter("@deductedamt",deducamt),
               new SqlParameter("@agentcommission",agentcommission), 
            new SqlParameter("@tran_id",txnid),
            new SqlParameter("@ds_id",dsid),   new SqlParameter("@systemip",Ip),
            new SqlParameter("@service",service)
        };
        return SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "B2B_update_agent_amount_insertin_ATT", p);
    }

    public int InsertTransactionWithPendingStatus(string WLID, string Ipaddress, string RefNo, string AgentId,  string OperatotId,string MobileNo, decimal Amount, string ServiceVendor, string PortalName, string ServiceName)
    {
        SqlParameter[] InsertWithPendingStatusParam = new SqlParameter[]
        {
            new SqlParameter("@WLID",WLID),
            new SqlParameter("@IPAddress",Ipaddress),
            new SqlParameter("@ReferenceNumber",RefNo),
            new SqlParameter("@AgentID",AgentId),
            new SqlParameter("@operatorID",OperatotId),
             new SqlParameter("@Mobileno",Convert.ToInt32(MobileNo)),
            new SqlParameter("@Amount",Convert.ToDecimal(Amount)),
            new SqlParameter("@ServiceVendor",ServiceVendor),
            new SqlParameter("@PortalName",PortalName),
            new SqlParameter("@SERVICE_NAME",ServiceName),
            new SqlParameter("@ErrorMessage",SqlDbType.Int)
        };
        InsertWithPendingStatusParam[10].Direction = ParameterDirection.Output;
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "Insert_Flight_Transaction_With_Pending_Status", InsertWithPendingStatusParam);
        int result = Convert.ToInt32(InsertWithPendingStatusParam[10].Value);
        return result;
    }

    public int InsertTransactionWithCancelStatus(string WLID, string Ipaddress, string RefNo, string AgentId, string OperatotId, string MobileNo, decimal Amount, string ServiceVendor, string PortalName, string ServiceName)
    {
        SqlParameter[] InsertWithPendingStatusParam = new SqlParameter[]
        {
            new SqlParameter("@WLID",WLID),
            new SqlParameter("@IPAddress",Ipaddress),
            new SqlParameter("@ReferenceNumber",RefNo),
            new SqlParameter("@AgentID",AgentId),
            new SqlParameter("@operatorID",OperatotId),
            new SqlParameter("@Mobileno",MobileNo),
            new SqlParameter("@Amount",Convert.ToDecimal(Amount)),
            new SqlParameter("@ServiceVendor",ServiceVendor),
            new SqlParameter("@PortalName",PortalName),
            new SqlParameter("@SERVICE_NAME",ServiceName),
            new SqlParameter("@ErrorMessage",SqlDbType.Int)
        };
        InsertWithPendingStatusParam[10].Direction = ParameterDirection.Output;
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "Insert_Flight_Transaction_With_Cancel_Status", InsertWithPendingStatusParam);
        int result = Convert.ToInt32(InsertWithPendingStatusParam[10].Value);
        return result;
    }


   public int Credit_Agent_Amount_Insertin_ATT(string WLID, string AgentID, string txnid, string Tranlog)
    {
        try
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WLID",Convert.ToInt64(WLID)),
                new SqlParameter("@refrenceno",txnid),
                  new SqlParameter("@AgentID",AgentID),
                new SqlParameter("@TranLog",Tranlog)
            };
            int a = SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "spAir_updatestatus_fail", param);
            return a;
        }
        catch (Exception ex)
        {
            AirEpointSupport obj = new AirEpointSupport();
            obj.WriteToLog(ex.Message, "StoreProcedure", "UpdateAllBOOKING_InformationRelatedTable__" + WLID + AgentID + txnid + Tranlog);
            return 1;
        }
    }
    public int Credit_Agent_Amount_Insertin_ATT_Flight_Refund(string AgentID, string txnid, string ipaddress, decimal Amount)
    {
        SqlParameter[] param = new SqlParameter[]
            {
                 new SqlParameter("@AgentUserName",AgentID),
                new SqlParameter("@TransactionID",txnid),
                new SqlParameter("@AMOUNT",Amount),                 
                new SqlParameter("@IPAddress",ipaddress),
                 new SqlParameter("@ErrorMessage",SqlDbType.Int)
            };
        param[4].Direction = ParameterDirection.Output;
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_PushBalanceAfterFlightRefund", param);
        int result = Convert.ToInt32(param[4].Value);
        return result;

    }
    public decimal GetCompanyCommission_Perc(int agentid, string HURL)
    {
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "B2B_Air_Get_Intl_Commission", new SqlParameter[] {new SqlParameter("@agentid",agentid) });
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count == 1)
            {
                decimal IntlFlightCOmm = Convert.ToDecimal(ds.Tables[0].Rows[0]["IntlFlightCommP"]);
                decimal MDShare = Convert.ToDecimal(ds.Tables[0].Rows[0]["MDShare"]);
                return (IntlFlightCOmm * MDShare / 100);
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }
    public decimal GetAgentCommission_Perc(int agentid,string HURL)
    {   
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "B2B_Air_Get_Intl_Commission", new SqlParameter[] { new SqlParameter("@agentid", agentid) });
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count == 1)
            {
                decimal IntlFlightCOmm = Convert.ToDecimal(ds.Tables[0].Rows[0]["IntlFlightCommP"]);
                decimal AgentShare = Convert.ToDecimal(ds.Tables[0].Rows[0]["AgentShare"]);
                return (IntlFlightCOmm * AgentShare / 100);
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }

    public decimal Domestic_GetCompanyCommission_Perc(string agentid,string HURL)
    {     
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "B2B_Air_Get_Dom_Commission", new SqlParameter[] { new SqlParameter("@agentid", agentid) });
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count == 1)
            {
               // decimal IntlFlightCOmm = Convert.ToDecimal(ds.Tables[0].Rows[0]["DomFlightCommP"]);
                decimal MDShare = Convert.ToDecimal(ds.Tables[0].Rows[0]["MDShare"]);
                return  MDShare;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }
    public decimal Domestic_GetAgentCommission_Perc(string agentid,string HURL)
    {
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "B2B_Air_Get_Dom_Commission", new SqlParameter[] { new SqlParameter("@agentid", agentid) });
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count == 1)
            {
               // decimal IntlFlightCOmm = Convert.ToDecimal(ds.Tables[0].Rows[0]["DomFlightCommP"]);
                decimal AgentShare = Convert.ToDecimal(ds.Tables[0].Rows[0]["AgentShare"]);
                //return (IntlFlightCOmm * AgentShare / 100);
                return AgentShare;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }
 
    public void SendMessage(string msg, string mobileno)
    {
        try
        {
            string send_url = "http://www.smszone.in/sendsms.asp?page=SendSmsBulk&username=919212088766&password=8417&number=" + mobileno + "&message=" + msg;
            Uri uri = new Uri(send_url);
            string data = "field-keywords=ASP.NET 2.0";
            if (uri.Scheme == Uri.UriSchemeHttp)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
                request.Method = WebRequestMethods.Http.Post;
                request.ContentLength = data.Length;
                request.ContentType = "application/x-www-form-urlencoded";
                StreamWriter writer = new StreamWriter(request.GetRequestStream());
                writer.Write(data);
                writer.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            }
        }
        catch
        {
        }
    }
}
