﻿Imports System.Data.OleDb
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.MasterPage
Imports AjaxControlToolkit

Public Class SurveyItems
    Inherits System.Web.UI.Page
    Dim OleDBConn As OleDBConnection
    Dim OleDBCommand As OleDBCommand
    Dim OleDBDataReader, OleDBItemDataReader As OleDBDataReader
    Dim OleDBQuery As String
    Dim OleDB_DataAdapter, OleDB_SurveyItemsDataAdapter As OleDBDataAdapter
    Dim OleDB_DataTable, OleDB_SurveyItemsDataTable As DataTable
    Dim MdlPop As ModalPopupExtender
    Dim StrDataTableFilter, StrDataTableSort, StrStudentCode As String
    Dim IntItemCount, IntItemNo, IntItemAnwser, IntStudentCode As Integer


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session.Timeout > 45 Then ShowPanel("Label1", "Panel5", "Button1")
        OleDBConn = New OleDBConnection(Session("Connection"))
        OleDBConn.Open()
        OleDBCommand = New OleDBCommand()
        OleDBCommand.Connection = OleDBConn
        StrStudentCode = Session("StudentCode")
        'If StrStudentCode = "" Then ShowPanel("Label1", "Panel5", "Button1") : Exit Sub
        'IntStudentCode = Convert.ToInt32(StrStudentCode)
        'Result(3, 30, 35, 24, 25)
        'Exit Sub
        Session("ItemText") = ""
        Session("ItemCode") = ""

        '************************* Start Chech Survey Item Count************************/

        'IntItemCount = Convert.ToInt16(Session("ItemCount"))
        If IntItemNo = 0 Then
            IntItemNo = Convert.ToInt32(Session("ItemNo"))

        End If

        '************************* End  Chech Survey Item Count************************/

        '************************* Start Fill Table Adapter with Survey Items ************************/
        OleDBQuery = "SELECT * FROM SurveyItem"
        OleDBCommand.CommandText = OleDBQuery
        OleDB_SurveyItemsDataAdapter = New OleDBDataAdapter()
        OleDB_SurveyItemsDataTable = New DataTable()
        OleDB_SurveyItemsDataAdapter.SelectCommand = OleDBCommand
        OleDB_SurveyItemsDataAdapter.Fill(OleDB_SurveyItemsDataTable)

        '************************* End Fill Table Adapter with Survey Result ************************/
        If Session("ItemCount") = Session("ItemNo") Then Session("ItemNo") = Session("ItemNo") + 1
        If Session("ItemCount") < Session("ItemNo") Then
            OleDBQuery = "SELECT * FROM SurveyItemsResponse where (StudentCode = " & StrStudentCode & ")"
            OleDBCommand.CommandText = OleDBQuery
            OleDB_DataAdapter = New OleDBDataAdapter()
            OleDB_DataTable = New DataTable()
            OleDB_DataAdapter.SelectCommand = OleDBCommand
            OleDB_DataAdapter.Fill(OleDB_DataTable)
            Dim IntLing As Integer = 0
            Dim IntLogic As Integer = 0
            Dim IntVisual As Integer = 0
            Dim IntNatural As Integer = 0
            For Each column As DataColumn In OleDB_DataTable.Columns
                Select Case Left(column.ColumnName, Trim(column.ColumnName).Length - 2)
                    Case "Ling"
                        IntLing = IntLing + OleDB_DataTable.Rows(0).Item(column.ColumnName)
                    Case "Logic"
                        IntLogic = IntLogic + OleDB_DataTable.Rows(0).Item(column.ColumnName)
                    Case "Visual"
                        IntVisual = IntVisual + OleDB_DataTable.Rows(0).Item(column.ColumnName)
                    Case "Natural"
                        IntNatural = IntNatural + OleDB_DataTable.Rows(0).Item(column.ColumnName)

                End Select
            Next
            OleDB_DataAdapter.Dispose()
            OleDB_DataTable.Dispose()
            Result(StrStudentCode, IntLing, IntLogic, IntVisual, IntNatural)
            Exit Sub
        Else

            GetSurveyItem(Session("ItemNo"))

        End If



    End Sub

    Private Sub GetSurveyItem(ByVal IntItemID As Integer)
        'If IntItemID = Session("previusItem") Then Exit Sub

        StrDataTableFilter = "ItemNo =" & IntItemID
        StrDataTableSort = "ItemNo"
        Dim foundRows As DataRow() = OleDB_SurveyItemsDataTable.Select(StrDataTableFilter, StrDataTableSort, DataViewRowState.OriginalRows)
        For Each Row As DataRow In foundRows
            LblItemText.Text = Row("ItemText").ToString
            ' LblItemText.Text = Row("ItemNo").ToString & " : " & Row("ItemText").ToString
            Session("ItemCode") = Row("ItemCode").ToString
        Next
        Session("previusItem") = IntItemID

    End Sub

    Private Sub BtnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnStart.Click
        Try


            SessionValue(3, CInt(Session("ItemCount")))

        Catch ex As Exception
            ShowPanel("Label1", "Panel5", "Button1")

        Finally
        End Try


    End Sub

    Protected Sub BtnStart0_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnStart0.Click
        Try


            SessionValue(2, CInt(Session("ItemCount")))

        Catch ex As Exception
            ShowPanel("Label1", "Panel5", "Button1")

        Finally
        End Try
    End Sub

    Private Sub BtnStart1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnStart1.Click
        Try


            SessionValue(1, CInt(Session("ItemCount")))

        Catch ex As Exception
            ShowPanel("Label1", "Panel5", "Button1")

        Finally
        End Try
    End Sub

    Sub SessionValue(ByVal StudentItemAnswer As Integer, ByVal IntItemCount As Integer)
        Try
            'On Error Resume Next
            If IntItemCount >= Session("ItemNo") Then
                OleDBQuery = "SELECT StudentCode FROM SurveyItemsResponse WHERE (StudentCode = " & StrStudentCode & ")"
                OleDBCommand = New OleDBCommand()
                OleDBCommand.Connection = OleDBConn
                OleDBCommand.CommandText = OleDBQuery
                OleDBItemDataReader = OleDBCommand.ExecuteReader()
                If OleDBItemDataReader.HasRows Then
                    OleDBQuery = "Update SurveyItemsResponse set " & Session("ItemCode") & "=" & StudentItemAnswer & " where (StudentCode = " & StrStudentCode & ")"
                Else
                    OleDBQuery = "Insert into SurveyItemsResponse (StudentCode," & Session("ItemCode") & ") Values (" & StrStudentCode & "," & StudentItemAnswer & ")"
                End If
                OleDBItemDataReader.Close()
                OleDBCommand.CommandText = OleDBQuery
                OleDBCommand.ExecuteNonQuery()
                OleDBCommand.Cancel()
                OleDBCommand.Dispose()
                Session("ItemNo") = Session("ItemNo") + 1
                GetSurveyItem(Session("ItemNo"))
            End If
        Catch ex As Exception
            ShowPanel("Label1", "Panel5", "Button1")

        Finally
        End Try
    End Sub


    Sub ShowPanel(ByVal StrTargetControlID As String, ByVal StrPopupControlID As String, ByVal StrOkControlID As String)
        MdlPop = CType(Master.FindControl("mdlpopup"), ModalPopupExtender)
        With MdlPop
            .DropShadow = "True"
            .TargetControlID = StrTargetControlID
            .PopupControlID = StrPopupControlID
            .OkControlID = StrOkControlID
            .X = "400"
            .Y = "250"
            .Show()
            .Focus()
            .Enabled() = True
        End With


    End Sub


    'Sub Result(ByVal StudentCode As Integer, ByVal IntLingA As Integer, ByVal IntLogicA As Integer, ByVal IntVisualA As Integer, ByVal IntNaturalA As Integer)
    '    OleDBQuery = "SELECT StudentCode FROM StudentResult WHERE (StudentCode = " & StudentCode & ")"
    '    OleDBCommand = New OleDBCommand()
    '    OleDBCommand.Connection = OleDBConn
    '    OleDBCommand.CommandText = OleDBQuery
    '    OleDBDataReader = OleDBCommand.ExecuteReader()
    '    If OleDBDataReader.HasRows Then
    '        OleDBDataReader.Close()
    '        OleDBQuery = "delete from StudentResult where (StudentCode = " & StudentCode & ")"
    '        OleDBCommand.CommandText = OleDBQuery
    '        OleDBCommand.ExecuteNonQuery()
    '    Else
    '        OleDBDataReader.Close()

    '    End If

    '    ' Insert Ling'
    '    OleDBQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "1" & "," & IntLingA & ")"
    '    InsertResult(OleDBQuery)

    '    ' Insert Logic'

    '    OleDBQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "2" & "," & IntLogicA & ")"
    '    InsertResult(OleDBQuery)

    '    ' Insert Visual
    '    'OleDBQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "3" &  "," &  IntVisualA & ")"
    '    'InsertResult(OleDBQuery)

    '    'Insert natural

    '    OleDBQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "4" & "," & IntNaturalA & ")"
    '    InsertResult(OleDBQuery)

    '    ' Read Special Choice
    '    OleDBQuery = "SELECT        SurveyDmain.SpecialStudy, SurveyDmain.DomainID"
    '    OleDBQuery = OleDBQuery & " FROM            SurveyDmain INNER JOIN"
    '    OleDBQuery = OleDBQuery & " StudentID ON SurveyDmain.DomainID = StudentID.SpecialEdu"
    '    OleDBQuery = OleDBQuery & "  Where (StudentCode =" & StudentCode & ")"
    '    OleDBCommand.CommandText = OleDBQuery
    '    OleDBDataReader = OleDBCommand.ExecuteReader()
    '    Dim IntSpecialEdu As Integer
    '    Dim StrSpecialEdu As String
    '    If OleDBDataReader.HasRows Then
    '        OleDBDataReader.Read()
    '        IntSpecialEdu = OleDBDataReader("DomainID")
    '        StrSpecialEdu = OleDBDataReader("SpecialStudy")
    '    End If
    '    OleDBDataReader.Close()

    '    ' Read Result
    '    OleDBQuery = "SELECT        TOP (100) PERCENT SurveyDmain.DomainText, SurveyDmain.DomainID, SurveyDmain.Domain_Decription, SurveyDmain.SpecialStudy, StudentResult.Domain_Result"
    '    OleDBQuery = OleDBQuery & " FROM            StudentResult INNER JOIN"
    '    OleDBQuery = OleDBQuery & " SurveyDmain ON StudentResult.Domain_code = SurveyDmain.DomainID"
    '    OleDBQuery = OleDBQuery & " WHERE(StudentResult.StudentCode = " & StudentCode & ")"
    '    OleDBQuery = OleDBQuery & " ORDER BY StudentResult.Domain_Result DESC"
    '    OleDBCommand.CommandText = OleDBQuery
    '    OleDB_DataAdapter = New OleDBDataAdapter()
    '    OleDB_DataTable = New DataTable()
    '    OleDB_DataAdapter.SelectCommand = OleDBCommand
    '    OleDB_DataAdapter.Fill(OleDB_DataTable)
    '    Session("ResultText1") = ""
    '    Session("ResultText2") = ""
    '    Session("ResultText1") = "<p><span style='font-size:16px; font-weight:bold; color:Blue'>عزيزى " & Session("StudentName") & "<br/>     بناء على إجابتك على اسئلة المقياس فإن أعلى الذكاءات لديك هو الذكاء "
    '    Dim DobResultSum As Double = 0
    '    For Each Row As DataRow In OleDB_DataTable.Rows
    '        DobResultSum = DobResultSum + Row.Item("Domain_Result")
    '    Next
    '    Dim IntRowCount As Integer = 0
    '    For Each Row As DataRow In OleDB_DataTable.Rows

    '        'For Each column As DataColumn In OleDB_DataTable.Columns
    '        If IntRowCount = 0 Then
    '            Session("ResultText1") = Session("ResultText1") & Row.Item("DomainText") & " والتخصص الذى يتناسب معه هو </span><span style='font-size:18px; font-weight:bold; color:Red;'>" & Row.Item("SpecialStudy") & " بنسبة " & Math.Round(Row.Item("Domain_Result") / DobResultSum * 100, 0) & "%</span></p>"
    '            Session("ResultText1") = Session("ResultText1") & "<span style='font-size:18px; font-weight:bold; color:blue;'>وذلك لتمتعك بالصفات التاليه</span></p><p>"
    '            OleDBQuery = "SELECT        StudentResult.StudentCode, SurveyItem.ItemResultText, StudentResult.Domain_code"
    '            OleDBQuery = OleDBQuery & " FROM            StudentResult INNER JOIN"
    '            OleDBQuery = OleDBQuery & "  SurveyItem ON StudentResult.Domain_code = SurveyItem.DomainID"
    '            OleDBQuery = OleDBQuery & " WHERE (StudentResult.Domain_code =" & Row.Item("DomainID") & ")  AND (StudentResult.StudentCode =" & StudentCode & ")"
    '            OleDBCommand.CommandText = OleDBQuery
    '            OleDBDataReader = OleDBCommand.ExecuteReader()
    '            If OleDBDataReader.HasRows Then
    '                Do While OleDBDataReader.Read
    '                    Session("ResultText1") = Session("ResultText1") & "<span style='font-size:18px; font-weight:bold; color:red;'>" & OleDBDataReader("ItemResultText") & "</span><br/>"
    '                Loop
    '                Session("ResultText1") = Session("ResultText1") & "</p>"

    '            End If
    '            OleDBDataReader.Close()


    '            If IntSpecialEdu = Row.Item("DomainID") Then
    '                Session("ResultText1") = Session("ResultText1") & "<p><span style='font-size:16px; font-weight:bold; color:green;'> وهذا يتفق مع التخصص الذى تريد أن تلتحق به وهو </span><span style='font-size:18px; font-weight:bold; color:Red;'>" & StrSpecialEdu & "</span></p>"
    '            Else
    '                Session("ResultText1") = Session("ResultText1") & "<p><span style='font-size:16px; font-weight:bold; color:green;'> وهذا لايتفق مع التخصص الذى تريد أن تلتحق به وهو </span><span style='font-size:18px; font-weight:bold; color:Red;'>" & StrSpecialEdu & "</span></p>"

    '            End If
    '        Else

    '            Session("ResultText2") = Session("ResultText2") & "<p><span style='font-size:16px; font-weight:bold; color:Black;'> ثم يليه الذكاء " & Row.Item("DomainText") & " والتخصص الذى يتناسب معه هو </span><span style='font-size:18px; font-weight:bold; color:Red;'>" & Row.Item("SpecialStudy") & " بنسبة " & Math.Round(Row.Item("Domain_Result") / DobResultSum * 100) & "%</span></p>"
    '        End If

    '        'Next
    '        IntRowCount = IntRowCount + 1
    '    Next
    '    OleDB_DataAdapter.Dispose()
    '    OleDB_DataTable.Dispose()



    '    OleDBCommand.Cancel()
    '    OleDBCommand.Dispose()
    '    If Session("ItemCount") > Session("ItemNo") Then
    '        Session("ItemFinished") = "Finished"
    '        ShowPanel("LblResult", "PnlResult", "BtnResultOk")
    '    End If


    'End Sub


    Sub Result(ByVal StudentCode As Integer, ByVal IntLingA As Integer, ByVal IntLogicA As Integer, ByVal IntVisualA As Integer, ByVal IntNaturalA As Integer)

        Session("ItemFinished") = "Finished"
        OleDBQuery = "SELECT StudentCode FROM StudentResult WHERE (StudentCode = " & StudentCode & ")"
        OleDBCommand = New OleDBCommand()
        OleDBCommand.Connection = OleDBConn
        OleDBCommand.CommandText = OleDBQuery
        OleDBDataReader = OleDBCommand.ExecuteReader()
        If OleDBDataReader.HasRows Then
            OleDBDataReader.Close()
            OleDBQuery = "delete from StudentResult where (StudentCode = " & StudentCode & ")"
            OleDBCommand.CommandText = OleDBQuery
            OleDBCommand.ExecuteNonQuery()
        Else
            OleDBDataReader.Close()

        End If

        ' Insert Ling'
        OleDBQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "1" & "," & IntLingA & ")"
        InsertResult(OleDBQuery)

        ' Insert Logic'

        OleDBQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "2" & "," & IntLogicA & ")"
        InsertResult(OleDBQuery)

        ' Insert Visual
        'OleDBQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "3" &  "," &  IntVisualA & ")"
        'InsertResult(OleDBQuery)

        'Insert natural

        OleDBQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "4" & "," & IntNaturalA & ")"
        InsertResult(OleDBQuery)

        ' Read Special Choice
        OleDBQuery = "SELECT        SurveyDmain.SpecialStudy, SurveyDmain.DomainID"
        OleDBQuery = OleDBQuery & " FROM            SurveyDmain INNER JOIN"
        OleDBQuery = OleDBQuery & " StudentID ON SurveyDmain.DomainID = StudentID.SpecialEdu"
        OleDBQuery = OleDBQuery & "  Where (StudentCode =" & StudentCode & ")"
        OleDBCommand.CommandText = OleDBQuery
        OleDBDataReader = OleDBCommand.ExecuteReader()
        Dim IntSpecialEdu As Integer
        Dim StrSpecialEdu As String
        If OleDBDataReader.HasRows Then
            OleDBDataReader.Read()
            IntSpecialEdu = OleDBDataReader("DomainID")
            StrSpecialEdu = OleDBDataReader("SpecialStudy")
        End If
        OleDBDataReader.Close()

        ' Read Result
        OleDBQuery = "SELECT SurveyDmain.DomainText, SurveyDmain.DomainID, SurveyDmain.Domain_Decription, SurveyDmain.SpecialStudy, StudentResult.Domain_Result"
        OleDBQuery = OleDBQuery & " FROM            StudentResult INNER JOIN"
        OleDBQuery = OleDBQuery & " SurveyDmain ON StudentResult.Domain_code = SurveyDmain.DomainID"
        OleDBQuery = OleDBQuery & " WHERE(StudentResult.StudentCode = " & StudentCode & ")"
        OleDBQuery = OleDBQuery & " ORDER BY StudentResult.Domain_Result DESC"
        OleDBCommand.CommandText = OleDBQuery
        OleDB_DataAdapter = New OleDBDataAdapter()
        OleDB_DataTable = New DataTable()
        OleDB_DataAdapter.SelectCommand = OleDBCommand
        OleDB_DataAdapter.Fill(OleDB_DataTable)
        Session("ResultText1") = ""
        Session("ResultText2") = ""

        Session("ResultText1") = "<p><span style='font-size:16px; font-weight:bold; color:Blue'>عزيزى " & Session("StudentName") & "<br/>     بناء على إجابتك على اسئلة المقياس فإن أعلى الذكاءات لديك هو الذكاء "
        Dim DobResultSum As Double = 0
        For Each Row As DataRow In OleDB_DataTable.Rows
            DobResultSum = DobResultSum + Row.Item("Domain_Result")
        Next
        Dim IntRowCount As Integer = 0
        For Each Row As DataRow In OleDB_DataTable.Rows

            'For Each column As DataColumn In OleDB_DataTable.Columns
            If IntRowCount = 0 Then
                'Session("ResultText1") = Session("ResultText1") & Row.Item("DomainText") & " والتخصص الذى يتناسب معه هو </span><span style='font-size:18px; font-weight:bold; color:Red;'>" & Row.Item("SpecialStudy") & "</span></p>"
                Session("ResultText1") = Session("ResultText1") & Row.Item("DomainText") & " والتخصص الذى يتناسب معه هو </span><span style='font-size:18px; font-weight:bold; color:Red;'>" & Row.Item("SpecialStudy") & " بنسبة " & Math.Round(Row.Item("Domain_Result") / DobResultSum * 100, 0) & "%</span></p>"
                Session("ResultText1") = Session("ResultText1") & "<span style='font-size:18px; font-weight:bold; color:blue;'>وذلك لتمتعك بالصفات التاليه</span></p><p>"
                OleDBQuery = "SELECT        StudentResult.StudentCode, SurveyItem.ItemResultText, StudentResult.Domain_code"
                OleDBQuery = OleDBQuery & " FROM            StudentResult INNER JOIN"
                OleDBQuery = OleDBQuery & "  SurveyItem ON StudentResult.Domain_code = SurveyItem.DomainID"
                OleDBQuery = OleDBQuery & " WHERE (StudentResult.Domain_code =" & Row.Item("DomainID") & ")  AND (StudentResult.StudentCode =" & StudentCode & ")"
                OleDBCommand.CommandText = OleDBQuery
                OleDBDataReader = OleDBCommand.ExecuteReader()
                If OleDBDataReader.HasRows Then
                    Do While OleDBDataReader.Read
                        Session("ResultText1") = Session("ResultText1") & "<span style='font-size:18px; font-weight:bold; color:red;'>" & OleDBDataReader("ItemResultText") & "</span><br/>"
                    Loop
                    Session("ResultText1") = Session("ResultText1") & "</p>"

                End If
                OleDBDataReader.Close()
                If IntSpecialEdu = Row.Item("DomainID") Then
                    Session("ResultText1") = Session("ResultText1") & "<p><span style='font-size:16px; font-weight:bold; color:green;'> وهذا يتفق مع التخصص الذى تريد أن تلتحق به وهو </span><span style='font-size:18px; font-weight:bold; color:Red;'>" & StrSpecialEdu & "</span></p>"
                Else
                    Session("ResultText1") = Session("ResultText1") & "<p><span style='font-size:16px; font-weight:bold; color:green;'> وهذا لايتفق مع التخصص الذى تريد أن تلتحق به وهو </span><span style='font-size:18px; font-weight:bold; color:Red;'>" & StrSpecialEdu & "</span></p>"

                End If
            Else
                Session("ResultText2") = Session("ResultText2") & "<p><span style='font-size:16px; font-weight:bold; color:Black;'> ثم يليه الذكاء " & Row.Item("DomainText") & " والتخصص الذى يتناسب معه هو </span><span style='font-size:18px; font-weight:bold; color:Red;'>" & Row.Item("SpecialStudy") & " بنسبة " & Math.Round(Row.Item("Domain_Result") / DobResultSum * 100) & "%</span></p>"

                'Session("ResultText2") = Session("ResultText2") & "<p><span style='font-size:16px; font-weight:bold; color:Black;'> ويليه الذكاء " & Row.Item("DomainText") & " والتخصص الذى يتناسب معه هو </span><span style='font-size:18px; font-weight:bold; color:Red;'>" & Row.Item("SpecialStudy") & "</span></p>"
            End If

            'Next
            IntRowCount = IntRowCount + 1
        Next
        OleDB_DataAdapter.Dispose()
        OleDB_DataTable.Dispose()



        OleDBCommand.Cancel()
        OleDBCommand.Dispose()
        If Session("ItemCount") > Session("ItemNo") Then
            Session("ItemFinished") = "Finished"
            ShowPanel("LblResult", "PnlResult", "BtnResultOk")
        End If



    End Sub
    Sub InsertResult(ByVal StrOleDBQuery As String)
        OleDBCommand.CommandText = OleDBQuery
        OleDBCommand.ExecuteNonQuery()
    End Sub

    Private Sub SurveyItems_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        'OleDBConn.Close()
    End Sub
End Class