﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class Admin_MentorCorrection
    Inherits System.Web.UI.Page

#Region "Events"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("AdminID") Is Nothing Then
            Response.Redirect("AdminLogin.aspx?sesn=expire")
            Exit Sub
        End If

        If Not IsPostBack Then
            fillcombo()
        End If
    End Sub

    Protected Sub ddlPE_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPE.SelectedIndexChanged
        Try
            If ddlPE.SelectedIndex > 0 Then
                ViewState("SelectedUser") = ddlPE.SelectedValue.ToString
                fillgrid(ddlPE.SelectedValue.ToString)
                trProductDetail.Visible = False
            Else
                gvProduct.DataSource = Nothing
                gvProduct.DataBind()
                trProductDetail.Visible = False
            End If
        Catch ex As Exception
        End Try
    End Sub

    'Protected Sub gvProduct_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvProduct.PageIndexChanging
    '    Try
    '        gvProduct.PageIndex = e.NewPageIndex
    '        fillgrid(ViewState("SelectedUser").ToString)
    '        trProductDetail.Visible = False
    '    Catch ex As Exception

    '    End Try
    'End Sub

    Protected Sub gvProduct_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvProduct.SelectedIndexChanged
        Dim detailviewDs As New DataSet
        con.Open()
        'detailviewDs.Clear()
        ViewState("ProductID") = gvProduct.SelectedDataKey.Value
        da = New SqlDataAdapter("sp_ProductDetails", con)
        da.SelectCommand.CommandType = CommandType.StoredProcedure
        da.SelectCommand.Parameters.Add(New SqlParameter("@Product_Kid", SqlDbType.NVarChar))
        da.SelectCommand.Parameters("@Product_Kid").Value = RemoveLiterals(gvProduct.SelectedDataKey.Value)
        'da = New SqlDataAdapter(Str, con)
        da.Fill(detailviewDs)
        tdRemarks.InnerHtml = "<b>Remarks: </b>" + gvProduct.SelectedRow.Cells(6).Text.ToString
        tddetail.InnerHtml = "<b>Description:  </b>" + detailviewDs.Tables(0).Rows(0).Item("Product_Description").ToString
        imgProductImage.ImageUrl = "~/Upload Image/Product Image/UploadImages/" & detailviewDs.Tables(0).Rows(0).Item(1).ToString
        imgProductImage.Attributes.Add("alt", detailviewDs.Tables(0).Rows(0).Item(1).ToString)
        Dim imgpath As String
        imgpath = Server.MapPath(imgProductImage.ImageUrl)
        KeepAspectRatioNew(imgProductImage, imgpath, 200, 200)
        dtViewProductInfo.DataSource = detailviewDs.Tables(0)
        dtViewProductInfo.DataBind()
        colorgrid(detailviewDs)
        ViewState("DetailView") = detailviewDs
        dtViewProductInfo.Rows(0).Visible = False
        dtViewProductInfo.Rows(1).Visible = False
        dtViewProductInfo.Rows(15).Visible = False
        trProductDetail.Visible = True
        'txtRemarks.Text = ""
        'ScriptManager.GetCurrent(Me).SetFocus(txtRemarks)
    End Sub

    Protected Sub gvProduct_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvProduct.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            'e.Row.Cells(3).Text = Format$(Convert.ToDateTime(e.Row.Cells(3).Text), "dd/MM/yyyy")
            'If e.Row.Cells(4).Text <> "-" Then
            '    e.Row.Cells(4).Text = Format$(Convert.ToDateTime(e.Row.Cells(4).Text), "dd/MM/yyyy")
            'End If
            If e.Row.Cells(5).Text = "Pending" Then
                'e.Row.Cells(5).Text = "Go For Correction"
                e.Row.Cells(5).ForeColor = Drawing.Color.Red
                'Else
                '    e.Row.Cells(5).Text = "Corrected by PE"
            End If
        End If
    End Sub

#End Region

#Region "Methods"

    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
            dtViewProductInfo.Rows(2).Cells(1).ForeColor = Drawing.Color.Red
            dtViewProductInfo.Rows(2).Cells(1).Text = """Will Generate After Approval."""
            dtViewProductInfo.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
            dtViewProductInfo.Rows(1).Cells(1).Text = "Required"
            dtViewProductInfo.Rows(1).Cells(1).ForeColor = Drawing.Color.Red
        End If
        If detailviewDs.Tables(0).Rows(0).Item("Model No").ToString = Nothing Then
            dtViewProductInfo.Rows(4).Cells(1).Text = "Required"
            dtViewProductInfo.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
            dtViewProductInfo.Rows(5).Cells(1).Text = "Required"
            dtViewProductInfo.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
        ''    dtViewProductInfo.Rows(5).Cells(1).Text = "Required"
        ''    dtViewProductInfo.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
            dtViewProductInfo.Rows(6).Cells(1).Text = "Required"
            dtViewProductInfo.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
            dtViewProductInfo.Rows(7).Cells(1).Text = "Required"
            dtViewProductInfo.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
        ' ''    dtViewProductInfo.Rows(9).Cells(1).Text = "Required"
        ' ''    dtViewProductInfo.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
            dtViewProductInfo.Rows(8).Cells(1).Text = "Required"
            dtViewProductInfo.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
            dtViewProductInfo.Rows(9).Cells(1).Text = "Required"
            dtViewProductInfo.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
            dtViewProductInfo.Rows(10).Cells(1).Text = "Required"
            dtViewProductInfo.Rows(10).Cells(1).ForeColor = Drawing.Color.Red
        End If
        'If detailviewDs.Tables(0).Rows(0).Item("Approved Date").ToString = Nothing OrElse detailviewDs.Tables(0).Rows(0).Item("Product Code").ToString = "" Then
        '    dtViewProductInfo.Rows(11).Cells(1).ForeColor = Drawing.Color.Red
        '    dtViewProductInfo.Rows(11).Cells(1).Text = """Will Generate After Approval."""
        '    dtViewProductInfo.Rows(11).Cells(1).Style.Add("font-size", "8pt")
        'End If
    End Sub

    Protected Sub fillcombo()
        Try
            Dim dt As DataTable
            dt = ReturnDataTable("SELECT DISTINCT QC_Master.QC_PEID, AdminUser.AdminUser_Name FROM QC_Master INNER JOIN AdminUser ON QC_Master.QC_PEID = AdminUser.AdminUser_Kid WHERE (QC_Master.QC_Flag = N'C') AND (AdminUser.AdminUser_ReportingTo = N'" + Session("g_User").ToString + "') Order By AdminUser.AdminUser_Name")
            ddlPE.DataSource = dt
            ddlPE.DataTextField = "AdminUser_Name"
            ddlPE.DataValueField = "QC_PEID"
            ddlPE.DataBind()
            ddlPE.Items.Insert(0, New ListItem("Select", "0"))
        Catch ex As Exception
        End Try
    End Sub

    Protected Sub fillgrid(ByVal UserID As String)
        Try
            Dim dt As New DataTable
            Dim sqlCmd As New SqlCommand
            Dim sqlCon As SqlConnection
            Dim adp As SqlDataAdapter
            sqlCon = New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
            sqlCon.Open()
            With sqlCmd
                .CommandText = "Sp_QualityCheck"
                .CommandType = Data.CommandType.StoredProcedure
                .Connection = sqlCon
                .Parameters.AddWithValue("@Mode", "Select_BackwardProduct")
                .Parameters.AddWithValue("@CompanyId", Session("g_Company").ToString)
                .Parameters.AddWithValue("@QC_Status", "C")
                .Parameters.AddWithValue("@PEId", UserID.ToString)
            End With
            adp = New SqlDataAdapter(sqlCmd)
            adp.Fill(dt)
            gvProduct.DataSource = dt
            gvProduct.DataBind()
            If dt.Rows.Count = 3 Then
                divProduct.Style.Add("height", "100px")
            ElseIf dt.Rows.Count = 5 Then
                divProduct.Style.Add("height", "150px")
            ElseIf dt.Rows.Count = 7 Then
                divProduct.Style.Add("height", "200px")
            ElseIf dt.Rows.Count = 9 Then
                divProduct.Style.Add("height", "250px")
            ElseIf dt.Rows.Count > 9 Then
                divProduct.Style.Add("height", "280px")
            End If
        Catch ex As Exception
        End Try
    End Sub

#End Region

End Class
