'use strict';
angular.module('qrzk')
    .controller('PaperAnalysisCtrl', function ($q, $cookieStore, $scope, $http,$modal) {
        $scope.pageModel = {};
        $scope.pageModel.pageSize = 15;
        $scope.search = {};
        $scope.page='page'
        $scope.search.order='asc'
        $scope.load = function () {
            _goPage(1);
        };
        $http.get('papers/analysis/queryAllBatch').success(function (data) {
            $scope.bacthList = data;
        }).error(function (data, code) {
            alert(code);
        });
        $scope.load();
        $scope.goPage = function (pageNum) {
            _goPage(pageNum);
        }
        $scope.sort = function (key) {
            $scope.search.sort=key;
            $scope.search.order=$scope.search.order=='asc'?'desc':'asc';
            _goPage(1);
        }
        function _goPage(pageNum) {
            if (pageNum < 1 || (pageNum > $scope.pageModel.totalPage && $scope.pageModel.totalPage > 0)) {
                return;
            }
            $scope.pageModel.curPage = pageNum;
            const params = {
                'page': pageNum
                , 'rows': $scope.pageModel.pageSize
                , 'keyword': $scope.search.keyword
                , 'batch': $scope.search.batch
                , 'sort': $scope.search.sort
                , 'order': $scope.search.order
            };
            $http.post('papers/analysis/page', params).success(function (data) {
                $scope.pageModel.array = data.rows;
                $scope.pageModel.total = data.total;//总页数
                $scope.pageModel.totalPage = Math.ceil(data.total / $scope.pageModel.pageSize);
            })
                .error(function (data, code) {
                    alert(code);
                });
        }

        $scope.showChart = function (row,type,name){
            $http.get('papers/analysis/list/' + row.batch+'/'+row.courseCode).success(function (data) {
                $scope.page='chart';
                $scope.show(data,type,name);
            }).error(function (data, code) {
                alert(code);
            });
        }

        $scope.show = function (list,type,name) {

            const tooltip = {
                useHTML: true,
                formatter: function () {
                    const _this = this;
                    const row = list.filter((n) => n.province.toString() === _this.x.toString())[0];
                    return '<span style="font-size:10px">' + row.province + '</span><table>'
                        + '<tr><td style="color:' + this.series.color + ';padding:0">覆盖面: </td><td style="padding:0"><b>' + row.coverage + '</b></td></tr>'
                        + '<tr><td style="color:' + this.series.color + ';padding:0">科学性: </td><td style="padding:0"><b>' + row.scientificity + '</b></td></tr>'
                        + '<tr><td style="color:' + this.series.color + ';padding:0">难易度: </td><td style="padding:0"><b>' + row.facilityValue + '</b></td></tr>'
                        + '<tr><td style="color:' + this.series.color + ';padding:0">认知层次: </td><td style="padding:0"><b>' + row.cognitiveLevel + '</b></td></tr>'
                        + '<tr><td style="color:' + this.series.color + ';padding:0">题型题量: </td><td style="padding:0"><b>' + row.questionType + '</b></td></tr>'
                        + '<tr><td style="color:' + this.series.color + ';padding:0">试卷规范: </td><td style="padding:0"><b>' + row.paperNorm + '</b></td></tr>'
                        + '<tr><td style="color:' + this.series.color + ';padding:0">实考人数: </td><td style="padding:0"><b>' + row.actualNumber + '</b></td></tr>'
                        + '<tr><td style="color:' + this.series.color + ';padding:0">及格人数: </td><td style="padding:0"><b>' + row.passesNumber + '</b></td></tr>'
                        + '<tr><td style="color:' + this.series.color + ';padding:0">及格率: </td><td style="padding:0"><b>' + row.passRate + '</b></td></tr>'
                        + '<tr><td style="color:' + this.series.color + ';padding:0">平均分: </td><td style="padding:0"><b>' + row.average + '</b></td></tr></table>'
                }
            };

            const chart = {
                type: 'column'
            };
            const title = {
                text: list[0].batch + '考次，课程【' + list[0].courseName + '】'+name+'柱状图'
            };
            const subtitle = {
                // text: 'Source: runoob.com'
            };

            const xAxis = {
                categories: list.map(item => {
                    return item.province.toString();
                }),
                crosshair: true
            };
            const yAxis = {
                min: 0,
                title: {
                    text: name
                }
            };
            const plotOptions = {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
                }
            };
            const credits = {
                enabled: false
            };

            const series = [{
                name: '省份',
                data: list.map(item => {
                    return parseInt(item[type]);
                })
            }];

            const json = {};
            json.chart = chart;
            json.title = title;
            json.subtitle = subtitle;
            json.tooltip = tooltip;
            json.xAxis = xAxis;
            json.yAxis = yAxis;
            json.series = series;
            json.plotOptions = plotOptions;
            json.credits = credits;
            $('#container').highcharts(json);
        }
        var uploadModal = $modal({title:'上传数据',scope: $scope, templateUrl: 'resources/app/paperanalysis/upload.html', show: false});
        $scope.upload = function(){
            $scope.uploadModal = uploadModal;
            uploadModal.$promise.then(uploadModal.show);
        }
        $scope.$on('PaperAnalysisUploadCtrl', function (event) {
            $scope.load();
        });
    }).controller('PaperAnalysisUploadCtrl', function ($q, $cookieStore, $scope, $http, appConst, $rootScope, $state, $window, toaster, FileUploader,) {
    $scope.batch=''
    $scope.uploader = new FileUploader();

    $scope.files = $scope.uploader.queue;
    $scope.upload = function () {
        // const batch = prompt('请输入考次', '');
        if($scope.batch) {
            debugger
            $scope.loading = $q.defer().promise;
            const files = $scope.uploader.queue;
            angular.extend(files[0], {url: 'papers/analysis/upload/' +$scope.batch}).upload();
        } else {
            toaster.pop('error', '请输入考次');
        }
    };

    $scope.uploader.onSuccessItem = function (fileItem, response, status, headers) {
        if (response.flag === 'success') {
            toaster.pop('success', '导入成功！');
        } else {
            toaster.pop('error', response.msg);
        }
        $scope.uploader.clearQueue();
        $scope.files = $scope.uploader.queue;
    };


    $scope.uploader.onCompleteAll = function () {
        //$scope.load();
        $scope.loading = null;
        $scope.uploader.clearQueue();
        $rootScope.$broadcast('PaperAnalysisUploadCtrl');
        $scope.uploadModal.hide();
    };




})
