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_TemplateModify : 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;
    int TemplateID;
    string Status = "";
    #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"]);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_ChooseSMSReceiver", ci) + "','" + rm.GetString("Alert_SelectNotificationtriggeraftervalue", ci) + "','" + rm.GetString("Alert_EntervalidNotificationtriggeraftervalue", ci) + "','" + rm.GetString("Alert_EntervalidNotificationtriggereveryvalue", ci) + "','" + rm.GetString("Alert_EntervalidLessHoursLimitation", ci) + "','" + rm.GetString("Alert_SelectLessHoursLimitation", ci) + "','" + rm.GetString("Alert_EnterNumberofdaysfieldvalue", ci) + "','" + rm.GetString("Alert_EntervalidNumberinNumberofdays", ci) + "','" + rm.GetString("Alert_EnterMessage", ci) + "','" + rm.GetString("Alert_SelectTags", ci) + "','" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "','" + rm.GetString("Alert_SelectNotificationtriggereveryvalue", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        //Set UserRights
        if (!IsPostBack)
            objCommon.menu("Configuration", "244");
        UserRights();
        // LoadStrings Function fills the Label from the Resource File
        LoadStrings(ci);
        EmployeeID = Convert.ToInt32(Convert.ToString(Session["EmpAutoID"]));
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        if (!IsPostBack)
        {
            TemplateID = Convert.ToInt32(Request.QueryString["TemplateId"]);
            bindTemplateDetails(TemplateID);
        }
        btn_AddReceivers.OnClientClick = "return navi('SMSReceivers.aspx?TemplateID=" + Convert.ToInt32(Request.QueryString["TemplateId"]) + "')";
    }
    protected void btnChooseFilters_Click(object sender, EventArgs e)
    {
        string strMsg = "";
        strMsg = "<script language='javascript'>";
        if (txtTemplateName.Text == "Late Entry" || txtTemplateName.Text == "Early Exit")
        {
            string reportName = txtTemplateName.Text == "Late Entry" ? "LateEntry" : "EarlyExit";
            strMsg = strMsg + "openURL('../Reports/TA_UI_Report_LateInstance.aspx?Source=SMSTemplate&TemplateName=" + reportName + "&TemplateId=" + Convert.ToInt32(Request.QueryString["TemplateId"]) + "');";
        }
        else if (txtTemplateName.Text == "Less Hours Worked")
        {
            strMsg = strMsg + "openURL('../Reports/TA_UI_Report_LessHoursWorked.aspx?Source=SMSTemplate&TemplateName=LessHours&TemplateId=" + Convert.ToInt32(Request.QueryString["TemplateId"]) + "');";
        }
        else if (txtTemplateName.Text == "Absent - Single Day" || txtTemplateName.Text == "Continuous Absent")
        {
            strMsg = strMsg + "openURL('../Reports/TA_UI_Report_Absentes.aspx?Source=SMSTemplate&TemplateName=AbsentSingle&TemplateId=" + Convert.ToInt32(Request.QueryString["TemplateId"]) + "');";
        }
        strMsg = strMsg + "</script>";
        Page.RegisterStartupScript("open report window", strMsg);
        return;
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["hdnfldUsergroup"] = "";
        Session["strUsergrpName"] = "";
        Session["ExistingTemplate"] = "";
        Session["EMailParentId"] = "";
        Session["hdnSMSFilters"] = "";
        Response.Redirect("SMS_TemplateList.aspx");
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        string strScript = "";
        if (txtTemplateName.Text == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_Templatenameshouldnotbeempty", ci) + "');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            return;
        }
        else if (txtReceivers.Text == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_Enterthereceivers", ci) + "');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            return;
        }
        else if (txtTemplateName.Text == "Late Entry" || txtTemplateName.Text == "Early Exit" || txtTemplateName.Text == "Less Hours Worked" || txtTemplateName.Text == "Continuous Absent" || txtTemplateName.Text == "Absent - Single Day")
        {
            if (txtlateNotification.Text == "")
            {
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("Alert_EntertheNotificationtriggeraftervalue", ci) + "');";
                strScript = strScript + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                return;
            }
        }
        else if (txtTemplateName.Text == "Late Entry")
        {
            if (txtContinuousLateEntry.Text == "")
            {
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("Alert_EntertheNotificationtriggereveryvalue", ci) + "');";
                strScript = strScript + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                return;
            }
        }
        else if (txtTemplateName.Text == "Less Hours Worked")
        {
            if (txtlessHoursLimit.Text == "")
            {
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("Alert_EntertheLessHoursLimitation", ci) + "');";
                strScript = strScript + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                return;
            }
        }
        else if (txtTemplateName.Text == "Continuous Absent")
        {
            if (txtContinuousabsent.Text == "")
            {
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("Alert_Enterthecontinuousabsentfieldvalue", ci) + "');";
                strScript = strScript + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                return;
            }
        }
        else if (txtMessage.Text == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_EntertheMessage", ci) + "');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            return;
        }
        else if (txtTags.Text == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_SelectTags", ci) + "');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            return;
        }
        int ToEmployee;
        ToEmployee = rbtnEmployeeNotification.Items[0].Selected == true ? 1 : 0;
        int InsertedId = 0;
        objsms.UpdatingSMSTemplateDetails(Convert.ToInt32(Request.QueryString["TemplateId"]), Convert.ToString(txtReceivers.Text), Convert.ToString(txtlateNotification.Text), Convert.ToString(txtContinuousLateEntry.Text), Convert.ToString(txtMessage.Text), Convert.ToString(EmployeeID), ToEmployee, Convert.ToString(txtlessHoursLimit.Text), string.IsNullOrEmpty(txtContinuousabsent.Text) ? Convert.ToInt16(0) : Convert.ToInt16(txtContinuousabsent.Text), Convert.ToString(hdnFilters.Value), Convert.ToString(hdnReportType.Value), Convert.ToString(txtTags.Text));
        Session["hdnfldUsergroup"] = "";
        Session["strUsergrpName"] = "";
        Session["ExistingTemplate"] = "";
        Session["EMailParentId"] = "";
        Session["hdnSMSFilters"] = "";
        strScript = strScript + "<script language='javascript'>";
        strScript = strScript + "alert('" + rm.GetString("Alert_SMSTemplateModifiedSuccessfully", ci) + "');";
        strScript = strScript + "location.replace('SMS_TemplateList.aspx');";
        strScript = strScript + "</script>";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);

    }
    private void LoadStrings(CultureInfo ci)
    {
        lblTemplatename.Text = rm.GetString("lblTemplatename", ci);
        lnkbtnChooseFilter.Text = "&nbsp;&nbsp;" + rm.GetString("lnkbtnChooseFilter", ci);
        lblReceivers.Text = rm.GetString("lblReceivers", ci);
        btn_AddReceivers.Text = "&nbsp;&nbsp;" + rm.GetString("btn_AddReceivers", ci);
        lbllateNotifcation.Text = rm.GetString("lbllateNotifcation", ci);
        lbl_Header.Text = "SMS Template Modify";
        lblContinuousabsent.Text = rm.GetString("lblContinuousabsent", ci);
        lblDays.Text = rm.GetString("lblDays", ci);
        lblContinuousLateEntry.Text = rm.GetString("lblContinuousLateEntry", ci);
        lblContinuousLateEntry1.Text = rm.GetString("lblContinuousLateEntry1", ci);
        lbllessHoursLimit.Text = rm.GetString("lbllessHoursLimit", ci);
        lblEmployeeNotification.Text = rm.GetString("lblEmployeeNotification", ci);
        lblTags.Text = rm.GetString("lblTags", ci);
        //lnkbtnSaveTags.Text = "&nbsp;&nbsp;" + rm.GetString("lnkbtnSaveTags", ci);
        smsbtn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        smsbtnBack.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        lblMessage.Text = rm.GetString("lblMessage", ci);
        lblreceivershdr.Text = rm.GetString("AddReceivers", ci);
    }
    private void UserRights()
    {
        //Todo: Need to change ID value in below
        ds_Checkuser = objCheckUser.checkRights(244, Convert.ToInt32(Session["userGroup_Id"]), "modifyPrv");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {

        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    private void bindTemplateDetails(int TempID)
    {
        DataSet dsTemplateDetails = new DataSet();
        dsTemplateDetails = objsms.SelectTemplateDetails(TempID);
        if (dsTemplateDetails.Tables.Count > 0)
        {
            if (dsTemplateDetails.Tables[0].Rows.Count > 0)
            {
                txtTemplateName.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["TemplateName"]);
                txtReceivers.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["Receivers"]);
                txtMessage.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["Message"]);
                hdnFilters.Value = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["Filters"]);
                hdnReportType.Value = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["ReportType"]);
                //hdnCompanyId.Value = Convert.ToString(dsEmailSchedule.Tables[0].Rows[0]["CompanyId"]);
                txtTags.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["Tags"]);
                if (txtTemplateName.Text == "Late Entry")
                {
                    //Todo: Need to call table columns here                    
                    lateEntry.Style.Add("display", "block");
                    lbllateNotification1.Style.Add("display", "block");
                    Continuous_lateEntry.Style.Add("display", "block");
                    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                    txtlateNotification.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["First_TriggeringTime"]);
                    txtContinuousLateEntry.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["Second_TriggeringTime"]);
                    BindTags("LateEntry");
                }
                else if (txtTemplateName.Text == "Early Exit")
                {
                    //Todo: Need to call table columns here                    
                    lateEntry.Style.Add("display", "block");
                    lbllateNotification1.Style.Add("display", "block");
                    lbllateNotification1.Text = rm.GetString("EarlyNotification", ci);
                    txtlateNotification.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["First_TriggeringTime"]);
                    BindTags("EarlyExit");
                }
                else if (txtTemplateName.Text == "Less Hours Worked")
                {
                    //Todo: Need to call table columns here                    
                    LessHours.Style.Add("display", "block");
                    lateEntry.Style.Add("display", "block");
                    lbllateNotification1.Style.Add("display", "block");
                    lbllateNotification1.Text = rm.GetString("EarlyNotification", ci);
                    txtlateNotification.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["First_TriggeringTime"]);
                    txtlessHoursLimit.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["LessHrsLimitation"]);
                    BindTags("LessHours");
                }
                else if (txtTemplateName.Text == "Absent - Single Day")
                {
                    //Todo: Need to call table columns here                    
                    lateEntry.Style.Add("display", "block");
                    lbllateNotification1.Style.Add("display", "block");
                    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                    txtlateNotification.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["First_TriggeringTime"]);
                    BindTags("AbsentSingleDay");
                }
                else if (txtTemplateName.Text == "Continuous Absent")
                {
                    //Todo: Need to call table columns here                    
                    ContinuousAbsent.Style.Add("display", "block");
                    lateEntry.Style.Add("display", "block");
                    lbllateNotification1.Style.Add("display", "block");
                    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                    txtlateNotification.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["First_TriggeringTime"]);
                    txtContinuousabsent.Text = Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["Continue_AbsentDays"]);
                    BindTags("AbsentContinuousDay");
                }
                else if (txtTemplateName.Text == "Leave Application - Apply" || txtTemplateName.Text == "Leave Application - Forward" ||
                    txtTemplateName.Text == "Leave Application - Approved" || txtTemplateName.Text == "Leave Application - Rejected"
                    )
                {
                    //Todo: Need to call table columns here                    
                    EmployeeNotification.Style.Add("display", "block");
                    lateEntry.Style.Add("display", "block");
                    lnkbtnChooseFilter.Enabled = false;
                    txtlateNotification.Enabled = false;
                    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                    BindTags("LeaveApplication");
                }
                //else if (txtTemplateName.Text == "Leave Application - Forward")
                //{
                //    EmployeeNotification.Style.Add("display", "block");
                //    lateEntry.Style.Add("display", "block");
                //    txtlateNotification.Enabled = false;
                //    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                //    lnkbtnChooseFilter.Enabled = false;
                //    BindTags("LeaveApplication");
                //}
                //else if (txtTemplateName.Text == "Leave Application - Approved")
                //{
                //    EmployeeNotification.Style.Add("display", "block");
                //    lateEntry.Style.Add("display", "block");
                //    lbllateNotification1.Style.Add("display", "block");
                //    txtlateNotification.Enabled = false;
                //    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                //    lnkbtnChooseFilter.Enabled = false;
                //    BindTags("LeaveApplication");
                //}
                //else if (txtTemplateName.Text == "Leave Application - Rejected")
                //{
                //    EmployeeNotification.Style.Add("display", "block");
                //    lateEntry.Style.Add("display", "block");
                //    lbllateNotification1.Style.Add("display", "block");
                //    txtlateNotification.Enabled = false;
                //    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                //    lnkbtnChooseFilter.Enabled = false;
                //    BindTags("LeaveApplication");
                //}
                else if (txtTemplateName.Text == "Permission Application - Apply" || txtTemplateName.Text == "Permission Application - Forward" ||
                    txtTemplateName.Text == "Permission Application - Approved" || txtTemplateName.Text == "Permission Application - Rejected"
                    )
                {
                    EmployeeNotification.Style.Add("display", "block");
                    lateEntry.Style.Add("display", "block");
                    lbllateNotification1.Style.Add("display", "block");
                    txtlateNotification.Enabled = false;
                    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                    lnkbtnChooseFilter.Enabled = false;
                    BindTags("PermissionApplication");
                }
                //else if (txtTemplateName.Text == "Permission Application - Forward")
                //{
                //    EmployeeNotification.Style.Add("display", "block");
                //    lateEntry.Style.Add("display", "block");
                //    lbllateNotification1.Style.Add("display", "block");
                //    txtlateNotification.Enabled = false;
                //    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                //    lnkbtnChooseFilter.Enabled = false;
                //    BindTags("PermissionApplication");
                //}
                //else if (txtTemplateName.Text == "Permission Application - Approved")
                //{
                //    EmployeeNotification.Style.Add("display", "block");
                //    lateEntry.Style.Add("display", "block");
                //    lbllateNotification1.Style.Add("display", "block");
                //    txtlateNotification.Enabled = false;
                //    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                //    lnkbtnChooseFilter.Enabled = false;
                //    BindTags("PermissionApplication");
                //}
                else if (txtTemplateName.Text == "OnDuty - Apply" || txtTemplateName.Text == "OnDuty - Forward" || txtTemplateName.Text == "OnDuty - Approved" || txtTemplateName.Text == "OnDuty - Rejected")
                {
                    EmployeeNotification.Style.Add("display", "block");
                    lateEntry.Style.Add("display", "block");
                    lbllateNotification1.Style.Add("display", "block");
                    txtlateNotification.Enabled = false;
                    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                    lnkbtnChooseFilter.Enabled = false;
                    BindTags("OnDuty");
                }
                //else if (txtTemplateName.Text == "OnDuty - Forward")
                //{
                //    EmployeeNotification.Style.Add("display", "block");
                //    lateEntry.Style.Add("display", "block");
                //    lbllateNotification1.Style.Add("display", "block");
                //    txtlateNotification.Enabled = false;
                //    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                //    lnkbtnChooseFilter.Enabled = false;
                //    BindTags("OnDuty");
                //}
                //else if (txtTemplateName.Text == "OnDuty - Approved")
                //{
                //    EmployeeNotification.Style.Add("display", "block");
                //    lateEntry.Style.Add("display", "block");
                //    lbllateNotification1.Style.Add("display", "block");
                //    txtlateNotification.Enabled = false;
                //    lbllateNotification1.Text = rm.GetString("lbllateNotification1", ci);
                //    lnkbtnChooseFilter.Enabled = false;
                //    BindTags("OnDuty");
                //}
                if (Convert.ToString(dsTemplateDetails.Tables[0].Rows[0]["ToEmployee"]) == "True")
                {
                    rbtnEmployeeNotification.SelectedValue = "1";                
                }
                else
                {
                    rbtnEmployeeNotification.SelectedValue = "0";
                }
            }
        }
    }
    private void BindTags(string TemplateName)
    {
        ddlTags.Items.Clear();
        DataSet dsTags = new DataSet();
        dsTags = objsms.SelectTagDetailsUsingTemplate(TemplateName);
        if (dsTags.Tables.Count > 0)
        {
            if (dsTags.Tables[0].Rows.Count > 0)
            {
                ddlTags.DataSource = dsTags;
                ddlTags.DataValueField = "Tags";
                ddlTags.DataTextField = "Tags";
                ddlTags.DataBind();
            }
        }
        ListItem lstItem = new ListItem();
        lstItem.Value = "0";
        lstItem.Text = "Insert Tags";
        ddlTags.Items.Insert(0, lstItem);
    }
}
