var ut = require('../models/users');
var rq = require('request');
var online = true;

module.exports = {
    userLogin: function (req, res) {
        var account = req.body.account;
        var pw = req.body.pw;

        if (online) {
            rq.post(
                {
                    url: 'http://sso.smartcost.com.cn/api/jzlogin',
                    form: {username: account, userpasswd: pw},
                    encoding: 'utf8'
                },
                function (err, response, userData) {
                    if (response.statusCode !== 200) {
                        res.json({state: false, error: '通行证验证失败！出错代号：' + response.statusCode});
                    } else {
                        switch (userData) {
                            case '-2':
                                res.json({state: false, error: '密码错误！'});
                                break;
                            case '-22':
                                res.json({state: false, error: '输入的邮箱/手机无效！'});
                                break;
                            default:
                                userData = JSON.parse(userData);
                                req.session.userID = userData[0].id;
                                req.session.userAccount = userData[0].username;
                                req.session.userEmail = userData[0].useremail;
                                req.session.userMobile = userData[0].mobile;
                                res.json({state: true, data: userData});
                        }

                    }
                }
            );
        }
        else {
            ut.findUser(account, pw, function (err, userData) {
                if (err) {
                    res.json({state: false, error: err});
                }
                else {
                    req.session.userID = userData[0].profile.id;
                    req.session.userName = userData[0].profile.userName;
                    req.session.userAccount = userData[0].profile.userAccount;
                    req.session.userEmail = userData[0].profile.useRemail;
                    req.session.userMobile = userData[0].profile.Mobile;
                    res.json({state: true, data: userData});
                }
                ;
            })
        }
    },

    userReg: function (req, res) {
        var account = req.body.account;
        var pw = req.body.pw;
        var name = req.body.name;

        ut.checkAccount(account, function (userData) {
            if (!userData) {  // 找不到用户，则注册
                ut.addUser(account, pw, name, function (err) {
                    if (err) {
                        res.json({state: false, error: '“注册失败，原因：' + err});
                    }
                    else {
                        res.json({state: true});
                    }
                });
            }
            else {  // 找到用户，则提示
                res.json({state: false, error: '“' + account + '”已存在！'});
            }
            ;
        })
    }

}



