Imports System.Data
Imports AppCommon

Partial Class recruit_uc_uc_Job
    Inherits System.Web.UI.UserControl

#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 "********************** プロパティ **********************"

    Private m_Href As String = ""
    Public Property Href() As String
        Get
            Return m_Href
        End Get
        Set(ByVal value As String)
            m_Href = value
        End Set
    End Property

#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_SORT.SelectedValue = "登録日 DESC"
        End If

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

#End Region

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

    Protected Sub gv_求人一覧_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles gv_求人一覧.PageIndexChanging
        CType(sender, GridView).PageIndex = e.NewPageIndex
        DataBind_gv_求人一覧()
    End Sub

    Protected Sub gv_求人一覧_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv_求人一覧.RowCommand
        If e.CommandName = "求人票_Web" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = CType(sender, GridView).Rows(index)

            Dim strDetailLink As String = "JobInfo.aspx"
            Dim strCompanyCd As String = CType(row.FindControl("lbl_企業CD"), Label).Text
            Dim strBaseCd As String = CType(row.FindControl("lbl_拠点CD"), Label).Text
            Dim strOfferYear As String = CType(row.FindControl("lbl_求人年度"), Label).Text
            Dim strBranchNo As String = CType(row.FindControl("lbl_枝NO"), Label).Text

            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 index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = CType(sender, GridView).Rows(index)
            Dim path As String = m_strPdf & Server.HtmlDecode(CType(row.FindControl("lbl_求人年度"), Label).Text) & "\" & Server.HtmlDecode(CType(row.FindControl("lbl_PDF名"), Label).Text) & ".pdf"

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

                'ファイル名
                Dim fileName As String = "求人票_" & CType(row.FindControl("lbl_企業名"), Label).Text & "_" & CType(row.FindControl("lbl_求人年度"), Label).Text & 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))
                Response.ContentType = m_strContextType

                'ファイルを書き出し
                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

    Protected Sub gv_求人一覧_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_求人一覧.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            '[求人票]PDF
            Dim pathPDF As String = m_strPdf & Server.HtmlDecode(CType(e.Row.FindControl("lbl_求人年度"), Label).Text) & "\" & Server.HtmlDecode(CType(e.Row.FindControl("lbl_PDF名"), Label).Text) & ".pdf"
            Dim btnPDF As Button = CType(e.Row.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.Row.FindControl("link_企業名"), HyperLink)
            lbl企業名 = CType(e.Row.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.Row.FindControl("lbl_給与"), Label).Text
            If Integer.TryParse(strPay, intPay) Then
                strPay = CType((intPay / 10000), String)
            End If
            CType(e.Row.FindControl("lbl_給与"), Label).Text = strPay + "万円"
        End If
    End Sub

    Protected Sub ddl_求人年度検索_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_求人年度検索.PreRender
        '初回読み込み時のみ求人年度検索の要素を調べ、今年度のデータが含まれていれば初期値を今年度にする
        If Not IsPostBack 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

            Using spm As New StoredProcedureManager("SPCMB求人年度今年")
                spm.AddPrmIn("@求人年度", Data.SqlDbType.SmallInt, selectYear)
                spm.Fill()

                If spm.ds.Tables(0).Rows.Count > 0 Then
                    ddl_求人年度検索.SelectedValue = selectYear
                End If
            End Using

            DataBind_gv_求人一覧()
        End If
    End Sub

    Protected Sub ddl_SORT_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_SORT.SelectedIndexChanged
        DataBind_gv_求人一覧()
    End Sub

    Protected Sub btn_検索_Click(sender As Object, e As EventArgs) Handles btn_検索.Click
        DataBind_gv_求人一覧()
    End Sub

#End Region

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

    Private Sub DataBind_gv_求人一覧()
        '職種の選択肢をカンマ区切りに加工
        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

        Using spm As New StoredProcedureManager("SP求人検索")
            spm.AddPrmIn("@求人年度", Data.SqlDbType.SmallInt, ddl_求人年度検索.SelectedValue)
            spm.AddPrmIn("@業種CD", Data.SqlDbType.VarChar, 20, ddl_業種CD検索.SelectedValue)
            spm.AddPrmIn("@職種CD", Data.SqlDbType.VarChar, 2000, hf_職種検索.Value)
            spm.AddPrmIn("@企業名", Data.SqlDbType.VarChar, 2000, txt_企業名検索.Text)
            spm.AddPrmIn("@新着", Data.SqlDbType.Bit, cb_新着検索.Checked)
            spm.AddPrmIn("@勤務地", Data.SqlDbType.VarChar, 2000, hf_勤務地検索.Value)
            spm.AddPrmIn("@給与", Data.SqlDbType.Int, txt_給与検索.Text)
            spm.AddPrmIn("@加入保険_健康保険", Data.SqlDbType.Bit, cb_加入保険検索_健康保険.Checked)
            spm.AddPrmIn("@加入保険_厚生年金", Data.SqlDbType.Bit, cb_加入保険検索_厚生年金.Checked)
            spm.AddPrmIn("@加入保険_労災保険", Data.SqlDbType.Bit, cb_加入保険検索_労災保険.Checked)
            spm.AddPrmIn("@加入保険_雇用保険", Data.SqlDbType.Bit, cb_加入保険検索_雇用保険.Checked)
            spm.AddPrmIn("@寮", Data.SqlDbType.Bit, cb_寮検索.Checked)
            spm.AddPrmIn("@休日", Data.SqlDbType.VarChar, 20, ddl_休日検索.SelectedValue)
            spm.AddPrmIn("@新既", Data.SqlDbType.VarChar, 20, ddl_募集対象.SelectedValue)
            spm.AddPrmIn("@SORT", Data.SqlDbType.VarChar, 100, ddl_SORT.SelectedValue)
            spm.Fill()

            With gv_求人一覧
                .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_業種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
