Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Mail
Imports ModCommon

Partial Class Admin_product_specific_inquiry
    Inherits System.Web.UI.Page
    Shared Category(10) As String
    Shared count As Integer
    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
    Shared ReInquiry As String
    Shared ProductID As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            
            main.Visible = True
            lblerror.Visible = False
            If Session("AdminID") Is Nothing Then
                Response.Redirect("AdminLogin.aspx?sesn=expire")
                Exit Sub
            End If
            If Not IsPostBack() Then
                fillgrid()
                ReInquiry = ""
                pnlReply.Visible = False
                'strdetailid = ""
                pnlDetail.Visible = False
                'ibtnClose.Attributes.dd("onclick", "ClosePanel(this)")
                'Dim lnkdel As LinkButton = CType(gvInquiryMaster.FindControl("lnkDelete"), LinkButton)
                'lnkdel.Attributes.Add("onclick", "return ConfrmMsg();")
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub
    Protected Sub fillgrid()
        Try

            Dim strQry As String = "SELECT Product_Master.Product_Name, Product_Master.Product_Code, Product_Master.Product_ModelNo, Product_Specific_Inquiry.PSE_CompanyName, Product_Specific_Inquiry.PSE_ContactPersonName, Product_Specific_Inquiry.PSE_FlagCall, Product_Specific_Inquiry.PSE_FlagCall, Product_Specific_Inquiry.PSE_Status, Product_Specific_Inquiry.PSE_Kid,convert(nvarchar(25),Product_Specific_Inquiry.PSE_InquiryDate,103) as [Inquiry Date], Product_Specific_Inquiry.PSE_ProductID FROM         Product_Specific_Inquiry INNER JOIN Product_Master ON Product_Specific_Inquiry.PSE_ProductID = Product_Master.Product_Kid where PSE_Isdeleted = '0' and Product_Isdeleted = '0' order by Product_Specific_Inquiry.PSE_InquiryDate desc"
            Dim sda As New SqlDataAdapter(strQry, con)
            Dim ds As New DataSet()
            sda.Fill(ds)
            gvInquiryMaster.DataSource = ds
            gvInquiryMaster.DataBind()

            lblTotalRecords.Text = "<b style='color:red; size:8pt;'>Total No. Of Records - </b><b style='color:blue; size:8pt;'>" + ds.Tables(0).Rows.Count.ToString + "</b>"

            System.Threading.Thread.Sleep(500)
            'Dim i As Integer
            'For i = 0 To gvInquiryMaster.Rows.Count - 1
            '    If gvInquiryMaster.Rows(i).Cells(6).Text = "Y" Then
            '        gvInquiryMaster.Rows(i).Cells(6).Text = "Call Me"
            '    Else
            '        gvInquiryMaster.Rows(i).Cells(6).Text = "Dont Call"
            '    End If
            'Next

            '      dr = Nothing
            con.Close()
            'cmd.Dispose()
            'con.Dispose()

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub
    Protected Sub lnkDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim lnkdel As LinkButton = CType(sender, LinkButton)
            ExecuteQuery("DELETE FROM Product_Specific_Inquiry WHERE     (PSE_Kid = '" & lnkdel.CommandArgument & "')")
            fillgrid()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub lnkReply_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            pnlDetail.Visible = False
            Dim lnkRe As LinkButton = CType(sender, LinkButton)
            ReInquiry = lnkRe.CommandArgument.Trim

            Dim strstatus As String = ReturnValue("select PSE_Status from Product_Specific_Inquiry where PSE_Kid = '" & ReInquiry & "'")
            If strstatus <> "Solved" Then
                pnlReply.Visible = True
                txtReply.Text = ""
                ScriptManager1.SetFocus(txtReply)
                txtTO.Text = ReturnValue("Select PSE_EmailID from Product_Specific_Inquiry where PSE_Kid = '" & lnkRe.CommandArgument.ToString.Trim & "'")
                txtFrom.Text = "contact@atcomaart.com"

            Else
                showmessage("Inquiry is already solved", lblerror)
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub btnReply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReply.Click
        Try
            'Dim frommail As String = txtFrom.Text
            '//Dim tomail As String = txtTO.Text
            'Dim mail As New MailMessage
            'mail.To = txtTO.Text
            'mail.From = txtFrom.Text
            'mail.Body = txtReply.Text
            'mail.Subject = "Inquiry Replay"
            'SmtpMail.SmtpServer = ""
            'SmtpMail.Send(mail)

            ExecuteQuery("UPDATE    Product_Specific_Inquiry SET PSE_Status='Solved', PSE_ReplyFrom = '" & txtFrom.Text & "', PSE_Reply = '" & txtReply.Text & "' WHERE PSE_Kid = '" & ReInquiry & "'")
            System.Threading.Thread.Sleep(1000)
            pnlReply.Visible = False
            ' ''Dim mail As New System.Net.Mail.MailMessage("contact@atcomaart.com", txtTO.Text, "Product Specific Inquiry Reply.", txtReply.Text.ToString)
            ' ''mail.IsBodyHtml = True
            ' ''mail.Priority = Net.Mail.MailPriority.High
            ' ''Dim smtpclient As New System.Net.Mail.SmtpClient("smtp.rediffmailpro.com")
            ' ''smtpclient.Credentials = New System.Net.NetworkCredential("contact@atcomaart.com", "passw123")
            ' ''smtpclient.Send(mail)

            MailSender.SendEmail(txtTO.Text, "Product Specific Inquiry Reply.", txtReply.Text.ToString, MailFormat.Html, "")


            showmessage("Reply sent successfully", lblerror)
            fillgrid()
            txtTO.Text = ""
            txtReply.Text = ""
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            txtReply.Text = ""
            ScriptManager1.SetFocus(txtReply)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub gvInquiryMaster_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Protected Sub gvInquiryMaster_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvInquiryMaster.PageIndexChanging
        Try
            gvInquiryMaster.PageIndex = e.NewPageIndex
            fillgrid()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnClose.Click
        Try
            pnlReply.Visible = False
            'fillgrid()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub lnkDetail_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim lnkdet As LinkButton = CType(sender, LinkButton)
            ProductID = ReturnValue("Select PSE_ProductID from Product_Specific_Inquiry where PSE_Kid = '" & lnkdet.CommandArgument & "'")


            lblCat.Text = ""
            Dim catid As String
            catid = ""
            catid = ReturnValue("Select Product_CategoryID from Product_Master where Product_Kid= '" & RemoveLiterals(ProductID.ToString()) & "'and Product_IsDeleted='0'")
            ' get categoryid according productid
            count = 0
            FillCategoryName(catid)
            Dim proddt As DataTable
            proddt = ReturnDataTable("Select Product_Code,Product_Name,Product_ModelNo from Product_Master where Product_Kid= '" & RemoveLiterals(ProductID.ToString()) & "'and Product_IsDeleted='0'")

            lblProductCode.Text = proddt.Rows(0).Item("Product_Code").ToString.Trim
            lblProductName.Text = proddt.Rows(0).Item("Product_Name").ToString.Trim
            lblModelNo.Text = proddt.Rows(0).Item("Product_ModelNo").ToString.Trim
            proddt.Clear()

            Dim dt As DataTable
            dt = ReturnDataTable("Select * From Product_Specific_Inquiry where PSE_Kid ='" & lnkdet.CommandArgument & "'")
            System.Threading.Thread.Sleep(500)

            lblCompanyName.Text = dt.Rows(0).Item("PSE_CompanyName").ToString.Trim
            lblContactPerson.Text = dt.Rows(0).Item("PSE_ContactPersonName").ToString.Trim
            lblPhoneNo.Text = dt.Rows(0).Item("PSE_PhoneCountryCode").ToString.Trim & "-" & dt.Rows(0).Item("PSE_PhoneCityCode").ToString.Trim & "-" & dt.Rows(0).Item("PSE_PhoneNo").ToString.Trim
            lblMobileNo.Text = dt.Rows(0).Item("PSE_MobileCountryCode").ToString.Trim & "-" & dt.Rows(0).Item("PSE_MobileNo").ToString.Trim
            lblEmailID.Text = dt.Rows(0).Item("PSE_EmailID").ToString.Trim
            lblQtyRequired.Text = dt.Rows(0).Item("PSE_QtyRequired").ToString.Trim & "   " & ReturnValue("Select Unit_Name From Unit_Master where Unit_Kid ='" & dt.Rows(0).Item("PSE_QtyRequiredUnit").ToString.Trim & "'")
            lblRequiredDate.Text = dt.Rows(0).Item("PSE_RequiredByDate").ToString.Trim
            lblRequiredPlace.Text = dt.Rows(0).Item("PSE_RequiredPlace").ToString.Trim
            lblSpecificRequirement.Text = dt.Rows(0).Item("PSE_SpecificRequirement").ToString.Trim
            If dt.Rows(0).Item("PSE_FlagCall") = "N" Then
                lblCall.Text = "Dont Call"
            Else
                lblCall.Text = "Call Me"
            End If
            If IsDBNull(dt.Rows(0).Item("PSE_SendOtherSupplier")) Then
                lblSendOtherSupp.Text = "No"
            Else
                If dt.Rows(0).Item("PSE_SendOtherSupplier") = "False" Then
                    lblSendOtherSupp.Text = "No"
                ElseIf dt.Rows(0).Item("PSE_SendOtherSupplier") = "True" Then
                    lblSendOtherSupp.Text = "Yes"
                End If
            End If

            'tdDetail.InnerText = ReturnValue("select InquiryDetails_Detail from InquiryDetails where InquriyDetails_Kid='" & strdetailid & "'")
            pnlDetail.Visible = True
            pnlReply.Visible = False
            main.Visible = False
            ScriptManager1.SetFocus(ibtndetailClose)
            dt.Clear()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try

    End Sub

    Public Sub FillCategoryName(ByVal Catid As String)
        ' Fill category herarchy in label

        Dim dt As DataTable
        dt = ReturnDataTable("Select Category_Kid,Category_Code,Category_Name,Category_parentid From Category_Master Where Category_Kid ='" & Catid & "' and Category_Isdeleted = '0'")
        Dim ParentId As String
        ParentId = dt.Rows(0).Item(3).ToString()
        If ParentId = "Root Category" Then
            Dim j As Integer
            Category(count) = dt.Rows(0).Item(2).ToString() & "  >  "
            For j = 0 To count
                lblCat.Text += Category(count)
                count = count - 1
            Next
            Exit Sub
        Else
            Category(count) = dt.Rows(0).Item(2).ToString() & "  >  "
            count = count + 1
            FillCategoryName(dt.Rows(0).Item(3).ToString())
        End If
        dt.Clear()
    End Sub

    Protected Sub ibtndetailClose_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Try
            main.Visible = True
            pnlDetail.Visible = False
            System.Threading.Thread.Sleep(500)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyval")
        End Try
    End Sub

    'Protected Sub gvInquiryMaster_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvInquiryMaster.RowDataBound
    '    If e.Row.RowType = DataControlRowType.DataRow Then
    '        If e.Row.Cells(7).Text.Trim <> "" And e.Row.Cells(7).Text.Trim <> "&nbsp;" Then
    '            e.Row.Cells(7).Text = Format$(Convert.ToDateTime(e.Row.Cells(7).Text.Trim), "dd-MMM-yyyy")
    '        End If
    '    End If
    'End Sub
End Class

