/**
 * Created by chen on 2017/7/17.
 */


import BaseController from "../../common/base/base_controller";
const projectModel = require("../../pm/models/project_model");
let config = require("../../../config/config.js");
module.exports =function (app) {
    const baseController = new BaseController();
    app.get('/main', baseController.init, function(req, res) {
        let pm = require('../../pm/controllers/pm_controller');

        pm.checkProjectRight(req.session.sessionUser.id, req.query.project, async function (hasRight, projectData, shareInfo) {
            if (hasRight) {
                //分享的项目，只读、协作（允许编辑）
                let projectReadOnly = false,
                    projectCooperate = false;
                if(req.session.sessionUser.id !== projectData.userID){
                    projectData._doc.readOnly = true;
                    projectCooperate = !!shareInfo.allowCooperate;
                    //允许协作的项目允许编辑，非只读
                    projectReadOnly = !projectCooperate;
                }
                res.render('building_saas/main/html/main.html',
                    {
                        userAccount: req.session.userAccount,
                        userID: req.session.sessionUser.id,
                        projectData: projectData,
                        compilationName: req.session.sessionCompilation.name,
                        versionName: `纵横建筑云计价(${req.session.compilationVersion})`,
                        projectReadOnly: projectReadOnly,
                        projectCooperate: projectCooperate,
                        LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
                        overWriteUrl:req.session.sessionCompilation.overWriteUrl
                    });
            } else {
                res.redirect('/pm');
            }
        });
    });
}
