Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Net
Imports System.Net.Mail

Partial Class Admin_view_supplier_products
    Inherits System.Web.UI.Page
    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connectionstring"))
    Dim da As New SqlDataAdapter
    Dim ds As New DataSet
    Dim detailviewDs As DataSet
    Dim Category(10) As String
    Dim i As Integer = 0
    Dim supplierid As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ''DetailsView1.Rows.Item(0).Width = 150
        If Session("AdminID") Is Nothing Then
            Response.Redirect("AdminLogin.aspx?sesn=expire")
            Exit Sub
        End If

        If Not IsPostBack Then
            'Call Fill_Combo("SELECT DISTINCT Supplier_Master.Supplier_Kid, Supplier_Master.Supplier_Name FROM  SupplierLogin RIGHT OUTER JOIN  Supplier_Master ON SupplierLogin.AdminUser_Kid = Supplier_Master.Supplier_AdminUserId WHERE SupplierLogin.AdminUser_IsDeleted = '0' and SupplierLogin.UserType_Kid='00000E6' and Supplier_Master.Supplier_Isdeleted='0' and Supplier_Master.Supplier_Kid!='' order by Supplier_Name", ddlSupplierName)

            'fill the combo box of supplier
            ''Call Fill_Combo("SELECT DISTINCT  dbo.Supplier_Master.Supplier_Kid, dbo.Supplier_Master.Supplier_Name FROM dbo.Supplier_Master INNER JOIN dbo.Product_Master ON dbo.Supplier_Master.Supplier_Kid = dbo.Product_Master.Product_SupplierId INNER JOIN dbo.AdminUser ON dbo.Supplier_Master.Supplier_AdminUserId = dbo.AdminUser.AdminUser_Kid WHERE (dbo.Product_Master.Product_Approved = 'N') AND (dbo.Supplier_Master.Supplier_Kid <> '') AND (dbo.Supplier_Master.Supplier_IsDeleted = '0') AND (dbo.Supplier_Master.Supplier_Approved = 'Y') AND(dbo.Product_Master.Product_IsDeleted = '0') ORDER BY dbo.Supplier_Master.Supplier_Name", ddlSupplierName)

            'fill the combo box of supplier
            filldropdown()

            Image1.Visible = False
            gridheader.Visible = False
            detailsheader.Visible = False
            detailshow.Visible = False
            lblerror.Visible = False
            btndelete.Attributes.Add("onclick", "return confirm('Are You Sure Want to Delete?');")
            btnMultiApprove.Attributes.Add("onclick", "return confirm('All Selected Products will be approved anyways, Are you sure?');")
            txtFlag.Text = "True"
            txtOther.Text = "True"
            tdBasic.Visible = False
        End If
        lblInfo.Visible = False
        lblOtherError.Visible = False

    End Sub

    Protected Sub filldropdown()
        Dim dt As New DataTable
        'Dim i As Integer

        Dim temp_str As String
        Dim dt_temp As New DataTable
        Dim j As Integer = 0
        Dim dt1 As New DataTable
        Dim mycat As String = ""
        Dim admincat As String = ""
        ' Dim finalstr As String
        Dim arr

        Try

            'admincat = ReturnValue("select Replace(AdminUser_CategoryId,' - ',',') from AdminUser where AdminUser_Kid='" & Session("AdminID") & "'")

            'finalstr = admincat.Substring(0, admincat.Length - 1)

            'arr = Split(finalstr, ",")

            'For i = 0 To arr.Length - 1
            '    temp_str = "select distinct Supplier_Kid, Supplier_Code, Supplier_Name, Supplier_Address,Supplier_CategoryId FROM Supplier_Master WHERE Supplier_FinancialYearId = '" & Session("G_FinYear") & "'AND Supplier_CompanyId = '" & Session("G_Company") & "'and supplier_Approved = 'Y' AND Supplier_CategoryId <> '' and Supplier_CategoryId like '%" & arr(i).ToString & "%' AND Supplier_IsDeleted = '0' order by Supplier_Name asc"
            '    dt_temp = ReturnDataTable(temp_str)
            '    dt1.Merge(dt_temp, True)
            'Next
            temp_str = "select distinct Supplier_Kid, Supplier_Code, Supplier_Name, Supplier_Address,Supplier_CategoryId FROM Supplier_Master WHERE Supplier_CompanyId = '" & Session("G_Company") & "'and supplier_Approved = 'Y' AND Supplier_IsDeleted = '0' order by Supplier_Name asc"
            dt1 = ReturnDataTable(temp_str)
        Catch ex As Exception

        End Try

        'for finding unique record from the datatable
        ddlSupplierName.DataSource = FindUniqueRecord(dt1, "Supplier_Kid")
        ddlSupplierName.DataTextField = "Supplier_Name"
        ddlSupplierName.DataValueField = "Supplier_Kid"
        ddlSupplierName.DataBind()

        ddlSupplierName.Items.Insert(0, "Select Supplier")
    End Sub
  
    Protected Sub ddlSupplierName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSupplierName.SelectedIndexChanged
        supplierid = RemoveLiterals(ddlSupplierName.SelectedItem.Value.ToString.Trim)
        tdBasic.Visible = True
        tddetail.InnerHtml = ""
        'Dim str As String = "SELECT   Product_Kid, Product_Name + ' - ' + isnull(Product_ModelNo,'') as Product_Name FROM Product_Master  WHERE   Product_SupplierId='" & supplierid & "' and   Product_Approved = 'Y' and Product_IsDeleted='0' order by Product_ApprovedDate DESC"
        'con.Open()
        'da = New SqlDataAdapter(str, con)
        'da.Fill(ds)
        'GvApproved.DataSource = ds.Tables(0)
        'GvApproved.DataBind()
        'Dim str1 As String
        'Dim ds1 As New DataSet
        'str1 = "SELECT   Product_Kid, Product_Name + ' - ' + isnull(Product_ModelNo,'') as Product_Name FROM Product_Master  WHERE   Product_SupplierId='" & supplierid & "' and   Product_Approved = 'N' and Product_IsDeleted='0' order by product_RegDate DESC"
        'da = New SqlDataAdapter(str1, con)
        'da.Fill(ds1)
        'GvNonApproved.DataSource = ds1.Tables(0)
        'GvNonApproved.DataBind()
        bindNonapprovedgrid()
        bindapprovedgrid()
        fillSupplierInfo()
        con.Close()

        gridheader.Visible = True
        detailsheader.Visible = False
        Image1.Visible = False
        detailshow.Visible = False
        lblerror.Visible = False
        btnapproved.Visible = False
        btndelete.Visible = False
        chkAll.Checked = False
        chkAll.Text = "Select All"
    End Sub

    Private Sub fillSupplierInfo()
        Try
            Dim dt As New DataTable
            dt = ReturnDataTable("SELECT dbo.Supplier_Master.Supplier_Kid, dbo.Supplier_Master.Supplier_Name, dbo.Supplier_Master.Supplier_Address,dbo.Supplier_Master.Supplier_TelCountryCode, dbo.Supplier_Master.Supplier_TelCityCode, dbo.Supplier_Master.Supplier_TelNumber,dbo.SupplierContactPerson_Master.SupplierContactPerson_Name, dbo.SupplierContactPerson_Master.SupplierContactPerson_Designation,dbo.SupplierContactPerson_Master.SupplierContactPerson_Title FROM dbo.Supplier_Master Left Outer JOIN dbo.SupplierContactPerson_Master ON dbo.Supplier_Master.Supplier_Kid = dbo.SupplierContactPerson_Master.SupplierContactPerson_SupplierId WHERE (dbo.Supplier_Master.Supplier_IsDeleted = '0') AND (dbo.Supplier_Master.Supplier_Kid = '" & RemoveLiterals(ddlSupplierName.SelectedItem.Value.ToString.Trim) & "')")
            If dt.Rows.Count > 0 Then
                tdBasic.Visible = True
                HiddenField1.Value = dt.Rows(0).Item("Supplier_Kid")
                lnkSName.Text = IIf(dt.Rows(0).Item("Supplier_Name") <> "", dt.Rows(0).Item("Supplier_Name"), "N.A")
                lblAddress.Text = IIf(dt.Rows(0).Item("Supplier_Address") <> "", dt.Rows(0).Item("Supplier_Address"), "N.A.")
                lblContactPerson.Text = dt.Rows(0).Item("SupplierContactPerson_Title").ToString + dt.Rows(0).Item("SupplierContactPerson_Name").ToString + "," + dt.Rows(0).Item("SupplierContactPerson_Designation").ToString
                If lblContactPerson.Text = "," Then
                    lblContactPerson.Text = ""
                End If
                If dt.Rows(0).Item("Supplier_TelNumber").ToString = Nothing Then
                    lblPhoneNo.Text = ""
                Else
                    If dt.Rows(0).Item("Supplier_TelCountryCode").ToString = Nothing AndAlso dt.Rows(0).Item("Supplier_TelCityCode") = Nothing Then
                        lblPhoneNo.Text = dt.Rows(0).Item("Supplier_TelNumber")
                    ElseIf dt.Rows(0).Item("Supplier_TelCountryCode").ToString = Nothing Then
                        lblPhoneNo.Text = dt.Rows(0).Item("Supplier_TelCityCode") + "-" + dt.Rows(0).Item("Supplier_TelNumber")

                    Else
                        lblPhoneNo.Text = dt.Rows(0).Item("Supplier_TelCountryCode") + "-" + dt.Rows(0).Item("Supplier_TelCityCode") + "-" + dt.Rows(0).Item("Supplier_TelNumber")
                    End If
                End If
            Else
                tdBasic.Visible = False
            End If
            
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub GvApproved_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GvApproved.PageIndexChanging
        GvApproved.PageIndex = e.NewPageIndex
        bindapprovedgrid()
        System.Threading.Thread.Sleep(2000)
        detailshow.Visible = False
        detailsheader.Visible = False
        lblerror.Visible = False
    End Sub

    'Protected Sub GvNonApproved_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GvNonApproved.PageIndexChanging
    '    GvNonApproved.PageIndex = e.NewPageIndex
    '    bindNonapprovedgrid()
    '    System.Threading.Thread.Sleep(2000)
    '    detailshow.Visible = False
    '    detailsheader.Visible = False
    '    btndelete.Visible = False
    '    btnapproved.Visible = False
    '    lblerror.Visible = False
    'End Sub

    Protected Sub lblApname_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnk As LinkButton = CType(sender, LinkButton)
        System.Threading.Thread.Sleep(500)
        binddetailsview(lnk.CommandArgument.ToString)
        System.Threading.Thread.Sleep(2000)
        detailsheader.Visible = True
        dtitle.InnerText = "Detail Of Approved Product"
        detailshow.Visible = True
        btnapproved.Visible = False
        btndelete.Visible = False
        lblerror.Visible = False
        lblOtherError.Visible = True
        DetailsView1.Rows(11).Visible = False
        DetailsView1.Rows(14).Visible = True
    End Sub

    Protected Sub lblNApname_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnk As LinkButton = CType(sender, LinkButton)
        'System.Threading.Thread.Sleep(500)
        binddetailsview(lnk.CommandArgument.ToString)
        'System.Threading.Thread.Sleep(2000)
        detailsheader.Visible = True
        detailshow.Visible = True
        dtitle.InnerText = "Detail Of Non Approved Product"
        btnapproved.Visible = True
        btndelete.Visible = True
        DetailsView1.Rows(11).Visible = True
        DetailsView1.Rows(14).Visible = False
        lblerror.Visible = False
        lblOtherError.Visible = True
        If txtFlag.Text = "False" Then
            btnapproved.Attributes.Add("onclick", "alert('Please Fill All The Product Details to Approve the Product'); return false; ")
        Else
            If txtOther.Text = "False" Then
                lblOtherError.Visible = True
                btnapproved.Attributes.Add("onclick", "return ConfrmMessage();")
            Else
            End If
        End If
    End Sub

    Private Sub colorgrid(ByVal detailviewDs As DataSet)
        
        If detailviewDs.Tables(0).Rows(0).Item("Product Code").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Product Code").ToString = "" Then
            DetailsView1.Rows(2).Cells(1).ForeColor = Drawing.Color.Red
            DetailsView1.Rows(2).Cells(1).Text = """Will Generate After Approval."""
            DetailsView1.Rows(2).Cells(1).Style.Add("font-size", "8pt")
        End If
        If detailviewDs.Tables(0).Rows(0).Item("Product_Image").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Product_Image").ToString = "" Then
            DetailsView1.Rows(1).Cells(1).Text = "Required"
            DetailsView1.Rows(1).Cells(1).ForeColor = Drawing.Color.Red
        End If
        If detailviewDs.Tables(0).Rows(0).Item("Model No").ToString = Nothing Then
            DetailsView1.Rows(4).Cells(1).Text = "Required"
            DetailsView1.Rows(4).Cells(1).ForeColor = Drawing.Color.Red
        End If
        If detailviewDs.Tables(0).Rows(0).Item("Price").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Price").ToString.Contains(" 0.00") Then
            DetailsView1.Rows(5).Cells(1).Text = "Required"
            DetailsView1.Rows(5).Cells(1).ForeColor = Drawing.Color.Red
        End If

        ''If detailviewDs.Tables(0).Rows(0).Item("Atco Price").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Atco Price").ToString <= "0.00" Then
        ''    DetailsView1.Rows(5).Cells(1).Text = "Required"
        ''    DetailsView1.Rows(5).Cells(0).ForeColor = Drawing.Color.Red
        ''End If

        If detailviewDs.Tables(0).Rows(0).Item("Min Order Qty").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Min Order Qty").ToString.StartsWith("0.00") Then
            DetailsView1.Rows(6).Cells(1).Text = "Required"
            DetailsView1.Rows(6).Cells(1).ForeColor = Drawing.Color.Red
        End If
        If detailviewDs.Tables(0).Rows(0).Item("Shipping Qty").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Shipping Qty").ToString.StartsWith("0.00") Then
            DetailsView1.Rows(7).Cells(1).Text = "Required"
            DetailsView1.Rows(7).Cells(1).ForeColor = Drawing.Color.Red
        End If
        ' ''If detailviewDs.Tables(0).Rows(0).Item("Pcs Per Carton/Pckg").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Pcs Per Carton/Pckg").ToString <= "0.00" Then
        ' ''    DetailsView1.Rows(9).Cells(1).Text = "Required"
        ' ''    DetailsView1.Rows(9).Cells(1).ForeColor = Drawing.Color.Red
        ' ''End If
        If detailviewDs.Tables(0).Rows(0).Item("Shipping Weight").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Shipping Weight").ToString.StartsWith("0.00") Then
            DetailsView1.Rows(8).Cells(1).Text = "Required"
            DetailsView1.Rows(8).Cells(1).ForeColor = Drawing.Color.Red
        End If
        If detailviewDs.Tables(0).Rows(0).Item("Shipping Dimension").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Shipping Dimension").ToString.Contains("0X0X0") OrElse detailviewDs.Tables(0).Rows(0).Item("Shipping Dimension").ToString.Contains("0 X 0 X 0") Then
            DetailsView1.Rows(9).Cells(1).Text = "Required"
            DetailsView1.Rows(9).Cells(1).ForeColor = Drawing.Color.Red
        End If
        If detailviewDs.Tables(0).Rows(0).Item("Dispatch In").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Dispatch In").ToString.StartsWith("0") Then
            DetailsView1.Rows(10).Cells(1).Text = "Required"
            DetailsView1.Rows(10).Cells(1).ForeColor = Drawing.Color.Red
        End If

    End Sub

    Private Sub binddetailsview(ByVal kid As String)
        Dim detailviewDs As New DataSet
        con.Open()
        'detailviewDs.Clear()
        da = New SqlDataAdapter("ProductDetails_Proc", con)
        da.SelectCommand.CommandType = CommandType.StoredProcedure
        da.SelectCommand.Parameters.Add(New SqlParameter("@Product_Kid", SqlDbType.NVarChar))
        da.SelectCommand.Parameters("@Product_Kid").Value = RemoveLiterals(kid)
        'da = New SqlDataAdapter(Str, con)
        da.Fill(detailviewDs)
        txtProdKid.Text = detailviewDs.Tables(0).Rows(0).Item(0).ToString
        Image1.ImageUrl = "~/Upload Image/Product Image/UploadImages/" & detailviewDs.Tables(0).Rows(0).Item(1).ToString
        Image1.Attributes.Add("alt", detailviewDs.Tables(0).Rows(0).Item(1).ToString)
        Dim imgpath As String
        imgpath = Server.MapPath(Image1.ImageUrl)
        KeepAspectRatioNew(Image1, imgpath, 200, 200)
        DetailsView1.DataSource = detailviewDs.Tables(0)
        DetailsView1.DataBind()
        'DetailsView1.Rows(3).Cells(1).Text = DetailsView1.Rows(3).Cells(1).Text   ''  "<a href='../suppliers/product-information.aspx?ID=" & RemoveLiterals(kid) & "' >" &  & "</a>"
        DetailsView1.Rows(3).Cells(1).Style.Add("font-weight", "bold")
        Session("productid") = RemoveLiterals(kid).ToString
        Session("SupplierId") = ddlSupplierName.SelectedValue
        colorgrid(detailviewDs)
        DetailsView1.Rows(0).Visible = False
        DetailsView1.Rows(1).Visible = False
        'DetailsView1.Rows(3).Style.Add("visibility", "hidden")
        Image1.Visible = True
        FillCategoryNameProduct(RemoveLiterals(DetailsView1.Rows(12).Cells(1).Text))
        DetailsView1.Rows(12).Visible = False
        If CheckOthers() = True Then
            txtOther.Text = "True"
        Else
            txtOther.Text = "False"
        End If
        If CheckValidation(detailviewDs) = True Then
            txtFlag.Text = "True"
        Else
            txtFlag.Text = "False"
            Exit Sub
        End If
      
        con.Close()

    End Sub

    Public Sub FillCategoryNameProduct(ByVal Catid As String)
        Try
            tddetail.InnerHtml = ""
            Dim dt As DataTable
            dt = ReturnDataTable("Select * From Category_Master Where Category_Kid ='" & RemoveLiterals(Catid) & "' and Category_Isdeleted = '0' and Category_CompanyId ='" & Session("g_Company").ToString() & "'")
            If dt.Rows.Count > 0 Then
                Dim ParentId As String
                ParentId = dt.Rows(0).Item("Category_ParentId").ToString()
                If ParentId = "Root Category" Then
                    Dim j As Integer
                    Category(i) += dt.Rows(0).Item("Category_Name").ToString() & "  >  "
                    For j = 0 To i
                        tddetail.InnerHtml += Category(i)
                        i = i - 1
                    Next
                    Exit Sub
                Else
                    Category(i) = dt.Rows(0).Item("Category_Name").ToString() & "  >  "
                    i = i + 1
                    FillCategoryNameProduct(dt.Rows(0).Item("Category_ParentId").ToString())
                End If
            Else
                tddetail.InnerHtml = ""
            End If
            dt.Clear()
        Catch ex As Exception
        End Try
    End Sub

    Protected Sub btnapproved_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnapproved.Click
        lblOtherError.Visible = True
        CheckOthers()
        'System.Threading.Thread.Sleep(1000)
        Try
            If checkgrid() = True Then
                Dim ProductCode As String = GenerateProductCode(RemoveLiterals(ddlSupplierName.SelectedItem.Value))
                If ProductCode.Length < 6 Then
                    lblerror.Visible = True
                    lblerror.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;There is some technical problem, Please try later."
                    Exit Sub
                Else
                    Dim pcode As String = ReturnValue("select ProductCode from product_Master where Product_Code='" & ProductCode & "'")
                    If pcode = "" Then
                        Dim str As String = "update product_Master set Product_Approved='Y' , Product_ApprovedDate='" & Date.Now() & "',Product_ClientIP='" & Request.UserHostAddress & "',Product_Code='" & ProductCode & "' where product_Kid='" & RemoveLiterals(DetailsView1.Rows(0).Cells(1).Text) & "'"
                        con.Open()
                        da = New SqlDataAdapter(str, con)
                        da.SelectCommand.ExecuteNonQuery()
                        System.Threading.Thread.Sleep(1000)
                        con.Close()
                        detailsheader.Visible = False
                        detailshow.Visible = False
                        lblerror.Visible = True
                        lblerror.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;Product Approved Successfully."
                        lblInfo.Visible = False
                        lblOtherError.Visible = False
                        bindapprovedgrid()
                        System.Threading.Thread.Sleep(1000)
                        bindNonapprovedgrid()
                        btnapproved.Visible = False
                        btndelete.Visible = False
                        Dim dt As DataTable
                        dt = ReturnDataTable("Select Receive_Email from Receive_Alert where Receive_Sub2CatCode=(Select Product_CategoryId from Product_Master where product_Kid='" & RemoveLiterals(DetailsView1.Rows(0).Cells(1).Text) & "' and Product_IsDeleted='0') and Receive_Isdeleted='0'")
                        If dt.Rows.Count > 0 Then
                            sendmailforproductalerts(dt)
                            System.Threading.Thread.Sleep(1000)
                        End If
                        tddetail.InnerHtml = ""
                        System.Threading.Thread.Sleep(1000)
                    Else
                        lblerror.Visible = True
                        lblerror.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;The Generated Product Code is Duplicate, Please try later Or Contact to Site Administrator."
                        Exit Sub
                    End If

                End If

            Else
                lblerror.Visible = True
                lblerror.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;Please Fill All The Product Details to Approve the Product"
            End If
        Catch ex As Exception
            lblerror.Visible = True
            lblerror.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;There is some technical problem, Please try later."
        End Try
    End Sub
    Public Sub sendmailforproductalerts(ByVal dt As DataTable)
        Try
            Dim str As String
            str = ""
            For j As Integer = 0 To dt.Rows.Count - 1
                If (j = dt.Rows.Count - 2) Then
                    str += dt.Rows(j).Item("Receive_Email").ToString & ","
                Else
                    str += dt.Rows(j).Item("Receive_Email")
                End If
            Next

            Dim strURL As String
            Dim dt1 As DataTable
            dt1 = ReturnDataTable("Select Product_Kid, CatName, ProdName from ProductDisplay where Product_Kid='" & RemoveLiterals(DetailsView1.Rows(0).Cells(1).Text) & "'")
            strURL = "http://www.atcomaart.com" & "/pd/" & EncodeURL(dt1.Rows(0).Item("Product_Kid")) & "/" & dt1.Rows(0).Item("CatName") & "/" & dt1.Rows(0).Item("ProdName") & ".aspx"

            Dim StrMail As String = ""
            StrMail = "<div style='font-family:Arial;FONT-SIZE: 10pt;'>"
            StrMail += "Dear ,<b> Customer </b><br/><br/>We are glad to provide you with the latest information on product alert.<br/>Following is the list of latest updated product :"
            StrMail += "<table cellspacing='0' cellpadding='1' align='left' style='FONT-SIZE: 10pt;FONT-FAMILY: Arial'>"
            StrMail += "<tr> <td><b><a href='" & strURL.ToString & "' target='_blank'>" & DetailsView1.Rows(3).Cells(1).Text & " </a></b><br/> <b>Click on the above mentioned link for the respective product.</b></td></tr>"
            StrMail += "</table>"
            StrMail += "<br/><br/>Wishing you a good time<br/>Thanks & Regards<br/> ATCOMAART TEAM,<br/><a href='www.atcomaart.com'> (www.atcomaart.com)</a>"
            StrMail += "</Div>"

            Dim mailmsg As New MailMessage()
            mailmsg.From = New MailAddress("contact@atcomaart.com", "AtcoMaart Ltd.")
            mailmsg.To.Add(New MailAddress("" & str & "", "atcomaart Ltd."))
            mailmsg.IsBodyHtml = True
            mailmsg.Priority = MailPriority.High
            mailmsg.Body = StrMail
            mailmsg.Subject = "Product Alert From AtcoMaart"
            Dim smtp As New SmtpClient("smtp.rediffmailpro.com")
            smtp.Credentials = New Net.NetworkCredential("contact@atcomaart.com", "vikram")
            smtp.Send(mailmsg)
        Catch ex As Exception

        End Try
    End Sub
    Private Function CheckOthers() As Boolean
        Try
            lblOtherError.Text = "<ul>"
            Dim ProdApp As String
            ProdApp = ReturnValue("SELECT ProdApp_Kid+'-'+ProdApp_Application FROM dbo.ProductApplication WHERE  ProdApp_ProductId = '" & RemoveLiterals(txtProdKid.Text) & "' ")
            If ProdApp.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "Product Application" + "&nbsp;" + "</li>" + "<br />"
                txtOther.Text = "False"
                'lblOtherError.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'  />" + "&nbsp" + "Product Application" + "&nbsp" + "<br />"
            End If

            Dim Feature As String
            Feature = ReturnValue("SELECT ProdFeatures_Kid+'-'+ProdFeatures_Features FROM dbo.ProductFeatures WHERE ProdFeatures_ProductId = '" & RemoveLiterals(txtProdKid.Text) & "' ")
            If Feature.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "Features" + "</li>" + "<br />"
                txtOther.Text = "False"
            End If

            Dim TechSpec As String
            TechSpec = ReturnValue("SELECT TechSpec_Kid +'-'+TechSpec_ParameterID FROM dbo.ProductTechnicalSpecification WHERE TechSpec_ProductId = '" & RemoveLiterals(txtProdKid.Text) & "'")
            If TechSpec.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "Technical Specifaction" + "</li>" + "<br />"
                txtOther.Text = "False"
            End If

            Dim SpecialNotes As String
            SpecialNotes = ReturnValue("SELECT ProdSpecInstr_Kid+'-'+ProdSpecInstr_Filename FROM dbo.ProductSpecialNotesInstruction WHERE ProdSpecInstr_ProductId = '" & RemoveLiterals(txtProdKid.Text) & "'")
            If SpecialNotes.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "SpecialNotes" + "</li>" + "<br />"
                txtOther.Text = "False"
            End If

            Dim ProductMSDS As String
            ProductMSDS = ReturnValue("SELECT MSDS_Kid+'-'+MSDS_UploadedDataSheetName FROM dbo.MaterialSafetyDataSheet WHERE MSDS_ProductId = '" & RemoveLiterals(txtProdKid.Text) & "'")
            If ProductMSDS.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "ProductMSDS" + "</li>" + "<br />"
                txtOther.Text = "False"
            End If

            Dim Consumable As String
            Consumable = ReturnValue("SELECT c_kid+'-'+c_cname FROM dbo.Consumables WHERE c_productid = '" & RemoveLiterals(txtProdKid.Text) & "'")
            If Consumable.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "Consumable" + "</li>" + "<br />"
                txtOther.Text = "False"
            End If

            Dim Catelogue As String
            Catelogue = ReturnValue("SELECT Catalog_kid+'-'+Catalog_upload FROM dbo.Catalog WHERE Catalog_productid = '" & RemoveLiterals(txtProdKid.Text) & "' ")
            If Catelogue.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "Catelogue" + "</li>" + "<br />"

                txtOther.Text = "False"
            End If

            Dim Standards As String
            Standards = ReturnValue("SELECT ProdStdApproval_Kid+'-'+ProdStdApproval_Filename FROM dbo.ProductStandardApproval WHERE ProdStdApproval_ProductId = '" & RemoveLiterals(txtProdKid.Text) & "'")
            If Standards.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "Product Standard Standards" + "</li>" + "<br />"
                txtOther.Text = "False"
            End If

            Dim UserManual As String
            UserManual = ReturnValue("SELECT InstCommManual_Kid FROM dbo.InstallationManual WHERE InstCommManual_ProductId = '" & RemoveLiterals(txtProdKid.Text) & "'")
            If UserManual.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "UserManual" + "</li>" + "<br />"
                txtOther.Text = "False"
            End If

            Dim Gurantee As String
            Gurantee = ReturnValue("SELECT gwd_Kid FROM dbo.Product_GuarantyWarrantydetails WHERE gwd_ProductId = '" & RemoveLiterals(txtProdKid.Text) & "'")
            If Gurantee.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "Gurantee/Warantee" + "</li>" + "<br />"
                txtOther.Text = "False"
            End If

            Dim PackagingDetails As String
            PackagingDetails = ReturnValue("SELECT ProdPackaging_Kid FROM dbo.ProductPackaging WHERE ProdPackaging_ProductId = '" & RemoveLiterals(txtProdKid.Text) & "'")
            If PackagingDetails.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "Packaging Details" + "</li>" + "<br />"
                txtOther.Text = "False"
            End If

            Dim Installation As String
            Installation = ReturnValue("SELECT InstallationComm_Kid FROM dbo.ProductInstallationCommissioning WHERE InstallationComm_ProductId = '" & RemoveLiterals(txtProdKid.Text) & "'")
            If Installation.ToString = "" Then
                lblOtherError.Text += "<li>" + "&nbsp;" + "Installation Commission" + "</li>" + "<br />"
                txtOther.Text = "False"
            End If
            lblOtherError.Style.Add("list-style-type", "circle")

            If txtOther.Text = "False" Then
                lblInfo.Visible = True
                Return False
            Else
                lblInfo.Visible = False
                Return True
            End If
        Catch ex As Exception

        End Try
    End Function
    'client side validation for Apporval

    Private Function checkgrid() As Boolean

        If DetailsView1.Rows(0).Cells(1).Text = "&nbsp;" Then
            Return False
        ElseIf DetailsView1.Rows(1).Cells(1).Text = "&nbsp;" Then
            Return False
        ElseIf Image1.ImageUrl = "~/Upload Image/Product Image/UploadImages/" Then
            Return False
            'ElseIf DetailsView1.Rows(2).Cells(1).Text = "&nbsp;" Then  ''for ATCOMAART Product Code
            '    Return False
            'ElseIf DetailsView1.Rows(3).Cells(1).Text = "&nbsp;" Then  '' for ATCO Price
            '    Return False
        ElseIf DetailsView1.Rows(4).Cells(1).Text = "&nbsp;" Then
            Return False
        ElseIf DetailsView1.Rows(5).Cells(1).Text = "&nbsp;" Then
            Return False
        ElseIf DetailsView1.Rows(6).Cells(1).Text = "&nbsp;" Then
            Return False
        ElseIf DetailsView1.Rows(7).Cells(1).Text = "&nbsp;" Then
            Return False
        ElseIf DetailsView1.Rows(8).Cells(1).Text = "&nbsp;" Then
            Return False
           
        ElseIf DetailsView1.Rows(9).Cells(1).Text = "&nbsp;" Then
            Return False
        ElseIf DetailsView1.Rows(10).Cells(1).Text = "&nbsp;" Then
            Return False
            ' ElseIf DetailsView1.Rows(11).Cells(1).Text = "&nbsp;" Then
            'Return False
        ElseIf DetailsView1.Rows(13).Cells(1).Text = "&nbsp;" Then
            Return False
        Else
            Return True
        End If

    End Function

    'java script validation for approval button click

    Private Function CheckValidation(ByVal detailviewDs As DataSet) As Boolean

        'If detailviewDs.Tables(0).Rows(0).Item("Price").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Price").ToString.Contains(" 0.00") Then
        '    txtFlag.Text = "False"
        '    Return False
        ''ElseIf detailviewDs.Tables(0).Rows(0).Item("Atco Price").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Atco Price").ToString <= "0.00" Then
        ''    txtFlag.Text = "False"
        ''    Return False
        If detailviewDs.Tables(0).Rows(0).Item("Product_Image").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Product_Image").ToString = "" Then
            txtFlag.Text = "False"
            Return False
        ElseIf detailviewDs.Tables(0).Rows(0).Item("Model No").ToString = Nothing Then
            txtFlag.Text = "False"
            Return False
        ElseIf detailviewDs.Tables(0).Rows(0).Item("Min Order Qty").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Min Order Qty").ToString.StartsWith("0.00") Then
            txtFlag.Text = "False"
            Return False
        ElseIf detailviewDs.Tables(0).Rows(0).Item("Shipping Qty").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Shipping Qty").ToString.StartsWith("0.00") Then
            txtFlag.Text = "False"
            Return False
            ' ''ElseIf detailviewDs.Tables(0).Rows(0).Item("Pcs Per Carton/Pckg").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Pcs Per Carton/Pckg").ToString <= "0.00" Then
            ' ''    txtFlag.Text = "False"
            ' ''    Return False
        ElseIf detailviewDs.Tables(0).Rows(0).Item("Shipping Weight").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Shipping Weight").ToString.StartsWith("0.00") Then
            txtFlag.Text = "False"
            Return False
        ElseIf detailviewDs.Tables(0).Rows(0).Item("Shipping Dimension").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Shipping Dimension").ToString.Contains("0X0X0") OrElse detailviewDs.Tables(0).Rows(0).Item("Shipping Dimension").ToString.Contains("0 X 0 X 0") Then
            txtFlag.Text = "False"
            Return False
        ElseIf detailviewDs.Tables(0).Rows(0).Item("Dispatch In").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Dispatch In").ToString.StartsWith("0") Then
            txtFlag.Text = "False"
            Return False
        Else
            Return True
        End If

    End Function

    Function bindNonapprovedgrid()
        Dim str As String = "SELECT   Product_Kid, Product_Name + ' - ' + isnull(Product_ModelNo,'') as Product_Name FROM Product_Master  WHERE   Product_SupplierId='" & RemoveLiterals(ddlSupplierName.SelectedItem.Value) & "' and   Product_Approved = 'N' and Product_IsDeleted='0' order by Product_RegDate DESC"
        con.Open()
        ds.Clear()
        da = New SqlDataAdapter(str, con)
        da.Fill(ds)
        GvNonApproved.DataSource = ds.Tables(0)
        GvNonApproved.DataBind()
        If ds.Tables(0).Rows.Count > 0 Then
            btnMultiApprove.Visible = True
            chkAll.Visible = True
        Else
            btnMultiApprove.Visible = False
            chkAll.Visible = False
        End If
        If ds.Tables(0).Rows.Count = 3 Then
            divNonApprovedProduct.Style.Add("height", "100px")
        ElseIf ds.Tables(0).Rows.Count = 5 Then
            divNonApprovedProduct.Style.Add("height", "150px")
        ElseIf ds.Tables(0).Rows.Count = 7 Then
            divNonApprovedProduct.Style.Add("height", "200px")
        ElseIf ds.Tables(0).Rows.Count > 9 Then
            divNonApprovedProduct.Style.Add("height", "200px")
        End If
        con.Close()
    End Function
    Function bindapprovedgrid()
        Dim str As String = "SELECT   Product_Kid, Product_Name + ' - ' + isnull(Product_ModelNo,'') as Product_Name FROM Product_Master  WHERE   Product_SupplierId='" & RemoveLiterals(ddlSupplierName.SelectedItem.Value) & "' and   Product_Approved = 'Y' and Product_IsDeleted='0' order by Product_ApprovedDate DESC"
        con.Open()
        ds.Clear()
        da = New SqlDataAdapter(str, con)
        da.Fill(ds)
        GvApproved.DataSource = ds.Tables(0)
        GvApproved.DataBind()
        con.Close()
    End Function
    Protected Sub btndelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndelete.Click
        Dim str As String = "update  product_Master set Product_IsDeleted=1,Product_ClientIP='" & Request.UserHostAddress & "' where product_Kid='" & RemoveLiterals(DetailsView1.Rows(0).Cells(1).Text) & "'"
        con.Open()
        da = New SqlDataAdapter(str, con)
        da.SelectCommand.ExecuteNonQuery()
        con.Close()
        detailsheader.Visible = False
        detailshow.Visible = False
        lblerror.Visible = True
        ExecuteQuery("delete from ProductCategory_Master where ProductCategory_ProductID='" & RemoveLiterals(DetailsView1.Rows(0).Cells(1).Text) & "'")
        lblerror.Text = "Product Deleted Successfully."
        bindNonapprovedgrid()
        btndelete.Visible = False
        btnapproved.Visible = False
    End Sub

    Protected Sub lnkSName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSName.Click
        Try
            'Dim supplierid As String = RemoveLiterals(lnksupplier.CommandArgument.Trim)
            Response.Redirect("../Suppliers/List_Product.aspx?Kid=" & HiddenField1.Value)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strKeyVal")
        End Try
    End Sub

    Protected Sub chkAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAll.CheckedChanged
        Try
            Dim i As Integer
            If GvNonApproved.Rows.Count > 0 Then
                If chkAll.Checked = True Then
                    For i = 0 To GvNonApproved.Rows.Count - 1
                        CType(GvNonApproved.Rows(i).FindControl("chkNonProduct"), CheckBox).Checked = True
                    Next
                    chkAll.Text = "Clear All"
                Else
                    For i = 0 To GvNonApproved.Rows.Count - 1
                        CType(GvNonApproved.Rows(i).FindControl("chkNonProduct"), CheckBox).Checked = False
                    Next
                    chkAll.Text = "Select All"
                End If
            End If
        Catch ex As Exception
           
        End Try
    End Sub

    Protected Sub btnMultiApprove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMultiApprove.Click
        Try
            Dim count As Integer = 0
            For i = 0 To GvNonApproved.Rows.Count - 1
                If CType(GvNonApproved.Rows(i).FindControl("chkNonProduct"), CheckBox).Checked = True Then
                    '' To Add IN Assing Prospect 
                    Dim ProductCode As String = GenerateProductCode(RemoveLiterals(ddlSupplierName.SelectedItem.Value))
                    If ProductCode.Length < 6 Then
                        lblerror.Visible = True
                        lblerror.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;There is some technical problem, Please try later."
                        Exit Sub
                    Else
                        Dim pcode As String = ReturnValue("select ProductCode from product_Master where Product_Code='" & ProductCode & "'")
                        If pcode = "" Then
                            Dim str As String = "update product_Master set Product_Approved='Y' , Product_ApprovedDate='" & Date.Now() & "',Product_ClientIP='" & Request.UserHostAddress & "',Product_Code='" & ProductCode & "' where product_Kid='" & CType(GvNonApproved.Rows(i).FindControl("lblNApname"), LinkButton).CommandArgument.ToString & "'"
                            con.Open()
                            da = New SqlDataAdapter(str, con)
                            da.SelectCommand.ExecuteNonQuery()
                            'System.Threading.Thread.Sleep(1000)
                            con.Close()
                            detailsheader.Visible = False
                            detailshow.Visible = False
                            lblInfo.Visible = False
                            lblOtherError.Visible = False
                           
                            Dim dt As DataTable
                            dt = ReturnDataTable("Select Receive_Email from Receive_Alert where Receive_Sub2CatCode=(Select Product_CategoryId from Product_Master where product_Kid='" & CType(GvNonApproved.Rows(i).FindControl("lblNApname"), LinkButton).CommandArgument.ToString & "' and Product_IsDeleted='0') and Receive_Isdeleted='0'")
                            If dt.Rows.Count > 0 Then
                                sendmailforproductalerts(dt)
                                'System.Threading.Thread.Sleep(1000)
                            End If
                            tddetail.InnerHtml = ""
                            'System.Threading.Thread.Sleep(1000)
                        Else
                            lblSelectMsg.Visible = True
                            lblSelectMsg.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;The Generated Product Code is Duplicate, Please try later Or Contact to Site Administrator."
                            Exit Sub
                        End If

                    End If

                    count = count + 1
                End If
            Next
            If count > 0 Then
                bindapprovedgrid()
                'System.Threading.Thread.Sleep(1000)
                bindNonapprovedgrid()
                btnapproved.Visible = False
                btndelete.Visible = False
                lblSelectMsg.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;Total <b stlyle='color:blue'>" + count.ToString + "</b> Products Approved Successfully."
                Exit Sub
            Else
                lblSelectMsg.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;Please Select Products.."
                Exit Sub
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class
