/**
 * Created by Zhong on 2017/8/3.
 */
import BaseController from "../../common/base/base_controller";
import CompilationModel from '../../users/models/compilation_model';
let config = require("../../../config/config.js");
import mongoose from 'mongoose';
const compilationModel = mongoose.model('compilation');
const rationLibModel = mongoose.model('std_ration_lib_map');
const fs = require('fs');
class ViewsController extends BaseController{
    async redirectMain(req, res){
        let filter = req.query.filter ? {compilationId: req.query.filter} : null;
        let allLibs = await rationLibModel.find({}, {_id: 0, recentOpr: 0}),
            rationLibs = allLibs.filter(lib => filter && lib.compilationId === filter.compilationId || !filter),
            allNames = allLibs.map(lib => lib.dispName);
        let compilationModel = new CompilationModel();
        let compilationList = await compilationModel.getCompilationList({_id: 1, name: 1});
        compilationList.unshift({_id: 'all', name: '所有'});
        let activeCompilation = compilationList.find(compilation => compilation._id.toString() === req.query.filter);
        if (activeCompilation) {
            activeCompilation.active = 'active';
        } else {
            compilationList[0].active = 'active'
        }
        res.render('maintain/ration_repository/main.html',
            {
                rationLibs: rationLibs,
                allNames: allNames,
                compilationList: compilationList,
                userAccount: req.session.managerData.username,
                userID: req.session.managerData.userID,
                LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
            });
    }
    async redirectRation(req, res){
        const repId = req.query.repository;
        const locked = req.query.locked || 'true';
        const redirectGlj = `/rationRepository/lmm?repository=${repId}&locked=${locked}`;
        const redirectCoe = `/rationRepository/coeList?repository=${repId}&locked=${locked}`;
        const redirectInstallation = `/rationRepository/installation?repository=${repId}&locked=${locked}`;
        let overWriteUrl = null;
        let priceProperties = [];
        let stdRationLib = await rationLibModel.findOne({ID: repId});
        if(stdRationLib){
            let compilation = await compilationModel.findOne({_id: mongoose.Types.ObjectId(stdRationLib.compilationId)});
            priceProperties = compilation.priceProperties ? compilation.priceProperties : [];
            let absoluteUrl = compilation.overWriteUrl ? req.app.locals.rootDir + compilation.overWriteUrl : req.app.locals.rootDir;
            overWriteUrl = fs.existsSync(absoluteUrl) && fs.statSync(absoluteUrl).isFile()? compilation.overWriteUrl : null;
        }
        res.render('maintain/ration_repository/dinge.html',
            {
                userAccount: req.session.managerData.username,
                userID: req.session.managerData.userID,
                redirectGlj: redirectGlj,
                redirectCoe: redirectCoe,
                redirectInstallation: redirectInstallation,
                LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
                priceProperties: JSON.stringify(priceProperties)
            });
    }
    async redirectGlj(req, res){
        const repId = req.query.repository;
        const locked = req.query.locked || 'true';
        const redirectRation = `/rationRepository/ration?repository=${repId}&locked=${locked}`;
        const redirectCoe = `/rationRepository/coeList?repository=${repId}&locked=${locked}`;
        const redirectInstallation = `/rationRepository/installation?repository=${repId}&locked=${locked}`;
        let overWriteUrl = null;
        let priceProperties = [];
        let stdRationLib = await rationLibModel.findOne({ID: repId});
        if(stdRationLib){
            let compilation = await compilationModel.findOne({_id: mongoose.Types.ObjectId(stdRationLib.compilationId)});
            priceProperties = compilation.priceProperties ? compilation.priceProperties : [];
            let absoluteUrl = compilation.overWriteUrl ? req.app.locals.rootDir + compilation.overWriteUrl : req.app.locals.rootDir;
            overWriteUrl = fs.existsSync(absoluteUrl) && fs.statSync(absoluteUrl).isFile()? compilation.overWriteUrl : null;
        }
        res.render('maintain/ration_repository/gongliao.html',
            {
                userAccount: req.session.managerData.username,
                userID: req.session.managerData.userID,
                redirectRation: redirectRation,
                redirectCoe: redirectCoe,
                redirectInstallation: redirectInstallation,
                LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
                priceProperties: JSON.stringify(priceProperties),
                rationLib: stdRationLib
            });
    }
    redirectCoeList(req, res){
        const repId = req.query.repository;
        const locked = req.query.locked || 'true';
        const redirectGlj = `/rationRepository/lmm?repository=${repId}&locked=${locked}`;
        const redirectRation = `/rationRepository/ration?repository=${repId}&locked=${locked}`;
        const redirectInstallation = `/rationRepository/installation?repository=${repId}&locked=${locked}`;
        res.render('maintain/ration_repository/fuzhu.html',
            {
                userAccount: req.session.managerData.username,
                userID: req.session.managerData.userID,
                redirectGlj: redirectGlj,
                redirectRation: redirectRation,
                redirectInstallation: redirectInstallation,
                LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
            });
    }
    async redirectInstallation(req, res){
        const repId = req.query.repository;
        const locked = req.query.locked || 'true';
        const redirectGlj = `/rationRepository/lmm?repository=${repId}&locked=${locked}`;
        const redirectCoe = `/rationRepository/coeList?repository=${repId}&locked=${locked}`;
        const redirectRation = `/rationRepository/ration?repository=${repId}&locked=${locked}`;
        let stdRationLib = await rationLibModel.findOne({ID: repId});
        res.render('maintain/ration_repository/anzhuang.html',
            {
                userAccount: req.session.managerData.username,
                userID: req.session.managerData.userID,
                redirectGlj: redirectGlj,
                redirectCoe: redirectCoe,
                redirectRation: redirectRation,
                LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
                rationLib: stdRationLib
            });
    }
}

export default ViewsController;