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


const BaseController = require("../../common/base/base_controller");
const projectModel = require("../../pm/models/project_model");
const pmFacade = require('../../pm/facade/pm_facade');
const systemSettingModel = require('../../system_setting/model/index');
let config = require("../../../config/config.js");
const OptionsDao = require('../../options/models/optionsModel');
const optionSetting = require('../../options/models/optionTypes');
const regionMap = {
    '重庆定额(2018)': '重庆市',
    '广东定额(2018)': '广东省'
};
module.exports =function (app) {
    const baseController = new BaseController();
    app.get('/main', baseController.init, function(req, res) {
        let pm = require('../../pm/controllers/pm_controller');
        const projectID = +req.query.project;
        pm.checkProjectRight(req.session.sessionUser.id, projectID, async function (hasRight, projectData, isOpenShareProject, allowCooperate,allowCopy) {
            if (hasRight) {
                //分享的项目，只读、协作（允许编辑）
                let projectReadOnly = false,
                    projectCooperate = false;
                if(req.session.sessionUser.id !== projectData.userID){
                    projectData._doc.readOnly = true;
                    projectCooperate = allowCooperate;
                    //允许协作的项目允许编辑，非只读
                    projectReadOnly = !projectCooperate;
                }
                let fileKind = '1'; //默认投标文件
                let constructProject = await pmFacade.getConstructionProject(projectID);
                if (constructProject && constructProject.property && constructProject.property.fileKind) {
                    fileKind = constructProject.property.fileKind;
                }
                let optionsDao = new OptionsDao();
                let options = await optionsDao.getOptions(req.session.sessionUser.id, req.session.sessionCompilation._id);
                if(options){
                    options = await optionsDao.saveOptions(req.session.sessionUser.id, req.session.sessionCompilation._id, optionSetting);
                }
                const markReadProjectIDs = isOpenShareProject ? await pmFacade.markShareItemsRead(projectID, req.session.sessionUser.id) : [];
                const version = await systemSettingModel.getVersion();
                res.render('building_saas/main/html/main.html',
                    {
                        userAccount: req.session.userAccount,
                        userID: req.session.sessionUser.id,
                        projectData: projectData,
                        compilationName: req.session.sessionCompilation.name,
                        region: regionMap[req.session.sessionCompilation.name] || '',
                        versionName: req.session.compilationVersion,
                        projectReadOnly: projectReadOnly,
                        projectCooperate: projectCooperate,
                        allowCopy: allowCopy||false,
                        LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
                        overWriteUrl:req.session.sessionCompilation.overWriteUrl,
                        fileKind: fileKind,
                        options:JSON.stringify(options),
                        markReadProjectIDs: JSON.stringify(markReadProjectIDs),
                        version
                    });
            } else {
                res.redirect('/pm');
            }
        });
    });
}
