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

var winston = require('winston');
winston.level = 'debug';
var requestHelper = require('./requestHelper');

var captchapng = require('captchapng');

router.get('/', function(req, res, next) {
  res.render('login', {title: "登录", path: '/login'});
});

router.post('/', function(req, res, next) {
  var username = req.body.username || 'shunjiean01';
  var password = req.body.password || '4539';
  var captcha = req.body.captcha || req.session.captcha;
  var captchaCookie = req.session.captcha || '';
  if(!captcha || captchaCookie != captcha){
    res.send({code:-1, message: "验证码不正确"});
  }else{
    requestHelper.loginFormInfo(function (loginFormInfo) {
      loginFormInfo['NewVerticalLoginTool1$txtUserName'] = username;
      loginFormInfo['NewVerticalLoginTool1$txtPassword'] = password;
      
      requestHelper.loginAction(loginFormInfo, function (err, cookies) {
        winston.debug('err', err);
        winston.debug('cookies', cookies);
        if(err){
          res.send({code:-1, message: err});
        }else{
          req.session.asp = cookies;
          winston.debug("cookies: ", cookies);
          res.send({code:0, message: 'success', cookies: cookies});
        }
      });
    })
  }
});


router.get('/png', function (req, res, next) {
  var width=!isNaN(parseInt(req.query.width))?parseInt(req.query.width):108;
  var height=!isNaN(parseInt(req.query.height))?parseInt(req.query.height):44;

  var code = parseInt(Math.random()*9000+1000);
  var codeStr = code + '';
  codeStr = codeStr.replace(/1/g, 2);
  codeStr = codeStr.replace(/7/g, 8);
  //req.session.checkcode = code;

  var p = new captchapng(width,height, parseInt(codeStr));
  p.color(0, 0, 0, 0);
  p.color(80, 80, 80, 255);

  var img = p.getBase64();
  var imgbase64 = new Buffer(img,'base64');

  req.session.captcha = codeStr;
  res.writeHead(200, {
    'Content-Type': 'image/png'
  });
  res.end(imgbase64);
  
});

module.exports = router;

@@ -392,25 +392,8 @@
 name
- %7C%7C 'shunjiean01'
 ;%0A  
@@ -428,18 +428,8 @@
 word
- %7C%7C '4539'
 ;%0A  
@@ -462,31 +462,8 @@
 tcha
- %7C%7C req.session.captcha
 ;%0A  

remove login user