Imports System.Configuration
Imports System.IO
Imports FarPoint.Win.Spread
Imports FarPoint.Win.Spread.Model


Namespace Intage.JTT.EP.CloseContactReport.Service.Report
    ''' <summary>
    ''' 帳票
    ''' </summary>
    Public MustInherit Class BaseReport
        Public Property RefreshReportData As Boolean = False

        ''' <summary>
        ''' 施設管理番号
        ''' </summary>
        Protected equipmentNo As String
        ''' <summary>
        ''' テンプレートフォルダー
        ''' </summary>
        Private templateFolder As String
        ''' <summary>
        ''' 保存フォルダー
        ''' </summary>
        Private workFolder As String

        ''' <summary>
        ''' 帳票フォーム
        ''' </summary>
        Private form As ReportForm
        ''' <summary>
        ''' spreadインスタンス
        ''' </summary>
        Protected fpSpread As FpSpread
        ''' <summary>
        ''' テンプレートシートビュー
        ''' </summary>
        Protected templateSheetView As SheetView

        ''' <summary>
        ''' 帳票作成時間
        ''' </summary>
        Protected ReportTime As DateTime

        ''' <summary>
        ''' ファイルの拡張子
        ''' </summary>
        ''' <returns>ファイルの拡張子</returns>
        Protected MustOverride ReadOnly Property Ext As String

        ''' <summary>
        ''' テンプレートファイル名
        ''' </summary>
        ''' <returns>テンプレートファイル名</returns>
        Protected MustOverride ReadOnly Property TemplateName As String

        ''' <summary>
        ''' 帳票名
        ''' </summary>
        ''' <returns></returns>
        Protected MustOverride ReadOnly Property ReportName As String

        ''' <summary>
        ''' 帳票の保存ファイルのパス
        ''' </summary>
        ''' <returns></returns>
        Public ReadOnly Property ReportFullName As String
            Get
                Return Path.Combine(Me.workFolder, ReportName)
            End Get
        End Property

        Sub New(ByVal templateFolder As String, ByVal workFolder As String)
            Me.equipmentNo = equipmentNo
            Me.templateFolder = templateFolder
            Me.workFolder = workFolder
            Me.ReportTime = DateTime.Now

            form = New ReportForm
            form.Show()
            form.Hide()

            Me.fpSpread = form.fpSpread
            Me.fpSpread.Open(Path.Combine(templateFolder, Me.TemplateName))

            ' フォルダ作成
            If Directory.Exists(Me.workFolder) = False Then
                Directory.CreateDirectory(Me.workFolder)
            End If

            Me.templateSheetView = Me.fpSpread.Sheets.Find("TemplatePDF")
        End Sub

        ''' <summary>
        ''' 保存と閉じる
        ''' </summary>
        Public Sub SaveAndClose()
            ' テンプレートシートを削除
            Me.fpSpread.Sheets.Remove(Me.templateSheetView)
            Me.fpSpread.ActiveSheet = Me.fpSpread.Sheets(0)

            Me.Save()
            form.Close()
            ' Me.fpSpread.Dispose()
        End Sub

        Protected Function GetNewSheet() As SheetView
            Dim newsheet As SheetView = FarPoint.Win.Serializer.LoadObjectXml(
            GetType(FarPoint.Win.Spread.SheetView),
            FarPoint.Win.Serializer.GetObjectXml(Me.templateSheetView, "CopySheet"), "CopySheet")

            Return newsheet
        End Function

        ''' <summary>
        ''' 保存
        ''' </summary>
        Protected MustOverride Sub Save()
    End Class

#Region "PDF帳票"
    ''' <summary>
    ''' PDF帳票
    ''' </summary>
    Public MustInherit Class PDFBaseReport
        Inherits BaseReport

        Protected dataSheet As SheetView

        Public Sub New()
            MyBase.New("TemplatePDF", "WorkPDF")

            dataSheet = Nothing
        End Sub

        ''' <summary>
        ''' 帳票の行数/1ページ
        ''' </summary>
        ''' <returns></returns>
        Protected MustOverride ReadOnly Property RowCount As Int32

        ''' <summary>
        ''' 帳票の列数/1ページ
        ''' </summary>
        ''' <returns></returns>
        Protected MustOverride ReadOnly Property ColumnCount As Int32

        ''' <summary>
        ''' 印刷Zoom
        ''' </summary>
        ''' <returns></returns>
        Protected MustOverride ReadOnly Property ZoomFactor As Single
        ''' <summary>
        ''' 印刷余白:左
        ''' </summary>
        ''' <returns></returns>
        Protected MustOverride ReadOnly Property Margin_Left As Int32
        ''' <summary>
        ''' 印刷余白:上
        ''' </summary>
        ''' <returns></returns>
        Protected MustOverride ReadOnly Property Margin_Top As Int32
        ''' <summary>
        ''' 印刷余白:下
        ''' </summary>
        ''' <returns></returns>
        Protected MustOverride ReadOnly Property Margin_Bottom As Int32

        ''' <summary>
        ''' ファイルの拡張子
        ''' </summary>
        ''' <returns>ファイルの拡張子</returns>
        Protected Overrides ReadOnly Property Ext As String
            Get
                Return ".pdf"
            End Get
        End Property


        ''' <summary>
        ''' 保存
        ''' </summary>
        Protected Overrides Sub Save()
            ' MyBase.fpSpread.SaveExcel(MyBase.ReportFullName + ".xlsx", FarPoint.Excel.ExcelSaveFlags.UseOOXMLFormat)

            Dim printset As PrintInfo = New PrintInfo()
            printset.PrintToPdf = True
            printset.PdfFileName = MyBase.ReportFullName
            printset.BestFitWithSpan = True
            printset.PdfWriteMode = FarPoint.Win.Spread.PdfWriteMode.New
            printset.Orientation = FarPoint.Win.Spread.PrintOrientation.Landscape
            printset.UseMax = True
            printset.Centering = Centering.Horizontal
            printset.PaperSize = New System.Drawing.Printing.PaperSize("A4", 1169, 826)
            printset.ShowBorder = False
            printset.ZoomFactor = Me.ZoomFactor

            printset.Margin.Left = Me.Margin_Left
            printset.Margin.Top = Me.Margin_Top
            printset.Margin.Bottom = Me.Margin_Bottom
            printset.Margin.Right = Me.Margin_Left

            dataSheet.PrintInfo = printset
            MyBase.fpSpread.SafePrint(MyBase.fpSpread, -1)
        End Sub

    End Class
#End Region ' PDF帳票
End Namespace
