Option Strict Off
Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
Imports Microsoft.Office.Interop.Excel.Constants
Imports Microsoft.Office.Interop.Excel.XlBordersIndex
Imports Microsoft.Office.Interop.Excel.XlLineStyle
Imports Microsoft.Office.Interop.Excel.XlBorderWeight
Imports Microsoft.Office.Interop.Excel.XlThemeFont
Imports Microsoft.Office.Interop.Excel.XlThemeColor
Imports Microsoft.Office.Interop.Excel.XlWindowState
Imports Microsoft.Office.Interop.Excel
Imports System.IO
Imports Microsoft.Office.Core
Public Class 訪談日報表
    Dim xlApp As Application
    Dim xlBook As Workbook
    Dim xlSheet As Worksheet
    Dim 已開啟 As Boolean
    Private Sub Set_訪談清單()
        Dim ds1 As New DataSet
        訪談紀錄_dgv.DataSource = Nothing : ds1.Clear()
        訪談紀錄_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        訪談紀錄_dgv.ColumnHeadersHeight = 25 : 訪談紀錄_dgv.AllowUserToAddRows = False : 訪談紀錄_dgv.RowTemplate.Height = 35
        Dim 集團 As String : If 集團_cb.Text = "" Then : 集團 = " AND (客戶資料表.集團 LIKE N'%' OR 客戶資料表.集團 IS NULL ) " : Else : 集團 = " AND (客戶資料表.集團 LIKE N'%" & 集團_cb.Text & "%' ) " : End If
        Dim YS員工 As String : If 業務員_cb.Text = "" Or 業務員_cb.Text = "所有人" Then : YS員工 = " AND ( 客戶訪談資料.訪談人 LIKE N'%' OR  客戶訪談資料.訪談人 IS NULL ) " : Else : YS員工 = " AND ( 客戶訪談資料.訪談人 LIKE N'%" & 業務員_cb.Text & "%') " : End If
        Dim 洽談物件 As String : If 洽談物件_cb.Text = "" Then : 洽談物件 = " AND ( 客戶訪談資料.洽談物件 LIKE N'%' OR 客戶訪談資料.洽談物件 IS NULL ) " : Else : 洽談物件 = " AND ( 客戶訪談資料.洽談物件 LIKE N'" & 洽談物件_cb.Text & "' ) " : End If
        SQL_訪談日報表(集團, 洽談物件, YS員工)
        da.Fill(ds1) : 訪談紀錄_dgv.DataSource = ds1.Tables(0) : conn.Close()

        訪談紀錄_dgv.Columns("項次").FillWeight = 15 : 訪談紀錄_dgv.Columns("流水號").Visible = False : 訪談紀錄_dgv.Columns("客戶編號").Visible = False
        訪談紀錄_dgv.Columns("日期").FillWeight = 50 : 訪談紀錄_dgv.Columns("客戶名稱").FillWeight = 30 : 訪談紀錄_dgv.Columns("姓名").FillWeight = 30
        訪談紀錄_dgv.Columns("電話").FillWeight = 50 : 訪談紀錄_dgv.Columns("聯繫方式").FillWeight = 50 : 訪談紀錄_dgv.Columns("洽談方式").FillWeight = 50
        訪談紀錄_dgv.Columns("YS員工").FillWeight = 30 : 訪談紀錄_dgv.Columns("洽談物件").FillWeight = 50 : 訪談紀錄_dgv.Columns("洽談時間").FillWeight = 30
        訪談紀錄_dgv.Columns("訪談內容").FillWeight = 160
        訪談紀錄_dgv.Columns("項次").ReadOnly = True : 訪談紀錄_dgv.Columns("日期").ReadOnly = True : 訪談紀錄_dgv.Columns("客戶名稱").ReadOnly = True
        訪談紀錄_dgv.Columns("姓名").ReadOnly = True : 訪談紀錄_dgv.Columns("電話").ReadOnly = True : 訪談紀錄_dgv.Columns("聯繫方式").ReadOnly = True
        訪談紀錄_dgv.Columns("YS員工").ReadOnly = True : 訪談紀錄_dgv.Columns("洽談物件").ReadOnly = True
        訪談紀錄_dgv.EditMode = DataGridViewEditMode.EditOnEnter
        For i As Integer = 0 To 訪談紀錄_dgv.Rows.Count - 1 : 訪談紀錄_dgv.Rows(i).Cells(0).Value = i + 1 : Next
    End Sub
    Private Sub 確認新增_bt_MouseEnter(sender As Object, e As EventArgs) Handles 確認新增_bt.MouseEnter
        ToolTip1.SetToolTip(Me.確認新增_bt, "資料存檔")
    End Sub
    Private Sub 列印資料表_bt_MouseEnter(sender As Object, e As EventArgs) Handles 列印資料表_bt.MouseEnter
        ToolTip1.SetToolTip(Me.列印資料表_bt, "列印訪談日報表")
    End Sub
    Private Sub 訪談日報表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        已開啟 = False : Me.MdiParent = YS_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
    End Sub
    Private Sub 訪談日報表_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        'SQL_客戶類別() : 分類1_cb.Items.Clear() : 分類1_cb.Items.Add("") : While (dr.Read()) : 分類1_cb.Items.Add(dr("類別")) : End While : conn.Close()
        SQL_集團類別() : 集團_cb.Items.Clear() : 集團_cb.Items.Add("") : While (dr.Read()) : 集團_cb.Items.Add(dr("集團")) : End While : conn.Close()
        SQL_業務員類別() : 業務員_cb.Items.Clear() : 業務員_cb.Items.Add("所有人") : While (dr.Read()) : 業務員_cb.Items.Add(dr("姓名")) : End While : conn.Close()
        If 登入人級別 <= 5 Then : 業務員_cb.Text = "所有人" : 業務員_cb.Enabled = True : Else : 業務員_cb.Text = gUserName : 業務員_cb.Enabled = False : End If
        Dim 訪談人 As String : If 業務員_cb.Text = "所有人" Or 業務員_cb.Text = "" Then : 訪談人 = "%" : Else : 訪談人 = 業務員_cb.Text : End If

        SQL_洽談物件類別(訪談人) : 洽談物件_cb.Items.Clear() : 洽談物件_cb.Items.Add("") : While (dr.Read()) : 洽談物件_cb.Items.Add(dr("洽談物件")) : End While : conn.Close()
        Set_訪談清單()
        已開啟 = True
    End Sub
    Private Sub 介面關閉_Closed(sender As Object, e As EventArgs) Handles MyBase.Closed
        MyModule1.虛擬桌面開啟()
    End Sub
    Private Sub 登入閒置控制_MouseMove(sender As Object, e As EventArgs) Handles MyBase.MouseMove
        timeNow = 0 : YS_ERP_SYS.Timer1.Enabled = False : YS_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 登入閒置控制_KeyPress(sender As Object, e As EventArgs) Handles MyBase.KeyPress
        timeNow = 0 : YS_ERP_SYS.Timer1.Enabled = False : YS_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 訪談紀錄_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 訪談紀錄_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 1.5)
        If e.RowIndex = 訪談紀錄_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 訪談紀錄_dgv("客戶名稱", e.RowIndex).Value.ToString <> 訪談紀錄_dgv("客戶名稱", e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(訪談紀錄_dgv.RowHeadersVisible, 訪談紀錄_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 訪談紀錄_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 訪談紀錄_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub 一日_bt_Click(sender As Object, e As EventArgs) Handles 一日_bt.Click
        開始日期_dtp.Value = 結束日期_dtp.Value : Set_訪談清單()
    End Sub
    Private Sub 一周_bt_Click(sender As Object, e As EventArgs) Handles 一周_bt.Click
        開始日期_dtp.Value = 結束日期_dtp.Value.AddDays(-6) : Set_訪談清單()
    End Sub
    Private Sub 一月_bt_Click(sender As Object, e As EventArgs) Handles 一月_bt.Click
        開始日期_dtp.Value = 結束日期_dtp.Value.AddDays(-30) : Set_訪談清單()
    End Sub
    Private Sub 一年_bt_Click(sender As Object, e As EventArgs) Handles 一年_bt.Click
        開始日期_dtp.Value = 結束日期_dtp.Value.AddDays(-364) : Set_訪談清單()
    End Sub
    Private Sub 關鍵字_tb_KeyDown(sender As Object, e As KeyEventArgs) Handles 關鍵字_tb.KeyDown
        If e.KeyCode = Keys.Enter Then : Set_訪談清單() : End If
    End Sub

    Private Sub 查詢_bt_Click(sender As Object, e As EventArgs) Handles 查詢_bt.Click
        Set_訪談清單()
    End Sub
    Private Sub 確認新增_bt_Click(sender As Object, e As EventArgs) Handles 確認新增_bt.Click
        For I As Integer = 0 To 訪談紀錄_dgv.Rows.Count - 1
            PA1 = 訪談紀錄_dgv.Rows(I).Cells("流水號").Value.ToString : PA2 = 訪談紀錄_dgv.Rows(I).Cells("訪談內容").Value.ToString
            PA3 = 訪談紀錄_dgv.Rows(I).Cells("內容追蹤").Value.ToString : PA4 = 訪談紀錄_dgv.Rows(I).Cells("洽談方式").Value.ToString
            Dim 訪談人 As String = 訪談紀錄_dgv.Rows(I).Cells("YS員工").Value.ToString : Dim 洽談物件 As String = 訪談紀錄_dgv.Rows(I).Cells("洽談物件").Value.ToString
            Dim 洽談時間 As String = 訪談紀錄_dgv.Rows(I).Cells("洽談時間").Value.ToString
            SQL_訪談表修改(訪談人, 洽談物件, 洽談時間)
        Next : MsgBox("存檔完成!!")
    End Sub
    Private Shared Function NewMethod(xlBook As Workbook) As Worksheet
        Return CType(xlBook.Worksheets.Add, Worksheet)
    End Function
    Private Sub 列印資料表_bt_Click(sender As Object, e As EventArgs) Handles 列印資料表_bt.Click
        xlApp = CType(CreateObject("Excel.Application"), Application)
        xlBook = xlApp.Workbooks.Add : xlApp.DisplayAlerts = True : xlBook.Activate() : xlSheet = NewMethod(xlBook) : xlSheet.Activate()
        xlApp.Visible = True : xlApp.Application.WindowState = xlMaximized

        Dim imagePath As String = System.Windows.Forms.Application.StartupPath & "\YSLOGO2.png"
        Dim cell As Range = xlSheet.Cells(1, 1) : Dim left As Single = CType(cell.Left, Single) : Dim top As Single = CType(cell.Top, Single)
        Dim width As Single = 80 : Dim height As Single = 32
        If Not System.IO.File.Exists(imagePath) Then : MessageBox.Show("圖片文件未找到: " & imagePath) : Return : End If
        xlSheet.Shapes.AddPicture(imagePath, MsoTriState.msoFalse, MsoTriState.msoCTrue, left, top, width, height)
        xlSheet.Cells(2, 9) = "日期: " & DateTime.Today.ToShortDateString()
        xlSheet.Cells(2, 6) = Label18.Text
        xlSheet.Cells(3, 2) = "洽談內容"
        xlSheet.Cells(3, 9) = "製表: " & gUserName
        Dim columnIndex As Integer = 1 ' Excel 的列索引從 1 開始
        For Each column As DataGridViewColumn In 訪談紀錄_dgv.Columns
            If column.Visible Then
                If column.Name = "姓名" Or column.Name = "電話" Or column.Name = "聯繫方式" Then : Else
                    xlSheet.Cells(4, columnIndex) = column.HeaderText
                    columnIndex += 1
                End If
            End If
        Next
        Dim rowIndex As Integer = 4
        For Each row As DataGridViewRow In 訪談紀錄_dgv.Rows
            If Not row.IsNewRow Then : columnIndex = 1 ' 重置列索引
                For Each column As DataGridViewColumn In 訪談紀錄_dgv.Columns
                    If column.Visible Then
                        If column.Name = "姓名" Or column.Name = "電話" Or column.Name = "聯繫方式" Then : Else
                            xlSheet.Cells(rowIndex + 1, columnIndex) = "'" & row.Cells(column.Index).Value.ToString()
                            columnIndex += 1
                        End If
                    End If
                Next
                rowIndex += 1
            End If
        Next
        AA(xlApp, xlSheet)
        '-------------轉換成PDF---------------------------------------------------------------------
        Dim SERT As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        xlBook.Sheets(1).ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, SERT & "\" & Strings.Format(開始日期_dtp.Value, "yyyy-MM-dd") & "到" & Strings.Format(結束日期_dtp.Value, "yyyy-MM-dd") & "(訪談紀錄).pdf", XlFixedFormatQuality.xlQualityStandard, True)
        '-------------關閉EXCEL---------------------------------------------------------------------
        xlSheet.PageSetup.PrintArea = "" : xlApp.Cells.Select()
        xlApp.Application.WindowState = xlMinimized
        xlBook.Close(False)
        Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
        Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
        Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
        xlApp.Quit() : xlSheet = Nothing : xlBook = Nothing : xlApp = Nothing : GC.Collect()
        '-------------開啟PDF---------------------------------------------------------------------
        Dim pdfFilePath As String = SERT & "\" & Strings.Format(開始日期_dtp.Value, "yyyy-MM-dd") & "到" & Strings.Format(結束日期_dtp.Value, "yyyy-MM-dd") & "(訪談紀錄).pdf"
        System.Diagnostics.Process.Start("""" & pdfFilePath & """")
    End Sub
    Private Sub AA(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        xlSheet.Activate() ' 激活工作表
        myExcel.ActiveWindow.View = XlWindowView.xlPageLayoutView
        myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup : .PrintTitleRows = "$1:$4" : .PrintTitleColumns = "$A:$I" : End With
        myExcel.Application.PrintCommunication = True : myExcel.ActiveSheet.PageSetup.PrintArea = "" : myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup : .Orientation = XlPageOrientation.xlLandscape
            .LeftHeader = "" : .CenterHeader = "" : .RightHeader = "" : .LeftFooter = "" : .CenterFooter = "" : .RightFooter = ""
            .LeftMargin = myExcel.Application.InchesToPoints(0.25) : .RightMargin = myExcel.Application.InchesToPoints(0.25) : .TopMargin = myExcel.Application.InchesToPoints(0.3)
            .BottomMargin = myExcel.Application.InchesToPoints(0.3) : .HeaderMargin = myExcel.Application.InchesToPoints(0.3) : .FooterMargin = myExcel.Application.InchesToPoints(0.3)
            .PrintHeadings = False : .PrintGridlines = False : .CenterHorizontally = False : .CenterVertically = False : .Draft = False : .FirstPageNumber = xlAutomatic
            .BlackAndWhite = False : .Zoom = False : .FitToPagesWide = 0 : .FitToPagesTall = 0 : .OddAndEvenPagesHeaderFooter = False : .DifferentFirstPageHeaderFooter = False
            .ScaleWithDocHeaderFooter = True : .AlignMarginsHeaderFooter = True
            .EvenPage.LeftHeader.Text = "" : .EvenPage.CenterHeader.Text = "" : .EvenPage.RightHeader.Text = "" : .EvenPage.LeftFooter.Text = "" : .EvenPage.CenterFooter.Text = ""
            .EvenPage.RightFooter.Text = "" : .FirstPage.LeftHeader.Text = "" : .FirstPage.CenterHeader.Text = "" : .FirstPage.RightHeader.Text = "" : .FirstPage.LeftFooter.Text = ""
            .FirstPage.CenterFooter.Text = "" : .FirstPage.RightFooter.Text = ""
        End With : myExcel.Application.PrintCommunication = True

        xlSheet.Cells.Select()
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 12 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False
            .Shadow = False : .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
        End With
        Dim 行数 As Integer = 訪談紀錄_dgv.Rows.Count + 4
        xlSheet.Range("A3:I" & 行数.ToString).Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With

        xlSheet.Columns("A:A").ColumnWidth = 3 : xlSheet.Columns("B:B").ColumnWidth = 13 : xlSheet.Columns("C:C").ColumnWidth = 10 : xlSheet.Columns("D:D").ColumnWidth = 10
        xlSheet.Columns("E:E").ColumnWidth = 10 : xlSheet.Columns("F:F").ColumnWidth = 10 : xlSheet.Columns("G:G").ColumnWidth = 20 : xlSheet.Columns("H:H").ColumnWidth = 41.5
        xlSheet.Columns("I:I").ColumnWidth = 18 : xlSheet.Rows(1).RowHeight = 18 : xlSheet.Rows(2).RowHeight = 18

        xlSheet.Range("A3:A4").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge

        xlSheet.Range("B3:H3").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge

        xlSheet.Range("B4:I4").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Range("A5:A" & 行数.ToString).Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Range("B5:I" & 行数.ToString).Select()
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        For i = 2 To 行数
            xlSheet.Rows(i & ":" & i).Select : xlSheet.Rows(i & ":" & i).EntireRow.AutoFit
            If xlSheet.Rows(i).RowHeight + 30 > 409 Then : xlSheet.Rows(i).RowHeight = 409 : Else : xlSheet.Rows(i).RowHeight = xlSheet.Rows(i).RowHeight + 30 : End If
        Next
    End Sub

    Private Sub 業務員_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 業務員_cb.SelectedIndexChanged
        If 已開啟 Then
            Dim 訪談人 As String : If 業務員_cb.Text = "所有人" Or 業務員_cb.Text = "" Then : 訪談人 = "%" : Else : 訪談人 = 業務員_cb.Text : End If
            SQL_洽談物件類別(訪談人) : 洽談物件_cb.Items.Clear() : 洽談物件_cb.Items.Add("") : While (dr.Read()) : 洽談物件_cb.Items.Add(dr("洽談物件")) : End While : conn.Close()
            Set_訪談清單()
        End If
    End Sub

    Private Sub 集團_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 集團_cb.SelectedIndexChanged
        If 已開啟 Then : Set_訪談清單() : End If
    End Sub

    Private Sub 洽談物件_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 洽談物件_cb.SelectedIndexChanged
        If 已開啟 Then : Set_訪談清單() : End If
    End Sub
End Class