/**
 * Created by Zhong on 2017/12/22.
 */

import BaseController from '../../common/base/base_controller';
import CompleViewModel from '../models/compleViewModel';
import EngineeringLibModel from "../../users/models/engineering_lib_model";
let config = require("../../../config/config.js");
let compleViewModel = new CompleViewModel();
import CompleRationDao from '../models/compleRationModel';
const compleRationDao = new CompleRationDao();
let callback = function (req, res, err, msg, data) {
    res.json({error: err, message: msg, data: data});
};

async function getGljLibId(sessionCompilation) {
    let gljLibId = null,
        rationValuation = sessionCompilation.ration_valuation,
        billValuation = sessionCompilation.bill_valuation,
        engineeringLibModel = new EngineeringLibModel(),
        valuationIDs = [] ;
    for(let r of rationValuation){//{ "glj_lib.0": {$exists:1} }
        if(r.id){
            valuationIDs.push(r.id);
        }
    }
    for(let b of billValuation){
        if(b.id){
            valuationIDs.push(b.id);
        }
    }
    if(valuationIDs.length > 0){
        let engineeringInfo = await engineeringLibModel.findDataByCondition({'valuationID': {"$in": valuationIDs},"glj_lib.0": {$exists:1}});//数组大于0
        gljLibId = engineeringInfo.glj_lib.length > 0 && typeof engineeringInfo.glj_lib !== 'undefined' ? engineeringInfo.glj_lib[0].id : null;
    }
    return gljLibId;
}

class CompleViewController extends BaseController{
    async redirectRation(req, res){
        const gljLibId = await getGljLibId(req.session.sessionCompilation);
        const redirectGlj = `/complementaryRation/glj`;
        const redirectCoe = `/complementaryRation/coe`;
        const redirectInstallation = `/complementaryRation/installation`;
        res.render('building_saas/complementary_ration_lib/html/dinge.html', {
            userID: req.session.sessionUser.id,
            redirectGlj: redirectGlj,
            redirectCoe: redirectCoe,
            redirectInstallation: redirectInstallation,
            gljLibId: gljLibId,
            compilationName: req.session.sessionCompilation.name,
            versionName: req.session.compilationVersion,
            LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
        });
    }

    async prepareInitData (req, res) {
        try {
            const gljLibId = await getGljLibId(req.session.sessionCompilation);
            const userId = req.session.sessionUser.id;
            const compilationId = req.session.sessionCompilation._id;
            const initData = await compleRationDao.prepareInitData(userId, compilationId, gljLibId);
            res.json({error: 0, message: 'success', data: initData});
        } catch (err) {
            res.json({error: 1, message: 'fail', data: null});
        }
    }

    async redirectGljList(req, res){
        const gljLibId = await getGljLibId(req.session.sessionCompilation);
        const redirectRation = `/complementaryRation/ration`;
        const redirectCoe = `/complementaryRation/coe`;
        const redirectInstallation = `/complementaryRation/installation`;
        res.render('building_saas/complementary_ration_lib/html/gongliao.html', {
            userID: req.session.sessionUser.id,
            redirectRation: redirectRation,
            redirectCoe: redirectCoe,
            redirectInstallation: redirectInstallation,
            gljLibId: gljLibId,
            compilationName: req.session.sessionCompilation.name,
            versionName: req.session.compilationVersion,
            LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
        });
    }

    async redirectCoeList(req, res){
        const gljLibId = await getGljLibId(req.session.sessionCompilation);
        const redirectRation = `/complementaryRation/ration`;
        const redirectGlj = `/complementaryRation/glj`;
        const redirectInstallation = `/complementaryRation/installation`;
        res.render('building_saas/complementary_ration_lib/html/fuzhu.html', {
            userID: req.session.sessionUser.id,
            redirectRation: redirectRation,
            redirectGlj: redirectGlj,
            redirectInstallation: redirectInstallation,
            gljLibId: gljLibId,
            compilationName: req.session.sessionCompilation.name,
            versionName: req.session.compilationVersion,
            LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
        });
    }

    redirectInstallation(req, res){
        const repId = req.query.repository;
        const redirectRation = `/complementaryRation/ration`;
        const redirectGlj = `/complementaryRation/glj?repository=${repId}`;
        const redirectCoe = `/complementaryRation/coe?repository=${repId}`;
        res.render('building_saas/complementary_ration_lib/html/anzhuang.html', {
            userID: req.session.sessionUser.id,
            redirectRation: redirectRation,
            redirectGlj: redirectGlj,
            redirectCoe: redirectCoe,
            compilationName: req.session.sessionCompilation.name,
            versionName: req.session.compilationVersion,
            LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
        });
    }

    getRationLib(req, res){
        let data = JSON.parse(req.body.data);
        compleViewModel.getRationLib(data.rationRepId, function (err, data) {
            callback(req, res, err, '', data);
        });
    }

    getRationLibs(req, res){
        let data = JSON.parse(req.body.data);
        compleViewModel.getRationLibs(req.session.sessionCompilation._id, data.ids, function (err, data) {
            callback(req, res, err, '', data);
        });
    }
}

export default CompleViewController;