Imports System.IO
Public Class frmCompanyProfile
    Dim fileExtension As String = ".png"
    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmCompanyProfile_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ExecuteSQLQuery("SELECT * FROM CompanyProfile")
        If sqlDT.Rows.Count > 0 Then
            txtCompanyName.Text = sqlDT.Rows(0)("CompanyName")
            txtAddress.Text = sqlDT.Rows(0)("Address")
            txtPhone.Text = sqlDT.Rows(0)("Phone")
            txtReceiptHeader.Text = sqlDT.Rows(0)("ReceiptHeader")
            txtReceiptFooter.Text = sqlDT.Rows(0)("ReceiptFooter")
            Try
                PictureBox1.ImageLocation = Application.StartupPath + "\Upload\Company\" + sqlDT.Rows(0)("CompanyLogoFileName")
            Catch ex As Exception
                PictureBox1.Image = PayrollManagementSystem.My.Resources.no_image
            End Try
        End If
    End Sub

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        If txtCompanyName.Text = "" Or txtAddress.Text = "" Or txtPhone.Text = "" Then
            ErrorProvider1.SetError(txtCompanyName, "Mandatory field.")
            ErrorProvider1.SetError(txtAddress, "Mandatory field.")
            ErrorProvider1.SetError(txtPhone, "Mandatory field.")
        Else
            ErrorProvider1.Clear()
            ExecuteSQLQuery("SELECT * FROM CompanyProfile")
            If sqlDT.Rows.Count > 0 Then
                ExecuteSQLQuery("UPDATE   CompanyProfile  SET CompanyName='" + str_repl(txtCompanyName.Text) + "', Address='" + str_repl(txtAddress.Text) + "', Phone='" + str_repl(txtPhone.Text) + "', ReceiptHeader='" + str_repl(txtReceiptHeader.Text) + "', ReceiptFooter='" + str_repl(txtReceiptFooter.Text) + "' ")
                If Not OpenFileDialog1.FileName = "" Then
                    UploadCompanyLogo()
                End If
                MsgBox("Record updated successful.", MsgBoxStyle.Information, "Information")
            Else
                ExecuteSQLQuery(" INSERT INTO CompanyProfile (CompanyName, Address, Phone, ReceiptHeader, ReceiptFooter) VALUES ('" + str_repl(txtCompanyName.Text) + "', '" + str_repl(txtAddress.Text) + "', '" + str_repl(txtPhone.Text) + "', '" + str_repl(txtReceiptHeader.Text) + "', '" + str_repl(txtReceiptFooter.Text) + "')  ")
                If Not OpenFileDialog1.FileName = "" Then
                    UploadCompanyLogo()
                End If
                MsgBox("New record has been added on database successfully.", MsgBoxStyle.Information, "Information")
            End If
        End If
    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.ImageLocation = OpenFileDialog1.FileName
            fileExtension = Path.GetExtension(OpenFileDialog1.FileName)
        End If
    End Sub
    Private Sub UploadCompanyLogo()
        Dim DestPath As String = Application.StartupPath + "\Upload\Company\"
        If Not Directory.Exists(DestPath) Then
            Directory.CreateDirectory(DestPath)
        End If
        System.IO.File.Delete(DestPath + "\" + "company_logo" + fileExtension)
        Dim ImageFileName As String = DestPath + "\" + OpenFileDialog1.SafeFileName
        PictureBox1.Image.Save(ImageFileName, System.Drawing.Imaging.ImageFormat.Png)
        System.IO.File.Move(DestPath + "\" + OpenFileDialog1.SafeFileName, DestPath + "\" + "company_logo" + fileExtension)
        ExecuteSQLQuery(" UPDATE CompanyProfile SET CompanyLogoFileName='" + ("company_logo" + fileExtension) + "' ")
    End Sub
End Class