Public Class frmManualAttendance
    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        If cmbParameter.Text = "" Or txtCriteria.Text = "" Then
            ErrorProvider1.SetError(cmbParameter, "Mandatory field.")
            ErrorProvider1.SetError(txtCriteria, "Mandatory field.")
        Else
            ErrorProvider1.Clear()
            sqlSTR = " SELECT        Employee_Information.EMPLOYEE_ID, Employee_Information.EMP_CODE, Employee_Information.EmployeesName, Employee_Information.FathersName, Employee_Information.Address, Employee_Information.PhoneNumber, " &
                         " Employee_Information.PHOTO_FILE_NAME, Employee_Information.MobileNumber, Employee_Information.DateOfBirth, Employee_Information.PassportNo, Employee_Information.Gender, Employee_Information.MaritalStatus, Employee_Information.JobTitle, " &
                         " Department.DepartmentName, Designation.DesignationName, WorkingHours.ShiftName, Employee_Information.DateOfCommencement, Employee_Information.ACTIVE_ACCOUNT, Employee_Information.BasicSalary FROM            Employee_Information LEFT OUTER JOIN " &
                         " WorkingHours ON Employee_Information.ShiftID = WorkingHours.ShiftID LEFT OUTER JOIN Designation ON Employee_Information.DESG_ID = Designation.DESG_ID LEFT OUTER JOIN  Department ON Employee_Information.DEP_ID = Department.DEP_ID " &
                         " WHERE   (Employee_Information." + cmbParameter.Text + " LIKE '%" + txtCriteria.Text + "%') ORDER BY Employee_Information.EMPLOYEE_ID"
            ExecuteSQLQuery(sqlSTR)
            If sqlDT.Rows.Count > 0 Then
                txtEmpID.Text = sqlDT.Rows(0)("EMPLOYEE_ID")
                txtEmployeesName.Text = sqlDT.Rows(0)("EmployeesName")
                txtFathersName.Text = sqlDT.Rows(0)("FathersName")
                txtShiftName.Text = sqlDT.Rows(0)("ShiftName")
                Try
                    PictureBox1.ImageLocation = Application.StartupPath + "\Upload\Employee\" + sqlDT.Rows(0)("PHOTO_FILE_NAME")
                Catch ex As Exception
                    PictureBox1.Image = PayrollManagementSystem.My.Resources.no_image
                End Try
            Else
                txtEmpID.Text = ""
                txtEmployeesName.Text = ""
                txtFathersName.Text = ""
                txtShiftName.Text = ""
                txtComment.Text = ""
                dtpEnrollDate.Value = Now
                dtpTime.Value = Now
                rbTimeIn.Checked = True
                MsgBox("No Record Found.", MsgBoxStyle.Information, "Information")
            End If
        End If
    End Sub

    Private Sub txtCriteria_TextChanged(sender As Object, e As EventArgs) Handles txtCriteria.TextChanged
        If txtCriteria.Text = "" Then
            txtEmpID.Text = ""
            txtEmployeesName.Text = ""
            txtFathersName.Text = ""
            txtShiftName.Text = ""
            txtComment.Text = ""
            dtpEnrollDate.Value = Now
            dtpTime.Value = Now
            rbTimeIn.Checked = True
        End If
    End Sub

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        If txtEmpID.Text = "" Then
            ErrorProvider1.SetError(cmbParameter, "Mandatory field.")
            ErrorProvider1.SetError(txtCriteria, "Mandatory field.")
        Else
            ErrorProvider1.Clear()
            ExecuteSQLQuery(" SELECT  * FROM   Leave1  WHERE  (EMPLOYEE_ID = " & num_fltr(txtEmpID.Text) & ") AND ('" & Format(dtpEnrollDate.Value, "yyyy-MM-dd") & "' BETWEEN StartDate AND EndDate) ")
            If sqlDT.Rows.Count > 0 Then
                MsgBox("The employee is on leave", MsgBoxStyle.Critical, "Critical")
            Else
                ExecuteSQLQuery("SELECT  * FROM   Employee_Information  WHERE  (EMPLOYEE_ID = " & num_fltr(txtEmpID.Text) & ")")
                Dim ShiftID As Double = sqlDT.Rows(0)("ShiftID")
                ExecuteSQLQuery("SELECT *  FROM      WorkingHours  WHERE   (ShiftID = " & num_fltr(ShiftID) & ")")
                If sqlDT.Rows.Count > 0 Then
                    Dim StartTime As DateTime = Format(sqlDT.Rows(0)("StartTime"), "HH:mm")
                    Dim EndTime As DateTime = Format(sqlDT.Rows(0)("EndTime"), "HH:mm")
                    ExecuteSQLQuery("SELECT * FROM  Attendance  WHERE   (EMPLOYEE_ID = " & num_fltr(txtEmpID.Text) & ") AND (AttendanceDate = '" & Format(dtpEnrollDate.Value, "yyyy-MM-dd") & "') AND (Status = 'TimeOut')")
                    If sqlDT.Rows.Count > 0 Then
                        MsgBox("Attendance already taken.", MsgBoxStyle.Critical, "Critical")
                    Else
                        If rbTimeIn.Checked = True Then
                            ExecuteSQLQuery("SELECT * FROM  Attendance  WHERE   (EMPLOYEE_ID = " & num_fltr(txtEmpID.Text) & ") AND (AttendanceDate = '" & Format(dtpEnrollDate.Value, "yyyy-MM-dd") & "') AND (Status = 'TimeIn')")
                            If sqlDT.Rows.Count > 0 Then
                                MsgBox("Employee already time in.", MsgBoxStyle.Critical, "Critical")
                            Else
                                ExecuteSQLQuery(" INSERT INTO Attendance (EMPLOYEE_ID, AttendanceDate, TimeIn, TimeIn_Comment, Status) VALUES " &
                                                " (" & num_fltr(txtEmpID.Text) & ", '" & Format(dtpEnrollDate.Value, "yyyy-MM-dd") & "', '" & Format(dtpTime.Value, "HH:mm") & "',  '" + str_repl(txtComment.Text) + "', 'TimeIn') ")
                                txtCriteria.Text = ""
                                MsgBox("Time In saved successfully.", MsgBoxStyle.Information, "Information")
                            End If
                        ElseIf rbTimeOut.Checked = True Then
                            ExecuteSQLQuery(" UPDATE Attendance SET TimeOut='" & Format(dtpTime.Value, "HH:mm") & "', TimeOut_Comment='" + str_repl(txtComment.Text) + "', Status = 'TimeOut' " &
                                            " WHERE        (EMPLOYEE_ID = " & num_fltr(txtEmpID.Text) & ") AND (AttendanceDate = '" & Format(dtpEnrollDate.Value, "yyyy-MM-dd") & "') AND (Status = 'TimeIn') ")
                            txtCriteria.Text = ""
                            MsgBox("Time Out saved successfully.", MsgBoxStyle.Information, "Information")
                        End If
                    End If
                Else
                    MsgBox("Working hours not found.", MsgBoxStyle.Critical, "Critical")
                End If
            End If
        End If
    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class