Option Strict Off
Imports System.Data.SqlClient
Public Class 新打卡系統
    Private Property MYSQL As String
    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 DTP As String : Dim DTP1 As String
    Dim 有無資料判別 As String = ""
    Private Sub 新打卡系統_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ConString = "Data Source=mssql-147629-0.cloudclusters.net,12711;Initial Catalog=GCM-AM-SYS;Persist Security Info=True;User ID=b70340;Password=Lee0911274990;Max pool size = 200;Connection Timeout=0"
        FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
        Label1.Text = Year(Today) : Label3.Text = Month(Today) : Label5.Text = Strings.Right(Format(Today(), "yyyy/MM/dd"), 2) : Label7.Text = Format(TimeOfDay(), "HH:mm:ss")
        Label16.Text = "" : Label19.Text = "" : Button1.Text = "打卡系統運行中" : Label20.Text = "" : Label21.Text = ""

        Label1.BackColor = Color.Transparent : Label2.BackColor = Color.Transparent : Label3.BackColor = Color.Transparent : Label4.BackColor = Color.Transparent : Label5.BackColor = Color.Transparent
        Label6.BackColor = Color.Transparent : Label7.BackColor = Color.Transparent : Label8.BackColor = Color.Transparent : Label15.BackColor = Color.Transparent : Label16.BackColor = Color.Transparent
        Label17.BackColor = Color.Transparent : Label19.BackColor = Color.Transparent

        Timer2.Enabled = True : TextBox2.Focus()
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Label16.Text = "" : Label19.Text = "" : Button1.Text = "打卡系統運行中" : Label20.Text = "" : Label21.Text = ""
        Timer1.Enabled = False
    End Sub
    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Label1.Text = Year(Today) : Label3.Text = Month(Today) : Label5.Text = Strings.Right(Format(Today(), "yyyy/MM/dd"), 2) : Label7.Text = Format(TimeOfDay(), "HH:mm:ss")
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        Label1.Text = Year(Today) : Label3.Text = Month(Today) : Label5.Text = Strings.Right(Format(Today(), "yyyy/MM/dd"), 2) : Label7.Text = Format(TimeOfDay(), "HH:mm:ss")

        If IsNumeric(TextBox2.Text) = True Then
            If TextBox2.Text.Length = 10 Then
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                MYSQL = "SELECT  使用者權限管理表.DIN, ras_Users.UserName FROM 使用者權限管理表 INNER JOIN ras_Users ON 使用者權限管理表.DIN = ras_Users.DIN 
                         WHERE (使用者權限管理表.ID卡號 LIKE N'" & TextBox2.Text & "')"
                cmd.Connection = conn : cmd.CommandText = MYSQL : dr = cmd.ExecuteReader
                If dr.Read() Then
                    Label16.Text = dr("UserName").ToString
                    Label19.Text = dr("DIN").ToString
                    conn.Close()
                Else
                    TextBox2.Text = "" : TextBox2.Focus() : conn.Close()
                End If
                Timer1.Enabled = False
                Me.Button1.PerformClick()
            End If
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer1.Enabled = True : Label21.Text = "0"
        Label20.Text = "打卡時間 : " & Label1.Text & "-" & Label3.Text & "-" & Label5.Text & " " & Label7.Text
        時間_dtp.Value = Now() : Label9.Text = Format(時間_dtp.Value, "yyyy-MM-dd HH:mm:ss")
        conn.Close() : If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        MYSQL = "SELECT ID FROM ras_AttRecord ORDER BY ID DESC"
        cmd.Connection = conn : cmd.CommandText = MYSQL : dr = cmd.ExecuteReader
        If dr.Read() Then
            Label21.Text = dr("ID") : Label21.Text = Val(Label21.Text) + 1
        End If : conn.Close()

        conn.Close() : If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        MYSQL = "SELECT ID FROM ras_AttRecord ORDER BY ID DESC"
        cmd.Connection = conn : cmd.CommandText = MYSQL : dr = cmd.ExecuteReader
        If dr.Read() Then
            Label21.Text = dr("ID")
        End If
        Label21.Text = Val(Label21.Text) + 1

        conn.Close() : If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        conn.Close() : If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        conn.Close() : If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        MYSQL = "INSERT INTO ras_AttRecord (DN, DIN, Clock, VerifyMode, Action, AttTypeId, CollectDate, JobCode, Antipassback, LastUpdatedUID,  LastUpdatedDate, Remark) 
                 VALUES (N'1', N'" & Label19.Text & "', N'" & Label9.Text & "', N'0', N'1', N'H01', 
                         " & Today() & ", 0, 0, 0, " & Today() & ", N'')"
        cmd.Connection = conn : cmd.CommandText = MYSQL : dr = cmd.ExecuteReader : conn.Close()

        TextBox2.Text = "" : TextBox2.Focus()
    End Sub
End Class