Imports System.IO
Imports MySql.Data.MySqlClient

Public Class frmEmployeeDetails1
    Dim fileExtension As String = ".png"
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If txtEmployeesCode.Text = "" Or txtEmployeesName.Text = "" Or txtMobileNumber.Text = "" Or cmbDepartment.SelectedValue Is Nothing Or cmbDepartment.SelectedIndex = -1 Or cmbDesignation.SelectedValue Is Nothing Or cmbDesignation.SelectedIndex = -1 Or cmbShift.SelectedValue Is Nothing Or cmbShift.SelectedIndex = -1 Then
            ErrorProvider1.SetError(txtEmployeesCode, "Mandatory field.")
            ErrorProvider1.SetError(txtEmployeesName, "Mandatory field.")
            ErrorProvider1.SetError(txtMobileNumber, "Mandatory field.")
            ErrorProvider1.SetError(cmbDepartment, "Mandatory field.")
            ErrorProvider1.SetError(cmbDesignation, "Mandatory field.")
            ErrorProvider1.SetError(cmbShift, "Mandatory field.")
        ElseIf Not IsNumeric(txtBasicSalary.Text) Then
            ErrorProvider1.SetError(txtBasicSalary, "Numeric input only!")
        Else
            ErrorProvider1.Clear()
            Dim EmployeeActive As String
            If chkActive.Checked = True Then
                EmployeeActive = "Y"
            Else
                EmployeeActive = "N"
            End If
            If btnSave.Text = "SAVE" Then
                txtBasicSalary.Text = Base64Encode(Encrypt(txtBasicSalary.Text))
                ExecuteSQLQuery(" INSERT INTO Employee_Information (EMP_CODE, EmployeesName, FathersName, Address, PhoneNumber, MobileNumber, DateOfBirth, PassportNo, PassportExpiry, Gender, MaritalStatus, JobTitle, DESG_ID, DEP_ID, ShiftID, DateOfCommencement, ACTIVE_ACCOUNT, BasicSalary) VALUES " &
                                " ('" + str_repl(txtEmployeesCode.Text) + "', '" + str_repl(txtEmployeesName.Text) + "', '" + str_repl(txtFathersName.Text) + "', '" + str_repl(txtAddress.Text) + "', '" + str_repl(txtPhoneNumber.Text) + "', '" + str_repl(txtMobileNumber.Text) + "', '" & Format(dtpDateOfBirth.Value, "yyyy-MM-dd") & "', '" + str_repl(txtPassportNo.Text) + "', '" & Format(dptPassportexpiry.Value, "yyyy-MM-dd") & "', '" + str_repl(cmbGender.Text) + "', '" + str_repl(cmbMaritalStatus.Text) + "', '" + str_repl(txtJobTitle.Text) + "', " & num_fltr(cmbDesignation.SelectedValue) & ", " & num_fltr(cmbDepartment.SelectedValue) & ", " & num_fltr(cmbShift.SelectedValue) & ", '" & Format(dtpDateOfCommencement.Value, "yyyy-MM-dd") & "', '" + EmployeeActive + "', '" & txtBasicSalary.Text & "') ")
                ExecuteSQLQuery("SELECT  EMPLOYEE_ID FROM     Employee_Information  ORDER BY EMPLOYEE_ID DESC")
                Dim EMPLOYEE_ID As Double = sqlDT.Rows(0)("EMPLOYEE_ID")
                If Not OpenFileDialog1.FileName = "" Then
                    UploadEmployeeImages(EMPLOYEE_ID)
                End If
                ExecuteSQLQuery(" INSERT INTO applicationuser (EMPLOYEE_ID, Full_Name, Fathers_Name, User_Name, Password, Email, UserType) VALUES " &
                                 " ('" + sqlDT.Rows(0)("EMPLOYEE_ID").ToString() + "', '" + str_repl(txtEmployeesName.Text) + "', '" + str_repl(txtFathersName.Text) + "', '" + str_repl(txtUserName.Text) + "', '" + str_repl(txtPassword.Text) + "', '" + str_repl(txtEmailID.Text) + "', 'User') ")
            ElseIf btnSave.Text = "UPDATE" Then
                txtBasicSalary.Text = Base64Encode(Encrypt(txtBasicSalary.Text))
                ExecuteSQLQuery(" UPDATE  Employee_Information SET  EMP_CODE='" + str_repl(txtEmployeesCode.Text) + "', EmployeesName='" + str_repl(txtEmployeesName.Text) + "', FathersName='" + str_repl(txtFathersName.Text) + "', Address='" + str_repl(txtAddress.Text) + "', PhoneNumber='" + str_repl(txtPhoneNumber.Text) + "', MobileNumber='" + str_repl(txtMobileNumber.Text) + "', BasicSalary='" & txtBasicSalary.Text & "',   " &
                                " DateOfBirth= '" & Format(dtpDateOfBirth.Value, "yyyy-MM-dd") & "' , PassportNo='" + str_repl(txtPassportNo.Text) + "', PassportExpiry= '" & Format(dptPassportexpiry.Value, "yyyy-MM-dd") & "' , Gender='" + str_repl(cmbGender.Text) + "', MaritalStatus='" + str_repl(cmbMaritalStatus.Text) + "', JobTitle='" + str_repl(txtJobTitle.Text) + "', DESG_ID=" & num_fltr(cmbDesignation.SelectedValue) & ", DEP_ID=" & num_fltr(cmbDepartment.SelectedValue) & ", ShiftID=" & num_fltr(cmbShift.SelectedValue) & ", DateOfCommencement='" & Format(dtpDateOfCommencement.Value, "yyyy-MM-dd") & "', ACTIVE_ACCOUNT= '" + EmployeeActive + "'  WHERE EMPLOYEE_ID=" & num_fltr(txtEmpID.Text) & " ")
                If Not OpenFileDialog1.FileName = "" Then
                    UploadEmployeeImages(num_fltr(txtEmpID.Text))
                End If
            End If
            btnSave.Text = "SAVE"
            btnReset.PerformClick()
            MsgBox("Record has been saved successfully.", MsgBoxStyle.Information, "Information")
        End If
    End Sub

    Private Sub frmEmployeeDetails1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        btnRefresh.PerformClick()
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        FillDataComboBox("SELECT  DESG_ID, DesignationName FROM Designation", "DESG_ID", "DesignationName", cmbDesignation)
        FillDataComboBox("SELECT  DEP_ID, DepartmentName FROM Department", "DEP_ID", "DepartmentName", cmbDepartment)
        FillDataComboBox("SELECT  ShiftID, ShiftName FROM WorkingHours", "ShiftID", "ShiftName", cmbShift)
        If txtEmpID.Text = "" Then
        Else
            ExecuteSQLQuery("SELECT * FROM ApplicationUser WHERE EMPLOYEE_ID='" & num_fltr(txtEmpID.Text) & "' ")
            If sqlDT.Rows.Count > 0 Then
                txtPassword.Text = sqlDT.Rows(0)("Password")
                txtUserName.Text = sqlDT.Rows(0)("User_Name")

            End If
        End If
    End Sub
    Private Sub UploadEmployeeImages(ByVal EMPLOYEE_ID As Double)
        Dim arrImage() As Byte
        Dim mstream As New System.IO.MemoryStream()
        Dim FileSize As UInt32
        Dim DestPath As String = Application.StartupPath + "\Upload\Employee\"
        If Not Directory.Exists(DestPath) Then
            Directory.CreateDirectory(DestPath)
        End If
        System.IO.File.Delete(DestPath + "\" + EMPLOYEE_ID.ToString + fileExtension)
        Dim ImageFileName As String = DestPath + "\" + OpenFileDialog1.SafeFileName
        PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Png)
        arrImage = mstream.GetBuffer()
        FileSize = mstream.Length
        mstream.Close()
        Dim con As New MySqlConnection(CnString)
        con.Open()
        Dim sql As String = "UPDATE Employee_Information SET PHOTO_FILE_NAME=@CompanyLogo WHERE EMPLOYEE_ID=" & EMPLOYEE_ID & " "
        Dim cmd As New MySqlCommand(sql, con)
        cmd.Parameters.AddWithValue("@CompanyLogo", arrImage)
        cmd.ExecuteNonQuery()
        con.Close()
        'System.IO.File.Move(DestPath + "\" + OpenFileDialog1.SafeFileName, DestPath + "\" + EMPLOYEE_ID.ToString + fileExtension)
        ' ExecuteSQLQuery(" UPDATE Employee_Information SET PHOTO_FILE_NAME='" + (EMPLOYEE_ID.ToString + fileExtension) + "'  WHERE EMPLOYEE_ID=" & EMPLOYEE_ID & " ")
    End Sub

    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        chkActive.Checked = True
        txtEmpID.Text = ""
        txtEmployeesCode.Text = ""
        txtEmployeesName.Text = ""
        txtFathersName.Text = ""
        txtAddress.Text = ""
        txtPhoneNumber.Text = ""
        txtMobileNumber.Text = ""
        dtpDateOfBirth.Value = Now
        txtPassportNo.Text = ""
        dptPassportexpiry.Value = Now
        cmbGender.Text = ""
        cmbMaritalStatus.Text = ""
        txtJobTitle.Text = ""
        txtBasicSalary.Text = ""
        dtpDateOfCommencement.Value = Now
        ErrorProvider1.Clear()
        PictureBox1.Image = PayrollManagementSystem.My.Resources.no_image
        fileExtension = ".png"
        OpenFileDialog1.FileName = ""
        btnRefresh.PerformClick()
    End Sub

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

    Private Sub lnkBrowse_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles lnkBrowse.LinkClicked
        OpenFileDialog1.Title = "Browse image"
        OpenFileDialog1.Filter = "Image Files (JPEG,GIF,BMP,PNG)|*.jpg;*.jpeg;*.gif;*.bmp;*.png;"
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
            fileExtension = Path.GetExtension(OpenFileDialog1.FileName)
        End If
    End Sub

    Private Sub btnDepartment_Click(sender As Object, e As EventArgs) Handles btnDepartment.Click
        'frmDepartment1.MdiParent = frmMDIParent1
        frmDepartment1.Show()
    End Sub
    Private Sub btnDesignation_Click(sender As Object, e As EventArgs) Handles btnDesignation.Click
        'frmDesignation1.MdiParent = frmMDIParent1
        frmDesignation1.Show()
    End Sub

    Private Sub btnShiftName_Click(sender As Object, e As EventArgs) Handles btnShiftName.Click
        'frmWorkingHours1.MdiParent = frmMDIParent1
        frmWorkingHours1.Show()
    End Sub

    Private Sub btnAddmoredetails_Click(sender As Object, e As EventArgs) Handles btnAddmoredetails.Click
        With frmMoreUserDetails
            .MdiParent = frmMDIParent
            .Show()
            .txtEmpID.Text = txtEmpID.Text
        End With
    End Sub
End Class