Imports EDSL.Entities
Imports EDSL.DAL
Imports System.Net.Mail
Imports System.IO

Partial Public Class ProductEnquiry1
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'pageInfo.Visible = False
        Dim breadcrumblabel As String
        Dim breadcrumbNavigate As String
        Dim breadTitle As String
        Dim func As New miscFunctions

        
        If Request.QueryString("pid") <> "" Then
            divJobEnquiry.Visible = True
            divCVEnquiry.Visible = False
            Dim objMainProduct As New entMainProduct
            objMainProduct = GetProduct(Request.QueryString("pid"))


            With objMainProduct
                breadcrumblabel = "Job Detail"
                breadcrumbNavigate = "~/Job-" + func.CleanUrlString(.ProductName) + "-" + .ProductID.ToString()
                breadTitle = "&nbsp;&nbsp;&raquo;&nbsp;&nbsp; <a>Submit your CV for " & .ProductName & "</a> "
                lblJob.Text = .ProductName
                hidConsultantEmail.Value = .ConsultantEmail
                hidConsultantID.Value = .ConsultantID
                hidJobID.Value = .ProductID
                hidJobRef.Value = .ProductDesc3
                CType(Me.Parent.FindControl("breadcrumb"), Breadcrumb).SetHyperLinkField(breadcrumbNavigate, breadcrumblabel, breadTitle)
            End With
        Else
            divJobEnquiry.Visible = False
            divCVEnquiry.Visible = True
        End If
  
        If Request.QueryString("t") = "Thankyou" Then
            Dim dbMessage As New dbMainMessage
            divThankyou.Visible = True
            Dim objMainProduct As New entMainProduct
            objMainProduct = GetProduct(Request.QueryString("cid"))

            If Request.QueryString("cid") <> "" And Request.QueryString("cid") <> "0" Then
                divThankyou.InnerHtml = dbMessage.GetMessage(14).ToString()
                breadcrumblabel = "Job Detail"
                breadcrumbNavigate = "~/Job-" + func.CleanUrlString(objMainProduct.ProductName) + "-" + Request.QueryString("cid") + ".html"
                breadTitle = ""
                CType(Me.Parent.FindControl("breadcrumb"), Breadcrumb).SetHyperLinkField(breadcrumbNavigate, breadcrumblabel, breadTitle)
                breadcrumblabel = "Sumbit Your CV"
                breadcrumbNavigate = "~/ProductEnquiry.aspx?pid=" & Request.QueryString("cid")
                breadTitle = "&nbsp;&nbsp;&raquo;&nbsp;&nbsp; <a>Thank you</a> "

            Else
                divThankyou.InnerHtml = dbMessage.GetMessage(2).ToString()
                breadcrumblabel = "Register your details"
                breadcrumbNavigate = "~/ProductEnquiry.aspx"
                breadTitle = "&nbsp;&nbsp;&raquo;&nbsp;&nbsp; <a>Thank you</a> "
            End If
            CType(Me.Parent.FindControl("breadcrumb"), Breadcrumb).SetHyperLinkField(breadcrumbNavigate, breadcrumblabel, breadTitle)

            divAppForm.Visible = False
            divJobEnquiry.Visible = False
            divCVEnquiry.Visible = False
            Dim pageInfo As Label = Me.Parent.FindControl("divContent")
            Dim pageHeader As HtmlControl = Me.Parent.FindControl("divPageHead")
            pageHeader.Visible = False
            pageInfo.Visible = False
        End If

    End Sub
    Private Function GetProduct(ByVal intProductID As Integer) As entMainProduct
        Dim objMain As New dbMainProduct
        Dim objMainProduct As entMainProduct = objMain.GetItemShowOnWebsite(intProductID)
        Return objMainProduct
    End Function

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        If (Page.IsValid) Then

            'Insert enquiry into database
            Dim enquiry As New entMainEnquiry
            Dim dbEnquiry As New dbMainEnquiry
            Dim enquiryID As New Integer

            With enquiry
                .EnquiryName = txtName.Text.Trim
                .EnquiryTelephone = txtPhone.Text.Trim
                .EnquiryEmail = txtEmail.Text.Trim
                .Ad4 = txtTown.Text.Trim
                .EnquiryMessage = txtCoverLetter.Text.Trim
                .EligibleUk = IIf(chkEligible.Checked, "1", "")
                .ConsultantID = hidConsultantID.Value.ToString()
                .ConsultantEmail = hidConsultantEmail.Value.ToString()
                .JobID = hidJobID.Value.ToString()

                If .JobID <> 0 Then
                    enquiryID = dbEnquiry.SaveItemApplyOnline(0, .EnquiryName, "", "", "", .Ad4, "", "", .EnquiryEmail, .EnquiryTelephone, "", .EnquiryMessage, "", .EligibleUk, .JobID, "", "", 0)
                Else
                    enquiryID = dbEnquiry.SaveItemEnquiryRegistration(0, .EnquiryName, "", "", "", .Ad4, "", "", .EnquiryEmail, .EnquiryTelephone, "", .EnquiryMessage, "", 0, 0, "", "", 0)
                End If



                If enquiryID = 0 Then
                    'Insert has failed
                    divThankyou.Visible = True
                    divAppForm.Visible = True
                    Dim dbMessage As New dbMainMessage
                    divThankyou.InnerHtml = dbMessage.GetMessage(10).ToString()
                Else
                    'Upload CV
                    Dim uploadFacility As New miscFunctions
                    .CVDocName = uploadFacility.UploadFile(fuldUpload, TheGlobals.UploadFileTypes.PDF, enquiryID.ToString())

                    'Send email 
                    Dim CVDocAttached As String
                    If .CVDocName <> "" Then
                        CVDocAttached = "Yes"
                    Else
                        CVDocAttached = "No"
                    End If
                    Dim emailTemplate As String = ""
                    If .JobID <> 0 Then
                        emailTemplate = "JobRequest.htm"
                    Else
                        emailTemplate = "CVsubmit.htm"
                    End If

                    Dim strFilePath As String = Server.MapPath("")
                    'strFilePath = Replace(strFilePath, "controlPayment", "")
                    Dim strVar As String = strFilePath & "\" & ConfigurationManager.AppSettings("EmailTemplate")
                    Dim emailDirectory As String = ConfigurationManager.AppSettings("EmailImageDirectory")
                    Dim mainEmail As String = ConfigurationManager.AppSettings("emailAddress")
                    Dim messageText As String
                    Dim messageSubject As String
                    Dim consultantEmail As String


                    Dim objReader As New System.IO.StreamReader(strVar + emailTemplate)
                    messageText = objReader.ReadToEnd()

                    If .JobID <> 0 Then
                        messageText = messageText.Replace("[#JobTitle]", lblJob.Text.ToString())
                        messageText = messageText.Replace("[#JobReference]", hidJobRef.Value.ToString())
                        messageSubject = "Job Application sent for: " & hidJobRef.Value.ToString()
                        consultantEmail = hidConsultantEmail.Value.ToString()
                    Else
                        messageSubject = "Application to register a CV"
                        consultantEmail = mainEmail
                    End If


                    If .EligibleUk = "1" Then
                        .EligibleUk = "Yes"
                    Else
                        .EligibleUk = "No"
                    End If
                    messageText = messageText.Replace("[#DateReceived]", FormatDateTime(Now(), DateFormat.LongDate))
                    messageText = messageText.Replace("[#ContactName]", .EnquiryName)
                    messageText = messageText.Replace("[#Telephone]", .EnquiryTelephone)
                    'messageText = messageText.Replace("[#Mobile]", .EnquiryMobile)
                    messageText = messageText.Replace("[#Email]", .EnquiryEmail)
                    messageText = messageText.Replace("[#Town]", .Ad4)
                    messageText = messageText.Replace("[#EligibleUK]", .EligibleUk)
                    messageText = messageText.Replace("[#Message]", .EnquiryMessage)
                    messageText = messageText.Replace("[#CVAttached]", CVDocAttached)
                    '** send the email **
                    If .EnquiryEmail <> "" Then
                        mainEmail = .EnquiryEmail
                    End If

                    Dim filePath As String = strFilePath & "\Uploads\cvsubmitted\" & .CVDocName
                    If File.Exists(filePath) Then
                        Dim oAttachFile As New Attachment(File.OpenRead(HttpContext.Current.Server.MapPath("~/Uploads/cvsubmitted/") + .CVDocName), .CVDocName)

                        dbMailHelper.SendMailMessage(mainEmail, consultantEmail, "", "", messageSubject, messageText, oAttachFile)
                    Else
                        Dim newEmail As New dbMailHelper
                        newEmail.SendMailMessage(mainEmail, consultantEmail, "", "", messageSubject, messageText)
                    End If



                    Response.Redirect("~/CandidateEnquiry-Thankyou-" & .JobID)
                End If

            End With


        End If







    End Sub

    Protected Sub customvalidator1_servervalidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        args.isvalid = True
        If Not chkeligible.checked Then
            args.isvalid = False
        End If
    End Sub

    Protected Sub CustomValidator2_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator2.ServerValidate
        args.IsValid = True
        If (txtEmail.Text.Trim().ToString() = "") And (txtPhone.Text.Trim().ToString() = "") Then
            args.IsValid = False
        End If



    End Sub

    Protected Sub CustomValidator3_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator3.ServerValidate
        args.IsValid = True
        Dim strFileName As String = fuldUpload.PostedFile.FileName
        Dim strExtensionName As String = System.IO.Path.GetExtension(strFileName).ToLower()

        Select Case strExtensionName
            Case ".pdf", ".doc", ".docx", ""
            Case Else
                args.IsValid = False
        End Select

    End Sub
End Class