Imports System.Data.SqlClient
Imports Microsoft.Reporting.WinForms
Imports System.Runtime.InteropServices
Imports System.IO


Public Class FrmRptInvoice
#Region "Mov. Pantalla"
    <DllImport("user32.Dll", EntryPoint:="ReleaseCapture")>
    Private Shared Sub ReleaseCapture()

    End Sub

    <DllImport("user32.Dll", EntryPoint:="SendMessage")>
    Private Shared Sub SendMessage(hWnd As IntPtr, wMsg As Integer, wParam As Integer, lParam As Integer)

    End Sub

#End Region
    Private Sub btnCerrar_Click(sender As Object, e As EventArgs) Handles btnCerrar.Click
        Me.Close()
    End Sub

    Private Sub PanelBarraTitulo_MouseMove(sender As Object, e As MouseEventArgs) Handles PanelBarraTitulo.MouseMove
        ReleaseCapture()
        SendMessage(Me.Handle, &H112&, &HF012&, 0)
    End Sub

    Private Sub lblEncabezado_MouseMove(sender As Object, e As MouseEventArgs) Handles lblEncabezado.MouseMove
        ReleaseCapture()
        SendMessage(Me.Handle, &H112&, &HF012&, 0)
    End Sub

    Private Sub FrmRptInvoice_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim cn As SqlConnection
            Dim ds, dsDoc As New DataSet
            Dim da As New SqlDataAdapter
            Dim Sql As String = Nothing
            Sql = "SELECT * FROM Invoices where Id=" & GlobalVariables.VarIDReg & ""
            cn = New SqlConnection(GlobalVariables.cstring)
            Dim cm As New SqlCommand(Sql, cn)
            cn.Open()
            da = New SqlDataAdapter(cm)
            ds = New DataSet()
            da.Fill(ds)

            Sql = "SELECT * FROM InvoicesDet where IdReg=" & GlobalVariables.VarIDReg & ""
            cn = New SqlConnection(GlobalVariables.cstring)
            cm = New SqlCommand(Sql, cn)
            cn.Open()
            da = New SqlDataAdapter(cm)
            dsDoc = New DataSet()
            da.Fill(dsDoc)

            '>>A PARTIR DE AQUI SE CONFIGURA EL REPORTE
            Dim strReport As String = "RptDocumento.rdlc"
            Dim rptDataSource As ReportDataSource
            Dim rptDataSource2 As ReportDataSource

            Dim ParaCoti(0) As ReportParameter
            ParaCoti(0) = New ReportParameter("Titulo", FrmInvoices.TipoReg.Text)

            With RPVDatos.LocalReport
                .ReportPath = strReport
                .DataSources.Clear()
                .SetParameters(ParaCoti)
            End With
            ' Usar el mismo nombre del dataset del diseño el reporte
            rptDataSource = New ReportDataSource("DsInvoice", ds.Tables(0))
            rptDataSource2 = New ReportDataSource("DsInvoiceDet", dsDoc.Tables(0))
            RPVDatos.LocalReport.DataSources.Add(rptDataSource)
            RPVDatos.LocalReport.DataSources.Add(rptDataSource2)
            'VERLO COMO YA SE VERIA EN PREVIEW
            RPVDatos.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout)
        Catch ex As Exception
            MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        RPVDatos.RefreshReport()
    End Sub

    Sub CreaPDF()
        Dim byteViewer As Byte() = RPVDatos.LocalReport.Render("PDF")
        Dim saveFileDialog1 As New SaveFileDialog()
        saveFileDialog1.Filter = "PDF files (.pdf)|*.pdf"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True

        Dim NombreProv As String = FrmInvoices.Cliente.Text
        Dim ParteNombre() As String = NombreProv.Split(" ")

        Dim newFile As New FileStream("C:\IRGodam\PDFS\" & FrmInvoices.Folio.Text & ParteNombre(0) & ".pdf", FileMode.Create)
        newFile.Write(byteViewer, 0, byteViewer.Length)
        newFile.Close()
    End Sub

    Dim lx, ly As Integer
    Dim sw, sh As Integer
    Dim VarVentanaEstatus As Boolean
    Private Sub PanelBarraTitulo_DoubleClick(sender As Object, e As EventArgs) Handles PanelBarraTitulo.DoubleClick
        If VarVentanaEstatus = False Then
            lx = Me.Location.X
            ly = Me.Location.Y
            sw = Me.Size.Width
            sh = Me.Size.Height

            'btnMaximizar.Visible = False
            ' btnRestaurar.Visible = True
            Me.Size = Screen.PrimaryScreen.WorkingArea.Size
            Me.Location = Screen.PrimaryScreen.WorkingArea.Location
            VarVentanaEstatus = True

        ElseIf VarVentanaEstatus = True Then
            Me.Size = New Size(sw, sh)
            Me.Location = New Point(lx, ly)
            ' btnMaximizar.Visible = True
            'btnRestaurar.Visible = False
            VarVentanaEstatus = False
        End If

    End Sub
End Class