/**
 * Created by wh on 2015/7/17.
 * 报告
 */
define(['../testDrive', 'jquery','prevload'],function(module, $){
    module.controller("testDrivingReportCtrl",function($scope,$resource,$location,$filter,$timeout,$routeParams,testDriveService){
        //initCopyRight(); //版权信息位置调整
        var pathname = 'testDrive';
        var apiDrivingReport = $resource('/'+pathname+'/api/testDrive/:tdriveId/report');  //获取所有报告 get
        var apiDrivingReportById = $resource('/'+pathname+'/api/testDrive/:tdriveId/report/:reportId');  //获取一条报告

        $scope.currentPage = 1;//车辆主页默认当前页
        $scope.pageRecord = 10; //分页记录数

        testDriveService.delQueryUrl("page",function(data){ //当前页
            $scope.currentPage = data;
        });

        //查询路径
        $scope.changePath = function(currentPage) {
            var hash = 'page='+currentPage;
            $location.path($location.path()).search(hash);
        };

        $scope.reportPaging = function(currentPage){
            if(!currentPage) $scope.currentPage = 1;
            else $scope.currentPage = currentPage;
            $scope.postData ={
                tdriveId:$scope.tdriveId,
                sortBy: "id-"
            };

            var nextMarker = parseInt($scope.pageRecord * ($scope.currentPage - 1)) + 1;
            $scope.postData.pageSize = $scope.pageRecord;
            $scope.postData.nextMarker = nextMarker;

            $scope.drivingReports = [];
            //试驾报告信息
            apiDrivingReport.get($scope.postData, function(data) {
                $scope.drivingReports = data.drivingReport;
                angular.forEach($scope.drivingReports,function(dr){
                    if(dr.oilUsed == null){
                        dr.oilUsed = 0;
                    }else{
                        dr.oilUsed = dr.oilUsed.toFixed(3);
                    }
                });

                if(data.total > 0){
                    PagingInfo(data.total);
                }else{
                    alert("暂无数据");
                }
            });
            $scope.currentPage =  (nextMarker - 1) / $scope.pageRecord + 1;
        };

        $scope.reportPaging($scope.currentPage);

        //打印预览
        $scope.printPreview = function(index){
            var id = $scope.drivingReports[index].id;
            window.open ('/testDrive/view/report/print.html?rid='+id+"&sid="+$scope.tdriveId, 'newwindow', 'height=670, width=720, top=50, left=301, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no');//这句要写成一行

            //$('#printpreview').modal('show');
        };
        //打印
        $scope.print = function(){
            var bdhtml = window.document.body.innerHTML;//获取当前页的html代码
            var sprnstr = "<!--startprint1-->";//设置打印开始区域
            var eprnstr = "<!--endprint1-->";//设置打印结束区域
            var prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr)+18); //从开始代码向后取html

            prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html
            window.document.body.innerHTML=prnhtml;
            window.print();
            //window.document.body.innerHTML=bdhtml;
        };

        //分页数据处理方法
        function PagingInfo(totalCount) {
            $scope.totalCount = totalCount;
            $scope.totalPage = Math.ceil($scope.totalCount / $scope.pageRecord);
            $scope.totalOption = [{}];
            for (var i = 0; i < $scope.totalPage; i++) {
                $scope.totalOption[i] = {size: i + 1};
            }
        }
    });
})


