﻿Imports RGBC_Forecast.SystemFunctions
Imports RGBC_Forecast.ProjectFunctions
Imports RGBC_Forecast.db

Partial Public Class OpenSeason
  Inherits System.Web.UI.Page

  Const idxDateStart As Integer = 2
  Const idxDateEnd As Integer = 3
  Const idxLastEdit As Integer = 5

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Session.Item("User") Is Nothing Then 'lame
      LogginUser(Request, Session)
    End If

    If Not Page.IsPostBack Then
      If CBool(Session("USER")!blnSpecial) <> True Then Response.Redirect("Home.aspx?Message=Insufficient rights to access this function.")

      btnSet.Enabled = False


      If CBool(Session.Item("USER")!blnSpecial) Then
        btnSet.Enabled = True
        lblMessage.Text = ""
      Else
        btnSet.Enabled = False
        lblMessage.Text = ""
      End If
    End If
  End Sub

  Private Sub OpenSeason_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
    If Not Page.IsPostBack Then
      calDateStart.SelectedDate = Date.Today
      calDateEnd.SelectedDate = DateSerial(Date.Today.Year, Date.Today.Month + 1, 1 - 1)
    End If
  End Sub

  Private Sub ShowLoading(ByVal blnShow As Boolean)
    Dim imgLoading As WebControls.Image = Page.Master.FindControl("imgLoading")

    If blnShow Then
      imgLoading.CssClass = "visible"
    Else
      imgLoading.CssClass = "invisible"
    End If

  End Sub

  Private Sub OpenSeason_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
    ShowLoading(False)
  End Sub

  Protected Sub btnSet_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSet.Click
    Dim BrandID As Integer = 0 'hard coded for now - don't know if they want to expand into Open Season per Brand
    Dim strLastUser As String = Session.Item("User")!strUserName
    Dim dtStamp As String = Date.Now.ToString("yyyyMMddHHmm")

    'check brand/date uniqueness - nah
    If False Then
      lblMessage.Text = "New Season Not Added. Brand/Date Already exists!"
    Else

      If db.doQuery("INSERT INTO mstOpenSeason (refBrandID, strDateStart, strDateEnd, strLastUser, strLastEdit) VALUES (" & BrandID & ", '" & calDateStart.SelectedDate.ToString("yyyyMMdd") & "', '" & calDateEnd.SelectedDate.ToString("yyyyMMdd") & "', '" & strLastUser & "', '" & dtStamp & "')") > 0 Then
        lblMessage.Text = "New Season Added"
        gvOpenSeason.DataBind()
      End If
    End If


  End Sub

  Protected Sub gvOpenSeason_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvOpenSeason.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then

      e.Row.Cells(idxDateStart).Text = DisplayDate(e.Row.Cells(idxDateStart).Text)
      e.Row.Cells(idxDateEnd).Text = DisplayDate(e.Row.Cells(idxDateEnd).Text)
      e.Row.Cells(idxLastEdit).Text = DisplayDate(e.Row.Cells(idxLastEdit).Text)

    End If
  End Sub
End Class