/**
 * Created by liz on 2015/3/28.
 */
var co = require('co');
var bizCore = require('@gopalroy/biz-core');
var bizTestDrive = require('@gopalroy/biz-testdrive');
var express = require('express');
var ApiHelper = require('./ApiHelper');
var debug = require('debug')('@gopalroy/test-drive');

var alarmAPI = (function(){
    function AlarmAPI(){
        var router = new express();
        //router.get('/writeAlarmInfo', this.writeAlarmInfo);
        router.put('/', this.update);
        router.get('/alarmTDriving', this.getAlarmTDrivingById); //获取单个报警信息的 试驾信息
        this.router = router;
    }

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

    AlarmAPI.prototype.resolveAlarm = function(req, res, next){
        co(function*(){
            req.incar.alarm = yield req.incar.testDrive.getAlarmByIdG(req.params.alarmId);
            next();
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    };

    //修改
    AlarmAPI.prototype.update = function(req,res){
        co(function*(){
            var data = req.body;
            var AlarmNew = req.incar.alarm;
            for(var k in data){
                if(data.hasOwnProperty(k)){
                    AlarmNew[k] = data[k]
                }
            }
            yield AlarmNew.updateG();
            res.json({status:'ok',info:AlarmNew});
        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };

    AlarmAPI.prototype.getAlarmTDrivingById = function(req,res){
        co(function*(){
            var alarm = req.incar.alarm;
            var result = yield bizTestDrive.TestDriveAlarm.getAlarmTDrivingByIdG(alarm.s4_id,alarm.id,alarm.alarmId);
            res.json({status:'ok',alarmTDriving:result});
        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    };





    ////写入报警信息
    //AlarmAPI.prototype.writeAlarmInfo = function(req,res){
    //    co(function*(){
    //        var tdriveId = this.tdriveId;
    //        var count = yield bizCore.Alarm.writeAlarmInfoG(tdriveId);
    //        res.json({status:'ok',total:count});
    //    }).catch(function(ex){
    //        res.status(500).send(ex.stack);
    //    });
    //};




    return AlarmAPI;
})();

module.exports = alarmAPI;
