Option Strict Off
Imports System.Data.SqlClient
Public Class 系統資料庫維護
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As New DataSet : Dim ds1 As New DataSet
    Dim dr As SqlDataReader
    Dim STR1 As String
    Dim dt As DateTime
    Dim DTP As String : Dim SSD1 As String : Dim SSD2 As String : Dim SSD3 As String : Dim SSD4 As String : Dim SSD5 As String : Dim SSD6 As String
    Dim aa As MsgBoxResult
    Dim NUM1 As Integer = 0 : Dim NUM2 As Integer = 0 : Dim NUM4 As Integer = 0 : Dim NUM5 As Integer = 0 : Dim NUM6 As Integer = 0 : Dim NUM7 As Integer = 0 : Dim NUM8 As Integer = 0
    Dim NUM9 As Integer = 0 : Dim NUM11 As Integer = 0 : Dim NUM12 As Integer = 0 : Dim NUM13 As Integer = 0 : Dim NUM14 As Integer = 0 : Dim NUM15 As Integer = 0
    Dim WSAQ(2) As Double
    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 已有考勤日期 FROM 已有考勤日期 ORDER BY 已有考勤日期 DESC"
        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).Width = 100
    End Sub
    Private Sub Set_人員清單()
        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
        If Microsoft.VisualBasic.Left(DTP, 4) > 2018 Then
            SQL1 = "SELECT 資料流水號 AS DataID, 序號, 姓名 FROM 人員名單 WHERE (購買日期 BETWEEN '2001-01-01' AND '" & DTP & "') OR (購買日期 LIKE '') ORDER BY DataID"
        Else
            SQL1 = "SELECT 資料流水號 AS DataID, 序號, 姓名 FROM 人員名單 WHERE (購買日期 BETWEEN '2001-01-01' AND '" & DTP & "')  ORDER BY DataID"
        End If
        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).Width = 120 : DataGridView2.Columns(1).Width = 120 : DataGridView2.Columns(2).Width = 120
    End Sub
    Private Sub 星期轉換()
        dt = DateTimePicker1.Value
        TextBox1.Text = dt.DayOfWeek.ToString()
        If TextBox1.Text = "Sunday" Then : TextBox1.Text = "日" : ElseIf TextBox1.Text = "Monday" Then : TextBox1.Text = "一"
        ElseIf TextBox1.Text = "Tuesday" Then : TextBox1.Text = "二" : ElseIf TextBox1.Text = "Wednesday" Then : TextBox1.Text = "三"
        ElseIf TextBox1.Text = "Thursday" Then : TextBox1.Text = "四" : ElseIf TextBox1.Text = "Friday" Then : TextBox1.Text = "五"
        ElseIf TextBox1.Text = "Saturday" Then : TextBox1.Text = "六" : End If
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(DateTimePicker1.Value, "yyyy/MM/dd")
    End Sub
    Private Sub 系統資料庫維護_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = 楠晉鞋業AAS : Me.AutoScroll = True
        Set_清單() : Set_人員清單() : 星期轉換()
        TextBox19.Text = "-1" : TextBox20.Text = "-1" : TextBox21.Text = "-1" : TextBox22.Text = "-1" : TextBox23.Text = "" : TextBox24.Text = "" : TextBox25.Text = "" : TextBox26.Text = ""
        TextBox27.Text = "0" : TextBox28.Text = "0" : TextBox29.Text = "0" : TextBox30.Text = "0" : TextBox31.Text = "0" : TextBox32.Text = "0" : TextBox33.Text = "0" : TextBox34.Text = "0"
    End Sub
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        DateTimePicker1.Text = DataGridView1.Rows(e.RowIndex).Cells("已有考勤日期").Value.ToString
    End Sub
    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
        Set_人員清單()
        星期轉換()
    End Sub
    Private Sub 第1班上下班()
        '-----------------------------------------------------------------------------------------------------第1班上班
        Dim ran = New Random(DateTime.Now.Millisecond) : TextBox2.Text = ran.Next(1, 4096)
        For NUM3 = 0 To 4096
            NUM4 = TextBox2.Text
            If NUM4 > 9 Then : NUM4 = NUM4 - 9 : TextBox2.Text = NUM4 : ElseIf NUM4 < 9 Then : TextBox2.Text = NUM4 : NUM3 = 4096 : End If
        Next
        Dim ran1 = New Random(DateTime.Now.Millisecond) : TextBox3.Text = ran1.Next(1, 4096)
        For NUM3 = 0 To 4096
            NUM1 = TextBox3.Text
            If NUM1 > 7 Then : NUM1 = NUM1 - 7 : TextBox3.Text = NUM1 : ElseIf NUM1 <= 7 Then : If NUM1 <= 5 Then : TextBox3.Text = 5 : NUM3 = 4096 : Else : TextBox3.Text = 5 : NUM3 = 4096
                End If : End If
        Next
        If TextBox3.Text = "0" Then : TextBox4.Text = "08" : Else : TextBox4.Text = "07" : End If
        '-----------------------------------------------------------------------------------------------------第1班下班
        Dim ran2 = New Random(DateTime.Now.Millisecond) : TextBox5.Text = ran2.Next(1, 4096)
        For NUM3 = 0 To 4096
            NUM2 = TextBox5.Text
            If NUM2 > 9 Then : NUM2 = NUM2 - 9 : TextBox5.Text = NUM2 : ElseIf NUM2 < 9 Then : TextBox5.Text = NUM2 : NUM3 = 4096 : End If
        Next
        Dim ran3 = New Random(DateTime.Now.Millisecond) : TextBox6.Text = ran3.Next(1, 4096)
        For NUM3 = 0 To 4096
            NUM5 = TextBox6.Text
            If NUM5 > 7 Then : NUM5 = NUM5 - 7 : TextBox6.Text = NUM5 : ElseIf NUM5 <= 7 Then : If NUM5 <= 5 Then : TextBox6.Text = 0 : NUM3 = 4096 : Else : TextBox6.Text = 0 : NUM3 = 4096
                End If : End If
        Next
        If TextBox6.Text = "0" Then : TextBox7.Text = "12" : Else : TextBox7.Text = "11" : End If
        '-----------------------------------------------------------------------------------------------------字串
        WSAQ(0) = 4 : SSD3 = TextBox4.Text & ":" & TextBox3.Text & TextBox2.Text : SSD4 = TextBox7.Text & ":" & TextBox6.Text & TextBox5.Text
    End Sub
    Private Sub 第2班上下班()
        '-----------------------------------------------------------------------------------------------------第2班上班
        Dim ran4 = New Random(DateTime.Now.Millisecond) : TextBox8.Text = ran4.Next(1, 4096)
        For NUM3 = 0 To 4096
            NUM6 = TextBox8.Text
            If NUM6 > 9 Then : NUM6 = NUM6 - 9 : TextBox8.Text = NUM6 : ElseIf NUM6 < 9 Then : TextBox8.Text = NUM6 : NUM3 = 4096 : End If
        Next
        Dim ran5 = New Random(DateTime.Now.Millisecond) : TextBox9.Text = ran5.Next(1, 4096)
        For NUM3 = 0 To 4096
            NUM7 = TextBox9.Text
            If NUM7 > 7 Then : NUM7 = NUM7 - 7 : TextBox9.Text = NUM7 : ElseIf NUM7 <= 7 Then : If NUM7 <= 5 Then : TextBox9.Text = 2 : NUM3 = 4096 : Else : TextBox9.Text = 2 : NUM3 = 4096
                End If : End If
        Next
        '-----------------------------------------------------------------------------------------------------第2班下班
        Dim ran6 = New Random(DateTime.Now.Millisecond) : TextBox10.Text = ran6.Next(1, 4096)
        For NUM3 = 0 To 4096
            NUM8 = TextBox10.Text
            If NUM8 > 9 Then : NUM8 = NUM8 - 9 : TextBox10.Text = NUM8 : ElseIf NUM8 < 9 Then : TextBox10.Text = NUM8 : NUM3 = 4096 : End If
        Next
        Dim ran7 = New Random(DateTime.Now.Millisecond) : TextBox11.Text = ran7.Next(1, 4096)
        For NUM3 = 0 To 4096
            NUM9 = TextBox11.Text
            If NUM9 > 7 Then : NUM9 = NUM9 - 7 : TextBox11.Text = NUM9 : ElseIf NUM9 <= 7 Then : If NUM9 <= 5 Then : TextBox11.Text = 3 : NUM3 = 4096 : Else : TextBox11.Text = 3 : NUM3 = 4096
                End If : End If
        Next
        '-----------------------------------------------------------------------------------------------------字串
        WSAQ(1) = 4 : SSD5 = "13:" & TextBox9.Text & TextBox8.Text : SSD6 = "17:" & TextBox11.Text & TextBox10.Text
    End Sub
    Private Sub 第3班上下班()
        If TextBox1.Text = "日" Or TextBox1.Text = "六" Or CheckBox1.Checked = True Then
            TextBox18.Text = 0
        Else
            Dim ran12 = New Random(DateTime.Now.Millisecond) : TextBox18.Text = ran12.Next(1, 4096)
            For NUM3 = 0 To 4096
                NUM15 = TextBox18.Text
                If NUM15 > 7 Then
                    NUM15 = NUM15 - 7 : TextBox18.Text = NUM15
                ElseIf NUM15 <= 7 Then
                    If NUM15 <= 4 Then : TextBox18.Text = 0 : NUM3 = 4096 : Else : TextBox18.Text = 1 : NUM3 = 4096 : End If : End If
            Next
        End If
        '-----------------------------------------------------------------------------------------------------第3班上下班
        If TextBox18.Text = "1" Then
            Dim ran8 = New Random(DateTime.Now.Millisecond) : TextBox12.Text = ran8.Next(1, 4096)
            For NUM3 = 0 To 4096
                NUM11 = TextBox12.Text
                If NUM11 > 9 Then : NUM11 = NUM11 - 9 : TextBox12.Text = NUM11 : ElseIf NUM11 < 9 Then : TextBox12.Text = NUM11 : NUM3 = 4096 : End If
            Next

            Dim ran9 = New Random(DateTime.Now.Millisecond) : TextBox13.Text = ran9.Next(1, 4096)
            For NUM3 = 0 To 4096
                NUM12 = TextBox13.Text
                If NUM12 > 7 Then : NUM12 = NUM12 - 7 : TextBox13.Text = NUM12 : ElseIf NUM12 <= 7 Then : If NUM12 <= 5 Then : TextBox13.Text = 5 : NUM3 = 4096
                    Else : TextBox13.Text = 5 : NUM3 = 4096
                    End If : End If
            Next
            If TextBox13.Text = "0" Then : TextBox14.Text = "18" : Else : TextBox14.Text = "17" : End If

            Dim ran10 = New Random(DateTime.Now.Millisecond) : TextBox15.Text = ran10.Next(1, 4096)
            For NUM3 = 0 To 4096
                NUM13 = TextBox15.Text
                If NUM13 > 9 Then : NUM13 = NUM13 - 9 : TextBox15.Text = NUM13 : ElseIf NUM13 < 9 Then : TextBox15.Text = NUM13 : NUM3 = 4096 : End If
            Next

            Dim ran11 = New Random(DateTime.Now.Millisecond) : TextBox16.Text = ran11.Next(1, 4096)
            For NUM3 = 0 To 4096
                NUM14 = TextBox16.Text
                If NUM14 > 7 Then : NUM14 = NUM14 - 7 : TextBox16.Text = NUM14 : ElseIf NUM14 <= 7 Then : If NUM14 <= 5 Then : TextBox16.Text = 0 : NUM3 = 4096
                    Else : TextBox16.Text = 0 : NUM3 = 4096
                    End If : End If
            Next
            If TextBox16.Text = "0" Then : TextBox17.Text = "20" : Else : TextBox17.Text = "19" : End If
            '-----------------------------------------------------------------------------------------------------字串
            WSAQ(2) = 2 : SSD1 = TextBox14.Text & ":" & TextBox13.Text & TextBox12.Text : SSD2 = TextBox17.Text & ":" & TextBox16.Text & TextBox15.Text
        Else
            TextBox12.Text = "" : TextBox13.Text = "" : TextBox14.Text = "" : TextBox15.Text = "" : TextBox16.Text = "" : TextBox17.Text = ""
            WSAQ(2) = 0 : SSD1 = "" : SSD2 = ""
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Set_日期格式轉換()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 已有考勤日期 FROM 已有考勤日期 WHERE 已有考勤日期 LIKE '" & DTP & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then
            MsgBox("資料重複") : conn.Close() : Exit Sub
        Else
            conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "INSERT INTO 已有考勤日期 (已有考勤日期) VALUES (N'" & DTP & "')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

            For i As Integer = 0 To DataGridView2.Rows.Count - 1
                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 : STR1 = dr("資料流水號").ToString : Else : STR1 = "" : End If
                conn.Close()
                Dim NUM10 As Integer
                NUM10 = Double.Parse(Strings.Right(STR1, 8)) + 1
                If NUM10 < 10 Then : STR1 = "AS" & "0000000" & NUM10
                ElseIf NUM10 > 9 And NUM10 < 100 Then : STR1 = "AS" & "000000" & NUM10
                ElseIf NUM10 > 99 And NUM10 < 1000 Then : STR1 = "AS" & "00000" & NUM10
                ElseIf NUM10 > 999 And NUM10 < 10000 Then : STR1 = "AS" & "0000" & NUM10
                ElseIf NUM10 > 9999 And NUM10 < 100000 Then : STR1 = "AS" & "000" & NUM10
                ElseIf NUM10 > 99999 And NUM10 < 100000 Then : STR1 = "AS" & "00" & NUM10
                ElseIf NUM10 > 999999 And NUM10 < 1000000 Then : STR1 = "AS" & "0" & NUM10
                ElseIf NUM10 > 9999999 Then : STR1 = "AS" & NUM10
                End If
                If TextBox1.Text = "日" Or CheckBox1.Checked = True Then
                    WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : WSAQ(1) = 0 : SSD5 = "" : SSD6 = ""
                Else
                    If (TextBox19.Text = i And TextBox27.Text = 0) Or (TextBox20.Text = i And TextBox28.Text = 0) Or
                        (TextBox21.Text = i And TextBox29.Text = 0) Or (TextBox22.Text = i And TextBox30.Text = 0) Then
                        WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : WSAQ(1) = 0 : SSD5 = "" : SSD6 = "" : WSAQ(2) = 0 : SSD1 = "" : SSD2 = ""
                    ElseIf (TextBox19.Text = i And TextBox27.Text = 1) Then
                        If TextBox31.Text = 1 Then
                            第1班上下班() : WSAQ(1) = 0 : SSD5 = "" : SSD6 = "" : WSAQ(2) = 0 : SSD1 = "" : SSD2 = ""
                        Else
                            WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : 第2班上下班() : 第3班上下班()
                        End If
                    ElseIf (TextBox20.Text = i And TextBox28.Text = 1) Then
                        If TextBox32.Text = 1 Then
                            第1班上下班() : WSAQ(1) = 0 : SSD5 = "" : SSD6 = "" : WSAQ(2) = 0 : SSD1 = "" : SSD2 = ""
                        Else
                            WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : 第2班上下班() : 第3班上下班()
                        End If
                    ElseIf (TextBox21.Text = i And TextBox29.Text = 1) Then
                        If TextBox33.Text = 1 Then
                            第1班上下班() : WSAQ(1) = 0 : SSD5 = "" : SSD6 = "" : WSAQ(2) = 0 : SSD1 = "" : SSD2 = ""
                        Else
                            WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : 第2班上下班() : 第3班上下班()
                        End If
                    ElseIf (TextBox22.Text = i And TextBox30.Text = 1) Then
                        If TextBox34.Text = 1 Then
                            第1班上下班() : WSAQ(1) = 0 : SSD5 = "" : SSD6 = "" : WSAQ(2) = 0 : SSD1 = "" : SSD2 = ""
                        Else
                            WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : 第2班上下班() : 第3班上下班()
                        End If
                    Else
                        第1班上下班() : 第2班上下班() : 第3班上下班()
                    End If
                End If
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 考勤資料庫 (資料流水號, DataID, 員工名稱, 出勤日期, 星期, 第1班上班, 第1班下班, 第1班合計, 第2班上班, 第2班下班, 
                            第2班合計, 第3班上班, 第3班下班, 第3班合計) VALUES (N'" & STR1 & "', " &
                                              "N'" & DataGridView2.Rows(i).Cells("DataID").Value & "', " &
                                              "N'" & DataGridView2.Rows(i).Cells("姓名").Value & "', " &
                                              "N'" & DTP & "', " &
                                              "N'" & TextBox1.Text & "', " &
                                              "N'" & SSD3 & "', " &
                                              "N'" & SSD4 & "', '" & WSAQ(0) & "', " &
                                              "N'" & SSD5 & "', " &
                                              "N'" & SSD6 & "', '" & WSAQ(1) & "', " &
                                              "N'" & SSD1 & "', N'" & SSD2 & "', '" & WSAQ(2) & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            Next
            Set_清單() : MsgBox("維護完成") : CheckBox1.Checked = False
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then
            Set_日期格式轉換()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 已有考勤日期 WHERE (已有考勤日期 = '" & DTP & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 考勤資料庫 WHERE (出勤日期 = '" & DTP & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()

            Set_清單() : MsgBox("刪除完成")
        End If
    End Sub
    Public Sub Delay(ByRef Interval As Double)  'Interval单位为毫秒
        Dim time As DateTime = DateTime.Now
        Dim Span As Double = Interval * 1234
        While ((DateTime.Now.Ticks - time.Ticks) < Span)
            Application.DoEvents()
        End While
    End Sub
    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
        Dim NUM1 As Integer = 0 : Dim NUM4 As Integer = 0 : Dim NUM5 As Integer = 0 : Dim NUM6 As Integer = 0 : Dim NUM7 As Integer = 0 : Dim NUM8 As Integer = 0
        If CheckBox2.Checked = True Then
            Dim ran = New Random(DateTime.Now.Millisecond) : TextBox19.Text = ran.Next(1, 4096) : NUM1 = TextBox19.Text
            For NUM3 = 0 To 4096
                If NUM1 > DataGridView2.Rows.Count - 1 Then : NUM1 = NUM1 - DataGridView2.Rows.Count - 1
                ElseIf NUM1 < DataGridView2.Rows.Count - 1 Then : NUM4 = NUM1 : NUM8 = NUM4
                    For I = 0 To NUM4
                        If NUM8 <= NUM4 And NUM8 > 5 Then : NUM8 = NUM8 - 5
                        ElseIf NUM8 <= 5 Then
                            If NUM8 > 3 Then : TextBox27.Text = 0 : TextBox31.Text = 0
                            Else : TextBox27.Text = 1 : If NUM8 = 3 Or NUM8 = 2 Then : TextBox31.Text = 0 : Else : TextBox31.Text = 1 : End If
                            End If : I = NUM4
                        End If
                    Next : NUM3 = 4096
                End If
            Next : Delay(3)
            Dim ran1 = New Random(DateTime.Now.Millisecond) : TextBox20.Text = ran1.Next(1, 4096) : NUM1 = TextBox20.Text
            For NUM3 = 0 To 4096
                If NUM1 > DataGridView2.Rows.Count - 1 Then : NUM1 = NUM1 - DataGridView2.Rows.Count - 1
                ElseIf NUM1 < DataGridView2.Rows.Count - 1 Then : NUM5 = NUM1 : NUM8 = NUM5
                    For I = 0 To NUM5
                        If NUM8 <= NUM5 And NUM8 > 5 Then : NUM8 = NUM8 - 5
                        ElseIf NUM8 <= 5 Then
                            If NUM8 > 3 Then : TextBox28.Text = 0 : TextBox32.Text = 0
                            Else : TextBox28.Text = 1 : If NUM8 = 3 Or NUM8 = 2 Then : TextBox32.Text = 0 : Else : TextBox32.Text = 1 : End If
                            End If : I = NUM5
                        End If
                    Next : NUM3 = 4096
                End If
            Next : Delay(3)
            Dim ran2 = New Random(DateTime.Now.Millisecond) : TextBox21.Text = ran2.Next(1, 4096) : NUM1 = TextBox21.Text
            For NUM3 = 0 To 4096
                If NUM1 > DataGridView2.Rows.Count - 1 Then : NUM1 = NUM1 - DataGridView2.Rows.Count - 1
                ElseIf NUM1 < DataGridView2.Rows.Count - 1 Then : NUM6 = NUM1 : NUM8 = NUM6
                    For I = 0 To NUM6
                        If NUM8 <= NUM6 And NUM8 > 5 Then : NUM8 = NUM8 - 5
                        ElseIf NUM8 <= 5 Then
                            If NUM8 > 3 Then : TextBox29.Text = 0 : TextBox33.Text = 0
                            Else : TextBox29.Text = 1 : If NUM8 = 3 Or NUM8 = 2 Then : TextBox33.Text = 0 : Else : TextBox33.Text = 1 : End If
                            End If : I = NUM6
                        End If
                    Next : NUM3 = 4096
                End If
            Next : Delay(3)
            Dim ran3 = New Random(DateTime.Now.Millisecond) : TextBox22.Text = ran3.Next(1, 4096) : NUM1 = TextBox22.Text
            For NUM3 = 0 To 4096
                If NUM1 > DataGridView2.Rows.Count - 1 Then : NUM1 = NUM1 - DataGridView2.Rows.Count - 1
                ElseIf NUM1 < DataGridView2.Rows.Count - 1 Then : NUM7 = NUM1 : NUM8 = NUM7
                    For I = 0 To NUM7
                        If NUM8 <= NUM7 And NUM8 > 5 Then : NUM8 = NUM8 - 5
                        ElseIf NUM8 <= 5 Then
                            If NUM8 > 3 Then : TextBox30.Text = 0 : TextBox34.Text = 0
                            Else : TextBox30.Text = 1 : If NUM8 = 3 Or NUM8 = 2 Then : TextBox34.Text = 0 : Else : TextBox34.Text = 1 : End If
                            End If : I = NUM7
                        End If
                    Next : NUM3 = 4096
                End If
            Next

            If NUM4 = NUM5 Then : NUM4 = NUM4 + 3 : If NUM4 > DataGridView2.Rows.Count - 1 Then : NUM4 = NUM4 - DataGridView2.Rows.Count - 1 : End If : End If
            If NUM4 = NUM6 Then : NUM4 = NUM4 + 3 : If NUM4 > DataGridView2.Rows.Count - 1 Then : NUM4 = NUM4 - DataGridView2.Rows.Count - 1 : End If : End If
            If NUM4 = NUM7 Then : NUM4 = NUM4 + 3 : If NUM4 > DataGridView2.Rows.Count - 1 Then : NUM4 = NUM4 - DataGridView2.Rows.Count - 1 : End If : End If
            If NUM5 = NUM6 Then : NUM5 = NUM5 + 3 : If NUM5 > DataGridView2.Rows.Count - 1 Then : NUM5 = NUM5 - DataGridView2.Rows.Count - 1 : End If : End If
            If NUM5 = NUM7 Then : NUM5 = NUM5 + 3 : If NUM5 > DataGridView2.Rows.Count - 1 Then : NUM5 = NUM5 - DataGridView2.Rows.Count - 1 : End If : End If
            If NUM6 = NUM7 Then : NUM6 = NUM6 + 3 : If NUM6 > DataGridView2.Rows.Count - 1 Then : NUM6 = NUM6 - DataGridView2.Rows.Count - 1 : End If : End If
            TextBox19.Text = NUM4 : TextBox20.Text = NUM5 : TextBox21.Text = NUM6 : TextBox22.Text = NUM7
            TextBox23.Text = DataGridView2.Rows(NUM4).Cells("姓名").Value.ToString : TextBox24.Text = DataGridView2.Rows(NUM5).Cells("姓名").Value.ToString
            TextBox25.Text = DataGridView2.Rows(NUM6).Cells("姓名").Value.ToString : TextBox26.Text = DataGridView2.Rows(NUM7).Cells("姓名").Value.ToString
        Else
            TextBox19.Text = "-1" : TextBox20.Text = "-1" : TextBox21.Text = "-1" : TextBox22.Text = "-1" : TextBox23.Text = "" : TextBox24.Text = "" : TextBox25.Text = "" : TextBox26.Text = ""
            TextBox27.Text = "0" : TextBox28.Text = "0" : TextBox29.Text = "0" : TextBox30.Text = "0" : TextBox31.Text = "0" : TextBox32.Text = "0" : TextBox33.Text = "0" : TextBox34.Text = "0"
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim ASDFG As Integer = 0
        For ii As Integer = 1 To TextBox35.Text
            CheckBox2.Checked = False
            DateTimePicker1.Value = DateTimePicker1.Value.AddDays(1)
            Set_清單()
            Set_日期格式轉換()
            ASDFG = ASDFG + 1
            If ASDFG = 2 Then
                CheckBox2.Checked = True : ASDFG = 0
            End If
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 已有考勤日期 FROM 已有考勤日期 WHERE 已有考勤日期 LIKE '" & DTP & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then
                MsgBox("資料重複") : conn.Close() : Exit Sub
            Else
                conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 已有考勤日期 (已有考勤日期) VALUES (N'" & DTP & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

                For i As Integer = 0 To DataGridView2.Rows.Count - 1
                    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 : STR1 = dr("資料流水號").ToString : Else : STR1 = "" : End If
                    conn.Close()
                    Dim NUM10 As Integer
                    NUM10 = Double.Parse(Strings.Right(STR1, 8)) + 1
                    If NUM10 < 10 Then : STR1 = "AS" & "0000000" & NUM10
                    ElseIf NUM10 > 9 And NUM10 < 100 Then : STR1 = "AS" & "000000" & NUM10
                    ElseIf NUM10 > 99 And NUM10 < 1000 Then : STR1 = "AS" & "00000" & NUM10
                    ElseIf NUM10 > 999 And NUM10 < 10000 Then : STR1 = "AS" & "0000" & NUM10
                    ElseIf NUM10 > 9999 And NUM10 < 100000 Then : STR1 = "AS" & "000" & NUM10
                    ElseIf NUM10 > 99999 And NUM10 < 100000 Then : STR1 = "AS" & "00" & NUM10
                    ElseIf NUM10 > 999999 And NUM10 < 1000000 Then : STR1 = "AS" & "0" & NUM10
                    ElseIf NUM10 > 9999999 Then : STR1 = "AS" & NUM10
                    End If
                    If TextBox1.Text = "日" Or CheckBox1.Checked = True Then
                        WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : WSAQ(1) = 0 : SSD5 = "" : SSD6 = ""
                    Else
                        If (TextBox19.Text = i And TextBox27.Text = 0) Or (TextBox20.Text = i And TextBox28.Text = 0) Or
                            (TextBox21.Text = i And TextBox29.Text = 0) Or (TextBox22.Text = i And TextBox30.Text = 0) Then
                            WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : WSAQ(1) = 0 : SSD5 = "" : SSD6 = "" : WSAQ(2) = 0 : SSD1 = "" : SSD2 = ""
                        ElseIf (TextBox19.Text = i And TextBox27.Text = 1) Then
                            If TextBox31.Text = 1 Then
                                第1班上下班() : WSAQ(1) = 0 : SSD5 = "" : SSD6 = "" : WSAQ(2) = 0 : SSD1 = "" : SSD2 = ""
                            Else
                                WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : 第2班上下班() : 第3班上下班()
                            End If
                        ElseIf (TextBox20.Text = i And TextBox28.Text = 1) Then
                            If TextBox32.Text = 1 Then
                                第1班上下班() : WSAQ(1) = 0 : SSD5 = "" : SSD6 = "" : WSAQ(2) = 0 : SSD1 = "" : SSD2 = ""
                            Else
                                WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : 第2班上下班() : 第3班上下班()
                            End If
                        ElseIf (TextBox21.Text = i And TextBox29.Text = 1) Then
                            If TextBox33.Text = 1 Then
                                第1班上下班() : WSAQ(1) = 0 : SSD5 = "" : SSD6 = "" : WSAQ(2) = 0 : SSD1 = "" : SSD2 = ""
                            Else
                                WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : 第2班上下班() : 第3班上下班()
                            End If
                        ElseIf (TextBox22.Text = i And TextBox30.Text = 1) Then
                            If TextBox34.Text = 1 Then
                                第1班上下班() : WSAQ(1) = 0 : SSD5 = "" : SSD6 = "" : WSAQ(2) = 0 : SSD1 = "" : SSD2 = ""
                            Else
                                WSAQ(0) = 0 : SSD3 = "" : SSD4 = "" : 第2班上下班() : 第3班上下班()
                            End If
                        Else
                            第1班上下班() : 第2班上下班() : 第3班上下班()
                        End If
                    End If
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "INSERT INTO 考勤資料庫 (資料流水號, DataID, 員工名稱, 出勤日期, 星期, 第1班上班, 第1班下班, 第1班合計, 第2班上班, 第2班下班, 
                            第2班合計, 第3班上班, 第3班下班, 第3班合計) VALUES (N'" & STR1 & "', " &
                                                  "N'" & DataGridView2.Rows(i).Cells("DataID").Value & "', " &
                                                  "N'" & DataGridView2.Rows(i).Cells("姓名").Value & "', " &
                                                  "N'" & DTP & "', " &
                                                  "N'" & TextBox1.Text & "', " &
                                                  "N'" & SSD3 & "', " &
                                                  "N'" & SSD4 & "', '" & WSAQ(0) & "', " &
                                                  "N'" & SSD5 & "', " &
                                                  "N'" & SSD6 & "', '" & WSAQ(1) & "', " &
                                                  "N'" & SSD1 & "', N'" & SSD2 & "', '" & WSAQ(2) & "')"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                Next
            End If
        Next
        Set_清單() : MsgBox("維護完成") : CheckBox1.Checked = False
    End Sub
End Class