Option Strict Off
Imports System.Data.SqlClient
Imports System.IO
Imports System.Net
Public Class 人員入職資料表
    Public Property Credentials As ICredentials
    ReadOnly conn As New SqlConnection
    ReadOnly da As New SqlDataAdapter
    ReadOnly cmd As New SqlCommand
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet
    Dim dr As SqlDataReader
    Dim DTP As String : Dim DTP1 As String
    Private Sub Set_使用者清單()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 人事資料表.員工編號, 使用者權限管理表.姓名 AS 系統登入姓名, 人事資料表.姓名, 人事資料表.入職日期
                FROM 使用者權限管理表 LEFT OUTER JOIN
                            人事資料表 ON 使用者權限管理表.員工編號 = 人事資料表.員工編號 AND 
                            使用者權限管理表.姓名 = 人事資料表.系統登入姓名
                WHERE (使用者權限管理表.CC49 = 0) AND (人事資料表.員工編號 IS NULL)
                ORDER BY 使用者權限管理表.職稱"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() : Set_grid()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Visible = False : DataGridView1.Columns(1).Width = 150 : DataGridView1.Columns(2).Visible = False : DataGridView1.Columns(3).Visible = False
    End Sub
    Private Sub Set_人員清單()
        DataGridView2.DataSource = Nothing : ds1.Clear()
        DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView2.ColumnHeadersHeight = 25
        DataGridView2.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 人事資料表.員工編號, 使用者權限管理表.姓名 AS 系統登入姓名, 人事資料表.姓名, 人事資料表.入職日期, 人事資料表.離職日期
                FROM 使用者權限管理表 LEFT OUTER JOIN
                            人事資料表 ON 使用者權限管理表.員工編號 = 人事資料表.員工編號 AND 
                            使用者權限管理表.姓名 = 人事資料表.系統登入姓名
                WHERE (使用者權限管理表.CC49 = 0) AND (人事資料表.員工編號 IS NOT NULL) AND (人事資料表.離職日期 LIKE '在職')
                ORDER BY 使用者權限管理表.職稱"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close() : Set_grid1()
    End Sub
    Private Sub Set_grid1()
        DataGridView2.Columns(0).Visible = False : DataGridView2.Columns(1).Width = 150 : DataGridView2.Columns(2).Visible = False : DataGridView2.Columns(3).Visible = False
        DataGridView2.Columns(4).Visible = False
    End Sub
    Private Sub 讀取圖片()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 人事表圖片 FROM 人事資料表 WHERE 員工編號 LIKE N'" & TextBox1.Text & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        While dr.Read() = True
            Dim unused As Byte() = New Byte(-1) {}
            Dim bytes As Byte() = DirectCast(dr.Item("人事表圖片"), Byte())
            Dim oStream As New MemoryStream(bytes)
            PictureBox1.Image = Bitmap.FromStream(oStream)
        End While
        conn.Close()
        PictureBox1.SizeMode = 4
    End Sub
    Private Sub 人員入職資料表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = WINPROFIT_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        Set_使用者清單() : Set_人員清單()
        TextBox1.Enabled = False : TextBox2.Enabled = False : TextBox7.Visible = False : GroupBox1.Visible = False
        Target1 = Target & AA(14) & "/"
        WebBrowser1.Url = New Uri(Target1)
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox2.Text = DataGridView1(1, e.RowIndex).Value.ToString
            TextBox1.Text = "" : TextBox3.Text = ""
            PictureBox1.Image = Nothing
        End If
    End Sub
    Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = DataGridView2(0, e.RowIndex).Value.ToString : TextBox2.Text = DataGridView2(1, e.RowIndex).Value.ToString : TextBox3.Text = DataGridView2(2, e.RowIndex).Value.ToString
            DateTimePicker1.Value = DataGridView2(3, e.RowIndex).Value.ToString : TextBox5.Text = DataGridView2(4, e.RowIndex).Value.ToString
            Dim TSFM As Integer = DateDiff("d", DateTimePicker1.Value, Today())
            TextBox6.Text = TSFM
            讀取圖片()
        End If
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
    Private Sub TextBox4_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox4.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            Try
                files = CType(e.Data.GetData(DataFormats.FileDrop), String())
                TextBox4.Text = files(files.Length - 1)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try
        End If

        Dim STR2 As Integer = 0
        TextBox7.Text = TextBox4.Text
        If TextBox4.Text = "" Then
        Else
            For i As Integer = 0 To 9999
                Dim STR1 As Integer = Strings.Len(TextBox7.Text)
                If Strings.Right((TextBox7.Text), 1) <> "\" Then
                    STR1 -= 1 : STR2 += 1
                    If STR1 = 0 Then
                        TextBox7.Text = ""
                        TextBox4.Text = ""
                        i = 9999
                    Else
                        TextBox7.Text = Strings.Left((TextBox7.Text), STR1)
                    End If
                Else
                    TextBox7.Text = Strings.Right((TextBox4.Text), STR2)
                    TextBox4.Text = TextBox7.Text
                    i = 9999
                End If
            Next
        End If
    End Sub
    Private Sub TextBox4_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TextBox4.DragDrop
        Dim files As Array = e.Data.GetData(DataFormats.FileDrop)
        For Each file As String In files
            TextBox4.AppendText(file + Environment.NewLine)
        Next
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(DateTimePicker1.Value, "yyyy/MM/dd")
        DTP1 = Format(DateTimePicker2.Value, "yyyy/MM/dd")
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox4.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
            MsgBox("請確定人事資料表的圖片檔已準備完成，並且有選擇未建檔的人事清單資料、人員中文姓名有填寫等")
        Else
            If TextBox1.Text <> "" Then
                MsgBox("該員工編號已經建檔完畢，請確認資料是否正確")
            Else
                conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT TOP (1) 員工編號 FROM 人事資料表 ORDER BY 員工編號 DESC"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then : TextBox1.Text = dr("員工編號").ToString : Else : TextBox1.Text = "EN00000000" : End If
                conn.Close()
                Dim NUM1 As Integer
                NUM1 = Double.Parse(Strings.Right(TextBox1.Text, 8)) + 1
                If NUM1 < 10 Then : TextBox1.Text = "EN" & "0000000" & NUM1
                ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox1.Text = "EN" & "000000" & NUM1
                ElseIf NUM1 > 99 And NUM1 < 1000 Then : TextBox1.Text = "EN" & "00000" & NUM1
                ElseIf NUM1 > 999 And NUM1 < 10000 Then : TextBox1.Text = "EN" & "0000" & NUM1
                ElseIf NUM1 > 9999 And NUM1 < 100000 Then : TextBox1.Text = "EN" & "000" & NUM1
                ElseIf NUM1 > 99999 And NUM1 < 100000 Then : TextBox1.Text = "EN" & "00" & NUM1
                ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : TextBox1.Text = "EN" & "0" & NUM1
                ElseIf NUM1 > 9999999 Then : TextBox1.Text = "EN" & NUM1
                End If

                Set_日期格式轉換()
                conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 人事資料表 (員工編號, 系統登入姓名, 姓名, 入職日期, 離職日期, 人事表圖片) SELECT N'" & TextBox1.Text & "',N'" & TextBox2.Text & "',N'" & TextBox3.Text &
                       "','" & DTP & "','在職', " &
                       "BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(14) & "\" & TextBox4.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
                Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
                frq = CType(WebRequest.Create(New Uri(Target1 & TextBox4.Text)), FtpWebRequest)
                fcr = New NetworkCredential(FTP帳號, FTP密碼)
                frq.Credentials = fcr
                frq.Method = WebRequestMethods.Ftp.DeleteFile
                frq.UseBinary = True
                frp = CType(frq.GetResponse, FtpWebResponse)
                frp.Close()
                WebBrowser1.Refresh()
                '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                TextBox4.Text = ""

                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "UPDATE 使用者權限管理表 SET  員工編號 = '" & TextBox1.Text & "' WHERE (姓名 = '" & TextBox2.Text & "')"
                cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

                MsgBox("新增完成")
                Set_使用者清單() : Set_人員清單()
                讀取圖片()
            End If
        End If
    End Sub
    Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
        If TextBox1.Text = "" Or TextBox4.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
            MsgBox("請確定人事資料表的圖片檔已準備完成，並且所有人事清單資料、人員中文姓名等資料正確")
        Else
            conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 人事資料表 SET  人事表圖片 = BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(14) & "\" & TextBox4.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE " &
                   "WHERE (員工編號 = '" & TextBox1.Text & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
            Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
            frq = CType(WebRequest.Create(New Uri(Target1 & TextBox4.Text)), FtpWebRequest)
            fcr = New NetworkCredential(FTP帳號, FTP密碼)
            frq.Credentials = fcr
            frq.Method = WebRequestMethods.Ftp.DeleteFile
            frq.UseBinary = True
            frp = CType(frq.GetResponse, FtpWebResponse)
            frp.Close()
            WebBrowser1.Refresh()
            '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            TextBox4.Text = ""
            MsgBox("修改完成")
            讀取圖片()
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
            MsgBox("請確定選中離職人員的人事資料表")
        Else
            GroupBox1.Visible = True
        End If
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Set_日期格式轉換()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "UPDATE 人事資料表 SET  離職日期 = '" & DTP1 & "' WHERE (員工編號 = '" & TextBox1.Text & "')"
        cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        GroupBox1.Visible = False
        Set_使用者清單() : Set_人員清單()
    End Sub
End Class