using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Drawing;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                         Started Date  : 12-Aug-2007
* Module          : Organisation Levels                               Last Modified : 28-Aug-2007
* Programmer      : Senthil Kumar NP
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_LevelChildAdd : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    cls_Common objcommon = new cls_Common();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    CultureInfo ci;
    byte[] image;
    string strLevelName = string.Empty;
    DataSet ds_ParentLevels = new DataSet();
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        //get the culture info to set the language
        string source_Language;
        string strSetAlert = "";
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        
        if (!IsPostBack)
        {
            site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);            
            //Set UserRights
            UserRights();
            LoadStrings(ci);

            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
            strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_Entervalid") + "','" + rm.GetString("Alert_number") + "','" + rm.GetString("Alert_PhoneNumber") + "','" + rm.GetString("Alert_Entervalidphonenumber") + "','" + rm.GetString("Alert_EntervalidFaxnumber") + "','" + rm.GetString("Alert_EntervalidMobilenumber") + "','" + rm.GetString("Alert_ConfirmSave") + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
            if (Request.QueryString["tid"] != null)
            {
                strLevelName = Convert.ToString(Request.QueryString["tid"]);
                ViewState["orgLevelid"] = Convert.ToInt32(Request.QueryString["tid"]);
            }
            int LevelID = Convert.ToInt32(ViewState["orgLevelid"]);
            objcommon.GetParentLevels_FromLevelID(LevelID);
            ds_ParentLevels = objcommon.GetParentLevels();
            hdnRequestId.Value = Convert.ToString(LevelID);
            LoadUTCTime();
            if (strLevelName != "1")
            {
                LogoId.Visible = false;
                if (ds_ParentLevels.Tables.Count > 0)
                {
                    if (ds_ParentLevels.Tables[0].Rows.Count > 0)
                    {
                        lbl_Man.Text = "<span style='color: Red'>|</span>" + rm.GetString("IndicatesMandatoryFields", ci) + " (" + Convert.ToString(ds_ParentLevels.Tables[0].Rows.Count) + " No.)";
                    }
                }
                else
                {
                    lbl_Man.Text = "<span style='color: Red'>|</span>" + rm.GetString("IndicatesMandatoryFields", ci) + " (1 No.)";
                }
            }
            else
            {
                if (ds_ParentLevels.Tables.Count > 0)
                {
                    if (ds_ParentLevels.Tables[0].Rows.Count > 0)
                    {
                        lbl_Man.Text = "<span style='color: Red'>|</span>" + rm.GetString("IndicatesMandatoryFields", ci) + " (" + Convert.ToString(ds_ParentLevels.Tables[0].Rows.Count) + " No.)";
                    }
                    else
                    {
                        lbl_Man.Text = "<span style='color: Red'>|</span>" + rm.GetString("IndicatesMandatoryFields", ci) + " (1 No.)";
                    }
                }
                else
                {
                    lbl_Man.Text = "<span style='color: Red'>|</span>" + rm.GetString("IndicatesMandatoryFields", ci) + " (1 No.)";
                }
                LogoId.Visible = true;
            }

            this.txtLevelName.Focus();
            int levelid = Convert.ToInt32(ViewState["orgLevelid"]);
            if (levelid != 0)
            {
                getLevelDetails();
            }
            objcommon.menu("OrgSettings", "6");
        }
    }
    #endregion
    #region UserDefinedFunctions
    // Dropdown Loading the Timezone Created by Maria Prasanth 15-Dec-2023
    private void LoadUTCTime()
    {
        DataSet ds_UTC = new DataSet();
        org_levelsEventData getUtc = new org_levelsEventData();
        ds_UTC = getUtc.GetUTCTimeZone();
        ddlUTCTime.DataSource = ds_UTC;

        ddlUTCTime.DataTextField = "CountryName";
        ddlUTCTime.DataValueField = "TimeZoneID";
        ddlUTCTime.DataBind();
        ddlUTCTime.Dispose();

        if(ViewState["orgLevelid"]!="")
        {
            int levelId = Convert.ToInt32(ViewState["orgLevelid"]);
            if(levelId==2)
            {
                ddlUTCTime.CssClass = "form-control select2 Man-dat";
                Label2.Visible = true;
                ddlUTCTime.Visible = true;
            }
            else
            {
                ddlUTCTime.CssClass = "form-control select2 Man-dat";
                Label2.Visible = false;
                ddlUTCTime.Visible = false;

            }
        }

    }

    protected void getLevelDetails()
    {
        org_levelsEventData selectLevel = new org_levelsEventData();
        DataSet dsLev = new DataSet();
        //dsLev = selectLevel.selectLevelDetails(Convert.ToInt32(ViewState["orgLevelid"]), "", Convert.ToString(Session["LevelRights"]));
        dsLev = selectLevel.selectLevelName(Convert.ToInt32(ViewState["orgLevelid"]), "", Convert.ToString(Session["LevelRights"]));
        if (dsLev.Tables[0].Rows.Count > 0)
        {
            if ((Convert.ToString(dsLev.Tables[0].Rows[0]["levelName"]) != ""))
            {
                this.lblHead.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["levelName"]) + " - " + rm.GetString("Org_AddNew", ci);
            }
        }
        dsLev.Clear();
        dsLev.Dispose();
    }

    private void LoadStrings(CultureInfo ci)
    {
        lblHead.Text = rm.GetString("Menu_Masters_OrgLevels", ci);
        lbl_Name.Text = rm.GetString("OrgLevels_CompDetails_Name", ci);
        lbl_Description.Text = rm.GetString("Designation_DesignationDesc", ci);
        lbl_City.Text = rm.GetString("Masters_Generic_City", ci);
        lbl_State.Text = rm.GetString("Masters_Generic_State", ci);
        lbl_Country.Text = rm.GetString("Masters_Generic_Country", ci);
        lbl_Zip.Text = rm.GetString("OrgLevels_CompDetails_Zip", ci);
        lbl_Phone1.Text = rm.GetString("OrgLevels_CompDetails_Phone1", ci);
        lbl_Phone2.Text = rm.GetString("OrgLevels_CompDetails_Phone2", ci);
        lbl_Fax.Text = rm.GetString("OrgLevels_CompDetails_Fax", ci);
        lbl_Mobile.Text = rm.GetString("OrgLevels_CompDetails_Mobile", ci);
        lbl_Email.Text = rm.GetString("OrgLevels_CompDetails_Email", ci);
        lbl_WebAdd.Text = rm.GetString("OrgLevels_CompDetails_WebAddr", ci);
        lbl_Logo.Text = rm.GetString("OrgLevels_CompDetails_Logo", ci);
        lbl_Man.Text = rm.GetString("IndicatesMandatoryFields", ci);
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("Alert_EnterName", ci);
        RegularExpressionValidator1.ErrorMessage = rm.GetString("Alert_EnterValidEmailId", ci);
        RegularExpressionValidator2.ErrorMessage = rm.GetString("Alert_EnterValidWebAddress", ci); ;
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(6, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_save_Click(object sender, EventArgs e)
    {
        org_levelsEventData ObjClass = new org_levelsEventData();
        if (this.upLogo.HasFile)
        {
            string logoFile = upLogo.PostedFile.FileName;
            int index = logoFile.LastIndexOf(".");
            string fileFormat = logoFile.Substring(index + 1);
            fileFormat = fileFormat.ToLower();
            if (fileFormat == "bmp" || fileFormat == "jpg" || fileFormat == "png")
            {
                image = upLogo.FileBytes;
            }
            else
            {
                base.Message(rm.GetString("Alert_PleaseSelectFileforlogo", ci));
                return;
            }
        }
        else
        {
            string contenttype = string.Empty;
            contenttype = "image/bmp";
            Image defaultImage = Image.FromFile(Server.MapPath("../Images/dummy.bmp"));
            using (MemoryStream ms = new MemoryStream())
            {
                defaultImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                image = ms.ToArray();
            }
        }
        org_levelsEventData selectLevel = new org_levelsEventData();
        int orgId = Convert.ToInt32(Session["parentLevelId"]);
        int MinLevel = 0;
        DataSet dsMin = new DataSet();
        dsMin = ObjClass.SelectMinLevel(Convert.ToInt32(ViewState["orgLevelid"]), Convert.ToInt32(Session["parentLevelId"]));
        if (dsMin != null)
        {
            if (dsMin.Tables.Count > 0)
            {
                if (dsMin.Tables[0].Rows.Count > 0)
                {
                    if (dsMin.Tables[0].Rows[0]["Result"] != DBNull.Value)
                    {
                        MinLevel = Convert.ToInt32(dsMin.Tables[0].Rows[0]["Result"]);
                    }
                }
            }
        }
        if (MinLevel == 1)
        {
            if (selectLevel.CheckLevelName(Convert.ToInt32(Session["parentLevelId"]), Convert.ToInt32(ViewState["orgLevelid"]), txtLevelName.Text.Trim()) == 0)
            {
                int timezoneId = 0;
                if(Convert.ToInt32(ViewState["orgLevelid"]) == 2)
                {
                    timezoneId = Convert.ToInt32(ddlUTCTime.SelectedIndex);
                }
               
                selectLevel.InsertChildLevel("I", Convert.ToInt32(ViewState["orgLevelid"]), Server.HtmlEncode(this.txtLevelName.Text.Trim().Replace("'", "''")), Convert.ToInt32(Session["parentLevelId"]), Server.HtmlEncode(this.txtDescription.Text.Trim().Replace("'", "''")), Server.HtmlEncode(this.txtCity.Text.Trim().Replace("'", "''")), Server.HtmlEncode(this.txtState.Text.Trim().Replace("'", "''")), Server.HtmlEncode(this.txtCountry.Text.Trim().Replace("'", "''")), this.txtZip.Text, this.txtPhone1.Text, this.txtPhone2.Text, this.txtFax.Text, this.txtMobile.Text, this.txtEmail.Text.Trim().Replace("'", "''"), this.txtWeb.Text.Trim().Replace("'", "''"), 0, image, Session["EmpAutoID"].ToString(), timezoneId);
                Session["parentLevelId"] = null;
                Session["orgChildId"] = null;
                Session["parentLevelId"] = null;
                cls_master_levels class1 = new cls_master_levels();
                DataSet ds = new DataSet();
                if (Session["UserGroupName"] != null)
                {
                    ds = class1.getOrgLevelUpdated(0, Convert.ToString(Session["LevelRights"]), 0, Convert.ToString(Session["UserGroupName"]));
                }
                //stores the organization levels.
                Session["OrgLevels"] = ds;
                string str = "alert('" + rm.GetString("Alert_LevelSavedSuccessfully", ci) + "');location.replace('TA_UI_Master_LevelDetails.aspx?tid=" + ViewState["orgLevelid"] + "');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
            }
            else
            {
                base.Message(rm.GetString("Alert_Levelnamealreadyexits", ci));
            }
        }
        else
        {
            base.Message(rm.GetString("Alert_Selectallorganizationlevels", ci));
        }
    }
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_LevelDetails.aspx?tid=" + ViewState["orgLevelid"] + "");
    }
    #endregion
}
