/**
 * Created by wh on 2015/7/7.
 */
var co = require('co');
var bizCore = require('@gopalroy/biz-core');
var express = require('express');
var ApiHelper = require('./ApiHelper');
var debug = require('debug')('@gopalroy/test-drive');

var staffAPI = (function(){
    function StaffAPI(){
        var router = new express();
        router.get('/', this.get);
        router.put('/', this.update);
        router.delete('/', this.remove);


        this.router = router;
    }

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

    StaffAPI.prototype.resolveStaff = function(req, res, next){
        co(function*(){
            req.incar.staff = yield req.incar.testDrive.getStaffByIdG(req.params.staffId);
            next();
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    };

    //查询
    StaffAPI.prototype.get = function(req, res){
        res.json(req.incar.staff.toSafe());
    };

    //修改
    StaffAPI.prototype.update = function(req, res){
        co(function*(){
            var data = req.body;
            var staffNew = req.incar.staff;
            for(var k in data){
                if(data.hasOwnProperty(k)){
                    staffNew[k] = data[k]
                }
            }

            yield staffNew.updateG();
            res.json({status:'ok',staffNew:staffNew});
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    };

    //删除
    StaffAPI.prototype.remove = function(req, res){
        co(function*(){
            yield req.incar.testDrive.removeStaffG(req.incar.staff);
            res.json({status:'ok'});
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    };

    //查询当前登录用户信息
    StaffAPI.getStaffLogin = function(req,res){
        var loginStaff  = req.incar.loginStaff;
        res.send(loginStaff.toSafe());
    };

    return StaffAPI;
})();

module.exports = staffAPI;
