Imports System.IO
Public Class 休假申請
    ReadOnly ds, ds1 As New DataSet
    Dim 生效 As String : Dim 圖片新建, 剛開啟 As Boolean
    Private Sub 讀取人員資料表()
        人員_dgv.DataSource = Nothing : ds.Clear()
        人員_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        人員_dgv.ColumnHeadersHeight = 25
        人員_dgv.AllowUserToAddRows = False
        If 登入人級別 = "00" Then : SQL2 = "" : Else : SQL2 = "AND CC63 = 0" : End If : SQL_考勤明細表_人員()
        da.Fill(ds) : 人員_dgv.DataSource = ds.Tables(0) : conn.Close()
        人員_dgv.Columns(0).FillWeight = 70 : 人員_dgv.Columns(1).FillWeight = 80 : 人員_dgv.Columns(2).FillWeight = 100 : 人員_dgv.Columns(3).Visible = False
        人員_dgv.Columns(4).Visible = False : 人員_dgv.Columns(5).Visible = False : 人員_dgv.Columns(6).Visible = False : 人員_dgv.Columns(7).Visible = False
        人員_dgv.Columns(8).Visible = False : 人員_dgv.Columns(9).Visible = False : 人員_dgv.Columns("到職日").Visible = False : 人員_dgv.Columns("CC18").Visible = False
        For i As Integer = 0 To 人員_dgv.Rows.Count - 1
            人員_dgv.Rows(i).Cells("No.").Value = i + 1
        Next
    End Sub
    Private Sub 年份_下拉表單資料載入()
        SQL_年份() : 年份_cb.Items.Clear() : While (dr.Read()) : 年份_cb.Items.Add(dr("年份")) : End While : conn.Close()
    End Sub
    Private Sub Set_考勤明細()
        月清單_DGV.DataSource = Nothing : ds1.Clear()
        月清單_DGV.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        月清單_DGV.ColumnHeadersHeight = 40
        月清單_DGV.AllowUserToAddRows = False
        PA = 員工編號_tb.Text : PA1 = 年份_cb.Text : PA2 = 月份_cb.Text : SQL_國定假日設定_月份考勤()
        da.Fill(ds1) : 月清單_DGV.DataSource = ds1.Tables(0) : conn.Close()

        月清單_DGV.Columns(0).FillWeight = 80 : 月清單_DGV.Columns(1).FillWeight = 80 : 月清單_DGV.Columns(2).FillWeight = 90 : 月清單_DGV.Columns(3).FillWeight = 90
        月清單_DGV.Columns("圖片資料庫").Visible = False : 月清單_DGV.Columns("圖片編號").Visible = False : 月清單_DGV.Columns(5).Visible = False
        月清單_DGV.Columns(6).Visible = False : 月清單_DGV.Columns("申請時數").FillWeight = 50 : 月清單_DGV.Columns("申請時間").FillWeight = 80
        For I As Integer = 0 To 月清單_DGV.Rows.Count - 1
            If 月清單_DGV.Rows(I).Cells("星期").Value.ToString = "星期六" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 月清單_DGV.Rows(I).Cells("星期").Value.ToString = "星期日" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 月清單_DGV.Rows(I).Cells("請假類別").Value.ToString = "國定假日" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 月清單_DGV.Rows(I).Cells("請假類別").Value.ToString <> "" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightYellow
            End If
        Next : 特假日期_tb.Text = 月清單_DGV("日期", 0).Value.ToString : Set_假期統計()
    End Sub
    Private Sub Set_假期統計()
        Dim ds As New DataSet
        假期統計_dgv.DataSource = Nothing : ds.Clear()
        假期統計_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        假期統計_dgv.ColumnHeadersHeight = 25
        假期統計_dgv.AllowUserToAddRows = False
        PA = 員工編號_tb.Text : PA1 = 特假日期_tb.Text : SQL_休假統計()
        da.Fill(ds) : 假期統計_dgv.DataSource = ds.Tables(0) : conn.Close()
    End Sub
    Private Sub 特休申請_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = YS_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        Panel2.SendToBack() : 視窗2_pl.Visible = False : 剛開啟 = True
        WebBrowser2.Navigate(Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
    End Sub
    Private Sub 特休申請_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        SQL_資料庫選擇() : If dr.Read() Then : 預設圖片庫_tb.Text = dr("文件資料庫").ToString : Else : MsgBox("圖片資料庫已滿，請找系統管理員!!") : Me.Close() : End If

        年份_下拉表單資料載入() : 年份_cb.Text = Year(Today) : 月份_cb.SelectedIndex = Month(Today) - 1
        請假類別_cb.SelectedIndex = 0 : PA1 = 年份_cb.Text : PA2 = 月份_cb.Text : 剛開啟 = False : 讀取人員資料表()

        If CC(16) = False Then : 人員_dgv.Enabled = False : 申請人_tb.Text = gUserName
            SQL_考勤明細表_讀取編號() : If dr.Read() Then : 員工編號_tb.Text = dr("帳號") : End If : Set_考勤明細()
        End If
    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 介面關閉_Closed(sender As Object, e As EventArgs) Handles MyBase.Closed
        MyModule1.虛擬桌面開啟()
    End Sub
    Private Sub 人員_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 人員_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            員工編號_tb.Text = 人員_dgv("帳號", e.RowIndex).Value.ToString : 申請人_tb.Text = 人員_dgv("姓名", e.RowIndex).Value.ToString
            請休假時數_cb.Text = "" : Set_考勤明細()
        End If
    End Sub
    Private Sub 月清單_DGV_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 月清單_DGV.CellClick
        If e.RowIndex = -1 Then : Else
            If 月清單_DGV.Rows(e.RowIndex).Cells("星期").Value.ToString = "星期六" Or 月清單_DGV.Rows(e.RowIndex).Cells("星期").Value.ToString = "星期日" Or
                月清單_DGV.Rows(e.RowIndex).Cells("請假類別").Value.ToString = "國定假日" Then
                If gUserName = "*系統管理者" Then
                    特假日期_tb.Text = 月清單_DGV("日期", e.RowIndex).Value.ToString
                    事由_tb.Text = 月清單_DGV("事由", e.RowIndex).Value.ToString
                    生效 = 月清單_DGV("是否生效", e.RowIndex).Value.ToString
                    請假類別_cb.Text = 月清單_DGV("請假類別", e.RowIndex).Value.ToString
                    請休假時數_cb.Text = 月清單_DGV("申請時數", e.RowIndex).Value.ToString
                    資料指定圖庫_tb.Text = 月清單_DGV("圖片資料庫", e.RowIndex).Value.ToString : 圖片流水號_tb.Text = 月清單_DGV("圖片編號", e.RowIndex).Value.ToString
                    '------------驗貨報告讀取--------------------------------------------------------------------------------------------
                    If 資料指定圖庫_tb.Text <> "" And 圖片流水號_tb.Text <> "" Then
                        圖片庫 = 資料指定圖庫_tb.Text : SQL_連線字串_圖片資料庫() : PA2 = 圖片流水號_tb.Text
                        SQL_請假圖() : PictureBox1.Image = Nothing
                        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
                    Else
                        PictureBox1.Image = Nothing
                    End If
                Else
                    特假日期_tb.Text = "" : 事由_tb.Text = "" : PictureBox1.Image = Nothing
                    MsgBox("星期六/星期日或國定假日不需要申請休假!!")
                End If
            Else
                特假日期_tb.Text = 月清單_DGV("日期", e.RowIndex).Value.ToString
                事由_tb.Text = 月清單_DGV("事由", e.RowIndex).Value.ToString
                生效 = 月清單_DGV("是否生效", e.RowIndex).Value.ToString
                請假類別_cb.Text = 月清單_DGV("請假類別", e.RowIndex).Value.ToString
                請休假時數_cb.Text = 月清單_DGV("申請時數", e.RowIndex).Value.ToString
                資料指定圖庫_tb.Text = 月清單_DGV("圖片資料庫", e.RowIndex).Value.ToString : 圖片流水號_tb.Text = 月清單_DGV("圖片編號", e.RowIndex).Value.ToString
                '------------驗貨報告讀取--------------------------------------------------------------------------------------------
                If 資料指定圖庫_tb.Text <> "" And 圖片流水號_tb.Text <> "" Then
                    圖片庫 = 資料指定圖庫_tb.Text : SQL_連線字串_圖片資料庫() : PA2 = 圖片流水號_tb.Text
                    SQL_請假圖() : PictureBox1.Image = Nothing
                    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
                Else
                    PictureBox1.Image = Nothing
                End If
            End If
        End If
    End Sub
    Private Sub 申請_bt_Click(sender As Object, e As EventArgs) Handles 申請_bt.Click
        If 特假日期_tb.Text = "" Or 請休假時數_cb.Text = "" Or 申請人_tb.Text = "" Then
            MsgBox("人員、日期、請休假時數 不能為空白!!")
        Else
            If 生效 = "已核准" Or 生效 = "未核准" Then : MsgBox("已簽核的資料不能再做修改") : Else
                SQL_請假申請讀取()
                If dr.Read Then : SQL_請假申請修改() : Else : SQL_請假申請新增() : End If
                Set_考勤明細() : MsgBox("儲存完成")
            End If
        End If
    End Sub
    Private Sub 刪除_bt_Click(sender As Object, e As EventArgs) Handles 刪除_bt.Click
        If 特假日期_tb.Text = "" Or 申請人_tb.Text = "" Then
            MsgBox("人員、日期、請休假時數 不能為空白!!")
        Else
            If 生效 = "已核准" Or 生效 = "未核准" Then : MsgBox("已簽核的資料不能再做修改")
            Else : SQL_申請刪除() : Set_考勤明細() : MsgBox("儲存完成") : End If
        End If
    End Sub
    Private Sub 確定導入_bt_Click(sender As Object, e As EventArgs) Handles 確定導入_bt.Click
        If 檔案路徑2_tb.Text <> "" Then
            If 特假日期_tb.Text = "" Or 請休假時數_cb.Text = "" Or 申請人_tb.Text = "" Then
                MsgBox("人員、日期、請休假時數 不能為空白!!")
            Else
                If 生效 = "已核准" Or 生效 = "未核准" Then : MsgBox("已簽核的資料不能再做修改") : Else
                    SQL_請假申請讀取()
                    If dr.Read Then
                        圖片導入準備() : SQL_請假申請修改() : 圖片導入()
                    Else
                        圖片導入準備() : SQL_請假申請新增() : 圖片導入()
                    End If
                    Set_考勤明細() : MsgBox("圖片導入完成")
                    Dim image As Image = Image.FromFile(檔案路徑2_tb.Text)
                    PictureBox1.Image = image : PictureBox1.SizeMode = 4
                End If
            End If
            檔案路徑2_tb.Text = ""
        Else : MsgBox("沒有準備圖片!!") : End If
    End Sub
    Private Sub 圖片導入準備()
        If 資料指定圖庫_tb.Text = "" Then
            圖片庫 = 預設圖片庫_tb.Text : SQL_連線字串_圖片資料庫() : 資料指定圖庫_tb.Text = 預設圖片庫_tb.Text
            SQL_查詢最後一筆圖片編號1()
            If dr.Read() Then : 圖片流水號_tb.Text = dr("流水號").ToString : Else : 圖片流水號_tb.Text = "0" : End If : conn.Close()
            Dim NUM1 As Integer
            NUM1 = Double.Parse(Strings.Right(圖片流水號_tb.Text, 8)) + 1
            If NUM1 < 10 Then : 圖片流水號_tb.Text = "IR" & "0000000" & NUM1
            ElseIf NUM1 > 9 And NUM1 < 100 Then : 圖片流水號_tb.Text = "IR" & "000000" & NUM1
            ElseIf NUM1 > 99 And NUM1 < 1000 Then : 圖片流水號_tb.Text = "IR" & "00000" & NUM1
            ElseIf NUM1 > 999 And NUM1 < 10000 Then : 圖片流水號_tb.Text = "IR" & "0000" & NUM1
            ElseIf NUM1 > 9999 And NUM1 < 100000 Then : 圖片流水號_tb.Text = "IR" & "000" & NUM1
            ElseIf NUM1 > 99999 And NUM1 < 100000 Then : 圖片流水號_tb.Text = "IR" & "00" & NUM1
            ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : 圖片流水號_tb.Text = "IR" & "0" & NUM1
            ElseIf NUM1 > 9999999 Then : 圖片流水號_tb.Text = "IR" & NUM1 : End If : 圖片新建 = True
        Else
            圖片庫 = 資料指定圖庫_tb.Text : SQL_連線字串_圖片資料庫() : 圖片新建 = False
        End If
    End Sub
    Private Sub 圖片導入()
        If 圖片新建 = True Then
            PA49 = 檔案路徑2_tb.Text : SQL_新增請假圖()
        Else
            PA49 = 檔案路徑2_tb.Text : SQL_修改請假圖()
        End If
    End Sub
    Private Sub 月份_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 月份_cb.SelectedIndexChanged
        If 剛開啟 = False Then : PA1 = 年份_cb.Text : PA2 = 月份_cb.Text : 讀取人員資料表() : Set_考勤明細() : End If
    End Sub
    Private Sub 年份_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 年份_cb.SelectedIndexChanged
        If 剛開啟 = False Then : PA1 = 年份_cb.Text : PA2 = 月份_cb.Text : 讀取人員資料表() : Set_考勤明細() : End If
    End Sub
    Private Sub 月清單_DGV_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 月清單_DGV.RowPostPaint
        For I As Integer = 0 To 月清單_DGV.Rows.Count - 1
            If 月清單_DGV.Rows(I).Cells("星期").Value.ToString = "星期六" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 月清單_DGV.Rows(I).Cells("星期").Value.ToString = "星期日" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 月清單_DGV.Rows(I).Cells("請假類別").Value.ToString = "國定假日" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 月清單_DGV.Rows(I).Cells("請假類別").Value.ToString <> "" Then : 月清單_DGV.Rows(I).DefaultCellStyle.BackColor = Color.LightYellow
            End If
        Next
    End Sub
    Private Sub 檔案名稱_tb_DragEnter(sender As Object, e As DragEventArgs) Handles 檔案名稱_tb.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            Try
                files = CType(e.Data.GetData(DataFormats.FileDrop), String())
                檔案名稱_tb.Text = files(files.Length - 1)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try
        End If

        Dim STR2 As Integer = 0
        檔案路徑2_tb.Text = 檔案名稱_tb.Text
        檔案路徑1_tb.Text = 檔案名稱_tb.Text
        If 檔案名稱_tb.Text = "" Then
        Else
            For i As Integer = 0 To 9999
                Dim STR1 As Integer = Strings.Len(檔案路徑1_tb.Text)
                If Strings.Right((檔案路徑1_tb.Text), 1) <> "\" Then
                    STR1 -= 1 : STR2 += 1
                    If STR1 = 0 Then
                        檔案路徑1_tb.Text = ""
                        檔案名稱_tb.Text = ""
                        i = 9999
                    Else
                        檔案路徑1_tb.Text = Strings.Left((檔案路徑1_tb.Text), STR1)
                    End If
                Else
                    檔案路徑1_tb.Text = Strings.Right((檔案名稱_tb.Text), STR2)
                    檔案名稱_tb.Text = 檔案路徑1_tb.Text
                    i = 9999
                End If
            Next
        End If
    End Sub
    Private Sub 檔案名稱_tb_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles 檔案名稱_tb.DragDrop
        Dim files As Array = e.Data.GetData(DataFormats.FileDrop)
        For Each file As String In files
            檔案名稱_tb.AppendText(file + Environment.NewLine)
        Next
    End Sub
    Private Sub 讀取料夾路徑_bt_Click(sender As Object, e As EventArgs) Handles 讀取料夾路徑_bt.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then : WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
    Private Sub CanceL2_bt_Click(sender As Object, e As EventArgs) Handles CanceL2_bt.Click
        If 視窗2_pl.Visible = False Then : 視窗2_pl.Visible = True : 視窗2_pl.BringToFront() : Else
            視窗2_pl.Visible = False : End If
    End Sub
    Private Sub 圖片導入系統_bt_Click(sender As Object, e As EventArgs) Handles 圖片導入系統_bt.Click
        If 視窗2_pl.Visible = False Then
            視窗2_pl.Location = New System.Drawing.Point((Me.Size.Width - 視窗2_pl.Size.Width) / 2, (Me.Size.Height - 視窗2_pl.Size.Height) / 2)
            視窗2_pl.Visible = True : 視窗2_pl.BringToFront()
        Else : End If
    End Sub
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        圖片傳遞 = PictureBox1.Image : 圖片放大視窗.ShowDialog()
    End Sub
    '----------------------自創視窗滑鼠拖曳功能---------------------------------------------------------------------------------------------------------
    Dim OldX, OldY As Long : Dim drag As Boolean
    Private Sub 視窗2_pl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 視窗2_pl.MouseDown
        If e.Button = MouseButtons.Left Then : OldX = e.X : OldY = e.Y : drag = True : End If
    End Sub
    Private Sub 視窗2_pl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 視窗2_pl.MouseMove
        If drag Then : 視窗2_pl.Left = 視窗2_pl.Left + e.X - OldX : 視窗2_pl.Top = 視窗2_pl.Top + e.Y - OldY : End If
    End Sub
    Private Sub 視窗2_pl_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 視窗2_pl.MouseUp
        drag = False
    End Sub
End Class