Imports System.Net   'Web
Imports System.IO
Imports MySql.Data.MySqlClient

Public Class frmCompanyProfile1
    Dim fileExtension As String = ".png"
    Private Sub frmCompanyProfile1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ExecuteSQLQuery("SELECT * FROM CompanyProfile")
        Dim arrImage1() As Byte
        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
                'arrImage1 = sqlDT.Rows(0)("CompanyLogoFileName")
                'Dim mstream As New System.IO.MemoryStream(arrImage1)
                PictureBox1.Image = Image.FromFile(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 uploadtest()
        Dim wrUpload As FtpWebRequest = DirectCast(WebRequest.Create _
         ("" & My.Settings.ftpserver & "company" & fileExtension & ""), FtpWebRequest)

        'Specify Username & Password'
        wrUpload.Credentials = New NetworkCredential(My.Settings.ftpuser, My.Settings.ftppassword)

        'Start Upload Process'
        wrUpload.Method = WebRequestMethods.Ftp.UploadFile

        'Locate File And Store It In Byte Array'
        Dim btfile() As Byte = File.ReadAllBytes(System.IO.Path.GetFullPath(OpenFileDialog1.FileName))

        'Get File'
        Dim strFile As Stream = wrUpload.GetRequestStream()

        'Upload Each Byte'
        strFile.Write(btfile, 0, btfile.Length)

        'Close'
        strFile.Close()

        'Free Memory'
        strFile.Dispose()
        Dim fileName As String = System.IO.Path.GetFullPath(OpenFileDialog1.FileName)

        Dim fi As New IO.FileInfo(fileName)

        Dim exists As Boolean = fi.Exists

        ' File ReadOnly ?

        Dim updatedTime As String = fi.Length

        ExecuteSQLQuery(" UPDATE CompanyProfile SET CompanyLogoFileName='" & ("company" + fileExtension) & "',CompanyLogoFiledetail='" & updatedTime & "'")


    End Sub
    Private Sub UploadCompanyLogo()
        Dim arrImage() As Byte
        Dim mstream As New System.IO.MemoryStream()

        Dim FileSize As UInt32

        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(mstream, System.Drawing.Imaging.ImageFormat.Png)
        ' PictureBox1.Image.Save(ImageFileName, 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 CompanyProfile SET CompanyLogoFileName =@CompanyLogo"
        Dim cmd As New MySqlCommand(sql, con)
        cmd.Parameters.AddWithValue("@CompanyLogo", arrImage)
        cmd.ExecuteNonQuery()
        con.Close()
        'System.IO.File.Move(DestPath + "\" + OpenFileDialog1.SafeFileName, DestPath + "\" + "company_logo" + fileExtension)
        'ExecuteSQLQuery(" UPDATE CompanyProfile SET CompanyLogoFileName='" + arrImage + "' ")
    End Sub

    Private Sub btnSubmit_Click_1(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
                    uploadtest()
                    '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
                    uploadtest()
                    'UploadCompanyLogo()
                End If
                MsgBox("New record has been added on database successfully.", MsgBoxStyle.Information, "Information")
            End If
        End If
    End Sub

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

    Private Sub lnkBrowse_OpenLink(sender As Object, e As DevExpress.XtraEditors.Controls.OpenLinkEventArgs) Handles lnkBrowse.OpenLink
        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


End Class