'use strict';

/**
 * 模块依赖.
 */
var mongoose = require('mongoose'),
	errorHandler = require('./errors.server.controller'),
	Customer = mongoose.model('Customer'),
	_ = require('lodash');

/**
 * 创建商品
 */
exports.create = function(req, res) {
	var customer = new Customer(req.body);
	customer.user = req.user;

	customer.save(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.json(customer);
		}
	});
};

/**
 * 显示当前商品
 */
exports.read = function(req, res) {
	res.json(req.customer);
};

/**
 * 更新商品
 */
exports.update = function(req, res) {
	var customer = req.customer;

	customer = _.extend(customer, req.body);

	customer.save(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.json(customer);
		}
	});
};

/**
 * 删除商品
 */
exports.delete = function(req, res) {
	var customer = req.customer;

	customer.remove(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.json(customer);
		}
	});
};

/**
 * 商品列表
 */
exports.list = function(req, res) {
    if(req.query.name||req.query.id||req.query.phone){
        Customer.find({ "$or" : [ {name:{$regex:req.query.name}},{id:{$regex:req.query.id,$ne:'walkin'}},{phone:{$regex:req.query.phone}} ] }).sort('-created').populate('user', 'displayName').exec(function(err, customers) {
            if (err) {
                return res.status(400).send({
                    message: errorHandler.getErrorMessage(err),
                    info:'something wrong1'
                });
            } else {
                res.json(customers);
                //console.log('dd');
            }
        });
    }else {
        Customer.find({id:{$ne:'walkin'}}).sort('-created').populate('user', 'displayName').exec(function (err, customers) {
            if (err) {
                return res.status(400).send({
                    message: errorHandler.getErrorMessage(err),
                    info:'something wrong'
                });
            } else {
                res.json(customers);
                //console.log('dd');
            }
        });
    }
};

/**
 * 商品中间件
 */
exports.customerByID = function(req, res, next, id) {
	Customer.findById(id).populate('user', 'displayName').exec(function(err, customer) {
		if (err) return next(err);
		if (!customer) return next(new Error('加载客户失败 ' + id));
		req.customer = customer;
		next();
	});
};

/**
 * 商品授权中间件
 */
exports.hasAuthorization = function(req, res, next) {
	if (req.customer.user.id !== req.user.id) {
		return res.status(403).send({
			message: '用户没有授权'
		});
	}
	next();
};

exports.todayAddCustomer = function (req,res) {
    Customer.find({created:{$lt: new Date(), $gte: new Date(new Date().setDate(new Date().getDate()-1))}}).exec(function (err, customers) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err),
                info:'something wrong'
            });
        } else {
            res.json(customers);
        }
    });
}
