﻿
Partial Class order_centre_JobOptions
    Inherits System.Web.UI.Page

    Private _webAdapter As New WebWS()
    Private _goPrint2Adapter As New com.racadtech.services.GoPrint2Client()
    Private _jobSettingsArray As String()

    Public ReadOnly Property WebAdapter() As WebWS
        Get
            Return _webAdapter
        End Get
    End Property
    Public ReadOnly Property GoPrint2Adapter() As com.racadtech.services.GoPrint2Client
        Get
            Return _goPrint2Adapter
        End Get
    End Property
    Public ReadOnly Property MasterKey() As String
        Get
            Return "877k31hg333-d03423332wq-312dkazlfs-433398j543"
        End Get
    End Property
    Public ReadOnly Property BaseImageURL() As String
        Get
            If Not Session("BaseImageURL") Is Nothing Then
                Return Session("BaseImageURL")
            Else
                Return "themes/orange/"
            End If
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("ThankYou") = Nothing

        If (Not System.Web.HttpContext.Current.User.Identity.IsAuthenticated()) Then
            Response.Redirect("~/order_centre/getting_started.aspx")
        End If
        Dim _storeId As Integer = Session("StoreId")
        Dim _userId As Guid = Session("UserId")

        Dim _packageId As Integer
        Dim _ds As New System.Data.DataSet

        _ds = _webAdapter.GetActivationDataSetByStoreId(_storeId)
        Session("UserId") = _ds.Tables(0).Rows(0)("UserId")
        _userId = Session("UserId")

        _ds = _webAdapter.GetStorePackagesByStoreId(_storeId)

        JobOptionsDiv.Visible = True

        'Dim _allUsers As MembershipUserCollection = Membership.GetAllUsers
        Dim _curUserProfile As ProfileCommon
        'For Each _existenUser As MembershipUser In _allUsers

        _curUserProfile = Profile.GetProfile(Membership.GetUser(_userId).UserName)


        'If _curUserProfile.StoreId.ToString = _storeId.ToString Then


        If _curUserProfile.ExpirationDate < Now() Then
            JobOptionsDiv.Visible = False
            NoOptionsDiv.Visible = True
            NoOptionsLabel.Text = "Unable to submit a job due to the following reason: trial period has expired."
        Else

            If _ds.Tables(0).Rows.Count > 0 Then
                For i As Integer = 0 To _ds.Tables(0).Rows.Count - 1
                    _packageId = _ds.Tables(0).Rows(i).Item("PackageId")
                    If _packageId = 9 Then
                        Dim _finishDate As DateTime = Now()
                        Dim _startDate As DateTime = CType(Now().Month & "/1/" & Now().Year, DateTime)
                        If com.racadtech.Framework.Modules.GoPrint2.GoPrint2Utils.IsUserAllowedToSubmitJobs(_storeId, _packageId, _startDate, _finishDate) Then
                            JobOptionsDiv.Visible = True
                        Else
                            NoOptionsDiv.Visible = True
                            NoOptionsLabel.Text = "Unable to submit a job due to the following reason: the store has limitation on the amount of submissions per month. Please contact your print store."
                        End If
                        Exit For
                    End If
                Next

            Else
                NoOptionsDiv.Visible = True
                NoOptionsLabel.Text = "Unable to submit a job due to the following reason: the store has not been subscribed to any package. Please contact your print store."
            End If
        End If



        'End If
        'Next




        'For Each _user As MembershipUser In Membership.GetAllUsers()
        '    Dim _profile As ProfileCommon = Profile.GetProfile(_user.UserName)
        '    If _profile.StoreId = _storeId Then
        '        _packageId = _profile.PackageId
        '        Exit For
        '    End If
        'Next

        'Dim _finishDate As DateTime = Now()
        'Dim _startDate As DateTime = CType(Now().Month & "/1/" & Now().Year, DateTime)

        'If com.racadtech.Framework.Modules.GoPrint2.GoPrint2Utils.IsUserAllowedToSubmitJobs(_storeId, _packageId, _startDate, _finishDate) Then
        '    JobOptionsDiv.Visible = True
        'Else
        '    NoOptionsDiv.Visible = True
        'End If

        Dim _userName As String = System.Web.HttpContext.Current.User.Identity.Name.ToString()

        ' Disable Language Panel.
        Dim _languagesPanel As Web.UI.WebControls.Panel = Me.Master.FindControl("LanguagesPanel")
        Dim _languagesSpacer As Web.UI.HtmlControls.HtmlGenericControl = Me.Master.FindControl("LanguagesSpacerLabel")
        _languagesPanel.Visible = False
        _languagesSpacer.InnerHtml = "<br /><br />"

        Dim _activationId As String = _webAdapter.GetActivationIdByStoreId(Session("StoreId"))
        Dim _lang As String = "en"
        If Not Request.Cookies.Item("lang") Is Nothing Then
            _lang = Request.Cookies.Item("lang").Value
        Else
            If Not Session("lang") Is Nothing Then
                _lang = Session("lang").ToString()
            End If
        End If

        If Page.IsPostBack = False Then
            'Rem set up default date as a sysdate for RadDatePicker
            'RadDatePicker.SelectedDate = System.DateTime.Now()
            'RadDatePicker.Calendar.RangeMinDate = System.DateTime.Now()
        End If
        'Dim SelectDataValue As String
        'SelectDataValue = Format(Convert.ToDateTime(RadDatePicker.SelectedDate.ToString()), "dd MMM yyyy")

        'Response.Write("StoreId: " & Session("StoreId"))
        'Response.Write("<br>" & Me._webAdapter.GetStoreJobSettingByStoreId(Session("StoreId")).Tables(0).Rows.Count)
        If Me._webAdapter.GetStoreJobSettingByStoreId(Session("StoreId")).Tables(0).Rows.Count > 0 Then

            Me.JobOptionsTd.Visible = True

            Dim _jobSettings As String = _goPrint2Adapter.GetJobSettingsWithLanguage(MasterKey, _activationId, _lang)
            Dim _sep As String() = {"%%_%%"}
            _jobSettingsArray = _jobSettings.Split(_sep, StringSplitOptions.RemoveEmptyEntries)
            Dim i As Integer

            For i = 0 To _jobSettingsArray.Length - 1
                Select Case i
                    Case 0
                        EnableJobOptionControl(Me.Label1, Me.DropDownList1, _jobSettingsArray(i))
                    Case 1
                        EnableJobOptionControl(Me.Label2, Me.DropDownList2, _jobSettingsArray(i))
                    Case 2
                        EnableJobOptionControl(Me.Label3, Me.DropDownList3, _jobSettingsArray(i))
                    Case 3
                        EnableJobOptionControl(Me.Label4, Me.DropDownList4, _jobSettingsArray(i))
                    Case 4
                        EnableJobOptionControl(Me.Label5, Me.DropDownList5, _jobSettingsArray(i))
                    Case 5
                        EnableJobOptionControl(Me.Label6, Me.DropDownList6, _jobSettingsArray(i))
                    Case 6
                        EnableJobOptionControl(Me.Label7, Me.DropDownList7, _jobSettingsArray(i))
                    Case 7
                        EnableJobOptionControl(Me.Label8, Me.DropDownList8, _jobSettingsArray(i))
                    Case 8
                        EnableJobOptionControl(Me.Label9, Me.DropDownList9, _jobSettingsArray(i))
                    Case 9
                        EnableJobOptionControl(Me.Label10, Me.DropDownList10, _jobSettingsArray(i))
                    Case 10
                        EnableJobOptionControl(Me.Label11, Me.DropDownList11, _jobSettingsArray(i))
                    Case 11
                        EnableJobOptionControl(Me.Label12, Me.DropDownList12, _jobSettingsArray(i))
                    Case 12
                        EnableJobOptionControl(Me.Label13, Me.DropDownList13, _jobSettingsArray(i))
                    Case 13
                        EnableJobOptionControl(Me.Label14, Me.DropDownList14, _jobSettingsArray(i))
                    Case 14
                        'EnableJobOptionControl(Me.Label15, Me.DropDownList15, _jobSettingsArray(i))
                End Select
            Next

        End If
    End Sub

    Private Sub EnableJobOptionControl(ByVal labelControl As Label, _
                                       ByVal dropDownControl As DropDownList, _
                                       ByVal data As String)
        labelControl.Visible = True
        dropDownControl.Visible = True
        PopualteJobOptionSection(labelControl, dropDownControl, data)
    End Sub

    Private Sub PopualteJobOptionSection(ByVal labelControl As Label, _
                                         ByVal dropDownControl As DropDownList, _
                                         ByVal data As String)

        Dim _sep1 As String() = {"#_SNAME_#"}
        Dim _dataArray As String() = data.Split(_sep1, StringSplitOptions.RemoveEmptyEntries)

        ' Define section name.
        labelControl.Text = _dataArray(0) & "&nbsp;:&nbsp;"

        ' Populate Drop Down Control.
        Dim _sep2 As String() = {"#_SVALUE_#"}
        Dim _valueArray As String() = _dataArray(1).Split(_sep2, System.StringSplitOptions.RemoveEmptyEntries)
        If (_dataArray.Length.Equals(0)) Then Return

        Dim i As Integer
        For i = 0 To _valueArray.Length - 1
            dropDownControl.Items.Add(New ListItem(_valueArray(i).ToString(), _valueArray(i).ToString()))
        Next
    End Sub

    Protected Sub NextButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NextButton.Click
        If Page.IsValid Then

            ' sectionName1##sectionValue1%%sectionName2##sectionValue2%%
            Dim _jobOptions As String = ""
            ' Define static required job options to database aswell.
            'Dim SelectDataValue As String
            If RadDatePicker.SelectedDate.ToString() = "" Then
                'Temporary comment the following code:
                '_jobOptions = _jobOptions & Me.DateRequiredLabel.Text & "##%%"

                _jobOptions = _jobOptions & Me.DateRequiredLabel.Text & "##-%%"

            Else
                _jobOptions = _jobOptions & Me.DateRequiredLabel.Text & "##" & Format(Convert.ToDateTime(RadDatePicker.SelectedDate.ToString()), "dd MMM yyyy hh:mm:ss") & "%%"
            End If

            If NumberOfCopiesTextBox.Text.ToString() = "" Then
                _jobOptions = _jobOptions & Me.NumberCopiesLabel.Text & "##-%%"
            Else
                _jobOptions = _jobOptions & Me.NumberCopiesLabel.Text & "##" & NumberOfCopiesTextBox.Text.ToString() & "%%"
            End If

            If Me._webAdapter.GetStoreJobSettingByStoreId(Session("StoreId")).Tables(0).Rows.Count > 0 Then

                Dim _sep1 As String() = {"#_SNAME_#"}
                Dim i As Integer = 0
                For i = 0 To _jobSettingsArray.Length - 1
                    _jobOptions = _jobOptions & _jobSettingsArray(i).Split(_sep1, StringSplitOptions.RemoveEmptyEntries)(0) & "##"
                    Select Case i
                        Case 0
                            If Me.DropDownList1.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList1.Text & "%%"
                            End If
                        Case 1
                            If Me.DropDownList2.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList2.Text & "%%"
                            End If
                        Case 2
                            If Me.DropDownList3.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList3.Text & "%%"
                            End If
                        Case 3
                            If Me.DropDownList4.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList4.Text & "%%"
                            End If
                        Case 4
                            If Me.DropDownList5.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList5.Text & "%%"
                            End If
                        Case 5
                            If Me.DropDownList6.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList6.Text & "%%"
                            End If
                        Case 6
                            If Me.DropDownList7.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList7.Text & "%%"
                            End If
                        Case 7
                            If Me.DropDownList8.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList8.Text & "%%"
                            End If
                        Case 8
                            If Me.DropDownList9.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList9.Text & "%%"
                            End If
                        Case 9
                            If Me.DropDownList10.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList10.Text & "%%"
                            End If
                        Case 10
                            If Me.DropDownList11.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList11.Text & "%%"
                            End If
                        Case 11
                            If Me.DropDownList12.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList12.Text & "%%"
                            End If
                        Case 12
                            If Me.DropDownList13.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList13.Text & "%%"
                            End If
                        Case 13
                            If Me.DropDownList14.Text = "" Then
                                _jobOptions = _jobOptions & "-%%"
                            Else
                                _jobOptions = _jobOptions & Me.DropDownList14.Text & "%%"
                            End If
                    End Select
                Next
            End If

            If CommentsInstructionsTextBox.Text.ToString() = "" Then
                _jobOptions = _jobOptions & Me.CommentsInstructionsLabel.Text & "##-%%"
            Else
                _jobOptions = _jobOptions & Me.CommentsInstructionsLabel.Text & "##" & CommentsInstructionsTextBox.Text.ToString() & "%%"
            End If
            Session("JobOptions") = _jobOptions
            Response.Redirect("~/order_centre/upload.aspx")
        End If
    End Sub

    'Protected Sub NextImageButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles NextImageButton.Click
    '    If Page.IsValid Then

    '        ' sectionName1##sectionValue1%%sectionName2##sectionValue2%%
    '        Dim _jobOptions As String = ""
    '        ' Define static required job options to database aswell.
    '        'Dim SelectDataValue As String
    '        'SelectDataValue = Format(Convert.ToDateTime(RadDatePicker.SelectedDate.ToString()), "dd MMM yyyy")
    '        _jobOptions = _jobOptions & Me.DateRequiredLabel.Text & "##" & Format(Convert.ToDateTime(RadDatePicker.SelectedDate.ToString()), "dd MMM yyyy hh:mm:ss") & "%%"
    '        ' _jobOptions = _jobOptions & Me.DateRequiredLabel.Text & "##" & RadDatePicker.SelectedDate.ToString() & "%%"
    '        _jobOptions = _jobOptions & Me.NumberCopiesLabel.Text & "##" & NumberOfCopiesTextBox.Text.ToString() & "%%"
    '        Dim _sep1 As String() = {"#_SNAME_#"}
    '        Dim i As Integer = 0
    '        For i = 0 To _jobSettingsArray.Length - 1
    '            _jobOptions = _jobOptions & _jobSettingsArray(i).Split(_sep1, StringSplitOptions.RemoveEmptyEntries)(0) & "##"
    '            Select Case i
    '                Case 0
    '                    _jobOptions = _jobOptions & Me.DropDownList1.Text & "%%"
    '                Case 1
    '                    _jobOptions = _jobOptions & Me.DropDownList2.Text & "%%"
    '                Case 2
    '                    _jobOptions = _jobOptions & Me.DropDownList3.Text & "%%"
    '                Case 3
    '                    _jobOptions = _jobOptions & Me.DropDownList4.Text & "%%"
    '                Case 4
    '                    _jobOptions = _jobOptions & Me.DropDownList5.Text & "%%"
    '                Case 5
    '                    _jobOptions = _jobOptions & Me.DropDownList6.Text & "%%"
    '                Case 6
    '                    _jobOptions = _jobOptions & Me.DropDownList7.Text & "%%"
    '                Case 7
    '                    _jobOptions = _jobOptions & Me.DropDownList8.Text & "%%"
    '                Case 8
    '                    _jobOptions = _jobOptions & Me.DropDownList9.Text & "%%"
    '                Case 9
    '                    _jobOptions = _jobOptions & Me.DropDownList10.Text & "%%"
    '                Case 10
    '                    _jobOptions = _jobOptions & Me.DropDownList11.Text & "%%"
    '                Case 11
    '                    _jobOptions = _jobOptions & Me.DropDownList12.Text & "%%"
    '                Case 12
    '                    _jobOptions = _jobOptions & Me.DropDownList13.Text & "%%"
    '                Case 13
    '                    _jobOptions = _jobOptions & Me.DropDownList14.Text & "%%"
    '            End Select
    '        Next
    '        _jobOptions = _jobOptions & Me.CommentsInstructionsLabel.Text & "##" & CommentsInstructionsTextBox.Text.ToString() & "%%"
    '        Session("JobOptions") = _jobOptions
    '        Response.Redirect("~/order_centre/upload.aspx")
    '    End If
    'End Sub
End Class
