Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DevExpress.Web
Imports DevExpress.Web.Data

Partial Public Class AllSent
    Inherits System.Web.UI.Page
    Dim sSQL As String = "SELECT MSG.Msg_Index AS [Index], MSG.Msg_Username AS [Created By], MSG.Msg_SiteNum AS [Site], LO.Lo_Name AS [Location], TE.Te_Name AS [Team], " &
    "MSG.Msg_Reason AS [Reason], MSG.Msg_Dt AS [Cr.Dt], MSG.Msg_Tm AS [Cr.Tm], MSG.Msg_Completed AS [Done], MSG.Msg_MedName AS [Medication], " &
    "CASE WHEN AU.Au_IndexMessage IS NULL THEN NULL ELSE " &
    "CONCAT(DATEDIFF(MINUTE, DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', MSG.Msg_Tm), CAST(MSG.Msg_Dt AS DATETIME)), DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', AU.Au_Tm),  CAST(AU.Au_Dt  AS DATETIME))) / 1440, 'D', " &
    "(DATEDIFF(MINUTE, DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', MSG.Msg_Tm), CAST(MSG.Msg_Dt AS DATETIME)), DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', AU.Au_Tm),  CAST(AU.Au_Dt  AS DATETIME))) % 1440) / 60, 'H', " &
    "DATEDIFF(MINUTE, DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', MSG.Msg_Tm), CAST(MSG.Msg_Dt AS DATETIME)), DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', AU.Au_Tm),  CAST(AU.Au_Dt  AS DATETIME))) % 60, 'M') END AS [Pr.Tm], " &
    "MSG.Msg_Assigned AS [Assignee] FROM tab_Messages MSG " &
    "LEFT JOIN tab_Locations LO On MSG.Msg_LocationIndex = LO.Lo_Index " &
    "LEFT JOIN tab_Teams TE On MSG.Msg_TeamIndex = TE.Te_Index " &
    "LEFT JOIN tab_Audit AU On MSG.Msg_Index = AU.Au_IndexMessage AND AU.Au_Action IN ( 'Completed' ) "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        'Dim totalSummary As ASPxSummaryItem = New ASPxSummaryItem()
        'totalSummary.FieldName = "index"
        'totalSummary.ShowInColumn = "index"
        'totalSummary.SummaryType = SummaryItemType.Average
        'ASPxGridView1.TotalSummary.Add(totalSummary)
        'GridViewFeaturesHelper.SetupGlobalGridViewBehavior(ASPxGridView2)
        If Not IsPostBack Then
            StartDate.Value = DateTime.Today.AddDays(-7).ToString("yyyy-MM-dd")
            ASPxLblStartDate.Text = StartDate.Value

            EndDate.Value = DateTime.Today.ToString("yyyy-MM-dd")
            ASPxLblEndDate.Text = EndDate.Value

            ASPxLblLogin.Text = HttpContext.Current.User.Identity.Name
            ASPxLblUser.Text = HttpContext.Current.User.Identity.Name

            cboMembers.AppendDataBoundItems = True
            cboMembers.Items.Insert(0, New ListItem("All Users", "All Users"))
            cboMembers.Items.Insert(1, New ListItem(HttpContext.Current.User.Identity.Name, HttpContext.Current.User.Identity.Name))
            cboMembers.SelectedIndex = 0
            cboMembers.DataBind()

            cboAssignees.AppendDataBoundItems = True
            cboAssignees.Items.Insert(0, New ListItem("All Assignees", "All Assignees"))
            cboAssignees.Items.Insert(1, New ListItem(HttpContext.Current.User.Identity.Name, HttpContext.Current.User.Identity.Name))
            cboAssignees.SelectedIndex = 0
            cboAssignees.DataBind()
        Else
            ASPxLblUser.Text = cboMembers.SelectedItem.Value
            ASPxLblAssignee.Text = cboAssignees.SelectedItem.Value

            ASPxLblAdm_Sites.Text = ASPxGridView2.GetRowValues(0, "Adm_Sites")
            ' Strip out spaces and single quotes
            ASPxLblAdm_Sites.Text = ASPxLblAdm_Sites.Text.Replace("'", "")
            ASPxLblAdm_Sites.Text = ASPxLblAdm_Sites.Text.Replace(" ", "")

            If IsDate(StartDate.Value) Then
                ASPxLblStartDate.Text = StartDate.Value
            Else
                StartDate.Value = DateTime.Today.ToString("yyyy-MM-dd")
                ASPxLblStartDate.Text = StartDate.Value
            End If

            If IsDate(EndDate.Value) Then
                ASPxLblEndDate.Text = EndDate.Value
            Else
                EndDate.Value = DateTime.Today.ToString("yyyy-MM-dd")
                ASPxLblEndDate.Text = EndDate.Value
            End If
            SqlDataSource1.SelectCommand = sSQL & " WHERE (MSG.Msg_Dt IS NOT NULL OR AU.Au_Action IS NULL OR AU.Au_Action = 'Completed') AND ([Msg_Username] = @Username AND [Msg_Dt] BETWEEN @Start_Date AND @End_Date) ORDER BY MSG.Msg_Index DESC"
            If cboMembers.SelectedItem.Value = "All Users" And cboAssignees.SelectedItem.Value = "All Assignees" Then
                SqlDataSource1.SelectCommand = sSQL & "WHERE (MSG.Msg_Dt IS NOT NULL OR AU.Au_Action IS NULL OR AU.Au_Action = 'Completed') AND ([Msg_Dt] BETWEEN @Start_Date And @End_Date) And Msg_SiteNum IN(Select * FROM STRING_SPLIT(@Adm_Sites,',')) ORDER BY MSG.Msg_Index DESC"
            ElseIf cboMembers.SelectedItem.Value <> "All Users" And cboAssignees.SelectedItem.Value <> "All Assignees" Then
                SqlDataSource1.SelectCommand = sSQL & "WHERE (MSG.Msg_Dt IS NOT NULL OR AU.Au_Action IS NULL OR AU.Au_Action = 'Completed') AND ([Msg_Username] = @Username AND [Msg_Dt] BETWEEN @Start_Date AND @End_Date) AND Msg_SiteNum IN(SELECT * FROM STRING_SPLIT(@Adm_Sites,',')) AND [Msg_Assigned] = @Assignee ORDER BY MSG.Msg_Index DESC"
            ElseIf cboMembers.SelectedItem.Value <> "All Users" And cboAssignees.SelectedItem.Value = "All Assignees" Then
                SqlDataSource1.SelectCommand = sSQL & "WHERE (MSG.Msg_Dt IS NOT NULL OR AU.Au_Action IS NULL OR AU.Au_Action = 'Completed') AND ([Msg_Username] = @Username AND [Msg_Dt] BETWEEN @Start_Date AND @End_Date) AND Msg_SiteNum IN(SELECT * FROM STRING_SPLIT(@Adm_Sites,',')) ORDER BY MSG.Msg_Index DESC"
            ElseIf cboMembers.SelectedItem.Value = "All Users" And cboAssignees.SelectedItem.Value <> "All Assignees" Then
                SqlDataSource1.SelectCommand = sSQL & "WHERE (MSG.Msg_Dt IS NOT NULL OR AU.Au_Action IS NULL OR AU.Au_Action = 'Completed') AND ([Msg_Assigned] = @Assignee AND [Msg_Dt] BETWEEN @Start_Date AND @End_Date) AND Msg_SiteNum IN(SELECT * FROM STRING_SPLIT(@Adm_Sites,',')) ORDER BY MSG.Msg_Index DESC"
            End If
        End If
            'ASPxGridView2.ExportToXlsx()
            ASPxGridView2.DataBind()
        If ASPxGridView2.VisibleRowCount = 0 Then
            Response.Redirect("403.html")
        End If

        ASPxGridView1.DataBind()

        ASPxWebControl.BackwardCompatibility.DataControlAllowReadUnlistedFieldsFromClientApiDefaultValue = True
    End Sub
    'Protected Function GetGroupSummaryValue(ByVal container As GridViewGroupFooterCellTemplateContainer) As Object
    '    Dim summaryItem = ASPxGridView1.GroupSummary.First(Function(i) i.Tag = "GroupMessages")
    '    Return ASPxGridView1.GetGroupSummaryValue(container.VisibleIndex, summaryItem)
    'End Function
    'Protected Function GetTotalSummaryValue() As Object
    '    Dim summaryItem = ASPxGridView1.TotalSummary.First(Function(i) i.Tag = "TotalMessages")
    '    Return ASPxGridView1.GetTotalSummaryValue(summaryItem)
    'End Function
    Protected Sub grid_BeforeExport(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridBeforeExportEventArgs)
        ASPxLblStartDate.Text = StartDate.Value.Substring(5, 2) + "/" + StartDate.Value.Substring(8, 2) + "/" + StartDate.Value.Substring(0, 4)
        ASPxLblEndDate.Text = EndDate.Value.Substring(5, 2) + "/" + EndDate.Value.Substring(8, 2) + "/" + EndDate.Value.Substring(0, 4)
        'ASPxGridView1.Columns("Created By").Visible = False
        ASPxGridView1.SettingsExport.ReportHeader = "All Messages Created By: " + cboMembers.SelectedItem.Value + " and Assigned To: " + cboAssignees.SelectedItem.Value + " from " + ASPxLblStartDate.Text + " to " + ASPxLblEndDate.Text
        ASPxGridView1.SettingsExport.ReportFooter = "Report Generated By: " + HttpContext.Current.User.Identity.Name + " on " + Date.Now.ToString()
    End Sub
    Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHome.Click
        Try
            Context.Response.Redirect("~/", False)
        Catch ex As Exception
            ' handle error here 
        End Try
    End Sub
    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        Try
            'Response.Redirect(Request.Url.AbsoluteUri)
            ASPxGridView1.DataBind()
        Catch ex As Exception
            ' handle error here 
        End Try
    End Sub
    Private Sub ASPxGridView1_DataBinding(sender As Object, e As EventArgs) Handles ASPxGridView1.DataBinding

    End Sub

    'Protected Sub ASPxGridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    '    Dim strTemp As String = e.Row.Cells(8).Text
    '    strTemp = strTemp.Replace("12:00:00 AM", "")
    '    e.Row.Cells(8).Text = strTemp
    '    'e.Row.Cells(8).Text = Convert.ToDateTime(strTemp).ToString("MM/dd/yyyy")
    'End Sub
End Class