/*
 * script route generated by KOMA
 * @author : Yan Yan Purdiansah
 */    
var express = require('express');
var router = express.Router();
var dateFormat = require('dateformat');    
var database = require('./dao');
var dao = new database.Dao();    
var sessionChecker = require('./security');
var appresource = {
    Title:'MsbRemoteserverRead',
    Heading:'MsbRemoteserver',
    Xmppdomain:{label :'Xmppdomain'},
    Remoteaddress:{label :'Remoteaddress'},
    Remoteport:{label :'Remoteport'},
    User:{label :'User'},
    Password:{label :'Password'},
    Permission:{label :'Permission'},
    }

router.get('/', sessionChecker, function(req, res, next) {
    var jsonObj = req.query;
    jsonObj.rowid = 0;
    req.session.previouspath = 'readMsbRemoteserver?'+req._parsedUrl.query;
    parameter = {
        session: req.session,
        cookies: req.cookies,
        resource: appresource,
        dateFormat: dateFormat,
    }
    var readMsbRemoteserver = new Promise((resolve, reject) => {
        setTimeout(() => {
            dao.readMsbRemoteserver(jsonObj,function(err,rows) {
                if(err) res.status(err.code).end();
                else{
                    if(rows[0]===undefined) res.redirect('/searchMsbRemoteserver');
                    else{ 
                        jsonObj = rows[0];
                        resolve(jsonObj);
                    }
                }
            });
        },1000);
    });
    
    var sendResult = (results) => {
        console.log(jsonObj)
        parameter.record = jsonObj;        
        res.render('MsbRemoteserverReadPage',parameter);
    }    
    Promise.all([readMsbRemoteserver]).then(sendResult);
});

module.exports = router;
