Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.IO.FileStream
Imports ModCommon
Partial Class Admin_ProductStandardsandCertifications
    Inherits System.Web.UI.Page
    Shared str As String
    Public strPath As String
    Shared img As Integer
    Dim code As String
    Dim name As String
    Dim logoname 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
            If (Not IsPostBack) Then
                FillGrid()
                Call CheckRights(Me, Session("g_User").ToString, "Product Standards and Certifications")
                img = 0
                BtnEdit.Enabled = False
                BtnDelete.Enabled = False
                BtnDelete.Attributes.Add("onclick", " return confirm('Are you sure you want to delete?');")
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub
    Protected Sub _Default_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        If Not IsValidConnectionString() Then CreateMessageAlert(Me, "Check the Connection String !", "StrKeyVal") 'If not a valid ConnectionString then Display the message 
    End Sub
    Protected Sub FillGrid()
        Try
            Dim qry As String
            Dim con As New SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()
            qry = "select PSM_Kid,PSM_Name,PSM_Logo,PSM_Code,PSM_Desc from ProductStandardsMaster where PSM_IsDeleted='0' and PSM_CompanyId='" & Session("g_Company").ToString & "' order by PSM_Name"
            Dim cmd As New SqlCommand(qry, con)
            'Dim dr As SqlDataReader                             'Define DataReader variable for storing the output of the query
            'dr = cmd.ExecuteReader      'Assign the output of query to DataReader variable
            Dim dt_grd As DataTable
            dt_grd = ReturnDataTable(qry)
            gvBrand.DataSource = dt_grd
            gvBrand.DataBind()
            Dim i As Integer
            For i = 0 To gvBrand.Rows.Count - 1
                Dim str_path As String
                str_path = Server.MapPath("~/Upload Image/Product Standard Logo/" & dt_grd.Rows(i).Item("PSM_Logo").ToString)   'gvBrand.Rows(i).Cells(4).Text)
                '  CType(gvBrand.Rows(i).Cells(3).FindControl("Image1"), Image).ImageUrl = str_path.ToString
                KeepAspectRatioNew(CType(gvBrand.Rows(i).Cells(3).FindControl("Image1"), Image), str_path, 40, 40)
            Next

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Dim strImageName As String = ""
        Dim file_name As String = ""
        If (BrandFileUpload.HasFile) Then
            Dim fileType As String = Path.GetExtension(BrandFileUpload.FileName)
            If fileType = ".gif" Or fileType = ".png" Or fileType = ".jpg" Or fileType = ".jpeg" Or fileType = ".bmp" Then
                strImageName = Final_FileUpload(BrandFileUpload, img, Server.MapPath("~\Upload Image\Product Standard Logo\"))
                lblMsg.Visible = False
            Else
                lblMsg.Visible = True
                lblMsg.Text = "Please Select image File Only!"
                BrandFileUpload.Focus()
                Exit Sub
            End If
        End If
        If IsAlreadyPresent("select count(*) from ProductStandardsMaster where PSM_Name='" & txtBrandName.Text & "'and PSM_IsDeleted='0' and PSM_CompanyId='" & Session("g_Company").ToString & "'") Then
            CreateMessageAlert(Me, " Record Already Exists !", "StrKeyVal")
            txtBrandName.Text = "" : txtBrandName.Focus()
            Exit Sub
        End If
        code = GenerateBrandCode()
        Dim con As New SqlConnection
        con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
        con.Open()
        Dim cmd As New SqlCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "ProductStandardsMaster_Proc"
        cmd.Connection = con
        cmd.Parameters.Add(New SqlParameter("@PSM_Kid", SqlDbType.NVarChar, 10)).Value = NewPrimaryKey(Me)
        cmd.Parameters.Add(New SqlParameter("@PSM_Code", SqlDbType.NVarChar, 25)).Value = Trim(code.ToString)
        cmd.Parameters.Add(New SqlParameter("@PSM_Name", SqlDbType.NVarChar, 200)).Value = (txtBrandName.Text)
        cmd.Parameters.Add(New SqlParameter("@PSM_Logo", SqlDbType.NVarChar, 200)).Value = (strImageName)
        cmd.Parameters.Add(New SqlParameter("@PSM_Desc", SqlDbType.NVarChar, 200)).Value = txtdesc.Text
        cmd.Parameters.Add(New SqlParameter("@PSM_CompanyId", SqlDbType.NVarChar, 10)).Value = (Session("g_Company").ToString)
        cmd.Parameters.Add(New SqlParameter("@PSM_UserId", SqlDbType.NVarChar, 10)).Value = (Session("g_User").ToString)
        cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 50)).Value = "ProductStandardsandCertifications"
        cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 10)).Value = "Insert"
        cmd.ExecuteNonQuery()
        FillGrid()
        CreateMessageAlert(Me, " Record Saved Succesfully !", "StrKeyVal")
    End Sub
    Protected Sub gvBrand_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvBrand.SelectedIndexChanged
        Try
            BtnEdit.Enabled = True
            BtnDelete.Enabled = True
            HiddenField1.Value = gvBrand.SelectedRow.Cells(1).Text
            HiddenField2.Value = gvBrand.SelectedRow.Cells(5).Text
            txtBrandName.Text = gvBrand.SelectedRow.Cells(2).Text
            txtdesc.Text = gvBrand.SelectedRow.Cells(6).Text
            txtBrandName.Focus()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub
    Protected Sub BtnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        Dim strImageName As String = ""
        Dim file_name As String = ""
        If (BrandFileUpload.HasFile) Then
            Dim fileType As String = Path.GetExtension(BrandFileUpload.FileName)
            If fileType = ".gif" Or fileType = ".png" Or fileType = ".jpg" Or fileType = ".jpeg" Or fileType = ".bmp" Then
                strImageName = Final_FileUpload(BrandFileUpload, img, Server.MapPath("~\Upload Image\Product Standard Logo\"))
                lblMsg.Visible = False
            Else
                lblMsg.Visible = True
                lblMsg.Text = "Please Select image File Only!"
                BrandFileUpload.Focus()
                Exit Sub
            End If
        Else
            strImageName = ReturnValue("Select PSM_Logo from ProductStandardsMaster where PSM_Kid = '" & HiddenField1.Value & "' and PSM_IsDeleted = '0'")
        End If
        Dim con As New SqlConnection
        con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
        con.Open()
        Dim cmd As New SqlCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "ProductStandardsMaster_Proc"
        cmd.Connection = con
        cmd.Parameters.Add(New SqlParameter("@PSM_Kid", SqlDbType.NVarChar, 10)).Value = HiddenField1.Value
        cmd.Parameters.Add(New SqlParameter("@PSM_Code", SqlDbType.NVarChar, 25)).Value = HiddenField2.Value
        cmd.Parameters.Add(New SqlParameter("@PSM_Name", SqlDbType.NVarChar, 200)).Value = (txtBrandName.Text)
        cmd.Parameters.Add(New SqlParameter("@PSM_Logo", SqlDbType.NVarChar, 200)).Value = (strImageName)
        cmd.Parameters.Add(New SqlParameter("@PSM_Desc", SqlDbType.NVarChar, 200)).Value = txtdesc.Text
        cmd.Parameters.Add(New SqlParameter("@PSM_CompanyId", SqlDbType.NVarChar, 10)).Value = (Session("g_Company").ToString)
        cmd.Parameters.Add(New SqlParameter("@PSM_UserId", SqlDbType.NVarChar, 10)).Value = (Session("g_User").ToString)
        cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 50)).Value = "ProductStandardsandCertifications"
        cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Update"
        cmd.ExecuteNonQuery()
        FillGrid()
        CreateMessageAlert(Me, " Record Updated Succesfully !", "StrKeyVal")
    End Sub
    Protected Sub BtnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Dim strImageName As String = ReturnValue("Select PSM_Logo from ProductStandardsMaster where PSM_Kid = '" & gvBrand.SelectedRow.Cells(1).Text & "' and PSM_IsDeleted = '0'")
        Dim con As New SqlConnection
        con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
        con.Open()
        Dim cmd As New SqlCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "ProductStandardsMaster_Proc"
        cmd.Connection = con
        cmd.Parameters.Add(New SqlParameter("@PSM_Kid", SqlDbType.NVarChar, 10)).Value = HiddenField1.Value
        cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "ProductStandardsandCertifications"
        cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Delete"
        cmd.ExecuteNonQuery()
        FillGrid()
        txtBrandName.Text = ""
        txtdesc.Text = ""
        CreateMessageAlert(Me, " Record Deleted Succesfully !", "StrKeyVal")
    End Sub
    Protected Function GenerateBrandCode()
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "BND" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from ProductStandardsMaster where PSM_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    Return code
                    Exit Function
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Function
    Protected Sub gvBrand_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvBrand.PageIndexChanging
        gvBrand.PageIndex = e.NewPageIndex
        FillGrid()
    End Sub

End Class
