'use strict';

var async = require('async');
var moment = require('moment');
var utils = require(__libs_path + '/utils');
var Task = require('../models/task.model');

exports.showList = function(req, res, next) {
    var skip = req.body.skip || 0;
    var limit = req.body.limit || 10;
    var key_word = '';

    var input = {
        key_word: key_word,
        skip: skip,
        limit: limit
    };

    async.waterfall([
        function (cb) {
            Task.getListing(input, function (err, res) {
                if (err) return cb(err);
                cb(null, res);
            })
        }
    ], function (err, result) {
        res.render('pages/task/index', {
            title: 'Task list',
            data: result,
            moment: moment,
            getPriorityName: utils.getPriorityName,
            getStatusName: utils.getStatusName,
        });
    });
}

exports.store = function (req, res, next) {
    var name = req.body.name;
    var priority = req.body.priority;
    var description = req.body.description;

    console.log(req.body.description);

    async.waterfall([
        function (cb) {
            Task.store({name: name, priority: priority, description: description}, function (err, res) {
                if (err) return cb(err);
                cb(null, res);
            })
        }
    ], function (err, result) {
        console.log(result);

        res.json({
            code: 200,
            message: 'OK',
            data: null
        })
    });
}

exports.ajaxList = function (req, res, next) {
    var key_word = req.body.key_word;
    var priority = parseInt(req.body.priority || -1);
    var status = parseInt(req.body.status || -1);

    var input = {
        key_word: key_word,
        priority: priority,
        status: status,
        skip: 0,
        limit: 10
    };

    async.waterfall([
        function (cb) {
            Task.getListing(input, function (err, res) {
                if (err) return cb(err);
                cb(null, res);
            })
        }
    ], function (err, result) {
        res.render('pages/task/partials/ajax_list', {
            data: result,
            moment: moment,
            getPriorityName: utils.getPriorityName,
            getStatusName: utils.getStatusName
        });
    });





}



exports.delete = function (req, res, next) {
    console.log(req.body.id);
}