'use strict';

var db = require(__config_path + '/db');
var consts = require(__config_path + '/consts');
var moment = require('moment');

exports.getListing = function (input, callback) {
    console.log(input);

    let sql = 'SELECT T.* FROM ' + consts.DB_TABLE.TASKS + ' T WHERE T.id > 0 ';

    if (input.key_word != '') {
        sql += ' AND (T.name LIKE "%' + input.key_word + '%" ';
        sql += ' OR T.description LIKE "%' + input.key_word + '%") ';
    }

    if (input.priority != -1) {
        sql += ' AND T.priority = ' + input.priority;
    }

    if (input.status != -1) {
        sql += ' AND T.status = ' + input.status;
    }

    sql += ' ORDER BY created_at DESC ';
    sql += ' LIMIT ' + input.skip + ',' + input.limit;

    var ss = db.query(sql, [], callback);

    console.log(ss.sql);

}

exports.store = function (data, callback) {
    let sql = 'INSERT INTO ' + consts.DB_TABLE.TASKS + ' SET ?';
    db.query(sql, {
        name: data.name,
        priority: data.priority,
        description: data.description,
        created_at: moment().format('YYYY-MM-DD HH:mm:ss'),
        updated_at: moment().format('YYYY-MM-DD HH:mm:ss')
    }, callback);
}