using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Masters_SMS_TemplateList : Base.BasePage
{
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    cls_Common objCommon = new cls_Common();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    SMSNotification objsms = new SMSNotification();
    DataSet ds_Checkuser = new DataSet();
    DataSet ds_Templates = new DataSet();
    string gstrUserGroupName = string.Empty;
    int EmployeeID = 0;
    string source_Language;
    string strScript = string.Empty;
    #endregion
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        // LoadStrings Function fills the Label from the Resource File       
        //Set UserRights
        if (!IsPostBack)
        {
            objCommon.menu("Configuration", "244");
        }
        UserRights();
        LoadStrings(ci);
        EmployeeID = Convert.ToInt32(Convert.ToString(Session["EmpAutoID"]));
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        if (gstrUserGroupName != "SuperAdmin")
        {
            btn_Enable.Visible = false;
            btn_Disable.Visible = false;
        }
        BindTemplates();
    }
    private void BindTemplates()
    {
        DataSet ds = new DataSet();
        if (Convert.ToString(ViewState["SMSTemplate"]) == "")
        {
            ds = objsms.SelectTemplateDetails(0);
        }
        else
        {
            ds = (DataSet)ViewState["SMSTemplate"];
        }
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                grdSmstemplate.DataSource = ds;
                grdSmstemplate.DataBind();
            }
        }
        ViewState["SMSTemplate"] = ds;
    }
    private void LoadStrings(CultureInfo ci)
    {
        btn_Enable.Text = "&nbsp;&nbsp;" + rm.GetString("SMSTemplateList_btnEnable", ci);
        btn_Disable.Text = "&nbsp;&nbsp;" + rm.GetString("SMSTemplateList_btnDisable", ci);
        grdSmstemplate.Columns[2].Caption = rm.GetString("SMSTemplateList_TemplateName", ci);
        grdSmstemplate.Columns[3].Caption = rm.GetString("SMSTemplateList_Message", ci);
        grdSmstemplate.Columns[4].Caption = rm.GetString("email_Status", ci);
        grdSmstemplate.Columns[5].Caption = rm.GetString("SMSTemplateList_LastModified", ci);
        lbl_Header.Text = rm.GetString("SMSTemplateList_Header", ci);
    }
    private void UserRights()
    {
        //Todo: Need to change ID value in below
        ds_Checkuser = objCheckUser.checkRights(244, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.btn_Disable.Visible = true;
                this.btn_Enable.Visible = true;
            }
            else
            {
                this.btn_Disable.Visible = false;
                this.btn_Enable.Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    protected void btn_Enable_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            objsms.UpdatingSMSTemplateStaus(Convert.ToString(hdnSelectRow.Value), "Enable", EmployeeID);
            ViewState["SMSTemplate"] = "";
            string str = "alert('" + rm.GetString("Alert_SMSTemplateenabledsuccessfully", ci) + "');location.replace('SMS_TemplateList.aspx');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_Norecordstoenable", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void btn_Disable_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            objsms.UpdatingSMSTemplateStaus(Convert.ToString(hdnSelectRow.Value), "Disable", EmployeeID);
            ViewState["SMSTemplate"] = "";
            string str = "alert('" + rm.GetString("Alert_SMSTemplatedisabledsuccessfully", ci) + "');location.replace('SMS_TemplateList.aspx');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_Norecordstodisable", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int intTemplateId = Convert.ToInt32(btn.CommandName);
        Response.Redirect("SMS_TemplateModify.aspx?TemplateId=" + intTemplateId);
    }
    protected void grdSmstemplate_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridViewTableDataCellEventArgs e)
    {
        Label DeletedRecord = (Label)grdSmstemplate.FindRowCellTemplateControl(e.VisibleIndex, null, "lblStatus");
        if (Convert.ToString(DeletedRecord.Text) == "Disabled")
        {
            DeletedRecord.ForeColor = System.Drawing.Color.Red;
        }
    }
    protected void Page_UnLoad(object sender, EventArgs e)
    {
    }
    protected void grdSmstemplate_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.GridViewRowType.Data)
        {
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            Label lblStatus = (Label)grdSmstemplate.FindRowCellTemplateControl(e.VisibleIndex, null, "lblStatus");
            if (lblStatus != null)
            {
                if (lblStatus.Text == "Enabled")
                {
                    lblStatus.Text = "Enabled";
                    //Preview.Enabled = true;
                    if (hdnEnabledData.Value == string.Empty)
                    {
                        hdnEnabledData.Value = e.GetValue("Templateid").ToString();
                    }
                    else
                    {
                        if (hdnEnabledData.Value.Contains(e.GetValue("Templateid").ToString()) == false)
                        {
                            hdnEnabledData.Value = hdnEnabledData.Value + "," + e.GetValue("Templateid").ToString();
                        }
                    }
                }
                else if (lblStatus.Text == "Disabled")
                {
                    if (hdnDisabledData.Value == string.Empty)
                    {
                        hdnDisabledData.Value = e.GetValue("Templateid").ToString();
                    }
                    else
                    {
                        if (hdnDisabledData.Value.Contains(e.GetValue("Templateid").ToString()) == false)
                        {
                            hdnDisabledData.Value = hdnDisabledData.Value + "," + e.GetValue("Templateid").ToString();
                        }
                    }
                }
            }
        }
    }
}
