'use strict';
angular.module('qrzk')
    .controller('ConfidentialCtrl', function ($cookieStore, $scope, $http, appConst, $rootScope, $state, $window, toaster,$timeout) {
        $scope.pageModel = {};
        $scope.pageModel.pageSize = 32;
        $scope.search = {};
        $scope.load = function () {
            _goPage(1);
        };
        $http.get('batch?sort=id').success(function (data) {
            $scope.bacthList=data.filter(e=>{return e.type==='供卷'});
        }).error(function (data, code) {
            alert(code);
        });
        $scope.load();
        $scope.goPage = function (pageNum) {
            _goPage(pageNum);
        }

        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
                , 'batchId': $scope.search.batchId
            };
            $http.post('confidential/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.onKeyUp = function (rowform,index) {
                if(event.keyCode === 13){
                    $timeout(function() {
                        const el = document.getElementById("btn_" + (index + 2));
                        angular.element(el).triggerHandler('click');
                    }, 0);
                    rowform.$submit();
                }
        }

        $scope.save = function (data, id) {
            angular.extend(data, {id: id});
            $scope.loading = $http.post('confidential/update', data)
                .success(function (res) {
                    if (res.result) {
                        toaster.pop('success', '保存成功！');
                    }
                });
        };

        $scope.downConfidential=function () {
            if (!$scope.search.batchId) {
                toaster.pop('error', '请选择批次！');
                return;
            }

            $scope.loading= $http({
                url: './confidential/confidentialPdf',
                method: "GET",//接口方法
                params: {
                    'batchId': $scope.search.batchId
                },
                headers: {
                    'Content-type': 'application/json'
                },
                responseType: 'arraybuffer' //重点属性，必须设置
            }).success(function (data, status, headers, config) {
                const blob = new Blob([data], {type: "application/pdf"}); //这里的格式
                const objectUrl = URL.createObjectURL(blob);
                //利用浏览器打开URL实现下载
                const a = document.createElement('a');
                document.body.appendChild(a);
                a.setAttribute('style', 'display:none');
                a.setAttribute('href', objectUrl);
                var filename="交接单.pdf";
                a.setAttribute('download', filename);
                a.click(); //触发点击，下载文件
                URL.revokeObjectURL(objectUrl);
            }).error(function (data, status, headers, config) {
            });
        };
    })
