﻿Imports System.Web.Configuration

Partial Public Class OnconPromo
   Inherits System.Web.UI.Page

   Private Shared strGVFilter As String = ""
   Private Shared dblRows As Double = 0
   Private Const idxRef = 0

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   End Sub

   Private Sub OnconPromo_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
      If txtEndDate.Attributes.Count = 0 Then
         txtStartDate.Attributes.Add("onfocus", "showCalendarControl(this);")
         txtEndDate.Attributes.Add("onfocus", "showCalendarControl(this);")
      End If

      If Not Page.IsPostBack Then
         txtStartDate.Text = "01-" & Date.Today.ToString("MM-yyyy")
         txtEndDate.Text = Date.Today.ToString("dd-MM-yyyy")

      End If
   End Sub

   Private Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
      Dim s As String = ""

      btnExport.Enabled = True

      If strGVFilter.Length > 0 Then s = "WHERE " & strGVFilter
      If sqlDB.CountRows("SELECT strRecordID FROM dwfOnconPromo " & s) = 0 Then
         lblMessage.Text = "<div width='100%' align='center'>- No Oncon Promo Entries found -</div>"
         'btnExport.Enabled = False
      End If
   End Sub

   Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExport.Click
      Dim arrStartDate, arrEndDate As String()
      Dim strStartDate, strEndDate As String
      Dim s As String = ""

      lblMessage.Text = ""
      sdsOnconPromo.FilterExpression = "1=1"

      arrStartDate = txtStartDate.Text.Split("-")
      strStartDate = arrStartDate(2) & arrStartDate(1) & arrStartDate(0)
      arrEndDate = txtEndDate.Text.Split("-")
      strEndDate = arrEndDate(2) & arrEndDate(1) & arrEndDate(0)

      sdsOnconPromo.FilterExpression &= " AND strDatePromo >= '" & strStartDate & "' AND strDatePromo <= '" & strEndDate & "' "

      strGVFilter = sdsOnconPromo.FilterExpression
      WebConfigurationManager.AppSettings("intListFilter") = strGVFilter

      If strGVFilter.Length > 0 Then s = "WHERE " & strGVFilter
      If sqlDB.CountRows("SELECT strRecordID FROM dwfOnconPromo " & s) = 0 Then
         lblMessage.Text = "<div width='100%' align='center'>- No Oncon Promo Entries found -</div>"
      Else
            lblMessage.Text = ""
            gvExport.Visible = True

            Response.Clear()
         Response.Buffer = True
         Response.ContentType = "application/vnd.ms-excel"
         Response.Charset = ""
         Response.AddHeader("Content-Disposition",
          "attachment;filename=OnconPromo_" & Session.Item("User").Replace(" ", "") & "_" & Date.Today.ToString("yyyyMMdd") & ".xls") 'v1.4.5 - 20160913 - Fixed logged In user & savings (i.e use session instead) - maanie
         Me.EnableViewState = False

         Dim oStringWriter As New System.IO.StringWriter()
         Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
         Dim frm As HtmlForm = New HtmlForm()
         Me.Controls.Add(frm)
         frm.Controls.Add(gvExport)
         frm.RenderControl(oHtmlTextWriter)
         Response.Write(oStringWriter.ToString())

         gvExport.Visible = False

         Response.End()
      End If
   End Sub
End Class