using System;
using System.Data;
using System.Resources;
using System.Web.UI;
using System.Threading;
using System.Globalization;

public partial class Masters_SMS_Configuration : Base.BasePage
{
    #region "Variable Declaration"
    SMSNotification objSMS = new SMSNotification();
    ResourceManager rm;
    CultureInfo ci;
    int EmployeeID = 0;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objcommon = new cls_Common();
    #endregion
    protected override void Page_Load(object sender, EventArgs e)
    {
        //session checking
        base.Page_Load(sender, e);
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        if (Session["Session_ActiveLanguage"] == null)
        {
            string strScript1 = "<script language='javascript' type='text/javascript'>";
            strScript1 += "alert('" + rm.GetString("Alert_SessionExpired", ci) + "');self.close();";
            strScript1 += " </script>";
            this.RegisterStartupScript("closeScript1", strScript1);
            return;
        }
       
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_EnterServiceURLName", ci) + "','" + rm.GetString("Alert_EnterDestinationKey", ci) + "','" + rm.GetString("Alert_EnterMessageKey", ci) + "','" + rm.GetString("Alert_EnterPhoneNumber", ci) + "','" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "');"; 
         strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        //Set UserRights
        if (!IsPostBack)
            objcommon.menu("Configuration", "243");
        UserRights();
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        EmployeeID = Convert.ToInt32(Convert.ToString(Session["EmpAutoID"]));
        if (!IsPostBack)
        {
            BindGatewayDetails();
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Url.Text = rm.GetString("ServiceURL", ci);
        lbl_Des.Text = rm.GetString("DestinationKey", ci);
        lblmsgKey.Text = rm.GetString("MessageKey", ci);
        lblphNO.Text = rm.GetString("EmployeeMaster_PhoneNumber", ci);
        lbl_Header.Text = rm.GetString("GatewayHeader", ci);
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
    }
    private void UserRights()
    {
      
        ds_Checkuser = objCheckUser.checkRights(243, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
            {
                this.btn_save.Visible = true;
            }
            else
            {
                this.btn_save.Visible = false;
            }
        }
        else
        {
           Response.Redirect("MasterLandingPage.aspx");
        }
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        string strScript = "";
        if (txt_Gateway.Text == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_EnterServiceURLName", ci) + "');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (txtDesignation.Text == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_EntertheDestinationKey", ci) + "');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (txtMsgkey.Text == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_EntertheMessageKey", ci) + "');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (txtphNo.Text == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_EnterthePhoneNumber", ci) + "');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        objSMS.UpdatingGatewayDetails(Convert.ToString(txt_Gateway.Text), Convert.ToString(txtDesignation.Text), Convert.ToString(txtMsgkey.Text), Convert.ToString(txtphNo.Text), Convert.ToInt16(EmployeeID));
        Session["GatewayDetails"] = "";
        BindGatewayDetails();
        strScript = strScript + "<script language='javascript'>";
        strScript = strScript + "alert('" + rm.GetString("Alert_SMSGatewayConfigurationSavedSuccessfully", ci) + "');";
        strScript = strScript + "</script>";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
    }
    private void BindGatewayDetails()
    {
        DataSet dsGateway = new DataSet();
        if (Convert.ToString(Session["GatewayDetails"]) == "")
        {
            dsGateway = objSMS.SelectGatewayDetails();
        }
        else
        {
            dsGateway = (DataSet)Session["GatewayDetails"];
        }
        if (dsGateway.Tables.Count > 0)
        {
            if (dsGateway.Tables[0].Rows.Count > 0)
            {
                txt_Gateway.Text = Convert.ToString(dsGateway.Tables[0].Rows[0]["ServiceUrl"]);
                txtDesignation.Text = Convert.ToString(dsGateway.Tables[0].Rows[0]["DestinationKey"]);
                txtMsgkey.Text = Convert.ToString(dsGateway.Tables[0].Rows[0]["MessageKey"]);
                txtphNo.Text = Convert.ToString(dsGateway.Tables[0].Rows[0]["PhNo"]);
            }
        }
        Session["GatewayDetails"] = dsGateway;
    }
}
