using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web;
using System.Web.UI.HtmlControls;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                   Started Date  : 27-Sep-2007
* Module          : Punch Types                                 Last Modified : 31-Oct-2007
* Programmer      : Arun vinoth.T
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
***************************************************************************************************************************/
public partial class Master_TA_UI_Master_PunchTypes_List : Base.BasePage
{
    #region VariableDeclaration
    PunchTypesEventdata objPunchTypesEventData = new PunchTypesEventdata();
    TerminalDatatypeEventData objTerminalDatatype = new TerminalDatatypeEventData();
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    LeaveEventData objLeave = new LeaveEventData();
    DataSet ds_Checkuser = new DataSet();
    int EmployeeID;
    string gstrUserGroupName = string.Empty;
    string source_Language;
    string strScript = string.Empty;
    cls_Common objCommon = new cls_Common();
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        GC.Collect();
        base.Page_Load(sender, e);
        grv_InOut.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (!Page.IsPostBack)
        {
            base.CheckPages(68);
            objCommon.menu("Device", "68");
        }
        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);
        EmployeeID = Convert.ToInt32(Session["EmpAutoID"]);
        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);
        btn_add.Focus();
        UserRights();
        if (Session["UserGroupName"] != null)
        {
            gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        }
        if (gstrUserGroupName != "SuperAdmin")
        {
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
        DataSet ds = new DataSet();
        ds = objTerminalDatatype.Select_TerminalDatatype(Convert.ToString(Session["UserGroupName"]));
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (Convert.ToString(dr["FileType"]) == "RDBMS")
                {
                    btnsync.Enabled = true;
                    break;
                }
            }
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_PunchTypes_Add.aspx");
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            objPunchTypesEventData.DeletePunchTypes("TemporaryDelete", hdnSelectRow.Value, EmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_PunchTypesdeletedsuccessfully", ci), "TA_UI_Master_PunchTypes_List.aspx");
        }
        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)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            string[] ChkBoxIDs;
            string ChkBoxIDs1 = "";
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < ChkBoxIDs.Length; i++)
            {
                int count = objPunchTypesEventData.CheckRestorePunchTypes(Convert.ToInt32(ChkBoxIDs[i]));
                if (count > 0)
                {
                    base.Message(rm.GetString("Message_Cannot_Restore", ci));
                    hdnSelectRow.Value = "";
                    return;
                }
            }
            objPunchTypesEventData.DeletePunchTypes("Restore", hdnSelectRow.Value, EmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_PunchTypesrestoredsuccessfully", ci), "TA_UI_Master_PunchTypes_List.aspx");
        }
        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)
        {
            objPunchTypesEventData.DeletePunchTypes("PermanentDelete", hdnSelectRow.Value, EmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_PunchTypedeletedsuccessfully", ci), "TA_UI_Master_PunchTypes_List.aspx");
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void btnsync_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds = objTerminalDatatype.Select_TerminalDatatype(Convert.ToString(Session["UserGroupName"]));
        if (ds.Tables[0].Rows.Count > 0)
        {
            string constr = string.Empty;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (Convert.ToString(dr["FileType"]) == "RDBMS" && Convert.ToInt32(dr["BrandId"]) == 3 && Convert.ToString(dr["IsDeleted"]) == "False")
                {
                    constr = Encryption.Decrypt(Convert.ToString(dr["ConnectionString"]), "");
                    objPunchTypesEventData.PunchTypeSync(constr);
                }
            }
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_PunchTypessynchronizationdonesuccessfully", ci) + "');";
            strScript = strScript + "location.replace('TA_UI_Master_PunchTypes_List.aspx');";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
        }
    }
    #endregion
    #region UserDefinedFunctions
    private string CheckDelete()
    {
        string ChkBoxIDs = string.Empty;
        return (ChkBoxIDs);
    }
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int gintPunchTypeID = Convert.ToInt32(btn.CommandName);
        int count = objPunchTypesEventData.CheckPunchTypes_Delete(gintPunchTypeID);
        if (count > 0)
        {
            base.Message(rm.GetString("Masters_PunchType_Cannot_Modified", ci));
            return;
        }
        Response.Redirect("TA_UI_Master_PunchTypes_Modify.aspx?PunchType_Id=" + gintPunchTypeID);
    }
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int gintPunchTypeID = Convert.ToInt32(btn.CommandName);
        int count = objPunchTypesEventData.CheckPunchTypes_Delete(gintPunchTypeID);
        if (count > 0)
        {
            base.Message(rm.GetString("Masters_PunchType_Cannot_Delete", ci));
            return;
        }
        objPunchTypesEventData.DeletePunchTypes("PermanentDelete", Convert.ToString(gintPunchTypeID), EmployeeID);
        base.MessageWithRedirect(rm.GetString("Alert_PunchTypedeletedsuccessfully", ci), "TA_UI_Master_PunchTypes_List.aspx");
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("PunchTypes_PunchTypesHeader", ci);
        btn_add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", ci);
        btn_Delete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_Restore.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Restore", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        grv_InOut.Columns[2].Caption = rm.GetString("TerminalMaster_Terminal_Id", ci);
        grv_InOut.Columns[3].Caption = rm.GetString("PunchTypes_PunchTypes", ci);
        grv_InOut.Columns[4].Caption = rm.GetString("PunchTypes_Value", ci);
        lblDataView.Text = rm.GetString("lblDataView", ci);
        btnsync.Text = "&nbsp;&nbsp;" + rm.GetString("lbl_DeviceDataSync", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(68, 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_PermanentDelete.Visible = true;
                this.grv_InOut.Columns[1].Visible = true;
                this.grv_InOut.Columns[7].Visible = true;
            }
            else
            {
                this.btn_PermanentDelete.Visible = false;
                this.grv_InOut.Columns[1].Visible = false;
                this.grv_InOut.Columns[7].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.grv_InOut.Columns[6].Visible = true;
            }
            else
            {
                this.grv_InOut.Columns[6].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 1)
            {
                this.grv_InOut.Columns[5].Visible = true;
            }
            else
            {
                this.grv_InOut.Columns[5].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region GridviewEvents
    protected void grv_InOut_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.GridViewRowType.Data)
        {
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            Label DeletedRecord = (Label)grv_InOut.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)grv_InOut.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            LinkButton LnkbtnDelete = (LinkButton)grv_InOut.FindRowCellTemplateControl(e.VisibleIndex, null, "btnDelete");
            if (deleteChkBxItem != null)
            {
                if (deleteChkBxItem.Disabled == false)
                {
                    LnkbtnDelete.Enabled = true;
                }
                else
                {
                    LnkbtnDelete.Enabled = false;
                    LnkbtnDelete.OnClientClick = null;
                }
            }
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True")
                {
                    DeletedRecord.Text = rm.GetString("Grid_True", ci);
                    if (hdnDeletedData.Value == string.Empty)
                    {
                        hdnDeletedData.Value = Convert.ToString(e.GetValue("PunchType_Id"));
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(Convert.ToString(e.GetValue("PunchType_Id"))) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + Convert.ToString(e.GetValue("PunchType_Id"));
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
            Label lblPunchType = (Label)grv_InOut.FindRowCellTemplateControl(e.VisibleIndex, null, "lblPunchType");
            if (lblPunchType != null)
            {
                if (lblPunchType.Text.Trim() == "In")
                {
                    lblPunchType.Text = rm.GetString("PunchType_In", ci);
                }
                else if (lblPunchType.Text.Trim() == "Out")
                {
                    lblPunchType.Text = rm.GetString("PunchType_Out", ci);
                }
                else if (lblPunchType.Text.Trim() == "Shift In")
                {
                    lblPunchType.Text = rm.GetString("PunchType_ShiftIn", ci);
                }
                else if (lblPunchType.Text.Trim() == "Shift Out")
                {
                    lblPunchType.Text = rm.GetString("PunchType_ShiftOut", ci);
                }
                else if (lblPunchType.Text.Trim() == "Break In")
                {
                    lblPunchType.Text = rm.GetString("PunchType_BreakIn", ci);
                }
                else if (lblPunchType.Text.Trim() == "Break Out")
                {
                    lblPunchType.Text = rm.GetString("PunchType_BreakOut", ci);
                }
                else if (lblPunchType.Text.Trim() == "Lunch In")
                {
                    lblPunchType.Text = rm.GetString("PunchType_LunchIn", ci);
                }
                else if (lblPunchType.Text.Trim() == "Lunch Out")
                {
                    lblPunchType.Text = rm.GetString("PunchType_LunchOut", ci);
                }
                else if (lblPunchType.Text.Trim() == "In Duty")
                {
                    lblPunchType.Text = rm.GetString("PunchType_InDuty", ci);
                }
                else if (lblPunchType.Text.Trim() == "Out Duty")
                {
                    lblPunchType.Text = rm.GetString("PunchType_OutDuty", ci);
                }
                else if (lblPunchType.Text.Trim() == "Official In")
                {
                    lblPunchType.Text = rm.GetString("PunchType_OfficialIn", ci);
                }
                else if (lblPunchType.Text.Trim() == "Official Out")
                {
                    lblPunchType.Text = rm.GetString("PunchType_OfficialOut", ci);
                }
            }
        }
    }
    #endregion

    protected void lnktbnexcel_Click(object sender, EventArgs e)
    {
        LinkButton lnkbtn = (LinkButton)sender;  //Called From  Grid Report Icon click / both excel and pdf
        DisableControls();
        ASPxGridViewExporter1.Landscape = true;
        ASPxGridViewExporter1.LeftMargin = 10;
        ASPxGridViewExporter1.RightMargin = 10;
        ASPxGridViewExporter1.ExportedRowType = DevExpress.Web.GridViewExportedRowType.All;
        ASPxGridViewExporter1.MaxColumnWidth = 400;
        string saveAsFileName = string.Format("List of PunchTypes - {0:d}", DateTime.Now).Replace("/", "-").Replace(" ", "");
        ASPxGridViewExporter1.FileName = saveAsFileName;
        ASPxGridViewExporter1.PaperKind = System.Drawing.Printing.PaperKind.A3;
        switch (lnkbtn.CommandName)
        {
            case "excel":
                ASPxGridViewExporter1.WriteXlsxToResponse();
                break;
            case "pdf":
                ASPxGridViewExporter1.WritePdfToResponse();
                break;
            case "csv":
                ASPxGridViewExporter1.WriteCsvToResponse();
                break;
        }
    }
    private void DisableControls()
    {
        grv_InOut.Columns[0].Visible = false;
        grv_InOut.Columns[1].Visible = false;
        grv_InOut.Columns[5].Visible = false;
        grv_InOut.Columns[6].Visible = false;
        grv_InOut.Columns[7].Visible = false;
    }
}