Imports System.Data
Imports AppVariable
Imports AppCommon

Partial Class uc_SearchVisit
    Inherits System.Web.UI.UserControl

    Private _thisYear As Integer
    Private _searchParam As SearchVisitParam
    Private _grvIndex As SearchVisitParam

#Region "************************* 定数 *************************"

#End Region

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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            SetDropDown訪問担当(ddl担当)
            SetDropDown学校訪問(ddl分類CD)
            SetDropDown都道府県(ddl県CD)

            Dim ret As String = Com_Get_変数("西暦今年")
            Dim nowDate As DateTime = Now
            If Not (Int32.TryParse(ret, _thisYear)) Then
                '※西暦今年が変数に入っていないことはないと思うが、なかった場合は今年度(4月-3月)の年を入れておく
                _thisYear = nowDate.Year
                If nowDate.Month = 3 Or nowDate.Month = 2 Or nowDate.Month = 1 Then
                    _thisYear -= 1
                End If
            End If

            If Session(S_Visit_Search_Param) IsNot Nothing Then
                _searchParam = CType(Session(S_Visit_Search_Param), SearchVisitParam)

                ddl閲覧ログ.SelectedValue = _searchParam.閲覧ログ
                txt訪問日From.Text = _searchParam.訪問日From
                txt訪問日To.Text = _searchParam.訪問日To
                ddl区分.SelectedValue = _searchParam.区分
                ddl担当.SelectedValue = _searchParam.担当
                ddl指定校.SelectedValue = _searchParam.指定校
                txt学校名.Text = _searchParam.学校名
                ddl県CD.SelectedValue = _searchParam.県CD
                ddl分類CD.SelectedValue = _searchParam.分類CD
            Else
                _searchParam = New SearchVisitParam
                _searchParam.閲覧ログ = ddl閲覧ログ.SelectedValue
                _searchParam.訪問日From = _thisYear.ToString & "/04/01"
                _searchParam.訪問日To = nowDate.ToString("yyyy/MM/dd")
                Session(S_Visit_Search_Param) = _searchParam

                txt訪問日From.Text = _searchParam.訪問日From
                txt訪問日To.Text = _searchParam.訪問日To
            End If

            If Session(S_Visit_Grv_SelectedIndex) IsNot Nothing Then
                _grvIndex = CType(Session(S_Visit_Grv_SelectedIndex), SearchVisitParam)

                gv訪問履歴.SelectedIndex = _grvIndex.SelectedIndex
                gv訪問履歴.PageIndex = _grvIndex.PageIndex
            Else
                gv訪問履歴.SelectedIndex = -1
                gv訪問履歴.PageIndex = 0
            End If

            Set訪問履歴()
        End If
    End Sub

#End Region

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

    Protected Sub btn_検索_Click(sender As Object, e As EventArgs) Handles btn_検索.Click
        'ページと選択行の位置を初期化
        gv訪問履歴.PageIndex = 0

        Session.Remove(S_Visit_Grv_SelectedIndex)
        Session.Remove(S_Visit_Search_Param)
        Session.Remove(S_Visit_Selected_History)

        Dim params As New SearchVisitParam
        '検索条件をセッションに保存
        params.閲覧ログ = ddl閲覧ログ.SelectedValue
        params.訪問日From = txt訪問日From.Text
        params.訪問日To = txt訪問日To.Text
        params.区分 = ddl区分.SelectedValue
        params.担当 = ddl担当.SelectedValue
        params.指定校 = ddl指定校.SelectedValue
        params.学校名 = txt学校名.Text
        params.県CD = ddl県CD.SelectedValue
        params.分類CD = ddl分類CD.SelectedValue

        Session(S_Visit_Search_Param) = params

        Response.Redirect(Request.RawUrl.ToString)
    End Sub

    Protected Sub btn_検索クリア_Click(sender As Object, e As EventArgs) Handles btn_検索クリア.Click
        'ページと選択行の位置を初期化
        gv訪問履歴.PageIndex = 0

        Session.Remove(S_Visit_Grv_SelectedIndex)
        Session.Remove(S_Visit_Search_Param)
        Session.Remove(S_Visit_Selected_History)

        Response.Redirect(Request.RawUrl.ToString)
    End Sub

    Protected Sub gv訪問履歴_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gv訪問履歴.SelectedIndexChanged
        '検索リストから選択された訪問履歴を保持
        Dim params As New VisitParam
        params.分類CD = CType(gv訪問履歴.SelectedRow.FindControl("lbl分類CD"), Label).Text
        params.学校CD = CType(gv訪問履歴.SelectedRow.FindControl("lbl学校CD"), Label).Text
        params.担当CD = CType(gv訪問履歴.SelectedRow.FindControl("lbl担当CD"), Label).Text
        params.訪問日 = CType(gv訪問履歴.SelectedRow.FindControl("lbl訪問日"), Label).Text
        Session(S_Visit_Selected_History) = params

        'ページの位置を保持
        '選択行の位置を保持
        If _grvIndex Is Nothing Then
            _grvIndex = New SearchVisitParam
        End If
        _grvIndex.SelectedIndex = gv訪問履歴.SelectedIndex
        _grvIndex.PageIndex = gv訪問履歴.PageIndex

        Session(S_Visit_Grv_SelectedIndex) = _grvIndex

        Response.Redirect(Request.RawUrl.ToString)
    End Sub

    Protected Sub gv訪問履歴_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv訪問履歴.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            'リストクリック時選択状態にする
            Dim instance As ClientScriptManager = Page.ClientScript
            Dim button As LinkButton = CType(e.Row.FindControl("lbtn_選択"), LinkButton)
            e.Row.Attributes("onclick") = instance.GetPostBackClientHyperlink(button, "")
        End If
    End Sub

    Protected Sub gv訪問履歴_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gv訪問履歴.PageIndexChanging
        '選択行を解除
        gv訪問履歴.SelectedIndex = -1
        'ページを移動
        gv訪問履歴.PageIndex = e.NewPageIndex

        'ページの位置を保持
        '選択行の位置を保持
        If _grvIndex Is Nothing Then
            _grvIndex = New SearchVisitParam
        End If
        _grvIndex.SelectedIndex = -1
        _grvIndex.PageIndex = gv訪問履歴.PageIndex

        Session(S_Visit_Grv_SelectedIndex) = _grvIndex

        Set訪問履歴()
    End Sub
#End Region

#Region "******************* Sub及びFunction ********************"
    Private Sub Set訪問履歴()
        WebControlsUtil.DeclareSP(sds訪問履歴, "学校_訪問履歴リスト_SELECT")
        
        Dim params = CType(Session(S_Visit_Search_Param), SearchVisitParam)

        With sds訪問履歴
            .SelectParameters.Add("未読", params.閲覧ログ)
            .SelectParameters.Add("日付01", params.訪問日From)
            .SelectParameters.Add("日付02", params.訪問日To)
            .SelectParameters.Add("区分", params.区分)
            .SelectParameters.Add("担当CD", params.担当)
            .SelectParameters.Add("学校名", params.学校名)
            .SelectParameters.Add("利用者", Convert.ToString(Session(S_COM_UserCD)))
            .SelectParameters.Add("指定校", params.指定校)
            .SelectParameters.Add("県CD", params.県CD)
            .SelectParameters.Add("分類CD", params.分類CD)
            .SelectParameters.Add("新着", "0")
        End With

        gv訪問履歴.DataBind()
    End Sub
#End Region


End Class


