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 Transactions_PopUp_CompensationEntry : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    int LevelID;
    DataSet ds = new DataSet();
    bool checkLevel;
    bool gcheckParentLevelExist;
    ShiftAllocationEvendata obj = new ShiftAllocationEvendata();
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking and reset date format
        base.Page_Load(sender, e);
        if (!IsPostBack)
        {
            btn_done1.Visible = false;
            hdnTxtField.Value = Request.QueryString["Txt"].ToString();
        }
        //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);
            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.SelectEmployee(Convert.ToInt32(Session["EmpAutoID"]),Level, Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), Convert.ToInt32(Session["currentSession"]));
            DataTable dtEmployee = new DataTable();
            ds.Tables[0].DefaultView.RowFilter = "CompOff_Eligible=True";
            dtEmployee = ds.Tables[0].DefaultView.ToTable();
            if (dtEmployee.Rows.Count > 0)
            {
                grv_Shift.DataSource = dtEmployee;
                grv_Shift.DataBind();
                grv_Shift.Visible = true;
                gcheckParentLevelExist = true;
                btn_done1.Visible = true;
            }
            else
            {
                gcheckParentLevelExist = false;
            }
            lbl_Error.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)
        {
            grv_Shift.Visible = true;
            btn_done1.Visible = true;
        }
        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);
            grv_Shift.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 = rm.GetString("btn_Go", ci);
        btn_done1.Text = 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);
    }
}
