var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/helloworld', function(req, res) {
    res.render('helloworld', { title: 'Hello, World!' });
});
router.get('/userlist', function(req, res) {
    var db = req.db;
    var collection = db.get('usercollection');
    collection.find({},{},function(e,docs){
        res.render('userlist', {
            "userlist" : docs
        });
    });
});

function renderLogin(req, res) {
	res.render('login.jade', {title: 'Login'});
}

router.get('/login', renderLogin);
router.post('/loginsub',function(req, res) {
    console.log("reach the server");

    var userName = "ben@gmail.com";
    var password = "abc123";
    if((userName.toLowerCase()=="ben@gmail.com")&&(password=="abc123")){
        console.log("success");
        //resp.success = true;
    } else {
        //resp.success = false;
    }
    // Return the resp object
    //res.send(resp);
});

router.post('/adduser', function(req, res) {

    // Set our internal DB variable
    var db = req.db;

    // Get our form values. These rely on the "name" attributes
    var userName = req.body.username;
    var userEmail = req.body.useremail;

    // Set our collection
    var collection = db.get('usercollection');

    // Submit to the DB
    collection.insert({
        "username" : userName,
        "email" : userEmail
    }, function (err, doc) {
        if (err) {
            // If it failed, return error
            res.send("There was a problem adding the information to the database.");
        }
        else {
            // And forward to success page
            res.redirect("userlist");
        }
    });
});

module.exports = router;


var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/helloworld', function(req, res) {
    res.render('helloworld', { title: 'Hello, World!' });
});
router.get('/userlist', function(req, res) {
    var db = req.db;
    var collection = db.get('usercollection');
    collection.find({},{},function(e,docs){
        res.render('userlist', {
            "userlist" : docs
        });
    });
});

function renderLogin(req, res) {
	res.render('login.jade', {title: 'Login'});
}

router.get('/login', renderLogin);
router.post('/loginsub',function(req, res) {
    console.log("reach the server");

    var userName = "ben@gmail.com";
    var password = "abc123";
    if((userName.toLowerCase()=="ben@gmail.com")&&(password=="abc123")){
        console.log("success");
        resp.success = true;
    } else {
        resp.success = false;
    }
    // Return the resp object
    res.send(resp);
});

router.post('/adduser', function(req, res) {

    // Set our internal DB variable
    var db = req.db;

    // Get our form values. These rely on the "name" attributes
    var userName = req.body.username;
    var userEmail = req.body.useremail;

    // Set our collection
    var collection = db.get('usercollection');

    // Submit to the DB
    collection.insert({
        "username" : userName,
        "email" : userEmail
    }, function (err, doc) {
        if (err) {
            // If it failed, return error
            res.send("There was a problem adding the information to the database.");
        }
        else {
            // And forward to success page
            res.redirect("userlist");
        }
    });
});

module.exports = router;


return resp object from the post request