Imports System.Data.SqlClient
Imports System.Data
Imports AppCommon

Partial Class mobile_recruit_ScheduleSP
    Inherits System.Web.UI.Page

    Private Const DATE_FMT As String = "yyyy/MM/dd"

    Private Enum COL_GRID
        C_企業CD = 0
        C_拠点CD = 1
        C_採用年度 = 2
        C_枝NO = 3
        C_実施日 = 4
        C_日付 = 5
        C_内容 = 6
    End Enum

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '---------------------------------------------------------------
        'HTMLタグを追加
        '---------------------------------------------------------------
        Dim ctl As Control = Page.FindControl("page_header")
        'ファビコン
        SetTagFavicon(ctl)

        If IsPostBack Then
            Exit Sub
        End If

        Call fncRefreshList(0)          'GRID更新
    End Sub

    Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu1.MenuItemClick
        Me.lblMessage.Text = ""
        With Me.Menu1
            Me.lnkCurrent.Text = "今" & .SelectedValue.ToString
            Call fncRefreshList(0)          'GRID更新
        End With
    End Sub

    Protected Sub btnクリア_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnクリア.Click
        Call clearItems()

        Call fncRefreshList(0, True)        '検索初期化
    End Sub

    Private Function fncRefreshList(ByVal intDirection As Integer, Optional ByVal blnInit As Boolean = False) As Boolean
        Dim strMode As String = String.Empty
        Dim strNewDate As String = String.Empty
        Dim intWeekNo As Integer = 0

        fncRefreshList = False

        Me.lbl表示期間.Text = ""

        '基準日がブランク、あるいは初期化指定があれば基準日として本日日付をセット
        If Me.hidDate.Value.ToString.Equals("") OrElse blnInit = True Then
            Me.hidDate.Value = Now.ToString(DATE_FMT)
        End If

        Select Case Me.Menu1.SelectedValue.ToString
            Case "年"
                strMode = "Y"
                strNewDate = DateAdd(DateInterval.Year, intDirection, Date.Parse(Me.hidDate.Value)).ToString(DATE_FMT)
                Me.lbl表示期間.Text = Year(Date.Parse(strNewDate)).ToString & "年の予定を表示しています。"

            Case "月"
                strMode = "M"
                strNewDate = DateAdd(DateInterval.Month, intDirection, Date.Parse(Me.hidDate.Value)).ToString(DATE_FMT)
                Me.lbl表示期間.Text = Date.Parse(strNewDate).ToString("yyyy年MM月") & "の予定を表示しています。"

            Case "週"
                strMode = "W"
                '日曜以外なら基準日を週初の日曜に変更
                intWeekNo = Weekday(Date.Parse(Me.hidDate.Value))
                If intWeekNo > 1 Then
                    Me.hidDate.Value = DateAdd(DateInterval.Day, (intWeekNo - 1) * -1, Date.Parse(Me.hidDate.Value)).ToString(DATE_FMT)
                End If
                strNewDate = DateAdd(DateInterval.WeekOfYear, intDirection, Date.Parse(Me.hidDate.Value)).ToString(DATE_FMT)
                Me.lbl表示期間.Text = Date.Parse(strNewDate).ToString("yyyy年MM月dd日") & "から1週間分の予定を表示しています。"

            Case "日"
                strMode = "D"
                strNewDate = DateAdd(DateInterval.Day, intDirection, Date.Parse(Me.hidDate.Value)).ToString(DATE_FMT)
                Me.lbl表示期間.Text = Date.Parse(strNewDate).ToString("yyyy年MM月dd日") & "の予定を表示しています。"
        End Select

        Me.hidDate.Value = strNewDate

        '検索実行
        Call fncSetSelectPrm(strMode, Year(strNewDate), Month(strNewDate), Day(strNewDate))

        fncRefreshList = True

    End Function

    Private Function fncSetSelectPrm(ByVal strMode As String, ByVal int年 As Integer, ByVal int月 As Integer, ByVal int日 As Integer) As Boolean

        fncSetSelectPrm = False

        Me.hidMODE.Value = strMode
        Me.hid年.Value = int年
        Me.hid月.Value = int月
        Me.hid日.Value = int日

        fncSetSelectPrm = True
    End Function

    Protected Sub btn検索_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn検索.Click
        Me.lbl表示期間.Text = ""

        If Me.txtDay.Text <> "" AndAlso IsDate(Me.txtDay.Text) = False Then
            Me.lblMessage.Text = "<br />正しい日付を入力してください。"
            Exit Sub
        End If

        With Me.Menu1
            If Me.txtDay.Text <> "" Then
                For intIdx As Integer = 0 To .Items.Count - 1
                    If .Items(intIdx).Value = "日" Then
                        .Items(intIdx).Selected = True
                        Me.lnkCurrent.Text = "今日"
                        Exit For
                    End If
                Next
                Me.hidDate.Value = Me.txtDay.Text

            ElseIf Me.ddl月.Text <> "" Then
                If Me.ddl年.Text = "" Then
                    Me.ddl年.SelectedValue = Year(Now).ToString
                End If

                For intIdx As Integer = 0 To .Items.Count - 1
                    If .Items(intIdx).Value = "月" Then
                        .Items(intIdx).Selected = True
                        Me.lnkCurrent.Text = "今月"
                        Exit For
                    End If
                Next
                Me.hidDate.Value = Format(DateSerial(Me.ddl年.SelectedValue, Me.ddl月.SelectedValue, 1), "yyyy/MM/dd")

            ElseIf Me.ddl年.Text <> "" Then
                For intIdx As Integer = 0 To .Items.Count - 1
                    If .Items(intIdx).Value = "年" Then
                        .Items(intIdx).Selected = True
                        Me.lnkCurrent.Text = "今年"
                        Exit For
                    End If
                Next
                Me.hidDate.Value = Format(DateSerial(Me.ddl年.SelectedValue, 1, 1), "yyyy/MM/dd")

            Else
                Me.hidDate.Value = ""

            End If
        End With

        Call fncRefreshList(0)          'GRID更新
    End Sub

    Protected Sub lnkNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkNext.Click
        Call clearItems()
        Call fncRefreshList(1)          'GRID更新
    End Sub

    Protected Sub lnkPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPrev.Click
        Call clearItems()
        Call fncRefreshList(-1)         'GRID更新
    End Sub

    Protected Sub lnkCurrent_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCurrent.Click
        Call clearItems()
        Call fncRefreshList(0, True)    '検索初期化
    End Sub

    Protected Sub grdSchedule_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdSchedule.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow OrElse e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(COL_GRID.C_企業CD).Visible = False      '会社CD
            e.Row.Cells(COL_GRID.C_拠点CD).Visible = False      '拠点CD
            e.Row.Cells(COL_GRID.C_採用年度).Visible = False    '求人年度
            e.Row.Cells(COL_GRID.C_枝NO).Visible = False        '枝NO
            e.Row.Cells(COL_GRID.C_実施日).Visible = False      '実施日
        End If

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim hyp内容 As HyperLink

            '内容
            hyp内容 = TryCast(e.Row.Cells(COL_GRID.C_内容).FindControl("hyp内容"), HyperLink)
            hyp内容.NavigateUrl = "JobInfoSP.aspx" &
                                    "?CD=" & e.Row.Cells(COL_GRID.C_企業CD).Text &
                                    "&CD2=" & e.Row.Cells(COL_GRID.C_拠点CD).Text &
                                    "&Year=" & e.Row.Cells(COL_GRID.C_採用年度).Text &
                                    "&No=" & e.Row.Cells(COL_GRID.C_枝NO).Text &
                                    "&Job=1"
        End If
    End Sub

    Protected Sub ddl年_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl年.Init
        Dim idx As Integer = 0
        With sender
            For idx = .Items.Count - 1 To 0 Step -1
                .Items.RemoveAt(idx)
            Next

            .Items.Add(New ListItem("", ""))

            For idx = -1 To 1
                Dim yyyy As Integer = Year(Now) + idx
                .Items.Add(New ListItem(yyyy.ToString & "年", yyyy.ToString))
            Next
        End With
    End Sub

    Private Sub clearItems()
        Me.ddl年.Text = ""
        Me.ddl月.Text = ""
        Me.txtDay.Text = ""
        Me.lblMessage.Text = ""
    End Sub

End Class
