using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Masters_TA_UI_Master_AttandanceCode : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    GenericEventdata objGeneric = new GenericEventdata();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objcommon = new cls_Common();
    string gstrUserGroupName = string.Empty;
    int auEmpId;
    AttandanceCodeEventData objClass = new AttandanceCodeEventData();
    LeaveEventData objLeave = new LeaveEventData();
    string source_Language; string strScript = string.Empty;
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        gv.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            objcommon.menu("Others", "99");
        }
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "RestoreAlert('" + rm.GetString("Alert_Norecordsfound", ci) + "','" + rm.GetString("Alert_Areyousuretorestore", ci) + "','" + rm.GetString("Alert_Selecteddataisnotindeletedstatus", ci) + "','" + rm.GetString("Alert_Selectdatatorestore", ci) + "','" + rm.GetString("Alert_Nodeletedrecordsavailable", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("RestoreAlert", strSetAlert);
        auEmpId = Convert.ToInt32(Session["EmpAutoID"].ToString());
        //Set UserRights
        UserRights();
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        if (gstrUserGroupName == "SuperAdmin")
        {
            btn_Restore.Visible = false;            
            //gv.Columns[5].Visible = true;
        }
        else
        {
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
            //gv.Columns[5].Visible = false;
        }
        btn_add.Focus();
    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_AttandanceCodeAdd.aspx");
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            objClass.DeleteAttandance(hdnSelectRow.Value, auEmpId);
            string str = "alert('" + rm.GetString("Alert_AttandanceCodedeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_AttandanceCode.aspx');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void btn_Restore_Click(object sender, EventArgs e)
    {
        string[] ChkBoxIDs;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < ChkBoxIDs.Length; i++)
            {
                DataSet dsCheck = new DataSet();
                dsCheck = objClass.CheckAttandance(Convert.ToInt32(ChkBoxIDs[i]), "", "", 0, "U");
                if (dsCheck != null && dsCheck.Tables.Count > 0 && dsCheck.Tables[0].Rows.Count > 0)
                {
                    if (dsCheck.Tables[0].Rows[0]["RecCount"] != DBNull.Value)
                    {
                        if (Convert.ToInt16(dsCheck.Tables[0].Rows[0]["RecCount"]) > 0)
                        {
                            string strScript = "alert('" + rm.GetString("Alert_AttandanceCodealreadyexists", ci) + "');";
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", strScript, true);
                            return;
                        }
                    }
                }
                objClass.RestoreAttandance(Convert.ToInt32(ChkBoxIDs[i]), auEmpId);
            }
            string str = "alert('" + rm.GetString("Alert_AttandanceCoderestoredsuccessfully", ci) + "');location.replace('TA_UI_Master_AttandanceCode.aspx');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            objClass.PremanentDeleteAttandance(hdnSelectRow.Value, auEmpId);
            string str = "alert('" + rm.GetString("Alert_AttandanceCodedeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_AttandanceCode.aspx');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("AttandanceCode", ci);
        btn_add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", ci);
        btn_Delete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_Restore.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Restore", ci);
        gv.Columns[2].Caption = rm.GetString("Master_AttandanceCode", ci);
        gv.Columns[3].Caption = rm.GetString("Attandance", ci);
        gv.Columns[4].Caption = rm.GetString("EntranceMaster_Company", ci);        
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(99, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
            {
                this.btn_add.Visible = true;
            }
            else
            {
                this.btn_add.Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["deletePrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["deletePrv"]) == 1)
            {
                this.btn_Delete.Visible = false;
                this.gv.Columns[0].Visible = true;
                this.gv.Columns[6].Visible = true;
            }
            else
            {
                this.btn_Delete.Visible = false;
                this.gv.Columns[0].Visible = false;
                this.gv.Columns[6].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.gv.Columns[5].Visible = true;
            }
            else
            {
                this.gv.Columns[5].Visible = false;
            }            
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    protected void gv_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        if (e.RowType == DevExpress.Web.GridViewRowType.Data)
        {
            Label DeletedRecord = (Label)gv.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True")
                {
                    DeletedRecord.Text = rm.GetString("Grid_True", ci);
                    if (hdnDeletedData.Value == string.Empty)
                    {
                        hdnDeletedData.Value = e.GetValue("AttandanceID").ToString();
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(e.GetValue("AttandanceID").ToString()) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + e.GetValue("AttandanceID").ToString();
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
            Label lblAttandance = (Label)gv.FindRowCellTemplateControl(e.VisibleIndex, null, "lblAttandanceValue");
            if (lblAttandance.Text.Trim() == "Absent")
            {
                lblAttandance.Text = rm.GetString("Dash_Absent", ci);
            }
            else if (lblAttandance.Text.Trim() == "Present")
            {
                lblAttandance.Text = rm.GetString("AttendanceCode_Present", ci);
            }
            else if (lblAttandance.Text.Trim() == "Leave")
            {
                lblAttandance.Text = rm.GetString("LeaveApplication_Leave", ci);
            }
        }
    }
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        //ToDo: 23-11-2015
        LinkButton btn = sender as LinkButton;
        int IntAttcode = Convert.ToInt32(btn.CommandName);
        Response.Redirect("TA_UI_Master_AttandanceCodeModify.aspx?Id=" + IntAttcode);
    }
    protected void btn_delete_Click_1(object sender, EventArgs e)
    {
        //ToDo: 23-11-2015
        LinkButton btn = sender as LinkButton;
        int intAttandanceID = Convert.ToInt32(btn.CommandName);
        objClass.PremanentDeleteAttandance(Convert.ToString(intAttandanceID), auEmpId);
        string str = "alert('" + rm.GetString("Alert_AttandanceCodedeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_AttandanceCode.aspx');";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);

    }
}
