using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources;
public partial class Masters_PopUp_ShiftExchange : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    int LevelID;
    DataSet ds = new DataSet();
    bool checkLevel;
    bool gcheckParentLevelExist;
    ShiftAllocationEvendata obj = new ShiftAllocationEvendata();
    // ShiftExchange obj_ShiftExchange = new ShiftExchange();
    EmployeeEventData objEmployee = new EmployeeEventData();
    string isFrom = "";
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking and reset date format
        base.Page_Load(sender, e);
        if (Request.QueryString["isFrom"] != null)
        {
            isFrom = Request.QueryString["isFrom"].ToString();
        }
        if (!IsPostBack)
        {
            btn_done1.Visible = false;
            hdnTxtField.Value = Request.QueryString["Txt"].ToString();
            if (Request.QueryString["isFrom"] != null)
            {
                isFrom = Request.QueryString["isFrom"].ToString();
            }
            BindingEmployeeCategoryandType();
            acEmployeeId.TargetControlID = "txtEmployeeId";
        }
        //get the culture info to set the language
        if (Session["Session_ActiveLanguage"] == null)
        {
            string str = "<script language=\"javascript\">";
            str = str + "opener.focus();";
            //str = str + "alert(opener.location+'?fl=1&sid=';";
            str = str + "opener.location.href = opener.location ";
            str = str + "self.close();";
            str = str + "</script>";
            Response.Write(str);
        }
        else
        {
            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;
            aspnetForm.Attributes.CssStyle.Add("Direction", rm.GetString("Direction", ci));
            LoadStrings(ci);
            loadcss();
            string strSetAlert = "";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "SetAlertMessages_DeleteRestore('" + rm.GetString("Alert_ConfirmDelete", ci) + "','" + rm.GetString("Alert_SelectItemsToDelete", ci) + "','" + rm.GetString("Alert_ConfirmDeletePermanent", ci) + "','" + rm.GetString("Alert_Areyousuretorestore", ci) + "','" + rm.GetString("Alert_SelectItemsToRestore", ci) + "','" + rm.GetString("Alert_SelectItemToForwarded") + "','" + rm.GetString("Alert_SelectItemToApproved") + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        }
    }
    public void shiftDataBind()
    {
        //LevelID = (int)Session["currentSession"];
        LevelID = Convert.ToInt32(Session["currentSession"]);
        //Session["parentLevelId"] = null;
        if (LevelID == 0)
        {
            //ds = obj.SelectEmployee(LevelID);
            //grv_Shift.DataSource = ds.Tables[0];
            //grv_Shift.DataBind();
            //grv_Shift.Visible = true;
            //gcheckParentLevelExist = false;
            grv_Shift.DataSourceID = "";
            grv_Shift.DataBind();
            //lbl_Error.Text = "Select Organization Level";
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Selectallorganizationlevels", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("Display Message", strMsg);
            checkLevel = true;
            btn_done1.Visible = false;
            return;
        }
        else
        {
            Rpt_Common rptcommon = new Rpt_Common();
            string Level = "";
            Level = rptcommon.getNodes(LevelID, Convert.ToString(Session["LevelRights"]));
            ds = obj.SelectEmployeeforpopup(Convert.ToInt32(Session["EmpAutoId"]), Level, Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), Convert.ToInt32(Session["currentSession"]), txtEmployeeId.Text, txtEmployeeName.Text, Convert.ToInt32(ddlEmpCatagory.SelectedValue), Convert.ToInt32(ddlEmpType.SelectedValue), isFrom);
            if (ds.Tables[0].Rows.Count > 0)
            {
                grv_Shift.DataSource = ds.Tables[0];
                grv_Shift.DataBind();
                divShift.Visible = true;
                gcheckParentLevelExist = true;
                btn_done1.Visible = true;
            }
            else
            {
                gcheckParentLevelExist = false;
            }
            lbl_Error.Text = "";
        }
    }
    private void BindingEmployeeCategoryandType()
    {
        // employee category binding
        DataSet dsCategory = new DataSet();
        dsCategory = objEmployee.SelectCategoryDetailsEmployee("");
        if (dsCategory != null)
        {
            if (dsCategory.Tables[0].Rows.Count > 0)
            {
                ddlEmpCatagory.DataSource = dsCategory;
                ddlEmpCatagory.DataTextField = "CategoryName";
                ddlEmpCatagory.DataValueField = "CategoryId";
                ddlEmpCatagory.DataBind();
            }
            dsCategory.Dispose();
            foreach (ListItem item in ddlEmpCatagory.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
        // employee type binding
        DataSet dsType = new DataSet();
        dsType = objEmployee.SelectTypeDetailsEmployee("");
        if (dsType != null)
        {
            if (dsType.Tables[0].Rows.Count > 0)
            {
                ddlEmpType.DataSource = dsType;
                ddlEmpType.DataTextField = "TypeName";
                ddlEmpType.DataValueField = "TypeId";
                ddlEmpType.DataBind();
            }
            dsType.Dispose();
            foreach (ListItem item in ddlEmpType.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    public void CheckEmployeeId_CheckBox()
    {
        string strEmpId = "";
        strEmpId = (string)Session["EmployeeIds"];
        char[] Delimit ={ ',' };
        string[] Empid = strEmpId.Split(Delimit);
        foreach (GridViewRow gr in grv_Shift.Rows)
        {
            string empId = ((Label)gr.FindControl("lbl_EmpId")).Text.ToString();
            foreach (string str in Empid)
            {
                if (str == empId)
                {
                    HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)gr.FindControl("deletethis");
                    if (deleteChkBxItem != null)
                    {
                        deleteChkBxItem.Checked = true;
                        strEmpId = strEmpId.Replace(empId, "");
                        Session["EmployeeIds"] = strEmpId;
                    }
                }
            }
        }
    }
    //protected void grv_Shift_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    grv_Shift.EnableViewState = true;
    //    string ChkBoxIDs = "";
    //    bool BxsChkd = false;
    //    // Add EmployeeId in Session Variable.
    //    foreach (GridViewRow i in grv_Shift.Rows)
    //    {
    //        HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)i.FindControl("deletethis");
    //        if (deleteChkBxItem.Checked)
    //        {
    //            BxsChkd = true;
    //            if (ChkBoxIDs == "")
    //            {
    //                ChkBoxIDs = ((Label)i.FindControl("lbl_EmpId")).Text.ToString();
    //            }
    //            else
    //            {
    //                // Concatenate DataGrid item with comma for SQL Delete
    //                ChkBoxIDs = ChkBoxIDs + "," + ((Label)i.FindControl("lbl_EmpId")).Text.ToString();
    //            }
    //        }
    //    }
    //    if (Session["EmployeeIds"].ToString() == "")
    //    {
    //        Session["EmployeeIds"] = ChkBoxIDs;
    //    }
    //    else
    //    {
    //        if (ChkBoxIDs != "")
    //        {
    //            Session["EmployeeIds"] = Session["EmployeeIds"] + "," + ChkBoxIDs;
    //        }
    //    }
    //    grv_Shift.PageIndex = e.NewPageIndex;
    //    shiftDataBind();
    //    CheckEmployeeId_CheckBox();
    //}
    //Close the popup window
    //protected void btn_done1_Click(object sender, EventArgs e)
    //{
    //    DataSet ds = new DataSet();
    //    ds = obj.SelectEmployeeDetails();
    //    Session["EmpDetails"] = ds;
    //    string str = "<script language=\"javascript\">";
    //    str = str + "opener.focus();";
    //    str = str + "opener.location.href = opener.location ;";
    //    str = str + "self.close();";
    //    str = str + "</script>";
    //    Response.Write(str);
    //}
    protected void butGo_Click(object sender, EventArgs e)
    {
        Session["EmployeeIds"] = "";
        Session["currentSession"] = Session["parentLevelId"];
        //if (Session["parentLevelId"].ToString() == "" || Session["parentLevelId"].ToString() == "0")
        //{
        //    lbl_Error.Text = "Select Organization Level";
        //    return;
        //}
        Session["parentLevelId"] = null;
        shiftDataBind();
        if (checkLevel == true)
        {
            return;
        }
        if (gcheckParentLevelExist == true)
        {
            divShift.Visible = true;
            btn_done1.Visible = true;
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "$('html, body').animate({scrollTop: 800}, 'slow');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("Display Message ShiftAllocationAdd", strMsg);
        }
        else
        {
            btn_done1.Visible = false;
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("Display Message", strMsg);
            divShift.Visible = false;
            return;
        }
    }
    protected void grv_Shift_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == 0)
        {
            HtmlInputRadioButton rdbID = new HtmlInputRadioButton();
            rdbID = (HtmlInputRadioButton)e.Row.FindControl("rdbID");
            rdbID.Checked = true;
            hdnEmployeeCodeId.Value = rdbID.Value;
        }
    }
    protected void grv_Shift_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Shift.PageIndex = e.NewPageIndex;
        shiftDataBind();
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        butGo.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Go", ci);
        btn_done1.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Ok", ci);
        grv_Shift.Columns[1].HeaderText = rm.GetString("email_EmployeeId", ci);
        grv_Shift.Columns[2].HeaderText = rm.GetString("email_EmployeeName", ci);
        lblEmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblEmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lblFilter.Text = rm.GetString("lblFilter", ci);
        lbl_Man.Text = rm.GetString("IndicatesMandatoryFields", ci);
        lblEmpCategory.Text = rm.GetString("lblEmpCategory", ci);
        lblEmpType.Text = rm.GetString("lblEmpType", ci);

    }

    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
        htmlSelectEmpPop.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";
            styleselect2.Href = "../plugins/select2/select2.min-rtl.css";
            styleProgressBar.Href = "../css/ProgressBar-rtl.css";
            styleAutocomp.Href = "../bootstrap/css/autoCompleteNew-rtl.css";
        }
        else
        {
            //htmlSelectEmpPop.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";
            //styleselect2.Href = "../plugins/select2/select2.min-rtl.css";
            //stylefonts.Href = "../dist/css/fonts.css";
            //styleProgressBar.Href = "../css/ProgressBar-rtl.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";
            styleselect2.Href = "../plugins/select2/select2.min.css";
            styleProgressBar.Href = "../css/ProgressBar.css";
            styleAutocomp.Href = "../bootstrap/css/autoCompleteNew.css";
        }
    }

}
