﻿Imports DevExpress.Web
Imports DevExpress.Web.Data

Public Class Clienti
    Inherits System.Web.UI.Page

    Private copiedValues As Hashtable = Nothing
    Private copiedFields() As String = {"IDcliente", "Cognome", "Nome", "Posizione", "Reparto", "Email", "Email2", "Telefono", "Fax", "Cellulare", "URL", "IDOrigine", "IDSettore", "IDValutazione", "OptOut", "Skype", "Twitter", "Facebook", "Descrizione", "IndirizzoVia", "Comune", "IDProvincia", "IndirizzoCap", "IDNazione", "AssegnatoA", "Descrizione", "Referente", "TelefonoReferente", "DataNascita", "IndirizzoVia2", "Comune2", "IDProvincia2", "IndirizzoCap2", "IDNazione2", "IDCategoriaClienti", "Fido", "RagioneSociale", "EmailReferente", "PARTITAIVA", "CODICEFISCALE", "IDRUOLO", "DataAcquisizione", "Clienti", "IDZona"}

    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
        dsClienti.ConnectionString = Session("sConnectionString")
        dsNazioni.ConnectionString = Session("sConnectionString")
        dsNazioniDest.ConnectionString = Session("sConnectionString")
        dsCategoriaClienti.ConnectionString = Session("sConnectionString")
        dsProvince.ConnectionString = Session("sConnectionString")
        dsProvinceDest.ConnectionString = Session("sConnectionString")
        dsRuoli.ConnectionString = Session("sConnectionString")
        dsSettori.ConnectionString = Session("sConnectionString")
        dsZone.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 ASPxButton1_Click(sender As Object, e As EventArgs) Handles ClientiAziendeButton.Click

        Session("sIDCliente") = 0
        Session("sDescrizioneCliente") = ""

        '--- Prende il l'ID del cliente dalla RIGA SELEZIONATA ----------------------------
        Dim selected_IDCliente As Integer = 0
        Dim selected_DescrizioneCliente As String = ""

        For i = 0 To ASPxGridView1.VisibleRowCount - 1
            If (ASPxGridView1.Selection.IsRowSelected(i)) Then
                selected_IDCliente = ASPxGridView1.GetRowValues(i, "IDcliente")
                selected_DescrizioneCliente = ASPxGridView1.GetRowValues(i, "RagioneSociale")
            End If
        Next

        If selected_IDCliente = 0 Then Exit Sub      'esce se non selezionato nulla

        '--- prende l'ID del cliente selezionato dalla griglia e lo passa in una variabile di Sessione
        Session("sIDCliente") = selected_IDCliente
        Session("sDescrizioneCliente") = selected_DescrizioneCliente
        Response.Redirect("ClientiAziende.aspx")
    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 Cliente"

    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 Cliente"


    End Sub

    Protected Sub grid_CustomButtonCallback(ByVal sender As Object, ByVal e As ASPxGridViewCustomButtonCallbackEventArgs)
        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 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
End Class