using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                         Started Date  : 12-Aug-2007
* Module          : Organisation Levels                               Last Modified : 20-Aug-2007
* Programmer      : Senthil Kumar NP
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_LevelChildView : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    int del = 0;
    cls_Common objCommon = new cls_Common();
    byte[] Image;
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        loadcss();
        //get the culture info to set the language
        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;
        LoadStrings(ci);
        string gstrUserGroupName = string.Empty;
        //gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        if (gstrUserGroupName != "SuperAdmin")
        {
            lbl_Status.Visible = false;
            RowStatus.Visible = false;
            lbl_RecordStatus.Visible = false;
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
        if (Request.QueryString["tid"] != null)
        {
            if (Convert.ToInt32(Request.QueryString["tid"].ToString()) != 1)
            {
                LogoIdView.Visible = false;
            }
            else
            {
                LogoIdView.Visible = true;
            }
        }
        Cls_CheckUser chck = new Cls_CheckUser();
        DataSet ds = chck.checkRights(6, Convert.ToInt32(Session["userGroup_Id"]), " ");
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (ds.Tables[0].Rows[0]["viewPrv"] != DBNull.Value && Convert.ToInt32(ds.Tables[0].Rows[0]["viewPrv"]) == 1)
            {
                //
            }
            else
            {
                Response.Redirect("TA_UI_Master_OrganisationLevels.aspx");
            }
            if (ds.Tables[0].Rows[0]["deletePrv"] != DBNull.Value && Convert.ToInt32(ds.Tables[0].Rows[0]["deletePrv"]) == 1)
            {
                this.btn_delete.Visible = true;
                del = 1;
            }
            else
            {
                this.btn_delete.Visible = false;
                del = 0;
            }
            if (ds.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.btn_modify.Visible = true;
            }
            else
            {
                this.btn_modify.Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
        org_levelsEventData dels = new org_levelsEventData();
        int gid;
        gid = Convert.ToInt16(Session["levelid"]);
        DataSet dsDelete = new DataSet();
        dsDelete = dels.CheckForDel(gid, "C", "");
        if (dsDelete.Tables[0].Rows.Count > 0)
        {
            if (dsDelete.Tables[0].Rows[0]["checkFlag"] != DBNull.Value)
            {
                if (Convert.ToInt16(dsDelete.Tables[0].Rows[0]["checkFlag"]) > 0)
                {
                    btn_delete.Enabled = false;
                }
            }
        }
        try
        {
            if (!IsPostBack)
            {
                Session["PreviousPage"] = Convert.ToString(Request.UrlReferrer);
                Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Cache.SetNoStore();
                int id = Convert.ToInt16(Request.QueryString["tid"]);
                Session["orgLevelid"] = id;
                int levelid = Convert.ToInt16(Request.QueryString["chID"]);
                if (levelid != 0)
                {
                    Session["levelid"] = levelid;
                    getLevelDetails(levelid);
                }
                btn_delete.OnClientClick = "return confirm('" + rm.GetString("Alert_ConfirmDelete", ci) + "');";
                btn_Restore.OnClientClick = "return confirm('" + rm.GetString("Alert_Areyousuretorestore", ci) + "')";
                objCommon.menu("OrgSettings", "6");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    protected void getLevelDetails(int levelid)
    {
        org_levelsEventData selectLevel = new org_levelsEventData();
        DataSet dsLevHed = new DataSet();
        dsLevHed = selectLevel.selectLevelDetails(Convert.ToInt16(Session["orgLevelid"]), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["LevelRights"]), Convert.ToInt16(Session["parentLevelId"]));
        if (dsLevHed.Tables[0].Rows.Count > 0)
        {
            if ((Convert.ToString(dsLevHed.Tables[0].Rows[0]["levelName"]) != ""))
            {
                this.lblHead.Text = Convert.ToString(dsLevHed.Tables[0].Rows[0]["levelName"]) + " - View";
            }
        }
        dsLevHed.Clear();
        dsLevHed.Dispose();
        //org_levelsEventData selectLevel = new org_levelsEventData();
        DataSet dsLev = new DataSet();
        dsLev = selectLevel.selectOrgDetails(Convert.ToInt16(levelid));
        if (dsLev.Tables[0].Rows.Count > 0)
        {
            //getting level details
            DataSet ds_ParentLevels = new DataSet();
            objCommon.GetParentLevels_FromLevelID(Convert.ToInt16(dsLev.Tables[0].Rows[0]["levelId"]));
            ds_ParentLevels = objCommon.GetParentLevels();
            string htm = "";
            int orgID = Convert.ToInt16(levelid);
            org_levelsEventData getOrg = new org_levelsEventData();
            getOrg.GetOrgParentLevels_FromID(orgID);
            DataSet ds_getOrg = new DataSet();
            ds_getOrg = getOrg.GetOrgParentLevels();
            for (int iloop = 0; iloop <= ds_ParentLevels.Tables[0].Rows.Count - 2; iloop++)
            {
                HtmlGenericControl div = new HtmlGenericControl("div");
                div.Attributes.Add("class", "col-md-2");
                div.Style.Add("padding-top", "10px");
                div.Controls.Add(new LiteralControl(ds_ParentLevels.Tables[0].Rows[iloop][1].ToString()));
                pnl.Controls.Add(div);
                if (ds_getOrg.Tables[0].Rows.Count > 0)
                {
                    HtmlGenericControl div1 = new HtmlGenericControl("div");
                    div1.Attributes.Add("class", "col-md-3");
                    div1.Style.Add("padding-top", "10px");
                    div1.Controls.Add(new LiteralControl("<label class='form-control'>" + ds_getOrg.Tables[0].Rows[iloop][1].ToString() + "</label>"));
                    pnl.Controls.Add(div1);
                    pnl.Controls.Add(new LiteralControl("<div class='col-md-1'></div>"));
                }
            }
            //this.lblText.Text = htm;
            //end of getting level details
            if (dsLev.Tables[0].Rows[0]["name"] != System.DBNull.Value)
            {
                //this.lblHead.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["levelName"]) + " Details";
                this.lblLevelName.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["name"]);
            }
            if (dsLev.Tables[0].Rows[0]["description"] != System.DBNull.Value)
            {
                this.lblDescription.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["description"]);
            }
            if (dsLev.Tables[0].Rows[0]["city"] != System.DBNull.Value)
            {
                this.lblCity.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["city"]);
            }
            if (dsLev.Tables[0].Rows[0]["state"] != System.DBNull.Value)
            {
                this.lblState.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["state"]);
            }
            if (dsLev.Tables[0].Rows[0]["country"] != System.DBNull.Value)
            {
                this.lblCountry.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["country"]);
            }
            if (dsLev.Tables[0].Rows[0]["zip"] != System.DBNull.Value)
            {
                this.lblZip.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["zip"]);
            }
            if (dsLev.Tables[0].Rows[0]["phone1"] != System.DBNull.Value)
            {
                this.lblPhone1.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["phone1"]);
            }
            if (dsLev.Tables[0].Rows[0]["phone2"] != System.DBNull.Value)
            {
                this.lblPhone2.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["phone2"]);
            }
            if (dsLev.Tables[0].Rows[0]["fax1"] != System.DBNull.Value)
            {
                this.lblFax.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["fax1"]);
            }
            if (dsLev.Tables[0].Rows[0]["fax2"] != System.DBNull.Value)
            {
                this.lblMobile.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["fax2"]);
            }
            if (dsLev.Tables[0].Rows[0]["email"] != System.DBNull.Value)
            {
                this.lblEmail.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["email"]);
            }
            if (dsLev.Tables[0].Rows[0]["webAddress"] != System.DBNull.Value)
            {
                this.lblWeb.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["webAddress"]);
            }
            if (dsLev.Tables[0].Rows[0]["Isdeleted"] != System.DBNull.Value)
            {
                this.lbl_RecordStatus.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["Isdeleted"]);
                if (Convert.ToBoolean(dsLev.Tables[0].Rows[0]["Isdeleted"]))
                {
                    btn_delete.Visible = false;
                    btn_Restore.Visible = true;
                }
                else
                {
                    if (del == 1)
                    {
                        btn_delete.Visible = true;
                    }
                    else
                    {
                        btn_delete.Visible = false;
                    }
                    btn_Restore.Visible = false;
                }
            }
            if (dsLev.Tables[0].Rows[0]["logo"] != System.DBNull.Value)
            {
                Random rnd = new Random();
                this.imgLogo.Visible = true;
                Image = (byte[])dsLev.Tables[0].Rows[0]["logo"];
                GetImage(Image);
                //Clear temporary internet files
                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                this.imgLogo.ImageUrl = "logo" + levelid + ".bmp?rnd=" + rnd.NextDouble();
                //this.imgLogo.ImageUrl = "logo" + levelid + ".bmp";
            }
            else
            {
                this.imgLogo.Visible = false;
            }
        }
    }
    //Function Creates Image File
    public void GetImage(byte[] Logo)
    {
        DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "Masters");
        string strPath = Server.MapPath("logo" + Convert.ToInt16(Session["levelid"]) + ".bmp");
        //if (File.Exists(strPath))
        //{
        //    File.Delete(strPath);
        //}
        FileInfo[] files = dir.GetFiles("*.bmp");
        //deleting the files
        foreach (FileInfo file in files)
        {
            string str = AppDomain.CurrentDomain.BaseDirectory + "Masters\\";
            File.Delete(str + file.ToString());
        }
        MemoryStream stream = new MemoryStream(Logo);
        System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
        img.Save(strPath);
    }
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        //string strPage = Convert.ToString(Session["PreviousPage"]);
        //Session["PreviousPage"] = null;
        //Response.Redirect(strPage);
        Response.Redirect("TA_UI_Master_LevelDetails.aspx?tid=" + Session["orgLevelid"] + " &chID= " + Session["levelid"] + " &sid=" + Session.SessionID);
    }
    protected void btn_modify_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_LevelChildModify.aspx?tid=" + Session["orgLevelid"] + " &chID= " + Session["levelid"] + " &sid=" + Session.SessionID);
    }
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        org_levelsEventData del = new org_levelsEventData();
        int gid;
        gid = Convert.ToInt16(Session["levelid"]);
        DataSet ds = new DataSet();
        ds = del.CheckForDel(gid, "C", "");
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (ds.Tables[0].Rows[0]["checkFlag"] != DBNull.Value)
            {
                if (Convert.ToInt16(ds.Tables[0].Rows[0]["checkFlag"]) > 0)
                {
                    string strScript = "";
                    strScript = strScript + "<script language='javascript'>";
                    //strScript = strScript + "alert('Cannot Delete this Record, It is Already mapped');history.go(-1)";
                    strScript = strScript + "alert('" + rm.GetString("Alert_Cannot_Delete_this_Record_It_is_Already_mapped", ci) + "');history.go(-1)";
                    strScript = strScript + "</script>";
                    Page.RegisterStartupScript("DelAlert1", strScript);
                }
                else
                {
                    del.DelLevel(gid, "D", "TemporaryDelete");
                    //Response.Redirect("TA_UI_Master_OrganisationLevels.aspx");
                    //base.MessageWithRedirect("Level deleted successfully","TA_UI_Master_OrganisationLevels.aspx");
                    base.MessageWithRedirect(rm.GetString("Alert_Leveldeletedsuccessfully", ci), "TA_UI_Master_LevelDetails.aspx?tid=" + Session["orgLevelid"] + " &chID= " + Session["levelid"] + " &sid=" + Session.SessionID);
                }
            }
        }
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo 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);
        btn_modify.Text = " " + rm.GetString("btn_Modify", ci);
        btn_cancel.Text = " " + rm.GetString("btn_Back", ci);
        btn_delete.Text = " " + rm.GetString("btn_Delete", ci);
        lbl_Status.Text = rm.GetString("DeleteStatus", ci);
        btn_Restore.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Restore", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_PermanentDelete", ci);
    }
    protected void btn_Restore_Click(object sender, EventArgs e)
    {
        org_levelsEventData del = new org_levelsEventData();
        int gid;
        gid = Convert.ToInt16(Session["levelid"]);
        DataSet ds = new DataSet();
        ds = del.CheckForDel(gid, "R", "CheckRestore");
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (ds.Tables[0].Rows[0]["checkFlag"] != DBNull.Value)
            {
                if (Convert.ToInt16(ds.Tables[0].Rows[0]["checkFlag"]) > 0)
                {
                    string strScript = "";
                    strScript = strScript + "<script language='javascript'>";
                    //strScript = strScript + "alert('Already Exists.Cannot Restore this Record')";
                    strScript = strScript + "alert('" + rm.GetString("Alert_AlreadyExistsCannotRestorethisRecord", ci) + "')";
                    strScript = strScript + "</script>";
                    Page.RegisterStartupScript("RestoreAlert", strScript);
                    return;
                }
                else
                {
                    del.DelLevel(gid, "D", "Restore");
                    //Response.Redirect("TA_UI_Master_OrganisationLevels.aspx");
                    // base.MessageWithRedirect("Level restored successfully", "TA_UI_Master_OrganisationLevels.aspx");
                    base.MessageWithRedirect(rm.GetString("Alert_Levelrestoredsuccessfully", ci), "TA_UI_Master_LevelDetails.aspx?tid=" + Session["orgLevelid"] + " &chID= " + Session["levelid"] + " &sid=" + Session.SessionID);
                }
            }
        }
    }
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        org_levelsEventData del = new org_levelsEventData();
        int gid;
        gid = Convert.ToInt16(Session["levelid"]);
        DataSet ds = new DataSet();
        ds = del.CheckForDel(gid, "C", "");
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (ds.Tables[0].Rows[0]["checkFlag"] != DBNull.Value)
            {
                if (Convert.ToInt16(ds.Tables[0].Rows[0]["checkFlag"]) > 0)
                {
                    string strScript = "";
                    strScript = strScript + "<script language='javascript'>";
                    //strScript = strScript + "alert('Cannot Delete this Record, It is Already mapped')";
                    strScript = strScript + "alert('" + rm.GetString("Alert_Cannot_Delete_this_Record_It_is_Already_mapped", ci) + "')";
                    strScript = strScript + "</script>";
                    Page.RegisterStartupScript("DelAlert1", strScript);
                    return;
                }
                else
                {
                    del.DelLevel(gid, "D", "PermanentDelete");
                    //Response.Redirect("TA_UI_Master_OrganisationLevels.aspx");
                    //base.MessageWithRedirect("Level deleted successfully", "TA_UI_Master_OrganisationLevels.aspx");
                    base.MessageWithRedirect(rm.GetString("Alert_Leveldeletedsuccessfully", ci), "TA_UI_Master_OrganisationLevels.aspx");
                }
            }
        }
    }

    private void loadcss()
    {
        CultureInfo ci;
        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 Standard Demo
        LevelView.Attributes.Add("dir", rm.GetString("Direction", ci));
        if (source_Language == "Resources.Resource_AR")
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            stylecustom.Href = "../dist/css/custom-rtl.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
        else
        {
            //LevelView.Attributes.Add("dir", "rtl");
            //stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            //stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            //styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            //stylecustom.Href = "../dist/css/custom-rtl.css";
            //stylefonts.Href = "../dist/css/fonts.css";
            stylebootstrap.Href = "../bootstrap/css/bootstrap.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min.css";
            stylecustom.Href = "../dist/css/custom.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
    }
}
