Imports RGBC_ScoreCard.SystemFunctions
Imports RGBC_ScoreCard.ProjectFunctions
Imports RGBC_ScoreCard.db
Imports System.ComponentModel

'20170509 - v1.0.5 - reorder + added txtCategoryDescription - maanie

Public Class QuestionEdit
    Inherits System.Web.UI.Page

    Public QuestionID, QuestionnaireID As Integer
    Public dr As DataRow

    <BrowsableAttribute(False)> _
    Public Overridable Property SelectedText As String

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

        Try
            QuestionID = Integer.Parse(Request.QueryString("QuestionID"))
        Catch ex As Exception
            QuestionID = 0
        End Try

        If Not IsPostBack Then

            If QuestionID <> 0 Then

                HiddenQuestionID.Text = QuestionID.ToString()

                dr = db.getRow("SELECT * FROM scrQuestion WHERE scrQuestion.QuestionID =" & QuestionID)

                QuestionnaireID = dr!refQuestionnaireID
                ddlQuestionnaire.Enabled = False
                txtQuestion.Text = dr!strQuestion
            txtCategory.Text = dr!strCategory
            txtCategoryDescription.Text = dr!strCategoryDescription '20170509 - v1.0.5 - reorder + added txtCategoryDescription - maanie

                If Not ddlOptionType.Items.FindByValue(dr!strOptionType.ToString()) Is Nothing Then
                    ddlOptionType.SelectedValue = dr!strOptionType.ToString()
                End If

                txtOrder.Text = (dr!intOrder).ToString()
                chkComment.Checked = dr!blnComments
                chkActive.Checked = dr!blnActive
                lblLastUser.Text = (dr!strLastUser).ToString()
                lblLastEdit.Text = dr!strLastEdit

                lblHeading.Text = "Edit"
            Else
                chkActive.Checked = True
                HiddenQuestionID.Text = 0
                lblHeading.Text = "Add"
            End If
        Else
            'do nothing
        End If
    End Sub

    Private Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
        Dim imgLoading As WebControls.Image
        imgLoading = Page.Master.FindControl("imgLoading")
        imgLoading.CssClass = "invisible"
    End Sub

    Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim blnComment, blnActive As Integer
      Dim strQuestion, strCategory, strCategoryDescription As String

        QuestionID = Convert.ToInt32(HiddenQuestionID.Text)

        Try
            blnComment = CInt(chkComment.Checked)
            blnActive = CInt(chkActive.Checked)

            strQuestion = db.CleanString(txtQuestion.Text.ToString()).Trim()
         strCategory = db.CleanString(txtCategory.Text.ToString()).Trim()
         strCategoryDescription = db.CleanString(txtCategoryDescription.Text.ToString()).Trim()

         If QuestionID <> 0 Then
            'update question
            RGBC_ScoreCard.db.doQuery("UPDATE scrQuestion SET refQuestionnaireID=" & ddlQuestionnaire.SelectedValue & ", strQuestion='" & strQuestion & "', strCategory='" & strCategory & "' " & _
                                      " , strOptionType='" & ddlOptionType.SelectedItem.Text & "', strCategoryDescription='" & strCategoryDescription & "', intOrder=" & Convert.ToInt32(txtOrder.Text) & ", blnComments=" & blnComment & ", blnActive=" & blnActive & " " & _
                                      " , strLastUser= '" & Session.Item("USER")!strUserName & "' WHERE QuestionID=" & Convert.ToInt32(HiddenQuestionID.Text)) '20170509 - v1.0.5 - reorder + added txtCategoryDescription - maanie

            dr = db.getRow("SELECT * FROM scrQuestion WHERE scrQuestion.QuestionID =" & Convert.ToInt32(HiddenQuestionID.Text))

            lblLastUser.Text = (dr!strLastUser).ToString()
            lblLastEdit.Text = dr!strLastEdit

         Else
            'insert question
            RGBC_ScoreCard.db.doQuery("INSERT INTO scrQuestion(refQuestionnaireID, strQuestion, strCategory, strOptionType, strCategoryDescription, intOrder, blnComments, blnActive, strLastUser) " & _
                                         " VALUES (" & ddlQuestionnaire.SelectedValue & ",'" & strQuestion & "','" & strCategory & "','" & strCategoryDescription & "', '" & ddlOptionType.SelectedItem.Text & "', " & Convert.ToInt32(txtOrder.Text) & " " & _
                                         "," & blnComment & "," & blnActive & ", '" & Session.Item("USER")!strUserName & "' )") '20170509 - v1.0.5 - reorder + added txtCategoryDescription - maanie

            'GET last ID from scrQuestion table for incase an update to new question should be made
            dr = db.getRow("SELECT MAX(QuestionID) As QuestionID FROM scrQuestion")
            HiddenQuestionID.Text = dr!QuestionID

            dr = db.getRow("SELECT * FROM scrQuestion WHERE scrQuestion.QuestionID =" & Convert.ToInt32(HiddenQuestionID.Text))

            lblLastUser.Text = (dr!strLastUser).ToString()
            lblLastEdit.Text = dr!strLastEdit

         End If

         'Update all Cat Desc for all questions in this category
         RGBC_ScoreCard.db.doQuery("UPDATE scrQuestion SET strCategoryDescription = '" & strCategoryDescription & "' WHERE strCategory = '" & strCategory & "'")

         lblMessage.Text = "Question saved. "

        Catch ex As Exception
            lblMessage.Text = ex.Message & "<BR/>" & ex.StackTrace
        End Try
    End Sub

    Protected Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        Response.Redirect("Questionnaire.aspx")
    End Sub

    Protected Sub ddlQuestionnaire_DataBound(sender As Object, e As EventArgs) Handles ddlQuestionnaire.DataBound

        Try
            If QuestionnaireID = 0 Then 'get QnID from Request. 'note QnID on edit is set het because DataBound is preceded by the Page.Load which sets the QnID
                QuestionnaireID = Integer.Parse(Request.QueryString("QuestionnaireID"))
            End If
        Catch ex As Exception
            QuestionnaireID = 0
        End Try

        If Not ddlQuestionnaire.Items.FindByValue(QuestionnaireID.ToString()) Is Nothing Then
            ddlQuestionnaire.SelectedValue = QuestionnaireID.ToString()
        End If
    End Sub

    Protected Sub txtQuestion_TextChanged(sender As Object, e As EventArgs) Handles txtQuestion.TextChanged

    End Sub
End Class