﻿Imports DevExpress.Web
Imports DevExpress.Web.Data

Public Class Aziende
    Inherits System.Web.UI.Page

    Private copiedValues As Hashtable = Nothing
    Private copiedFields() As String = {"Descrizione", "Indirizzo", "Citta", "Cap", "IDProvincia", "IDNazione", "Telefono", "Telefono2", "Fax", "Email"}

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '********************************************************
        '--- Controllo Autenticazione per accesso alla pagina ---
        If Not Session("Autenticato") Then
            Response.Redirect("LoginUser.aspx")
        End If
        '********************************************************
        '--- imposta la connessione al database sui datasource
        dsAziende.ConnectionString = Session("sConnectionString")
        dsNazioni.ConnectionString = Session("sConnectionString")
        dsProvincie.ConnectionString = Session("sConnectionString")
        dsRuoli.ConnectionString = Session("sConnectionString")
        dsSettori.ConnectionString = Session("sConnectionString")

    End Sub

    Private Sub ASPxGridView1_RowInserting(sender As Object, e As ASPxDataInsertingEventArgs) Handles ASPxGridView1.RowInserting

        '----------------------------------------------------------------
        '--- aggiorna i dati in fase di inserimento di una nuova riga ---
        '----------------------------------------------------------------
        Dim xDateTime = DateTime.Now
        e.NewValues("Ins_DataOra") = xDateTime
        e.NewValues("Upd_DataOra") = xDateTime
        e.NewValues("Ins_Utente") = Session("sUtente_UserName")       '--- mettere la variabile di sessione
        e.NewValues("Upd_Utente") = Session("sUtente_UserName")        '--- mettere la variabile di sessione
        e.NewValues("LivelloAutorizzazione") = Session("sUtente_Livello")    '--- mettere la variabile di sessione

    End Sub

    Private Sub ASPxGridView1_RowUpdating(sender As Object, e As ASPxDataUpdatingEventArgs) Handles ASPxGridView1.RowUpdating

        '----------------------------------------------------------------
        '--- aggiorna i dati in fase di inserimento di una nuova riga ---
        '----------------------------------------------------------------
        Dim xDateTime = DateTime.Now
        e.NewValues("Upd_DataOra") = xDateTime
        e.NewValues("Upd_Utente") = Session("sUtente_UserName")        '--- mettere la variabile di sessione
    End Sub

    Protected Sub grid_CustomButtonCallback(ByVal sender As Object, ByVal e As ASPxGridViewCustomButtonCallbackEventArgs)

        '--- duplica
        If e.ButtonID <> "Clone" Then
            Return
        End If
        copiedValues = New Hashtable()
        For Each fieldName As String In copiedFields
            copiedValues(fieldName) = ASPxGridView1.GetRowValues(e.VisibleIndex, fieldName)
        Next fieldName
        ASPxGridView1.AddNewRow()

    End Sub


    Protected Sub ProdottiButton_Click(sender As Object, e As EventArgs) Handles ProdottiButton.Click

        '--- Prende il l'ID  dalla RIGA SELEZIONATA ----------------------------
        Dim selected_ID As Integer
        Dim selected_Descrizione As String = ""

        For i = 0 To ASPxGridView1.VisibleRowCount - 1
            If (ASPxGridView1.Selection.IsRowSelected(i)) Then
                selected_ID = ASPxGridView1.GetRowValues(i, "IDAzienda")
                selected_Descrizione = ASPxGridView1.GetRowValues(i, "Descrizione")
            End If
        Next

        '--- prende l'ID del cliente selezionato dalla griglia e lo passa in una variabile di Sessione
        Session("sIDAzienda") = selected_ID
        Session("sDescrizioneAzienda") = selected_Descrizione
        Response.Redirect("Prodotti.aspx")

    End Sub

    Private Sub ASPxGridView1_StartRowEditing(sender As Object, e As ASPxStartRowEditingEventArgs) Handles ASPxGridView1.StartRowEditing

        '--- titolo form edit in modifica
        ASPxGridView1.SettingsText.PopupEditFormCaption = "Modifica Scheda Azienda"

    End Sub

    Private Sub ASPxGridView1_InitNewRow(sender As Object, e As ASPxDataInitNewRowEventArgs) Handles ASPxGridView1.InitNewRow

        '--- titolo form edit in inserimento
        ASPxGridView1.SettingsText.PopupEditFormCaption = "Nuova Scheda Azienda"

    End Sub
    Protected Sub grid_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataInitNewRowEventArgs)
        If copiedValues Is Nothing Then
            Return
        End If
        For Each fieldName As String In copiedFields
            e.NewValues(fieldName) = copiedValues(fieldName)
        Next fieldName
    End Sub

    Private Sub ASPxGridView1_CustomCallback(sender As Object, e As ASPxGridViewCustomCallbackEventArgs) Handles ASPxGridView1.CustomCallback

    End Sub
End Class