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

public partial class Masters_SMSReceivers : Base.BasePage
{
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    userRightsEventData objUserRights = new userRightsEventData();
    SMSNotification objSMS = new SMSNotification();
    string source_Language;
    string strScript = string.Empty;
    bool chkchecked = false;
    #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;
        LoadStrings(ci);
        if (!IsPostBack)
        {
            BindUserGroup();
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lblUsergroup.Text = rm.GetString("AddGroups_GroupName", ci) + " :";
        btn_Ok.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Ok", ci);
        cbAll.Text = rm.GetString("cbAll", ci);
    }
    private void BindUserGroup()
    {
        DataSet ds = new DataSet();
        if (Convert.ToString(Session["ExistingTemplate"]) == "")
        {
            ds = objSMS.selectUserGroupForSMS(Convert.ToInt32(Request.QueryString["TemplateId"]));
        }
        else
        {
            ds = (DataSet)Session["ExistingTemplate"];
        }
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                chklstuserGroup.DataSource = ds;
                chklstuserGroup.DataValueField = "groupId";
                chklstuserGroup.DataTextField = "groupName";
                chklstuserGroup.DataBind();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ListItem thisIndex = chklstuserGroup.Items.FindByValue(Convert.ToString(ds.Tables[0].Rows[i]["groupId"]));
                    if (thisIndex != null)
                    {
                        if (Convert.ToString(ds.Tables[0].Rows[i]["status"]) == "1")
                        {
                            thisIndex.Selected = true;
                        }
                    }
                }
            }
        }
        Session["ExistingTemplate"] = ds;
    }
    private void ToCheckExistingUsergroup(int TemplateID)
    {
        DataSet dsExistingUsergrp = new DataSet();
        if (Convert.ToString(Session["ExistingTemplate"]) == "")
        {
            dsExistingUsergrp = objSMS.SelectReceiversDetails(TemplateID);
        }
        else
        {
            dsExistingUsergrp = (DataSet)Session["ExistingTemplate"];
        }
        if (dsExistingUsergrp != null)
        {
            if (dsExistingUsergrp.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsExistingUsergrp.Tables[0].Rows.Count; i++)
                {
                    ListItem thisIndex = chklstuserGroup.Items.FindByValue(Convert.ToString(dsExistingUsergrp.Tables[0].Rows[i]["UsergroupId"]));
                    if (thisIndex != null)
                    {
                        if (Convert.ToString(dsExistingUsergrp.Tables[0].Rows[i]["Status"]) == "1")
                        {
                            thisIndex.Selected = true;
                        }
                    }
                }
                if (dsExistingUsergrp.Tables[0].Rows.Count == chklstuserGroup.Items.Count)
                {
                    cbAll.Checked = true;
                }
                Session["ExistingTemplate"] = dsExistingUsergrp;
            }
            else
            {
                if (Convert.ToString(Session["hdnfldUsergroup"]) != string.Empty)
                {
                    string strhdnParameters = Convert.ToString(Session["hdnfldUsergroup"]);
                    string[] strArray = new string[50];
                    string[] strArray1 = new string[50];
                    strArray = strhdnParameters.Split(';');
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (chklstuserGroup != null && chklstuserGroup.Items.Count > 0)
                        {
                            for (int j = 0; j < chklstuserGroup.Items.Count; j++)
                            {
                                if (chklstuserGroup.Items[j].Value == strArray[i])
                                {
                                    chklstuserGroup.Items[j].Selected = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    protected void btn_Ok_Click(object sender, EventArgs e)
    {
        string strUsergrpID = "";
        strUsergrpID = getSelectedGroup();
        Session["hdnfldUsergroup"] = strUsergrpID;
        string strScript = "<script language=\"javascript\">";
        strScript = strScript + "{window.top.parent.document.getElementById('ctl00_BodyContentPH_hdnFilters').value='" + strUsergrpID + "';}";
        strScript = strScript + "{window.top.parent.document.getElementById('ctl00_BodyContentPH_txtReceivers').value='" + Convert.ToString(Session["strUsergrpName"]) + "';}";
        strScript = strScript + "parent.$('#divReceivers').modal('hide');";
        strScript = strScript + "</script>";
        Response.Write(strScript);
    }
    public string getSelectedGroup()
    {
        string strUsergrpID = "";
        string strUsergrpName = "";
        DataSet ds = new DataSet();
        if (Convert.ToString(Session["ExistingTemplate"]) != "")
        {
            ds = (DataSet)Session["ExistingTemplate"];
        }
        if (chklstuserGroup != null)
        {
            if (chklstuserGroup.Items.Count > 0)
            {
                for (int i = 0; i < chklstuserGroup.Items.Count; i++)
                {
                    if (chklstuserGroup.Items[i].Selected == true)
                    {
                        if (strUsergrpID == string.Empty)
                        {
                            strUsergrpID = chklstuserGroup.Items[i].Value;
                            strUsergrpName = Convert.ToString(chklstuserGroup.Items[i].Text);
                        }
                        else
                        {
                            strUsergrpID = strUsergrpID + ";" + chklstuserGroup.Items[i].Value;
                            strUsergrpName = strUsergrpName + ";" + Convert.ToString(chklstuserGroup.Items[i].Text);
                        }
                        if (ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                                {
                                    if (chklstuserGroup.Items[i].Value == Convert.ToString(ds.Tables[0].Rows[j][0]))
                                    {
                                        ds.Tables[0].Rows[j][2] = 1;
                                        break;
                                    }
                                }                                
                            }
                        }
                    }
                    else
                    {
                        if (ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                                {
                                    if (chklstuserGroup.Items[i].Value == Convert.ToString(ds.Tables[0].Rows[j][0]))
                                    {
                                        ds.Tables[0].Rows[j][2] = 0;
                                        break;
                                    }
                                }                                
                            }
                        }
                    }
                }
            }
        }
        ds.Tables[0].AcceptChanges();
        Session["ExistingTemplate"] = ds;
        Session["strUsergrpName"] = strUsergrpName;
        return strUsergrpID;
    }
}
