Imports RGBC_Promotions.SystemFunctions
Imports RGBC_Promotions.ProjectFunctions
Imports RGBC_Promotions.db

Partial Public Class MyBrands
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles cblBrands.PreRender
    Dim i, j As Integer

    If Not Page.IsPostBack Then
      If Session.Item("User") Is Nothing Then 'lame
        LogginUser(Request, Session)
      End If

      'not SPECIAL b.lehhh
      If Not Session.Item("USER")!blnSpecial Then
        Try
          ddlUser.Items.FindByValue(Session.Item("USER")!UserID).Selected = True
          j = ddlUser.Items.Count - 1
          For i = j To 0 Step -1
            If ddlUser.Items(i).Selected = False Then
              ddlUser.Items.RemoveAt(i)
            End If
          Next
        Catch ex As Exception
        Finally
          btnLoad_Click(sender, e)
        End Try

      End If
    End If
  End Sub

  Protected Sub btnLoad_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLoad.Click
    Dim ds As New DataSet
    Dim drUser As DataRow

    'clear selection
    'load selection

    If ddlUser.SelectedValue <> -1 Then
      drUser = db.getRow("SELECT * FROM mstUser WHERE UserID = " & ddlUser.SelectedValue)
      Try
        If Not drUser!strRegion = ddlRegion.SelectedValue Then 'wtf microshit?! keep on getting lame multi selection errors
          ddlRegion.ClearSelection()
          ddlRegion.Items.FindByValue(drUser!strRegion).Selected = True
        End If
      Catch ex As Exception
      End Try

      cblBrands.ClearSelection()

      ds = db.doQuery("SELECT * FROM mstUserBrand WHERE refUserID = " & ddlUser.SelectedValue)
      For Each row In ds.Tables(0).Rows
        Try
          cblBrands.Items.FindByValue(row!refBrandID).Selected = True

        Catch ex As Exception

        End Try

      Next

      btnSave.Enabled = True
    Else
      btnSave.Enabled = False
    End If
  End Sub

  Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
    'delete values
    'insert values

    If ddlUser.SelectedValue <> -1 Then
      db.doQuery("DELETE FROM mstUserBrand WHERE refUserID = " & ddlUser.SelectedValue)
      For Each item As ListItem In cblBrands.Items
        If item.Selected = True Then
          db.doQuery("INSERT INTO mstUserBrand(refUserID, refBrandID) VALUES (" & ddlUser.SelectedValue & ", " & item.Value & ")")
        End If
      Next

      db.doQuery("UPDATE mstUser SET strRegion = '" & ddlRegion.SelectedValue & "' WHERE UserID = " & ddlUser.SelectedValue)
      If Session("USER")!UserID = ddlUser.SelectedValue Then Session("USER")!strRegion = ddlRegion.SelectedValue
      cblBrands.DataBind()
      btnLoad_Click(sender, e)
    End If

  End Sub

  Private Sub MyBrands_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete

    Dim imgLoading As WebControls.Image

    imgLoading = Page.Master.FindControl("imgLoading")

    imgLoading.CssClass = "invisible"
  End Sub
End Class