var express = require('express');

var config = require("./config/config.js");
config.setToQaDb();
//config.setupCache();
var cfgCacheUtil = require("./config/cacheCfg");
cfgCacheUtil.setupDftCache();

var path = require('path');
var session = require('express-session');
var DBStore = require('connect-mongo')(session);

var URL = require('url')

var app = express();
var _rootDir = __dirname;

app.use(express.static(_rootDir));

app.set('views', path.join(__dirname, 'web'));
app.engine('.html', require('ejs').__express);
app.set('view engine', 'html');

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

app.use(session({
    name: 'usersSession',
    secret: 'session users secret',
    cookie: {maxAge: 1000*60*30},
    resave: false,
    saveUninitialized: true
    //*
    ,store: new DBStore({
        url: 'mongodb://' + config.current.server + ':' + config.current.port + '/usersManagesOpr'
    })
    //*/
}));

app.use(function (req, res, next) {
    var url = req.originalUrl, referer;
    if (!/^\/login/.test(req.originalUrl) && !req.session.userAccount) {
        if (/\/api/.test(req.originalUrl)) {
            referer = URL.parse(req.headers.referer);
            return res.redirect('/login' + '?referer=' + referer.path);
        } else {
            return res.redirect("/login" + '?referer=' + req.originalUrl);
        }
    }
    next();
});

app.use('/', require('./modules/users/routes/users_route'));

var rpt_Router = require("./modules/reports/routes/report_router");
app.get('/report',  function(req, res) {
    if (!req.session.userAccount) {
        res.redirect('/login');
    }
    else {
        res.render('maintain/report/rpt_test.html',
            {userAccount: req.session.userAccount,
                userID: req.session.userID});
    }
});
app.use("/report_api", rpt_Router);

var rptTpl_Router = require("./modules/reports/routes/report_router");
app.get('/rpt_tpl',  function(req, res) {
    if (!req.session.userAccount) {
        res.redirect('/login');
    }
    else {
        res.render('maintain/report/rpt_tpl_main.html',
            {userAccount: req.session.userAccount,
                userID: req.session.userID});
    }
});
app.use("/report_tpl_api", rptTpl_Router);

var rationRepository_Router = require("./modules/ration_repository/routes/ration_rep_routes");
app.get('/rationRepository/main', function(req, res) {
    res.render('maintain/ration_repository/main.html',
        {
            userAccount: req.session.userAccount,
            userID: req.session.userID
        });
});
app.get('/rationRepository/ration', function(req, res) {
    res.render('maintain/ration_repository/dinge.html',
        {
            userAccount: req.session.userAccount,
            userID: req.session.userID
        });
});
app.get('/rationRepository/lmm', function(req, res) {
    res.render('maintain/ration_repository/gongliao.html',
        {
            userAccount: req.session.userAccount,
            userID: req.session.userID
        });
});

app.get('/rationRepository/coeList', function(req, res) {
    res.render('maintain/ration_repository/fuzhu.html',
        {
            userAccount: req.session.userAccount,
            userID: req.session.userID
        });
});

app.use("/rationRepository/api",rationRepository_Router);


app.use(function(req, res, next) {
    res.status(404).send('404 Error');
});
app.use(function(err, req, res, next) {
    console.error(err.stack);
    res.status(500).send('500 Error');
});
app.listen(6080);
