Imports Org.BouncyCastle.Utilities
Public Class 日曆
    ReadOnly ds, ds1, ds2, ds3, ds4, ds5, ds6, ds7, ds8, ds9, ds10, ds11, ds12, ds13, ds14, ds15, ds16, ds17, ds18, ds19, ds20, ds21, ds22, ds23, ds24, ds25, ds26, ds27, ds28, ds29, ds30, ds31,
        ds32, ds33, ds34, ds35, ds36, ds37, ds38, ds39, ds40, ds41, ds42, ds43 As New DataSet
    Dim 星期 As String : Dim 天數, 總時數, 列高, 列高1, 欄寬1, 欄寬2, 欄寬3 As Integer
    Dim 首次開啟 As Boolean : Dim 對話框(13) As String
    Private Sub 年份_下拉表單資料載入()
        SQL_年份() : 年份_cb.Items.Clear() : While (dr.Read()) : 年份_cb.Items.Add(dr("年份")) : End While : conn.Close()
    End Sub
    Private Sub 讀取日期()
        日期_dgv.DataSource = Nothing : ds.Clear()
        日期_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        日期_dgv.ColumnHeadersHeight = 25 : 日期_dgv.AllowUserToAddRows = False
        SQL_排班系統_月份()
        da.Fill(ds) : 日期_dgv.DataSource = ds.Tables(0) : conn.Close()
        For i As Integer = 0 To 日期_dgv.Rows.Count - 1 : 日期_dgv.Rows(i).Cells("休假").Value = "" : Next
    End Sub
    Private Sub 讀取資料(項目 As String, 車輛編號 As String, 日期 As String, 班別 As Integer, dgv As DataGridView)
        Dim ds As New DataSet : dgv.DataSource = Nothing : ds.Clear()
        dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        dgv.ColumnHeadersHeight = 列高1 : dgv.AllowUserToAddRows = False : dgv.RowTemplate.Height = 列高
        SQL_人員動態_日曆(日期)
        da.Fill(ds) : dgv.DataSource = ds.Tables(0) : conn.Close()
        dgv.Columns(0).ReadOnly = True : dgv.Columns(1).ReadOnly = True : dgv.Columns(2).ReadOnly = True
        dgv.Columns(0).FillWeight = 欄寬1 : dgv.Columns(1).FillWeight = 欄寬2 : dgv.Columns(2).FillWeight = 欄寬3
    End Sub
    Private Sub 日曆變動()
        If 日期_dgv.Rows.Count > 0 Then
            總時數 = 0
            For no As Integer = 1 To 42
                Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = True
            Next
            Dim Q As Integer = 0
            星期 = 日期_dgv.Rows(0).Cells("星期").Value.ToString : 天數 = 日期_dgv.Rows.Count
            Dim dgvlist() As DataGridView = {Nothing, DGV1, DGV2, DGV3, DGV4, DGV5, DGV6, DGV7, DGV8, DGV9, DGV10, DGV11, DGV12, DGV13, DGV14, DGV15, DGV16, DGV17,
                DGV18, DGV19, DGV20, DGV21, DGV22, DGV23, DGV24, DGV25, DGV26, DGV27, DGV28, DGV29, DGV30, DGV31, DGV32, DGV33, DGV34, DGV35, DGV36, DGV37, DGV38,
                DGV39, DGV40, DGV41, DGV42}
            If 星期 = "星期日" Then
                For no As Integer = 1 To 42
                    If no > 天數 Then : Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    Dim 日期_lb As Label = CType(Me.Controls.Find("日期_lb" + no.ToString(), True)(0), Label) : 日期_lb.Text = no
                    If IsNumeric(Val(日期_lb.Text)) = True Then
                        If no < 10 Then : PA = "0" & no : Else : PA = no : End If : PA = 年份_cb.Text & 月份_cb.Text & PA
                        讀取資料(PA51, PA50, PA, no, dgvlist(no))
                    End If
                    If no >= 1 And no <= 天數 Then
                        Dim Labels As Label = CType(Me.Controls.Find("Label" + no.ToString(), True)(0), Label) : Labels.Text = 日期_dgv.Rows(Q).Cells("休假").Value.ToString
                        If 日期_dgv.Rows(Q).Cells("休假").Value.ToString <> "國定假日" And 日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期日" And
                        日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期六" Then
                            總時數 += 8
                        End If : Q += 1
                    End If
                Next
            End If
            If 星期 = "星期一" Then
                For no As Integer = 1 To 42
                    If no = 1 Then
                        Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    If no > 天數 + 1 Then : Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    Dim 日期_lb As Label = CType(Me.Controls.Find("日期_lb" + no.ToString(), True)(0), Label) : 日期_lb.Text = no - 1
                    If IsNumeric(Val(日期_lb.Text)) = True Then
                        If no - 1 < 10 Then : PA = "0" & no - 1 : Else : PA = no - 1 : End If : PA = 年份_cb.Text & 月份_cb.Text & PA
                        讀取資料(PA51, PA50, PA, no, dgvlist(no))
                    End If
                    If no >= 2 And no <= 天數 + 1 Then
                        Dim Labels As Label = CType(Me.Controls.Find("Label" + no.ToString(), True)(0), Label) : Labels.Text = 日期_dgv.Rows(Q).Cells("休假").Value.ToString
                        If 日期_dgv.Rows(Q).Cells("休假").Value.ToString <> "國定假日" And 日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期日" And
                        日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期六" Then
                            總時數 += 8
                        End If : Q += 1
                    End If
                Next
            End If
            If 星期 = "星期二" Then
                For no As Integer = 1 To 42
                    If no = 1 Or no = 2 Then
                        Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    If no > 天數 + 2 Then : Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    Dim 日期_lb As Label = CType(Me.Controls.Find("日期_lb" + no.ToString(), True)(0), Label) : 日期_lb.Text = no - 2
                    If IsNumeric(Val(日期_lb.Text)) = True Then
                        If no - 2 < 10 Then : PA = "0" & no - 2 : Else : PA = no - 2 : End If : PA = 年份_cb.Text & "/" & 月份_cb.Text & "/" & PA
                        讀取資料(PA51, PA50, PA, no, dgvlist(no))
                    End If
                    If no >= 3 And no <= 天數 + 2 Then
                        Dim Labels As Label = CType(Me.Controls.Find("Label" + no.ToString(), True)(0), Label) : Labels.Text = 日期_dgv.Rows(Q).Cells("休假").Value.ToString
                        If 日期_dgv.Rows(Q).Cells("休假").Value.ToString <> "國定假日" And 日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期日" And
                        日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期六" Then
                            總時數 += 8
                        End If : Q += 1
                    End If
                Next
            End If
            If 星期 = "星期三" Then
                For no As Integer = 1 To 42
                    If no = 1 Or no = 2 Or no = 3 Then
                        Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    If no > 天數 + 3 Then : Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    Dim 日期_lb As Label = CType(Me.Controls.Find("日期_lb" + no.ToString(), True)(0), Label) : 日期_lb.Text = no - 3
                    If IsNumeric(Val(日期_lb.Text)) = True Then
                        If no - 3 < 10 Then : PA = "0" & no - 3 : Else : PA = no - 3 : End If : PA = 年份_cb.Text & 月份_cb.Text & PA
                        讀取資料(PA51, PA50, PA, no, dgvlist(no))
                    End If
                    If no >= 4 And no <= 天數 + 3 Then
                        Dim Labels As Label = CType(Me.Controls.Find("Label" + no.ToString(), True)(0), Label) : Labels.Text = 日期_dgv.Rows(Q).Cells("休假").Value.ToString
                        If 日期_dgv.Rows(Q).Cells("休假").Value.ToString <> "國定假日" And 日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期日" And
                        日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期六" Then
                            總時數 += 8
                        End If : Q += 1
                    End If
                Next
            End If
            If 星期 = "星期四" Then
                For no As Integer = 1 To 42
                    If no = 1 Or no = 2 Or no = 3 Or no = 4 Then
                        Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    If no > 天數 + 4 Then : Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    Dim 日期_lb As Label = CType(Me.Controls.Find("日期_lb" + no.ToString(), True)(0), Label) : 日期_lb.Text = no - 4
                    If IsNumeric(Val(日期_lb.Text)) = True Then
                        If no - 4 < 10 Then : PA = "0" & no - 4 : Else : PA = no - 4 : End If : PA = 年份_cb.Text & 月份_cb.Text & PA
                        讀取資料(PA51, PA50, PA, no, dgvlist(no))
                    End If
                    If no >= 5 And no <= 天數 + 4 Then
                        Dim Labels As Label = CType(Me.Controls.Find("Label" + no.ToString(), True)(0), Label) : Labels.Text = 日期_dgv.Rows(Q).Cells("休假").Value.ToString
                        If 日期_dgv.Rows(Q).Cells("休假").Value.ToString <> "國定假日" And 日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期日" And
                        日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期六" Then
                            總時數 += 8
                        End If : Q += 1
                    End If
                Next
            End If
            If 星期 = "星期五" Then
                For no As Integer = 1 To 42
                    If no = 1 Or no = 2 Or no = 3 Or no = 4 Or no = 5 Then
                        Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    If no > 天數 + 5 Then : Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    Dim 日期_lb As Label = CType(Me.Controls.Find("日期_lb" + no.ToString(), True)(0), Label) : 日期_lb.Text = no - 5
                    If IsNumeric(Val(日期_lb.Text)) = True Then
                        If no - 5 < 10 Then : PA = "0" & no - 5 : Else : PA = no - 5 : End If : PA = 年份_cb.Text & 月份_cb.Text & PA
                        讀取資料(PA51, PA50, PA, no, dgvlist(no))
                    End If
                    If no >= 6 And no <= 天數 + 5 Then
                        Dim Labels As Label = CType(Me.Controls.Find("Label" + no.ToString(), True)(0), Label) : Labels.Text = 日期_dgv.Rows(Q).Cells("休假").Value.ToString
                        If 日期_dgv.Rows(Q).Cells("休假").Value.ToString <> "國定假日" And 日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期日" And
                        日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期六" Then
                            總時數 += 8
                        End If : Q += 1
                    End If
                Next
            End If
            If 星期 = "星期六" Then
                For no As Integer = 1 To 42
                    If no = 1 Or no = 2 Or no = 3 Or no = 4 Or no = 5 Or no = 6 Then
                        Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    If no > 天數 + 6 Then : Dim Panels As Panel = CType(Me.Controls.Find("Panel" + no.ToString(), True)(0), Panel) : Panels.Visible = False : End If
                    Dim 日期_lb As Label = CType(Me.Controls.Find("日期_lb" + no.ToString(), True)(0), Label) : 日期_lb.Text = no - 6
                    If IsNumeric(Val(日期_lb.Text)) = True Then
                        If no - 6 < 10 Then : PA = "0" & no - 6 : Else : PA = no - 6 : End If : PA = 年份_cb.Text & 月份_cb.Text & PA
                        讀取資料(PA51, PA50, PA, no, dgvlist(no))
                    End If
                    If no >= 7 And no <= 天數 + 6 Then
                        Dim Labels As Label = CType(Me.Controls.Find("Label" + no.ToString(), True)(0), Label) : Labels.Text = 日期_dgv.Rows(Q).Cells("休假").Value.ToString
                        If 日期_dgv.Rows(Q).Cells("休假").Value.ToString <> "國定假日" And 日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期日" And
                        日期_dgv.Rows(Q).Cells("星期").Value.ToString <> "星期六" Then
                            總時數 += 8
                        End If : Q += 1
                    End If
                Next
            End If
        End If
    End Sub
    Private Sub 日曆_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        YS_ERP_SYS.WindowState = 2 : Me.MdiParent = YS_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        列高 = 25 : 列高1 = 22 : 欄寬1 = 120 : 欄寬2 = 80 : 欄寬3 = 120
        年份_下拉表單資料載入() : 日期_dgv.Visible = False

        首次開啟 = False
        年份_cb.Text = Year(Today) : 月份_cb.SelectedIndex = Month(Today) - 1
        PA1 = 年份_cb.Text : PA2 = 月份_cb.Text
        首次開啟 = True
        讀取日期() : 日曆變動()
        For i As Integer = 1 To 42
            Dim dgv As DataGridView = CType(Me.Controls.Find("DGV" & i.ToString(), True)(0), DataGridView)
            dgv.RowsDefaultCellStyle.Font = New Font("微軟正黑體", 7.6)
        Next
    End Sub
    Private Sub 排班資料_Closing(sender As Object, e As EventArgs) Handles MyBase.Closed
        MyModule1.虛擬桌面開啟()
    End Sub
    Private Sub 月份_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 月份_cb.SelectedIndexChanged
        If 首次開啟 = True Then
            PA1 = 年份_cb.Text : PA2 = 月份_cb.Text : 讀取日期() : 日曆變動()
        End If
    End Sub
End Class