Imports System.Data
Imports AppCommon
Imports AppVariable

Partial Class Faculty_TimeTable
    Inherits System.Web.UI.Page

#Region "********************** メンバ変数 **********************"

    Private _createdLabel1 As String    '1行目
    Private _createdLabel2 As String    '2行目

#End Region

#Region "********************** プロパティ **********************"

    Protected Property TargetBeginDate As DateTime
        Get
            Return If(ViewState("TargetBeginDate") Is Nothing, New DateTime(Now.Year, Now.Month, 1), DirectCast(ViewState("TargetBeginDate"), DateTime))
        End Get
        Set(value As DateTime)
            ViewState("TargetBeginDate") = value
        End Set
    End Property

#End Region

#Region "******************** ページイベント ********************"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '---------------------------------------------------------------
        'HTMLタグを追加
        '---------------------------------------------------------------
        Dim ctl As Control = Page.Master.FindControl("msp_header")
        'CSS
        SetTagCss(ctl, "timetable.css")

        CreateCalendar(TargetBeginDate)
    End Sub

#End Region

#Region "***************** コントロールイベント *****************"

    Protected Sub btn_this_month_Click(sender As Object, e As EventArgs) Handles btn_this_month.Click
        TargetBeginDate = New DateTime(Now.Year, Now.Month, 1)
        CreateCalendar(TargetBeginDate)
    End Sub

    Protected Sub btn_prev_month_Click(sender As Object, e As ImageClickEventArgs) Handles btn_prev_month.Click
        TargetBeginDate = DateAdd(DateInterval.Month, -1, TargetBeginDate)
        CreateCalendar(TargetBeginDate)
    End Sub

    Protected Sub btn_next_month_Click(sender As Object, e As ImageClickEventArgs) Handles btn_next_month.Click
        TargetBeginDate = DateAdd(DateInterval.Month, 1, TargetBeginDate)
        CreateCalendar(TargetBeginDate)
    End Sub

    'Protected Sub lv_calendar_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles lv_calendar.ItemDataBound
    '    If e.Item.ItemType = ListViewItemType.DataItem Then
    '        If Not e.Item.FindControl("hoge") Is Nothing Then
    '            CType(e.Item.FindControl("hoge"), HtmlControl).Attributes.Add("style", "background-color:#000000;")
    '        End If
    '    End If
    'End Sub

#End Region

#Region "******************* Sub及びFunction ********************"

    Private Sub CreateCalendar(ByVal targetBeginDate As DateTime)
        Dim targetEndDate As DateTime = DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Month, 1, targetBeginDate))
        Dim beginDate As DateTime = DateAdd(DateInterval.Day, 1 - DatePart(DateInterval.Weekday, targetBeginDate), targetBeginDate)
        Dim endDate As DateTime = DateAdd(DateInterval.Day, 7 - DatePart(DateInterval.Weekday, targetEndDate), targetEndDate)

        '日付の生成, バインド
        Dim datesOfMonth _
            = Enumerable.Range(0, DateDiff(DateInterval.Day, beginDate, endDate) + 1).Select(Function(x) DateAdd(DateInterval.Day, x, beginDate)).ToList()
        lv_calendar.DataSource = datesOfMonth
        lv_calendar.DataBind()

        '日付毎の予定を取得, バインド
        Using spm As New StoredProcedureManager("時間割_授業_SELECT")
            spm.AddPrmIn("@教員CD", SqlDbType.VarChar, Session(S_COM_UserCD))
            spm.AddPrmIn("@対象日_開始", SqlDbType.DateTime, beginDate)
            spm.AddPrmIn("@対象日_終了", SqlDbType.DateTime, endDate)
            spm.Fill()

            For Each item As ListViewItem In lv_calendar.Items
                Using lv = DirectCast(item.FindControl("lv_schedule"), ListView)
                    lv.DataSource = spm.ds.Tables(0).AsEnumerable().Where(Function(row) row("日付") = datesOfMonth(item.DataItemIndex)).ToList()
                    lv.DataBind()
                End Using
            Next
        End Using
    End Sub

    Protected Function CreateLabel1(ByVal row As DataRow) As String
        Dim sb As New StringBuilder()

        sb.Append(row("時限").ToString())
        If sb.Length > 0 Then sb.Append("限:")

        sb.Append(row("講座名").ToString())

        _createdLabel1 = sb.ToString()
        Return _createdLabel1
    End Function

    Protected Function CreateLabel2(ByVal row As DataRow) As String
        Dim sb As New StringBuilder()

        If row("教室名").ToString().Length > 0 Then
            sb.Append("　　")
            sb.Append(row("教室名").ToString())
        End If

        _createdLabel2 = sb.ToString()
        Return _createdLabel2
    End Function

    Protected Function GetCreatedLabel() As String
        Dim sb As New StringBuilder()

        sb.Append(_createdLabel1)
        If _createdLabel2.Length > 0 Then
            sb.Append("<br />")
            sb.Append(_createdLabel2)
        End If

        Return sb.ToString
    End Function

#End Region

End Class
