using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Masters_TA_UI_Master_DashboardRights : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    userRightsEventData objUserRight = new userRightsEventData();
    cls_Common objCommon = new cls_Common();
    DataSet ds = new DataSet();
    int employeeId = 0;
    CultureInfo ci;
    #endregion
    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;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        // LoadStrings Function fills the Label from the Resource File
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        if (!Page.IsPostBack)
        {
            objCommon.menu("Previlege", "21");
            LoadStrings(ci);
            string strSetAlert = "";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "','" + rm.GetString("Alert_Selectanyonedisplayoption", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
            BindBoardDetails();
        }
    }
    private void BindBoardDetails()
    {
        DataSet ds = new DataSet();
        ds = objUserRight.SelectDashboardRights(Convert.ToInt32(Request.QueryString["gId"]));
        int firstlistItemcount = 0;
        int secondlistItemcount = 0;
        int ThirdlistItemcount = 0;
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                chklstAttendance.DataSource = ds.Tables[0];
                chklstAttendance.DataValueField = "DataId";
                chklstAttendance.DataTextField = "BoardChildName";
                chklstAttendance.DataBind();
                foreach (ListItem list in chklstAttendance.Items)
                {
                    if (list.Text == "Present")
                        list.Text = rm.GetString("AttendanceCode_Present", ci);
                    else if (list.Text == "Absent")
                        list.Text = rm.GetString("Dash_Absent", ci);
                    else if (list.Text == "Leave Taken")
                        list.Text = rm.GetString("BonusLeaveAllocation_LeaveTaken", ci);
                    else if (list.Text == "OnDuty")
                        list.Text = rm.GetString("Dash_OnDuty", ci);
                    else if (list.Text == "Punctuality")
                        list.Text = rm.GetString("Dash_Punctuality", ci);
                    else if (list.Text == "Attendance Summary")
                        list.Text = rm.GetString("Dashboard_AttSum", ci);
                    else if (list.Text == "Approved Leave")
                        list.Text = rm.GetString("Dash_ApprovedLeave", ci);
                    else if (list.Text == "Approved Onduty")
                        list.Text = rm.GetString("Dash_ApprovedOnDuty", ci);
                }

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ListItem AttendanceListItem = chklstAttendance.Items.FindByValue(Convert.ToString(ds.Tables[0].Rows[i]["DataId"]));
                    if (AttendanceListItem != null)
                    {
                        if (Convert.ToString(ds.Tables[0].Rows[i]["ViewPrev"]) == "1")
                        {
                            AttendanceListItem.Selected = true;
                            firstlistItemcount += 1;
                        }
                    }
                }
                if (firstlistItemcount == chklstAttendance.Items.Count)
                {
                    cbAttendanceAll.Checked = true;
                }
            }
            if (ds.Tables[1].Rows.Count > 0)
            {
                chklstApproval.DataSource = ds.Tables[1];
                chklstApproval.DataValueField = "DataId";
                chklstApproval.DataTextField = "BoardChildName";
                chklstApproval.DataBind();
                foreach (ListItem list in chklstApproval.Items)
                {
                    if (list.Text == "Leave")
                        list.Text = rm.GetString("LeaveApplication_Leave", ci);
                    else if (list.Text == "Onduty")
                        list.Text = rm.GetString("Dash_OnDuty", ci);
                    else if (list.Text == "Permission")
                        list.Text = rm.GetString("Dash_Permission", ci);
                }
                for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
                {
                    ListItem ApprovalListItem = chklstApproval.Items.FindByValue(Convert.ToString(ds.Tables[1].Rows[i]["DataId"]));
                    if (ApprovalListItem != null)
                    {
                        if (Convert.ToString(ds.Tables[1].Rows[i]["ViewPrev"]) == "1")
                        {
                            ApprovalListItem.Selected = true;
                            secondlistItemcount += 1;
                        }
                    }
                }
                if (secondlistItemcount == chklstApproval.Items.Count)
                {
                    cbApprovalAll.Checked = true;
                }
            }
            if (ds.Tables[2].Rows.Count > 0)
            {
                chklstException.DataSource = ds.Tables[2];
                chklstException.DataValueField = "DataId";
                chklstException.DataTextField = "BoardChildName";
                chklstException.DataBind();

                foreach (ListItem list in chklstException.Items)
                {
                    if (list.Text == "Late Entry")
                        list.Text = rm.GetString("Dashboard_Late", ci);
                    else if (list.Text == "Early Exit")
                        list.Text = rm.GetString("Dash_EarlyExit", ci);
                    else if (list.Text == "Less Hours Worked")
                        list.Text = rm.GetString("Dash_LessHoursWorked", ci);
                    else if (list.Text == "Overtime")
                        list.Text = rm.GetString("Menu_Reports_OverTime", ci);
                    else if (list.Text == "Discrepancy")
                        list.Text = rm.GetString("Report_Discrepancy", ci);
                    else if (list.Text == "Approved Overtime")
                        list.Text = rm.GetString("lbl_ApprovedOvertime", ci);
                    else if (list.Text == "Non Processed Record")
                        list.Text = rm.GetString("Dash_NonProcessedRecord", ci);
                }

                for (int i = 0; i < ds.Tables[2].Rows.Count; i++)
                {
                    ListItem ExceptionListItem = chklstException.Items.FindByValue(Convert.ToString(ds.Tables[2].Rows[i]["DataId"]));
                    if (ExceptionListItem != null)
                    {
                        if (Convert.ToString(ds.Tables[2].Rows[i]["ViewPrev"]) == "1")
                        {
                            ExceptionListItem.Selected = true;
                            ThirdlistItemcount += 1;
                        }
                    }
                }
                if (ThirdlistItemcount == chklstException.Items.Count)
                {
                    cbExcepAll.Checked = true;
                }
            }
            if (ds.Tables[3].Rows.Count > 0)
            {
                if (Convert.ToInt16(ds.Tables[3].Rows[0][0]) == 1)
                {
                    chkbxDash.Checked = true;
                }
                if (Convert.ToInt16(ds.Tables[3].Rows[0][1]) == 1)
                {
                    chkbxGrid.Checked = true;
                }
                if (Convert.ToInt16(ds.Tables[3].Rows[0][2]) == 1)
                {
                    chkbxChart.Checked = true;
                }
            }
            if (ds.Tables[4].Rows.Count > 0)
            {
                this.lblGroupName.Text = Convert.ToString(ds.Tables[4].Rows[0]["groupName"]);
            }
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderUserGroups.Text = rm.GetString("Menu_Masters_UserGroups", ci);
        lbl_UserGroup.Text = rm.GetString("UserGroups_UserGroupName", ci) + " :";
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        lblDashboardRights.Text = rm.GetString("lblDashboardRights", ci);
        lblAttendanceBoard.Text = rm.GetString("Dashboard_AttData", ci);
        lblApprovalBoard.Text = rm.GetString("Dashboard_AppData", ci);
        lblExceptionData.Text = rm.GetString("Dashboard_ExcData", ci);
        lblDisplay.Text = rm.GetString("lblDisplay", ci);
        cbAttendanceAll.Text = rm.GetString("cbAll", ci);
        cbApprovalAll.Text = rm.GetString("cbAll", ci);
        cbExcepAll.Text = rm.GetString("cbAll", ci);
        chkbxDash.Text = rm.GetString("Menu_Master_Dashboard", ci);
        chkbxGrid.Text = rm.GetString("Dash_Grid", ci);
        chkbxChart.Text = rm.GetString("Dash_Chart", ci);
    }

    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_groupView.aspx");
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        string strScript = "";
        if (chkbxDash.Checked == false && chkbxGrid.Checked == false && chkbxChart.Checked == false)
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_Selectanyonedisplayoption", ci) + "');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            return;
        }
        else
        {
            InsertDashboardRightsDetails(chklstAttendance, "AttendanceData");
            InsertDashboardRightsDetails(chklstApproval, "ApprovalData");
            InsertDashboardRightsDetails(chklstException, "ExceptionData");
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_DashboardRightssavedsuccessfully", ci) + "');";
            strScript = strScript + "location.replace('TA_UI_Master_groupView.aspx');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            return;
        }
    }
    private void InsertDashboardRightsDetails(CheckBoxList chkboxlist, string BoardName)
    {
        for (int i = 0; i < chkboxlist.Items.Count; i++)
        {
            objUserRight.InsertDashboardRightDetails
                (
                Convert.ToString(lblGroupName.Text),
                Convert.ToInt16(chkboxlist.Items[i].Value),
                chkbxDash.Checked == true ? 1 : 0,
                chkbxGrid.Checked == true ? 1 : 0,
                chkbxChart.Checked == true ? 1 : 0,
                Convert.ToInt32(Session["EmpAutoId"]),
                chkboxlist.Items[i].Selected == true ? 1 : 0
                );
        }
    }
}