Imports System.Data
Imports AppCommon
Imports AppVariable
Imports AppVariable_Kyujin

Partial Class mobile_recruit_JobSP
    Inherits System.Web.UI.Page

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

#End Region

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

    Private m_strPath As String = Com_Get_変数("PT_COMMON_DIRECTORY")
    Private m_strPdf As String = Com_Get_変数("PT_JOB_PDF_DIRECTORY")
    Private m_strExtension As String = Com_Get_変数("PT_JOB_PDF_EXTENSION")
    Private m_strContextType As String = Com_Get_変数("PT_JOB_PDF_CONTENTTYPE")

#End Region

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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            DataBind_ddl_求人年度検索()
            DataBind_ddl_業種CD検索()
            DataBind_lb_職種CD検索()
            DataBind_lb_勤務地検索()
            DataBind_ddl_休日検索()
            DataBind_ddl_募集対象()
            DataBind_ddl_SORT()

            '求人年度と並び順以外の検索条件を復元
            ddl_業種CD検索.SelectedValue = Session(S_Kyujin_Search_業種CD)
            txt_企業名検索.Text = Session(S_Kyujin_Search_企業名)
            cb_新着検索.Checked = Session(S_Kyujin_Search_新着)
            txt_給与検索.Text = Session(S_Kyujin_Search_給与)
            cb_加入保険検索_健康保険.Checked = Session(S_Kyujin_Search_健康保険)
            cb_加入保険検索_厚生年金.Checked = Session(S_Kyujin_Search_厚生年金)
            cb_加入保険検索_労災保険.Checked = Session(S_Kyujin_Search_労災保険)
            cb_加入保険検索_雇用保険.Checked = Session(S_Kyujin_Search_雇用保険)
            cb_寮検索.Checked = Session(S_Kyujin_Search_寮)
            ddl_休日検索.SelectedValue = Session(S_Kyujin_Search_休日)
            ddl_募集対象.SelectedValue = Session(S_Kyujin_Search_新既)

            '職種
            If Not Session(S_Kyujin_Search_職種CD) Is Nothing And Session(S_Kyujin_Search_職種CD) <> "" Then
                Dim strArray職種 As String() = Session(S_Kyujin_Search_職種CD).ToString.Split(",")
                For Each strCD In strArray職種
                    strCD = strCD.Substring(1, Len(strCD) - 2)  '前後の[']を除去

                    For Each obj In lb_職種CD検索.Items()
                        If obj.value = strCD Then
                            obj.selected = True
                        End If
                    Next
                Next
            End If

            '勤務地
            If Not Session(S_Kyujin_Search_勤務地) Is Nothing And Session(S_Kyujin_Search_勤務地) <> "" Then
                Dim strArray勤務地 As String() = Session(S_Kyujin_Search_勤務地).ToString.Split(",")
                For Each strCD In strArray勤務地
                    strCD = strCD.Substring(1, Len(strCD) - 2)  '前後の[']を除去

                    For Each obj In lb_勤務地検索.Items()
                        If obj.value = strCD Then
                            obj.selected = True
                        End If
                    Next
                Next
            End If

        End If

        'エラーメッセージをクリア
        lbl_Error.Text = ""
    End Sub

#End Region

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

    Protected Sub sds_求人一覧_Init(sender As Object, e As EventArgs) Handles sds_求人一覧.Init
        sds_求人一覧_select()
    End Sub

    Protected Sub ddl_求人年度検索_DataBound(sender As Object, e As EventArgs) Handles ddl_求人年度検索.DataBound
        '求人年度検索条件をセット
        Dim strSession求人年度 As String = Session(S_Kyujin_Search_求人年度)
        If ddl_求人年度検索.Items.FindByValue(strSession求人年度) Is Nothing Then
            ddl_求人年度検索.Items.Add(New ListItem(strSession求人年度, strSession求人年度))
        End If

        ddl_求人年度検索.Text = Session(S_Kyujin_Search_求人年度)
    End Sub

    Protected Sub ddl_SORT_DataBound(sender As Object, e As EventArgs) Handles ddl_SORT.DataBound
        '並び順検索条件をセット
        ddl_SORT.Text = Session(S_Kyujin_Search_SORT)
    End Sub


    Protected Sub ddl_SORT_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_SORT.SelectedIndexChanged

        Session(S_Kyujin_Search_SORT) = ddl_SORT.SelectedValue

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

    Protected Sub btn_検索_Click(sender As Object, e As EventArgs) Handles btn_検索.Click
        'ページインデックスを初期化
        Session(S_Kyujin_ListPager_StartRowIndex) = "0"

        '----検索条件をセッションに保存----        
        '職種の選択肢をカンマ区切りに加工
        Dim str職種検索 As String = ""
        For Each obj In lb_職種CD検索.Items()
            If obj.Selected Then
                str職種検索 = str職種検索 & ",'" & obj.Value() & "'"
            End If
        Next
        If str職種検索 <> "" Then
            Me.hf_職種検索.Value = Mid(str職種検索, 2, Len(str職種検索) - 1)
        Else
            Me.hf_職種検索.Value = ""
        End If

        '勤務地の選択肢をカンマ区切りに加工
        Dim str勤務地検索 As String = ""
        For Each obj In lb_勤務地検索.Items()
            If obj.Selected Then
                str勤務地検索 = str勤務地検索 & ",'" & obj.Value() & "'"
            End If
        Next
        If str勤務地検索 <> "" Then
            hf_勤務地検索.Value = Mid(str勤務地検索, 2, Len(str勤務地検索) - 1)
        Else
            hf_勤務地検索.Value = ""
        End If

        Session(S_Kyujin_Search_求人年度) = ddl_求人年度検索.SelectedValue
        Session(S_Kyujin_Search_業種CD) = ddl_業種CD検索.SelectedValue
        Session(S_Kyujin_Search_職種CD) = hf_職種検索.Value
        Session(S_Kyujin_Search_企業名) = txt_企業名検索.Text
        Session(S_Kyujin_Search_新着) = cb_新着検索.Checked
        Session(S_Kyujin_Search_勤務地) = hf_勤務地検索.Value
        Session(S_Kyujin_Search_給与) = txt_給与検索.Text
        Session(S_Kyujin_Search_健康保険) = cb_加入保険検索_健康保険.Checked
        Session(S_Kyujin_Search_厚生年金) = cb_加入保険検索_厚生年金.Checked
        Session(S_Kyujin_Search_労災保険) = cb_加入保険検索_労災保険.Checked
        Session(S_Kyujin_Search_雇用保険) = cb_加入保険検索_雇用保険.Checked
        Session(S_Kyujin_Search_寮) = cb_寮検索.Checked
        Session(S_Kyujin_Search_休日) = ddl_休日検索.SelectedValue
        Session(S_Kyujin_Search_新既) = ddl_募集対象.SelectedValue
        Session(S_Kyujin_Search_SORT) = ddl_SORT.SelectedValue
        '------------------------   

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

    Protected Sub btn_クリア_Click(sender As Object, e As EventArgs) Handles btn_クリア.Click
        'ページインデックスを初期化
        Session(S_Kyujin_ListPager_StartRowIndex) = "0"

        'セッションクリア
        Session.Remove(S_Kyujin_Search_求人年度)
        Session.Remove(S_Kyujin_Search_業種CD)
        Session.Remove(S_Kyujin_Search_職種CD)
        Session.Remove(S_Kyujin_Search_企業名)
        Session.Remove(S_Kyujin_Search_新着)
        Session.Remove(S_Kyujin_Search_勤務地)
        Session.Remove(S_Kyujin_Search_給与)
        Session.Remove(S_Kyujin_Search_健康保険)
        Session.Remove(S_Kyujin_Search_厚生年金)
        Session.Remove(S_Kyujin_Search_労災保険)
        Session.Remove(S_Kyujin_Search_雇用保険)
        Session.Remove(S_Kyujin_Search_寮)
        Session.Remove(S_Kyujin_Search_休日)
        Session.Remove(S_Kyujin_Search_新既)
        Session.Remove(S_Kyujin_Search_SORT)

        Response.Redirect(Request.RawUrl.ToString)

    End Sub

    Protected Sub lsv_求人一覧_PagePropertiesChanging(sender As Object, e As PagePropertiesChangingEventArgs) Handles lsv_求人一覧.PagePropertiesChanging

        Session(S_Kyujin_ListPager_StartRowIndex) = e.StartRowIndex

    End Sub

    Protected Sub lsv_求人一覧_DataBound(sender As Object, e As EventArgs) Handles lsv_求人一覧.DataBound
        If lsv_求人一覧.Items.Count > 0 Then

            Dim pager As DataPager = CType(lsv_求人一覧.FindControl("pager"), DataPager)

            If CInt(Session(S_Kyujin_ListPager_StartRowIndex)) > pager.TotalRowCount - 1 Then
                Session(S_Kyujin_ListPager_StartRowIndex) = "0"
            End If
            pager.SetPageProperties(Session(S_Kyujin_ListPager_StartRowIndex), pager.PageSize, True)

        End If
    End Sub

    Protected Sub lsv_求人一覧_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles lsv_求人一覧.ItemDataBound

        '[求人票]PDF
        Dim pathPDF As String = m_strPdf & Server.HtmlDecode(CType(e.Item.FindControl("lbl_求人年度"), Label).Text) & "\" & Server.HtmlDecode(CType(e.Item.FindControl("lbl_PDF名"), Label).Text) & ".pdf"
        Dim btnPDF As Button = CType(e.Item.FindControl("btn_求人票_PDF"), Button)
        If System.IO.File.Exists(m_strPath + pathPDF) Then
            btnPDF.Visible = True
        Else
            btnPDF.Visible = False
        End If

        '企業名
        Dim hl企業名 As HyperLink
        Dim lbl企業名 As Label
        hl企業名 = CType(e.Item.FindControl("link_企業名"), HyperLink)
        lbl企業名 = CType(e.Item.FindControl("lbl_企業名"), Label)
        If (hl企業名.NavigateUrl.IndexOf("http://") > -1 AndAlso Len(hl企業名.NavigateUrl) > 7) _
                OrElse (hl企業名.NavigateUrl.IndexOf("https://") > -1 AndAlso Len(hl企業名.NavigateUrl) > 8) Then
            lbl企業名.Visible = False
            hl企業名.Visible = True
        Else
            lbl企業名.Visible = True
            hl企業名.Visible = False
        End If

        '給与
        Dim strPay As String
        strPay = ""
        Dim intPay As Integer
        intPay = 0

        strPay = CType(e.Item.FindControl("lbl_給与"), Label).Text
        If Integer.TryParse(strPay, intPay) Then
            strPay = CType((intPay / 10000), String)
        End If
        CType(e.Item.FindControl("lbl_給与"), Label).Text = strPay + "万円"
    End Sub

    Protected Sub lsv_求人一覧_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles lsv_求人一覧.ItemCommand

        Dim strCompanyCd As String = CType(e.Item.FindControl("lbl_企業CD"), Label).Text
        Dim strBaseCd As String = CType(e.Item.FindControl("lbl_拠点CD"), Label).Text
        Dim strOfferYear As String = CType(e.Item.FindControl("lbl_求人年度"), Label).Text
        Dim strBranchNo As String = CType(e.Item.FindControl("lbl_枝NO"), Label).Text
        Dim strPDF名 As String = CType(e.Item.FindControl("lbl_PDF名"), Label).Text
        Dim str企業名 As String = CType(e.Item.FindControl("lbl_企業名"), Label).Text

        If e.CommandName = "求人票_Web" Then
            Dim strDetailLink As String = "JobInfoSP.aspx"
            strDetailLink = strDetailLink + "?CD=" & strCompanyCd & "&CD2=" & strBaseCd & "&Year=" & strOfferYear & "&No=" & strBranchNo & "&Job=1"
            Dim cs As ClientScriptManager = Page.ClientScript
            cs.RegisterClientScriptBlock(Me.GetType(), "求人票_Web", "<script>window.open('" + strDetailLink + "','_blank');</script>", False)

        ElseIf e.CommandName = "求人票_PDF" Then
            Dim path As String = m_strPdf & Server.HtmlDecode(strOfferYear) & "\" & Server.HtmlDecode(strPDF名) & ".pdf"

            If System.IO.File.Exists(m_strPath & path) Then
                '相対パスから物理ファイルパス取得
                Dim FilePath As String = m_strPath & path

                'ファイル名
                Dim fileName As String = "求人票_" & str企業名 & "_" & strOfferYear & m_strExtension

                'ダウンロードするファイル名
                Dim dlFileName As String

                'ファイル名が日本語の場合を考慮したダウンロードファイル名を作成
                If Request.Browser.Browser = "IE" OrElse Request.Browser.Browser = "Mozilla" Then
                    'IEの場合はファイル名をURLエンコード
                    dlFileName = HttpUtility.UrlEncode(fileName)
                Else
                    'IE以外の場合はそのままでOK
                    dlFileName = fileName
                End If

                'ダウンロード処理

                'Response情報クリア
                Response.ClearContent()

                'バッファリング
                Response.Buffer = True

                'HTTPヘッダー情報・MIMEタイプ設定
                Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", dlFileName))

                '2020/8/19 MOD START スマホ対応 /Liu
                ''--スマホでダウンロードすると直接ページ遷移になり、ポータルサイトの戻る機能が無効のため、再度ログインしないといけません
                'Response.ContentType = m_strContextType
                Response.ContentType = "application/octet-stream"
                '2020/8/19 MOD END /Liu

                'ファイルを書き出し
                Response.WriteFile(FilePath)
                Response.Flush()
                Response.End()
            Else
                'エラーメッセージを表示
                Me.lbl_Error.Text = "PDFファイルのダウンロードに失敗しました。"
                Me.lbl_Error.ForeColor = Drawing.Color.Red
            End If
        End If
    End Sub
    
#End Region

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

    ''' <summary>
    ''' SqlDataSourceパラメータ設定
    ''' </summary>
    ''' <remarks></remarks>
    Protected Sub sds_求人一覧_select()

        'DB接続文字列取得
        Dim conStr As String = System.Configuration.ConfigurationManager.ConnectionStrings(DB_strCon).ConnectionString

        '求人年度
        If Session(S_Kyujin_Search_求人年度) Is Nothing Then
            Dim selectYear As Integer

            '求人年度の初期値を決定（1-3月は求人年度＝昨年、それ以外は求人年度=今年）
            If Month(Now()) >= 1 And Month(Now()) <= 3 Then
                selectYear = Year(Now) - 1
            Else
                selectYear = Year(Now)
            End If

            Session(S_Kyujin_Search_求人年度) = selectYear
        End If

        '並び順
        If Session(S_Kyujin_Search_SORT) Is Nothing Then
            Session(S_Kyujin_Search_SORT) = "登録日 DESC"
        End If

        With sds_求人一覧

            'パラメータをクリア
            .SelectParameters.Clear()

            'ストアド設定
            .SelectCommand = "dbo.SP求人検索"
            .SelectCommandType = SqlDataSourceCommandType.StoredProcedure
            .CancelSelectOnNullParameter = False
            .ConnectionString = conStr

            'パラメータ追加            
            .SelectParameters.Add("求人年度", Session(S_Kyujin_Search_求人年度))
            .SelectParameters.Add("業種CD", Session(S_Kyujin_Search_業種CD))
            .SelectParameters.Add("職種CD", Session(S_Kyujin_Search_職種CD))
            .SelectParameters.Add("企業名", Session(S_Kyujin_Search_企業名))
            .SelectParameters.Add("新着", Session(S_Kyujin_Search_新着))
            .SelectParameters.Add("勤務地", Session(S_Kyujin_Search_勤務地))
            .SelectParameters.Add("給与", Session(S_Kyujin_Search_給与))
            .SelectParameters.Add("加入保険_健康保険", Session(S_Kyujin_Search_健康保険))
            .SelectParameters.Add("加入保険_厚生年金", Session(S_Kyujin_Search_厚生年金))
            .SelectParameters.Add("加入保険_労災保険", Session(S_Kyujin_Search_労災保険))
            .SelectParameters.Add("加入保険_雇用保険", Session(S_Kyujin_Search_雇用保険))
            .SelectParameters.Add("寮", Session(S_Kyujin_Search_寮))
            .SelectParameters.Add("休日", Session(S_Kyujin_Search_休日))
            .SelectParameters.Add("新既", Session(S_Kyujin_Search_新既))
            .SelectParameters.Add("SORT", Session(S_Kyujin_Search_SORT))
        End With

    End Sub

    Private Sub DataBind_ddl_求人年度検索()
        Using spm As New StoredProcedureManager("SPCMB求人年度")
            spm.Fill()

            With ddl_求人年度検索
                .DataValueField = "求人年度"
                .DataTextField = "求人年度文字列"
                .DataSource = spm.ds.Tables(0)
                .DataBind()
            End With
        End Using
    End Sub

    Private Sub DataBind_ddl_業種CD検索()
        Using spm As New StoredProcedureManager("SPCMB業種")
            spm.Fill()

            With ddl_業種CD検索
                .DataValueField = "業種CD"
                .DataTextField = "名前"
                .DataSource = spm.ds.Tables(0)
                .DataBind()
            End With
        End Using
    End Sub

    Private Sub DataBind_lb_職種CD検索()
        Using spm As New StoredProcedureManager("SPCMB職種")
            spm.Fill()

            With lb_職種CD検索
                .DataValueField = "職種CD"
                .DataTextField = "名前"
                .DataSource = spm.ds.Tables(0)
                .DataBind()
            End With
        End Using
    End Sub

    Private Sub DataBind_lb_勤務地検索()
        Using spm As New StoredProcedureManager("SPCMB勤務地")
            spm.Fill()

            With lb_勤務地検索
                .DataValueField = "勤務CD"
                .DataTextField = "名前"
                .DataSource = spm.ds.Tables(0)
                .DataBind()
            End With
        End Using
    End Sub

    Private Sub DataBind_ddl_休日検索()
        Using spm As New StoredProcedureManager("SPCMB休日")
            spm.Fill()

            With ddl_休日検索
                .DataValueField = "休日"
                .DataTextField = "休日表示"
                .DataSource = spm.ds.Tables(0)
                .DataBind()
            End With
        End Using
    End Sub

    Private Sub DataBind_ddl_募集対象()
        Using spm As New StoredProcedureManager("共通_変数_DDL")
            spm.AddPrmIn("@変数", SqlDbType.VarChar, "就職新既")
            spm.Fill()

            With ddl_募集対象
                .DataValueField = "内容"
                .DataTextField = "内容"
                .DataSource = spm.ds.Tables(0)
                .DataBind()
            End With
        End Using
    End Sub

    Private Sub DataBind_ddl_SORT()
        Using spm As New StoredProcedureManager("SPCMB求人検索SORT")
            spm.Fill()

            With ddl_SORT
                .DataValueField = "並び"
                .DataTextField = "SORT"
                .DataSource = spm.ds.Tables(0)
                .DataBind()
            End With
        End Using
    End Sub

#End Region

    
    
    
End Class
