Imports CAMS_ADMIN
'Imports CAMS_BL
'Imports System.IO
'Imports System.Diagnostics
'Imports System.GC
'Imports System.Runtime.InteropServices.Marshal
'Imports System.Globalization
'Imports System.Data
'Imports System.ComponentModel
'Imports System.Runtime.InteropServices
'Imports Microsoft.Office.Interop
'Imports Excel

Public Class frmWorkOffline
    Inherits CAMSBasePage
    Dim sRootExcelFile As String

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object
    Protected WithEvents btnGenerate As System.Web.UI.WebControls.Button
    Protected WithEvents mnuCams As Menu

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Access Validation to page
        If Not Me.IsPostBack Then
            Dim oEC_User As EC_User = Me.GetUserFromSession

            If Not oEC_User Is Nothing Then
                If Me.HasAccessTo(Me.GetControlID("frmWorkOffline.aspx"), oEC_User.AccessGroup) = False Then
                    Me.RedirectToMsgPage(Constants.CONST_ERROR_ACCESSDENIED_TOPAGE)
                End If
            End If

        End If
        'End Access Validation

        'Menu
        mnuCams.CurrentGroup = Constants.MENU_SPREADSHEET_ID
        mnuCams.CurrentItem = Constants.MENU_SPREADSHEET_WOFFLINE_ID
        'End Menu

        'Added by Raul Callupe
        If Me.IsPostBack = False Then
            Dim stbOutput As New System.Text.StringBuilder
            stbOutput.Append("<script language='Javascript'>")
            stbOutput.Append("var pag = window.parent.frames(0);")
            stbOutput.Append("pag.document.all.lblPath.innerHTML='Spreadsheet/Work Offline'")
            stbOutput.Append("</script>")
            Response.Write(stbOutput.ToString)
        End If
        'End Added

    End Sub


    'Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
    '    GenerateExcel()
    'End Sub


    'Private Sub GenerateExcel()

    '    Try
    '        'Set the Language Option to "en-US"
    '        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")

    '        'Creates and Open an instance of excel to verify if file is standardized 
    '        Dim oXL As Excel.Application
    '        Dim oWB As Excel.Workbook
    '        Dim oSheet As Excel.Worksheet
    '        Dim Range As Excel.Range

    '        '-----------------------------
    '        oXL = New Excel.Application
    '        oXL.Visible = True
    '        Dim sFileName As String = Me.Session.SessionID.ToString & Date.Now.Ticks.ToString & ".xls"
    '        File.Copy(Server.MapPath("./") & "Templates\WorkOffline.xls", Server.MapPath("./") & "Templates\" & sFileName, True)
    '        sRootExcelFile = Server.MapPath("./") & "Templates\" & sFileName
    '        oWB = oXL.Workbooks.Open(sRootExcelFile)
    '        oSheet = CType(oWB.Worksheets.Item(1), Excel.Worksheet)
    '        oXL.DisplayAlerts = False
    '        '---------------------


    '        Dim oEC_User As EC_User = Me.GetUserFromSession
    '        Dim oCC_Case As New CC_Case
    '        Dim i, j As Integer
    '        Dim dsCases As DataSet
    '        Dim row As DataRow
    '        dsCases = oCC_Case.GetWorkOffline(oEC_User.UserID, oEC_User.AccessGroup)

    '        With oSheet
    '            '.Unprotect()
    '            If dsCases.Tables(0).Rows.Count > 0 Then
    '                For j = 0 To dsCases.Tables(0).Rows.Count - 1
    '                    i = j + 1
    '                    .Cells(i + 1, 1) = dsCases.Tables(0).Rows(j)("CASEID").ToString
    '                    .Cells(i + 1, 2) = dsCases.Tables(0).Rows(j)("SURNAME").ToString
    '                    .Cells(i + 1, 3) = dsCases.Tables(0).Rows(j)("NAME").ToString
    '                    .Cells(i + 1, 4) = dsCases.Tables(0).Rows(j)("TITLE").ToString
    '                    .Cells(i + 1, 5) = dsCases.Tables(0).Rows(j)("ADDRESS").ToString
    '                    .Cells(i + 1, 6) = dsCases.Tables(0).Rows(j)("APPLDOB").ToString
    '                    .Cells(i + 1, 7) = dsCases.Tables(0).Rows(j)("TELEPHONE").ToString
    '                    .Cells(i + 1, 8) = dsCases.Tables(0).Rows(j)("APPLIDN").ToString
    '                    .Cells(i + 1, 9) = dsCases.Tables(0).Rows(j)("HA").ToString
    '                    .Cells(i + 1, 10) = dsCases.Tables(0).Rows(j)("OGREFSCHE").ToString
    '                    .Cells(i + 1, 11) = dsCases.Tables(0).Rows(j)("OGCCONTACT").ToString
    '                    .Cells(i + 1, 12) = dsCases.Tables(0).Rows(j)("OGCVISITED").ToString
    '                    .Cells(i + 1, 13) = dsCases.Tables(0).Rows(j)("OGAMDSCHE").ToString
    '                    .Cells(i + 1, 14) = dsCases.Tables(0).Rows(j)("GRANTOFFICEID").ToString
    '                    .Cells(i + 1, 15) = dsCases.Tables(0).Rows(j)("GRANTTYPEID").ToString
    '                    .Cells(i + 1, 16) = dsCases.Tables(0).Rows(j)("OGREFNUM").ToString
    '                    .Cells(i + 1, 17) = dsCases.Tables(0).Rows(j)("OGSGRANTTYPEID").ToString
    '                    .Cells(i + 1, 18) = dsCases.Tables(0).Rows(j)("OGSREFNUM").ToString
    '                    .Cells(i + 1, 19) = dsCases.Tables(0).Rows(j)("APPDATE").ToString
    '                    .Cells(i + 1, 20) = dsCases.Tables(0).Rows(j)("APPAPROVAL").ToString
    '                    .Cells(i + 1, 21) = dsCases.Tables(0).Rows(j)("APPREVSCH").ToString
    '                    .Cells(i + 1, 22) = dsCases.Tables(0).Rows(j)("CONTRACTOR").ToString
    '                    .Cells(i + 1, 23) = dsCases.Tables(0).Rows(j)("SL21ONSITE").ToString
    '                    .Cells(i + 1, 24) = dsCases.Tables(0).Rows(j)("WCCOS").ToString
    '                    .Cells(i + 1, 25) = dsCases.Tables(0).Rows(j)("WCCOMP").ToString
    '                    .Cells(i + 1, 26) = dsCases.Tables(0).Rows(j)("WCOVERCOST").ToString
    '                    .Cells(i + 1, 27) = dsCases.Tables(0).Rows(j)("WCGRANT").ToString
    '                    .Cells(i + 1, 28) = dsCases.Tables(0).Rows(j)("WCSOVERCOST").ToString
    '                    .Cells(i + 1, 29) = dsCases.Tables(0).Rows(j)("WCSGRANT").ToString
    '                    .Cells(i + 1, 30) = dsCases.Tables(0).Rows(j)("STATUS").ToString
    '                    .Cells(i + 1, 31) = dsCases.Tables(0).Rows(j)("CANCDATE").ToString
    '                    .Cells(i + 1, 32) = dsCases.Tables(0).Rows(j)("REASONID").ToString
    '                    .Cells(i + 1, 33) = dsCases.Tables(0).Rows(j)("COMMENTS").ToString
    '                Next

    '                'Formatting cells
    '                .Cells(1, 30) = "STATUS"
    '                .Range("A:E, G:I, N:R, V:V, AD:AD, AF:AG").NumberFormat = "@"
    '                '.Range("V:V").NumberFormat = "0"
    '                .Range("Z:AC").NumberFormat = "#,##0.00"
    '                .Range("F:F, J:M, S:U, W:Y, AE:AE").NumberFormat = "dd-mmm-yyyy"
    '                Dim strName As String = ValidateName("Cases belonging to " & Left(oEC_User.Name, 1) & ". " & oEC_User.Surname)
    '                .Name = strName
    '                '.Protect()
    '                oWB.Save()
    '                '*************************************
    '            End If
    '        End With


    '        If Not oWB Is Nothing Then
    '            DeleteReferences(oSheet)
    '            oXL.Workbooks(1).Close(False)
    '            DeleteReferences(oWB)
    '            oXL.Quit()
    '            DeleteReferences(oXL)
    '            System.GC.Collect()
    '        End If


    '        Dim fs As FileStream
    '        fs = New FileStream(sRootExcelFile, FileMode.Open, FileAccess.Read)
    '        Dim buffer(fs.Length) As Byte
    '        fs.Read(buffer, 0, buffer.Length)
    '        fs.Close()
    '        If File.Exists(sRootExcelFile) Then File.Delete(sRootExcelFile)

    '        Dim ms As MemoryStream
    '        ms = New MemoryStream
    '        ms.Write(buffer, 0, buffer.Length)
    '        Response.ContentType = "application/octet-stream"
    '        Response.AddHeader("Content-Disposition", "attachment; filename=WorkOffline.xls")
    '        'Get file data into a byte array.
    '        Dim obuffer(ms.Length) As Byte

    '        ms.Position = 0
    '        ms.Read(obuffer, 0, Convert.ToInt32(ms.Length))
    '        'Send the file to the browser.
    '        Response.BinaryWrite(obuffer)
    '        ms.Close()

    '    Catch Exc As Exception
    '        'Response.Write(Exc.Message)
    '    End Try
    'End Sub

    'Private Sub DeleteReferences(ByRef References As Object)
    '    Try
    '        Do Until System.Runtime.InteropServices.Marshal.ReleaseComObject(References) <= 0
    '        Loop
    '    Catch
    '    Finally
    '        References = Nothing
    '    End Try
    'End Sub

    'Public Function ValidateName(ByVal strName As String) As String
    '    If strName.Length > 0 Then Replace(strName, "/", "")
    '    If strName.Length > 0 Then Replace(strName, "*", "")
    '    If strName.Length > 0 Then Replace(strName, "\", "")
    '    If strName.Length > 0 Then Replace(strName, "?", "")
    '    If strName.Length > 0 Then Replace(strName, "[", "")
    '    If strName.Length > 0 Then Replace(strName, "]", "")
    '    If strName.Length > 31 Then Left(strName, 30)
    '    Return strName
    'End Function
End Class
