﻿/// <reference path="D:\VS2015\GB_Site\Web\Scripts/angular.js" />

angular.module('investmentTutorialsApp', ['rzModule', 'ngSanitize'])
    .controller('investmentTutorialsCtrl', ['$scope', '$http', '$q', '$location', '$filter', '$timeout', '$log', function ($scope, $http, $q, $location, $filter, $timeout, $log) {
        var self = this;
    }])
    .controller('MyBookMarkCtrl', ['$scope', '$http', '$q', function ($scope, $http, $q) {
        $scope.BookMarkList = $("#investingBookMark").data("val");

        $scope.LabelShowSuccess = false;
        $scope.LabelShowWarning = false;

        $scope.AreYouSure = function (val) {
            $scope.BookmarkDeletedId = val;
        }
        $scope.deleteBookMark = function (val) {
            $scope.LabelShowSuccess = false;
            $scope.LabelShowWarning = false;
            $http({
                method: 'Get',
                url: siteRoot + 'InvestmentTutorial/DeleteBookMark/' + val,
            }).then(function (res) {
                angular.element('.deleteBookMark').modal('hide');
                if (res.data == 1) {
                    $scope.LabelShowSuccess = true;

                    $scope.BookMarkList.forEach(function (element, index, array) {
                        if (element.MyBookmarkID === val) {
                            $scope.BookMarkList.splice(index, 1);
                        }
                    });

                } else {
                    $scope.LabelShowWarning = true;
                }
                
							}, (error) => { console.log(error.data); });
        }
    }])
    .controller('MyNotesCtrl', ['$scope', '$http', '$q', function ($scope, $http, $q) {
        $scope.NoteList = $("#investingNotes").data("val");

        $scope.LabelShowSuccess = false;
        $scope.LabelShowWarning = false;

        $scope.AreYouSure = function (val) {
            $scope.NoteDeletedId = val;
        }
        $scope.deleteNotes = function (val) {
            $scope.LabelShowSuccess = false;
            $scope.LabelShowWarning = false;
            $http({
                method: 'Get',
                url: siteRoot + 'InvestmentTutorial/DeleteMyNote/' + val,
            }).then(function (res) {
                angular.element('.deleteNotes').modal('hide');
                if (res.data == 1) {
                    $scope.LabelShowSuccess = true;

                    $scope.NoteList.forEach(function (element, index, array) {
                        if (element.MyNoteID === val) {
                            $scope.NoteList.splice(index, 1);
                        }
                    });

                } else {
                    $scope.LabelShowWarning = true;
                }

							}, (error) => {console.log(error.data); });
        }
    }])
    .controller('SearchContentCtrl', ['$scope', '$http', '$q', function ($scope, $http, $q) {
        $scope.URL = function (id) {

            if(id == 1){
                return "/InvestmentTutorial/Chapter/";
            } else if (id == 2) {
                return "/InvestmentTutorial/Section/";
            } else if (id == 3) {
                return "/InvestmentTutorial/SubSection/";
            }
        }

        $scope.SearchContent = function () {
            blockUI('DivSearchContent');
            $http({
                method: 'POST',
                url: siteRoot + 'InvestmentTutorial/Search/',
                params: { word: $scope.searchWord }
            }).then(function (res) {
                $scope.searchList = res.data;
							}, (error) => { unblockUI('DivSearchContent'); console.log(error.data); }).then(function () {
								unblockUI('DivSearchContent');
            });
        }
    }])
    .controller('SearchQuizCtrl', ['$scope', '$http', '$q', '$filter', function ($scope, $http, $q, $filter) {

        $scope.SearchQuiz = function () {
            blockUI('DivSearchContent');
            $http({
                method: 'POST',
                url: siteRoot + 'InvestmentTutorial/SearchQuiz/',
                params: { word: $scope.searchWord }
            }).then(function (res) {
                $scope.searchList = res.data;
                $scope.UniqueQuestion = $filter('unique')($scope.searchList.map(function (v) { return v.QuestionID; }))
							}, (error) => { unblockUI('DivSearchContent'); console.log(error.data); }).then(function () {
								unblockUI('DivSearchContent');
            });
        }
    }])
    .controller('QuizCtrl', ['$scope', '$http', '$q', '$filter', '$window', function ($scope, $http, $q, $filter, $window) {
        $scope.CurrentQuestion = parseInt(CurrentQuestion);
        $scope.TotalQuestion = TotalQuestion;
        $scope.QuizId = $.getQueryString('id');
        
        function GetQuestion(chapterId, QuestionId, CQId, flagId, Curr, answerId, rev) {
            blockUI('DivQuiz');
            $http({
                method: 'POST',
                url: siteRoot + 'InvestmentTutorial/ManageQuizTest',
                params: { chapterId: chapterId, questionId: QuestionId, chapterQuestionId: CQId, flag: flagId, answerId: answerId, review : rev }
            }).then(function (res) {
                $scope.CurrentQuestion = $scope.CurrentQuestion + Curr;
                $scope.quizs = res.data.quiz;
                $scope.reviewData = res.data.reviewData;
                $scope.QuestionId = res.data.quiz.Question.QuestionID;
							}, (error) => { unblockUI('DivQuiz'); console.log(error.data); }).then(function () {
								unblockUI('DivQuiz');
            });
        }
        GetQuestion($scope.QuizId, 0, 1, 1, 0, 0, $.getQueryString('review'));

        $scope.next = function (red) {
            if ($scope.AnswerID > 0) {
                if (red) {
                    GetQuestion($scope.QuizId, $scope.QuestionId, 0, 1, 0, $scope.AnswerID, 0);
                    $window.location.href = siteRoot + 'InvestmentTutorial/ExamReviewDetail/' + $.getQueryString('id');
                } else {
                    GetQuestion($scope.QuizId, $scope.QuestionId, 0, 1, 1, $scope.AnswerID, 0);
                }
                $scope.AnswerID = '0';
            } else {
                alert("Please Choose Your Answer!")
            }
        }

        $scope.prev = function () {
            GetQuestion($scope.QuizId, $scope.QuestionId, 0, 0, -1, 0, 0);
        }
    }])
    .controller('SectionQuizCtrl', ['$scope', '$http', '$q', '$filter', '$window', function ($scope, $http, $q, $filter, $window) {
        $scope.CurrentQuestion = parseInt(CurrentQuestion);
        $scope.TotalQuestion = TotalQuestion;
        $scope.SectionId = $.getQueryString('secid');
        $scope.Review = $.getQueryString('review') > 0 ? $.getQueryString('review') : 0;
        $scope.SubSectionId = $.getQueryString('subsecid') > 0 ? $.getQueryString('subsecid') : 0;

        function GetQuestion(sectionId, subsectionId, QuestionId, flagId, Curr, rev) {
            blockUI('DivQuiz');
            $http({
                method: 'POST',
                url: siteRoot + 'InvestmentTutorial/ManageSectionQuiz',
                params: { secid: sectionId, subsecid: subsectionId, questionId: QuestionId, flag: flagId, review: rev }
            }).then(function (res) {
                $scope.CurrentQuestion = $scope.CurrentQuestion + Curr;
                $scope.quizs = res.data.quiz;
                $scope.reviewData = res.data.message;
                $scope.Answers = res.data.quiz.Options;
                $scope.QuestionId = res.data.quiz.Question.QuestionID;
							}, (error) => { unblockUI('DivQuiz'); console.log(error.data); }).then(function () {
								unblockUI('DivQuiz');
            });
        }
        $scope.AnswerById = function (val) {
            return $filter('filter')($scope.Answers, { AnswerID: val });
        }
        function SubmitAnswer(questionId, answerId) {
            $http({
                method: 'POST',
                url: siteRoot + 'InvestmentTutorial/GreadeExam',
                params: { questionId: questionId, answerId: answerId }
            })
        }

        GetQuestion($scope.SectionId, $scope.SubSectionId, 0, 1, 0, $.getQueryString('review'));

        $scope.next = function (red) {
            if ($scope.AnswerID > 0) {
                SubmitAnswer($scope.QuestionId, $scope.AnswerID);
                if (red) {
                    GetQuestion($scope.SectionId, $scope.SubSectionId, $scope.QuestionId, 1, 1, 0);
                    $window.location.href = siteRoot + 'InvestmentTutorial/SectionExamDetail/' + $.getQueryString('id');
                } else {
                    GetQuestion($scope.SectionId, $scope.SubSectionId, $scope.QuestionId, 1, 1, 0);
                }
                $scope.AnswerID = '0';
            } else {
                alert("Please Choose Your Answer!")
            }
        }

        $scope.prev = function () {
            GetQuestion($scope.SectionId, $scope.SubSectionId, $scope.QuestionId, 0, -1, 0, 0);
        }
}]);