/**
 * pm: project management
 * Created by Mai on 2017/3/8.
 */

let express = require('express');
let pmController = require('./../controllers/pm_controller');

module.exports = function (app) {

    app.get('/pm', function(req, res){
        res.render('building_saas/pm/html/project-management.html',
            {userAccount: req.session.userAccount,
                userID: req.session.sessionUser.ssoId});
    });

    let pmRouter = express.Router();

    pmRouter.use(function (req, res, next) {
        if (/\/getNewProjectID/.test(req.originalUrl) || pmController.checkRight(req, res)) {
            next();
        } else {
            res.json({error: 1, message: '对不起，您无权限操作。', data: null});
        }
    });

    /*
     req.body = {data: '{user_id}'}
     */
    pmRouter.post('/getProjects', pmController.getProjects);

    /*
     req.body = {data: '{user_id: user_id, updateData: [{updateType, updateData}]}'}
     data.updateData.updateType: 1 of ['new', 'update', 'delete']
     */
    pmRouter.post('/updateProjects', pmController.updateProjects);

    /*
     req.body = {data: '{user_id: user_id, updateData: [{updateType, updateData}]}'}
     data.updateData.updateType: 1 of ['update', 'copy']
     */
    pmRouter.post('/copyProjects', pmController.copyProjects);
    pmRouter.post('/renameProject', pmController.rename);
    pmRouter.post('/beforeOpenProject', pmController.beforeOpenProject);
    pmRouter.post('/getProject', pmController.getProject);
    pmRouter.post('/getNewProjectID', pmController.getNewProjectID);

    app.use('/pm/api', pmRouter);
};


