﻿Imports System.Data.SqlClient
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 SqlConn As SqlConnection
    Dim SqlCommand As SqlCommand
    Dim SqlDataReader, SqlItemDataReader As SqlDataReader
    Dim SqlQuery As String
    Dim Sql_DataAdapter, Sql_SurveyItemsDataAdapter As SqlDataAdapter
    Dim Sql_DataTable, Sql_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")
        SqlConn = New SqlConnection(Session("Connection"))
        SqlConn.Open()
        SqlCommand = New SqlCommand()
        SqlCommand.Connection = SqlConn
        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 ************************/
        SqlQuery = "SELECT * FROM SurveyItem"
        SqlCommand.CommandText = SqlQuery
        Sql_SurveyItemsDataAdapter = New SqlDataAdapter()
        Sql_SurveyItemsDataTable = New DataTable()
        Sql_SurveyItemsDataAdapter.SelectCommand = SqlCommand
        Sql_SurveyItemsDataAdapter.Fill(Sql_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
            SqlQuery = "SELECT * FROM SurveyItemsResponse where (StudentCode = " & StrStudentCode & ")"
            SqlCommand.CommandText = SqlQuery
            Sql_DataAdapter = New SqlDataAdapter()
            Sql_DataTable = New DataTable()
            Sql_DataAdapter.SelectCommand = SqlCommand
            Sql_DataAdapter.Fill(Sql_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 Sql_DataTable.Columns
                Select Case Left(column.ColumnName, Trim(column.ColumnName).Length - 2)
                    Case "Ling"
                        IntLing = IntLing + Sql_DataTable.Rows(0).Item(column.ColumnName)
                    Case "Logic"
                        IntLogic = IntLogic + Sql_DataTable.Rows(0).Item(column.ColumnName)
                    Case "Visual"
                        IntVisual = IntVisual + Sql_DataTable.Rows(0).Item(column.ColumnName)
                    Case "Natural"
                        IntNatural = IntNatural + Sql_DataTable.Rows(0).Item(column.ColumnName)

                End Select
            Next
            Sql_DataAdapter.Dispose()
            Sql_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() = Sql_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
                SqlQuery = "SELECT StudentCode FROM SurveyItemsResponse WHERE (StudentCode = " & StrStudentCode & ")"
                SqlCommand = New SqlCommand()
                SqlCommand.Connection = SqlConn
                SqlCommand.CommandText = SqlQuery
                SqlItemDataReader = SqlCommand.ExecuteReader()
                If SqlItemDataReader.HasRows Then
                    SqlQuery = "Update SurveyItemsResponse set " & Session("ItemCode") & "=" & StudentItemAnswer & " where (StudentCode = " & StrStudentCode & ")"
                Else
                    SqlQuery = "Insert into SurveyItemsResponse (StudentCode," & Session("ItemCode") & ") Values (" & StrStudentCode & "," & StudentItemAnswer & ")"
                End If
                SqlItemDataReader.Close()
                SqlCommand.CommandText = SqlQuery
                SqlCommand.ExecuteNonQuery()
                SqlCommand.Cancel()
                SqlCommand.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)
    '    SqlQuery = "SELECT StudentCode FROM StudentResult WHERE (StudentCode = " & StudentCode & ")"
    '    SqlCommand = New SqlCommand()
    '    SqlCommand.Connection = SqlConn
    '    SqlCommand.CommandText = SqlQuery
    '    SqlDataReader = SqlCommand.ExecuteReader()
    '    If SqlDataReader.HasRows Then
    '        SqlDataReader.Close()
    '        SqlQuery = "delete from StudentResult where (StudentCode = " & StudentCode & ")"
    '        SqlCommand.CommandText = SqlQuery
    '        SqlCommand.ExecuteNonQuery()
    '    Else
    '        SqlDataReader.Close()

    '    End If

    '    ' Insert Ling'
    '    SqlQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "1" & "," & IntLingA & ")"
    '    InsertResult(SqlQuery)

    '    ' Insert Logic'

    '    SqlQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "2" & "," & IntLogicA & ")"
    '    InsertResult(SqlQuery)

    '    ' Insert Visual
    '    'SqlQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "3" &  "," &  IntVisualA & ")"
    '    'InsertResult(SqlQuery)

    '    'Insert natural

    '    SqlQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "4" & "," & IntNaturalA & ")"
    '    InsertResult(SqlQuery)

    '    ' Read Special Choice
    '    SqlQuery = "SELECT        dbo.SurveyDmain.SpecialStudy, dbo.SurveyDmain.DomainID"
    '    SqlQuery = SqlQuery & " FROM            dbo.SurveyDmain INNER JOIN"
    '    SqlQuery = SqlQuery & " dbo.StudentID ON dbo.SurveyDmain.DomainID = dbo.StudentID.SpecialEdu"
    '    SqlQuery = SqlQuery & "  Where (StudentCode =" & StudentCode & ")"
    '    SqlCommand.CommandText = SqlQuery
    '    SqlDataReader = SqlCommand.ExecuteReader()
    '    Dim IntSpecialEdu As Integer
    '    Dim StrSpecialEdu As String
    '    If SqlDataReader.HasRows Then
    '        SqlDataReader.Read()
    '        IntSpecialEdu = SqlDataReader("DomainID")
    '        StrSpecialEdu = SqlDataReader("SpecialStudy")
    '    End If
    '    SqlDataReader.Close()

    '    ' Read Result
    '    SqlQuery = "SELECT        TOP (100) PERCENT dbo.SurveyDmain.DomainText, dbo.SurveyDmain.DomainID, dbo.SurveyDmain.Domain_Decription, dbo.SurveyDmain.SpecialStudy, dbo.StudentResult.Domain_Result"
    '    SqlQuery = SqlQuery & " FROM            dbo.StudentResult INNER JOIN"
    '    SqlQuery = SqlQuery & " dbo.SurveyDmain ON dbo.StudentResult.Domain_code = dbo.SurveyDmain.DomainID"
    '    SqlQuery = SqlQuery & " WHERE(dbo.StudentResult.StudentCode = " & StudentCode & ")"
    '    SqlQuery = SqlQuery & " ORDER BY dbo.StudentResult.Domain_Result DESC"
    '    SqlCommand.CommandText = SqlQuery
    '    Sql_DataAdapter = New SqlDataAdapter()
    '    Sql_DataTable = New DataTable()
    '    Sql_DataAdapter.SelectCommand = SqlCommand
    '    Sql_DataAdapter.Fill(Sql_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 Sql_DataTable.Rows
    '        DobResultSum = DobResultSum + Row.Item("Domain_Result")
    '    Next
    '    Dim IntRowCount As Integer = 0
    '    For Each Row As DataRow In Sql_DataTable.Rows

    '        'For Each column As DataColumn In Sql_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>"
    '            SqlQuery = "SELECT        dbo.StudentResult.StudentCode, dbo.SurveyItem.ItemResultText, dbo.StudentResult.Domain_code"
    '            SqlQuery = SqlQuery & " FROM            dbo.StudentResult INNER JOIN"
    '            SqlQuery = SqlQuery & "  dbo.SurveyItem ON dbo.StudentResult.Domain_code = dbo.SurveyItem.DomainID"
    '            SqlQuery = SqlQuery & " WHERE (dbo.StudentResult.Domain_code =" & Row.Item("DomainID") & ")  AND (dbo.StudentResult.StudentCode =" & StudentCode & ")"
    '            SqlCommand.CommandText = SqlQuery
    '            SqlDataReader = SqlCommand.ExecuteReader()
    '            If SqlDataReader.HasRows Then
    '                Do While SqlDataReader.Read
    '                    Session("ResultText1") = Session("ResultText1") & "<span style='font-size:18px; font-weight:bold; color:red;'>" & SqlDataReader("ItemResultText") & "</span><br/>"
    '                Loop
    '                Session("ResultText1") = Session("ResultText1") & "</p>"

    '            End If
    '            SqlDataReader.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
    '    Sql_DataAdapter.Dispose()
    '    Sql_DataTable.Dispose()



    '    SqlCommand.Cancel()
    '    SqlCommand.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"
        SqlQuery = "SELECT StudentCode FROM StudentResult WHERE (StudentCode = " & StudentCode & ")"
        SqlCommand = New SqlCommand()
        SqlCommand.Connection = SqlConn
        SqlCommand.CommandText = SqlQuery
        SqlDataReader = SqlCommand.ExecuteReader()
        If SqlDataReader.HasRows Then
            SqlDataReader.Close()
            SqlQuery = "delete from StudentResult where (StudentCode = " & StudentCode & ")"
            SqlCommand.CommandText = SqlQuery
            SqlCommand.ExecuteNonQuery()
        Else
            SqlDataReader.Close()

        End If

        ' Insert Ling'
        SqlQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "1" & "," & IntLingA & ")"
        InsertResult(SqlQuery)

        ' Insert Logic'

        SqlQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "2" & "," & IntLogicA & ")"
        InsertResult(SqlQuery)

        ' Insert Visual
        'SqlQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "3" &  "," &  IntVisualA & ")"
        'InsertResult(SqlQuery)

        'Insert natural

        SqlQuery = "Insert into StudentResult (StudentCode, Domain_code, Domain_Result) Values (" & StudentCode & "," & "4" & "," & IntNaturalA & ")"
        InsertResult(SqlQuery)

        ' Read Special Choice
        SqlQuery = "SELECT        dbo.SurveyDmain.SpecialStudy, dbo.SurveyDmain.DomainID"
        SqlQuery = SqlQuery & " FROM            dbo.SurveyDmain INNER JOIN"
        SqlQuery = SqlQuery & " dbo.StudentID ON dbo.SurveyDmain.DomainID = dbo.StudentID.SpecialEdu"
        SqlQuery = SqlQuery & "  Where (StudentCode =" & StudentCode & ")"
        SqlCommand.CommandText = SqlQuery
        SqlDataReader = SqlCommand.ExecuteReader()
        Dim IntSpecialEdu As Integer
        Dim StrSpecialEdu As String
        If SqlDataReader.HasRows Then
            SqlDataReader.Read()
            IntSpecialEdu = SqlDataReader("DomainID")
            StrSpecialEdu = SqlDataReader("SpecialStudy")
        End If
        SqlDataReader.Close()

        ' Read Result
        SqlQuery = "SELECT        TOP (100) PERCENT dbo.SurveyDmain.DomainText, dbo.SurveyDmain.DomainID, dbo.SurveyDmain.Domain_Decription, dbo.SurveyDmain.SpecialStudy, dbo.StudentResult.Domain_Result"
        SqlQuery = SqlQuery & " FROM            dbo.StudentResult INNER JOIN"
        SqlQuery = SqlQuery & " dbo.SurveyDmain ON dbo.StudentResult.Domain_code = dbo.SurveyDmain.DomainID"
        SqlQuery = SqlQuery & " WHERE(dbo.StudentResult.StudentCode = " & StudentCode & ")"
        SqlQuery = SqlQuery & " ORDER BY dbo.StudentResult.Domain_Result DESC"
        SqlCommand.CommandText = SqlQuery
        Sql_DataAdapter = New SqlDataAdapter()
        Sql_DataTable = New DataTable()
        Sql_DataAdapter.SelectCommand = SqlCommand
        Sql_DataAdapter.Fill(Sql_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 Sql_DataTable.Rows
            DobResultSum = DobResultSum + Row.Item("Domain_Result")
        Next
        Dim IntRowCount As Integer = 0
        For Each Row As DataRow In Sql_DataTable.Rows

            'For Each column As DataColumn In Sql_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>"
                SqlQuery = "SELECT        dbo.StudentResult.StudentCode, dbo.SurveyItem.ItemResultText, dbo.StudentResult.Domain_code"
                SqlQuery = SqlQuery & " FROM            dbo.StudentResult INNER JOIN"
                SqlQuery = SqlQuery & "  dbo.SurveyItem ON dbo.StudentResult.Domain_code = dbo.SurveyItem.DomainID"
                SqlQuery = SqlQuery & " WHERE (dbo.StudentResult.Domain_code =" & Row.Item("DomainID") & ")  AND (dbo.StudentResult.StudentCode =" & StudentCode & ")"
                SqlCommand.CommandText = SqlQuery
                SqlDataReader = SqlCommand.ExecuteReader()
                If SqlDataReader.HasRows Then
                    Do While SqlDataReader.Read
                        Session("ResultText1") = Session("ResultText1") & "<span style='font-size:18px; font-weight:bold; color:red;'>" & SqlDataReader("ItemResultText") & "</span><br/>"
                    Loop
                    Session("ResultText1") = Session("ResultText1") & "</p>"

                End If
                SqlDataReader.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
        Sql_DataAdapter.Dispose()
        Sql_DataTable.Dispose()



        SqlCommand.Cancel()
        SqlCommand.Dispose()
        If Session("ItemCount") > Session("ItemNo") Then
            Session("ItemFinished") = "Finished"
            ShowPanel("LblResult", "PnlResult", "BtnResultOk")
        End If



    End Sub
    Sub InsertResult(ByVal StrSqlQuery As String)
        SqlCommand.CommandText = SqlQuery
        SqlCommand.ExecuteNonQuery()
    End Sub

    Private Sub SurveyItems_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        'SqlConn.Close()
    End Sub
End Class