/**
 * Created by wh on 2015/7/24.
 */

var co = require('co');
var bizTestDrive = require('@gopalroy/biz-testdrive');
var express = require('express');
var ApiHelper = require('./ApiHelper');
var debug = require('debug')('@gopalroy/test-drive');

var TripAnalysisAPI = (function(){
    function VehicleAnalysisAPI(){
        var router = new express();
        router.get('/', this.getAnalysisInfo);
        this.router = router;
    }

    VehicleAnalysisAPI.prototype.getRouter = function(){
        return this.router;
    };

    VehicleAnalysisAPI.prototype.resolveTripAnalysis = function(req, res, next){
        co(function*(){
            this.tdriveId = req.incar.testDrive.id;
            this.vehicleId = req.params.vehicleId;
            next();
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    };
    VehicleAnalysisAPI.prototype.getAnalysisInfo = function(req,res){
        co(function*(){
            var  data = req.query;
            var tm_start = "";
            var tm_end = "";
            if(data['tm_start']) tm_start = data['tm_start'];
            if(data['tm_end']) tm_end = data['tm_end'];
            var tdriveId = this.tdriveId;
            var vehicleId = this.vehicleId;
            var analysisData  = yield bizTestDrive.VehicleAnalysis.getCountingData(tdriveId,vehicleId,tm_start,tm_end);
            var analysisCount = yield bizTestDrive.VehicleAnalysis.getTestDrivingCount(tdriveId,vehicleId,tm_start,tm_end);     //出车次数
            res.json({status:'ok',analysisData:analysisData,analysisCount:analysisCount});
        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };







    return VehicleAnalysisAPI;
})();

module.exports = TripAnalysisAPI;
