/**
 * Created by Zhong on 2017/8/3.
 */
import BaseController from "../../common/base/base_controller";
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{
    redirectMain(req, res){
        res.render('maintain/ration_repository/main.html',
            {
                userAccount: req.session.managerData.username,
                userID: req.session.managerData.userID
            });
    }
    async redirectRation(req, res){
        const repId = req.query.repository;
        const redirectGlj = `/rationRepository/lmm?repository=${repId}`;
        const redirectCoe = `/rationRepository/coeList?repository=${repId}`;
        const redirectInstallation = `/rationRepository/installation?repository=${repId}`;
        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 redirectRation = `/rationRepository/ration?repository=${repId}`;
        const redirectCoe = `/rationRepository/coeList?repository=${repId}`;
        const redirectInstallation = `/rationRepository/installation?repository=${repId}`;
        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)
            });
    }
    redirectCoeList(req, res){
        const repId = req.query.repository;
        const redirectGlj = `/rationRepository/lmm?repository=${repId}`;
        const redirectRation = `/rationRepository/ration?repository=${repId}`;
        const redirectInstallation = `/rationRepository/installation?repository=${repId}`;
        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)
            });
    }
    redirectInstallation(req, res){
        const repId = req.query.repository;
        const redirectGlj = `/rationRepository/lmm?repository=${repId}`;
        const redirectCoe = `/rationRepository/coeList?repository=${repId}`;
        const redirectRation = `/rationRepository/ration?repository=${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)
            });
    }
}

export default ViewsController;