Public Class frmLeave1
    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        GroupControl2.Visible = True
        If txtEmployeesCode.Text = "" Then
            ErrorProvider1.SetError(txtEmployeesCode, "Mandatory field.")
        Else
            ErrorProvider1.Clear()
            ExecuteSQLQuery(" SELECT        Employee_Information.EMPLOYEE_ID, Employee_Information.EmployeesName, WorkingHours.ShiftName, Employee_Information.PHOTO_FILE_NAME " &
                            " FROM            Employee_Information LEFT OUTER JOIN  WorkingHours ON Employee_Information.ShiftID = WorkingHours.ShiftID  WHERE  (Employee_Information.EMP_CODE = '" + str_repl(txtEmployeesCode.Text) + "') ")
            If sqlDT.Rows.Count > 0 Then
                txtEmpID.Text = sqlDT.Rows(0)("EMPLOYEE_ID")
                txtEmployeesName.Text = sqlDT.Rows(0)("EmployeesName")
                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 = ""
                txtReason.Text = ""
                txtShiftName.Text = ""
                dtpStartDate.Value = Now
                PictureBox1.Image = PayrollManagementSystem.My.Resources.no_image
                MsgBox("No Record Found.", MsgBoxStyle.Information, "Information")
            End If
        End If
    End Sub

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        Dim Borrow As DateTime = Convert.ToDateTime(dtpStartDate.Text)
        Dim Back As DateTime = Convert.ToDateTime(dtpEndDate.Text)
        Dim CountDays As TimeSpan = Back.Subtract(Borrow)
        Dim TotalDays = Convert.ToInt32(CountDays.Days)
        If txtEmpID.Text = "" Or cmbLeaveType.Text = "" Then
            ErrorProvider1.SetError(txtEmployeesCode, "Mandatory field.")
            ErrorProvider1.SetError(cmbLeaveType, "Mandatory field.")
        ElseIf Not (Convert.ToInt32(CountDays.Days) >= 0) Then
            MsgBox("Please check the start and end date and try again.", MsgBoxStyle.Critical, "Critical")
        Else
            ErrorProvider1.Clear()
            If btnSubmit.Text = "SAVE" Then
                ExecuteSQLQuery(" INSERT INTO Leave1 (EMPLOYEE_ID, LeaveType, StartDate, EndDate, NumberOfDays, Reason) VALUES " &
                                 " (" & num_fltr(txtEmpID.Text) & ", '" + cmbLeaveType.Text + "' ,'" & Format(dtpStartDate.Value, "yyyy-MM-dd") & "','" & Format(dtpEndDate.Value, "yyyy-MM-dd") & "', " & TotalDays + 1 & ", '" + str_repl(txtReason.Text) + "' ) ")
            ElseIf btnSubmit.Text = "UPDATE" Then
                ExecuteSQLQuery(" UPDATE  Leave1 SET   EMPLOYEE_ID=" & num_fltr(txtEmpID.Text) & ", LeaveType='" + cmbLeaveType.Text + "', StartDate='" & Format(dtpStartDate.Value, "yyyy-MM-dd") & "', EndDate='" & Format(dtpEndDate.Value, "yyyy-MM-dd") & "', NumberOfDays=" & TotalDays + 1 & ", Reason= '" + str_repl(txtReason.Text) + "' " &
                                " WHERE LEVE_ENROLL_ID = " & num_fltr(txtLEVE_ENROLL_ID.Text) & " ")
            End If
            txtEmpID.Text = ""
            txtEmployeesName.Text = ""
            txtReason.Text = ""
            txtShiftName.Text = ""
            cmbLeaveType.Text = ""
            dtpStartDate.Value = Now
            dtpEndDate.Value = Now
            txtLEVE_ENROLL_ID.Text = ""
            btnSubmit.Text = "SAVE"
            PictureBox1.Image = PayrollManagementSystem.My.Resources.no_image
            MsgBox("Record has been saved successfully.", MsgBoxStyle.Information, "Information")
        End If
    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
    Private Sub txtEmployeesCode_TextChanged(sender As Object, e As EventArgs) Handles txtEmployeesCode.TextChanged
        If txtEmployeesCode.Text = "" Then
            txtEmpID.Text = ""
            txtEmployeesName.Text = ""
            txtReason.Text = ""
            txtShiftName.Text = ""
            cmbLeaveType.Text = ""
            dtpStartDate.Value = Now
            PictureBox1.Image = PayrollManagementSystem.My.Resources.no_image
        End If
    End Sub

    Private Sub frmLeave1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GroupControl2.Visible = False
        Label10.Visible = False
        TextBox1.Visible = False
        InitializeDatabaseConnection()
        sqlSTR = "SELECT   LeaveType, Description, id  FROM   tblleavetype  ORDER BY id"
        ExecuteSQLQuery(sqlSTR)
        FillDataComboBox(sqlSTR, "LeaveType", "LeaveType", cmbLeaveType)
    End Sub



    Private Sub cmbLeaveType_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles cmbLeaveType.SelectionChangeCommitted
        If cmbLeaveType.SelectedValue = "Annual Leave" Then
            TextBox1.Visible = True
            Label10.Visible = True
            leavecal()
        End If
    End Sub
    Private Sub leavecal()
        ExecuteSQLQuery("SELECT  DateOfCommencement  FROM   employee_information WHERE  (EMP_CODE = '" + str_repl(txtEmployeesCode.Text) + "')   ")
        Dim stdat As Date
        If sqlDT.Rows.Count > 0 Then
            stdat = Convert.ToDateTime(sqlDT.Rows(0)("DateOfCommencement"))
            Dim fidat As Date
            fidat = Date.Today.ToString("yyyy-MM-dd")
            'TextBox1.Text = DateDiff(DateInterval.Year, stdat, fidat)
            ExecuteSQLQuery("SELECT  *  FROM   leavesetting")
            If DateDiff(DateInterval.Year, stdat, fidat) <= 1 Then
                TextBox1.Text = sqlDT.Rows(0)("Annual1")
            ElseIf DateDiff(DateInterval.Year, stdat, fidat) >= 2 And DateDiff(DateInterval.Year, stdat, fidat) <= 5 Then
                TextBox1.Text = sqlDT.Rows(0)("Annual2")
            ElseIf DateDiff(DateInterval.Year, stdat, fidat) > 5 Then
                TextBox1.Text = sqlDT.Rows(0)("Annual3")
            End If


            End If

    End Sub

End Class