Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Mail
Imports ModCommon
Partial Class Admin_product_notfound_inquiry_reply

    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
    Shared cat As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session("AdminID") Is Nothing Then
                Response.Redirect("AdminLogin.aspx?sesn=expire")
                Exit Sub
            End If
            'Button1.Attributes.Add("onclick", "return fixGridheadet('" & gvInquiryMaster.ClientID.ToString & "'); ")
            lblerror.Visible = False
            If Not IsPostBack() Then
                If Session("AdminID") = Nothing Then
                    Response.Redirect("AdminLogin.aspx")
                End If
                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_NotFound_Inquiry.*,convert(nvarchar(25),Product_NotFound_Inquiry.PNFI_InquiryDate,103) as [Inquiry Date], Brand_Master.Brand_Name FROM Product_NotFound_Inquiry LEFT OUTER JOIN Brand_Master ON Product_NotFound_Inquiry.PNFI_BrandID = Brand_Master.Brand_Kid ORDER BY Product_NotFound_Inquiry.PNFI_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>"

            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
                If Not gvInquiryMaster.Rows(i).Cells(3).Text = "&nbsp;" Then
                    count = 0
                    cat = ""
                    gvInquiryMaster.Rows(i).Cells(3).Text = FillCategoryNameGrid(RemoveLiterals(gvInquiryMaster.Rows(i).Cells(3).Text.ToString.Trim))
                End If

            Next
            gvInquiryMaster.Columns(4).Visible = False
            '      dr = Nothing
            con.Close()
            'cmd.Dispose()
            'con.Dispose()

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Public Function FillCategoryNameGrid(ByVal Catid As String) As String
        ' Fill category herarchy in label
        Dim i As Integer
        Dim dtCat As New DataTable
        Dim strValue As String = ""
        dtCat = ReturnDataTable("WITH CategoryList(Category_Kid,Category_Name,Category_ParentId) AS (SELECT Category_Master1.Category_Kid, Category_Master1.Category_Name, Category_Master1.Category_ParentId FROM Category_Master AS Category_Master1 WHERE Category_Master1.Category_Kid = '" & RemoveLiterals(Catid.ToString) & "' UNION ALL SELECT Category_Master2.Category_Kid, Category_Master2.Category_Name, Category_Master2.Category_ParentId FROM Category_Master AS Category_Master2,CategoryList AS CategoryList WHERE Category_Master2.Category_Kid = CategoryList.Category_ParentId ) Select * From CategoryList")
        ' Dim str As String
        If dtCat.Rows.Count > 0 Then
            i = dtCat.Rows.Count
            While i > 0
                strValue = strValue + dtCat.Rows(i - 1).Item("Category_Name").ToString + " > "
                i = i - 1
            End While
        End If

        'Dim dt As DataTable
        'dt = ReturnDataTable("Select * 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
        '        cat += Category(count)
        '        count = count - 1
        '    Next

        '    Return cat
        '    Exit Function
        'Else
        '    Category(count) = dt.Rows(0).Item(2).ToString() & "  >  "
        '    count = count + 1
        '    FillCategoryNameGrid(dt.Rows(0).Item(3).ToString())
        'End If
        'dt.Clear()
        'Return cat
        Return strValue
    End Function

    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_NotFound_Inquiry WHERE     (PNFI_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 PNFI_Status from Product_NotFound_Inquiry where PNFI_Kid = '" & ReInquiry & "'")
            If strstatus <> "Solved" Then
                pnlReply.Visible = True
                txtReply.Text = ""
                ScriptManager1.SetFocus(txtReply)
                txtTO.Text = ReturnValue("Select PNFI_EmailID from Product_NotFound_Inquiry where PNFI_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 = "Reply for Not Found Product Inquiry."
            'SmtpMail.SmtpServer = ""
            'SmtpMail.Send(mail)

            ExecuteQuery("UPDATE    Product_NotFound_Inquiry SET PNFI_Status='Solved', PNFI_ReplyFrom = '" & txtFrom.Text & "', PNFI_Reply = '" & txtReply.Text & "' WHERE     PNFI_Kid = '" & ReInquiry & "'")

            pnlReply.Visible = False
            'Dim mail As New System.Net.Mail.MailMessage("contact@atcomaart.com", txtTO.Text, "Reply for Not Found Product Inquiry.", 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, "Reply for Not Found Product Inquiry.", txtReply.Text.ToString, MailFormat.Html, "")


            showmessage("Reply sent successfully", lblerror)
            fillgrid()
            txtReply.Text = ""
            txtTO.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) Handles btnCancel.Click
        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 = ""
            count = 0
            Dim catid As String
            catid = ""
            catid = ReturnValue("Select PNFI_CategoryID from Product_NotFound_Inquiry where PNFI_Kid= '" & RemoveLiterals(lnkdet.CommandArgument.ToString()) & "'and PNFI_IsDeleted='0'")
            ' get categoryid according productid
            count = 0
            If catid = "" Then
                lblCat.Text = ReturnValue("Select PNFI_BrandID from Product_NotFound_Inquiry where PNFI_Kid= '" & RemoveLiterals(lnkdet.CommandArgument.ToString()) & "'and PNFI_IsDeleted='0'")
                lblCat.Text = ReturnValue("Select Brand_Name from Brand_Master where Brand_Kid= '" & RemoveLiterals(lblCat.Text.ToString()) & "'and Brand_IsDeleted='0'")
            Else
                FillCategoryName(catid)
            End If

            'lblProductCode.Text = ReturnValue("Select Product_Code from Product_Master where Product_Kid= '" & RemoveLiterals(ProductID.ToString()) & "'and Product_IsDeleted='0'")
            lblProductName.Text = ReturnValue("Select PNFI_ProductName from Product_NotFound_Inquiry where PNFI_Kid= '" & RemoveLiterals(lnkdet.CommandArgument.ToString()) & "'and PNFI_IsDeleted='0'")
            'lblModelNo.Text = ReturnValue("Select Product_ModelNo from Product_Master where Product_Kid= '" & RemoveLiterals(ProductID.ToString()) & "'and Product_IsDeleted='0'")

            Dim dt As DataTable
            dt = ReturnDataTable("Select * From Product_NotFound_Inquiry where PNFI_Kid ='" & lnkdet.CommandArgument & "'")

            lblCompanyName.Text = dt.Rows(0).Item("PNFI_CompanyName").ToString.Trim
            lblContactPerson.Text = dt.Rows(0).Item("PNFI_ContactPersonName").ToString.Trim
            lblPhoneNo.Text = dt.Rows(0).Item("PNFI_PhoneCountryCode").ToString.Trim & "-" & dt.Rows(0).Item("PNFI_PhoneCityCode").ToString.Trim & "-" & dt.Rows(0).Item("PNFI_PhoneNo").ToString.Trim
            lblMobileNo.Text = dt.Rows(0).Item("PNFI_MobileCountryCode").ToString.Trim & "-" & dt.Rows(0).Item("PNFI_MobileNo").ToString.Trim
            lblEmailID.Text = dt.Rows(0).Item("PNFI_EmailID").ToString.Trim
            lblQtyRequired.Text = dt.Rows(0).Item("PNFI_QtyRequired").ToString.Trim & "   " & ReturnValue("Select Unit_Name From Unit_Master where Unit_Kid ='" & dt.Rows(0).Item("PNFI_QtyRequiredUnit").ToString.Trim & "'")
            lblRequiredDate.Text = dt.Rows(0).Item("PNFI_RequiredByDate").ToString.Trim
            lblRequiredPlace.Text = dt.Rows(0).Item("PNFI_RequiredPlace").ToString.Trim
            lblSpecificRequirement.Text = dt.Rows(0).Item("PNFI_SpecificRequirement").ToString.Trim
            If dt.Rows(0).Item("PNFI_FlagCall") = "N" Then
                lblCall.Text = "Dont Call"
            Else
                lblCall.Text = "Call Me"
            End If
            If IsDBNull(dt.Rows(0).Item("PNFI_SendOtherSupplier")) Then
                lblSendOtherSupp.Text = "No"
            Else
                If dt.Rows(0).Item("PNFI_SendOtherSupplier") = "False" Then
                    lblSendOtherSupp.Text = "No"
                ElseIf dt.Rows(0).Item("PNFI_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
            tblmain.Visible = False
        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 * 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) Handles ibtndetailClose.Click
        Try
            pnlDetail.Visible = False
            tblmain.Visible = True
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyval")
        End Try
    End Sub



End Class
