const db = require("../models");

exports.get = async function (req, res) {

    console.log(req.params.tipo);
    if (!req.params.tipo) return res.send({ error: "ingrese tipo" });
    if (isNaN(req.params.tipo)) return res.send({ error: "ingrese valor numúmerico en tipo" + req.params.tipo });


    var registros = {};

    if (req.params.tipo == 1)
        await GetCompras()
            .then(data => {
                return res.send(data);
            })
            .catch(err => {
                res.status(500).send({ error: err });
            });
    else if (req.params.tipo == 2)
        registros = await GetVentas()
            .then(data => {
                return res.send(data);
            })
            .catch(err => {
                res.status(500).send({ error: err });
            });


    /*
    var query = `select a.id_factura, b.nit, b.nombre, DATE_FORMAT(a.createdAt, '%d/%m/%Y %T')  as fecha, format(a.total, 2) as total, 
                a.estado, upper(c.usuario) as usuario,  
                case 
                    when a.estado = 1 then 'ACTIVA'
                    when a.estado = 2 then 'ANULADA'
                end as estatus,
                case
                    when a.estado = 1 then ''
                    when a.estado = 2 then DATE_FORMAT(a.updatedAt, '%d/%m/%Y %T')
                end as confirmacion
                from factura a
                inner join cliente b on a.id_cliente = b.id_cliente
                inner join usuario c on a.id_usuario = c.id_usuario `;

    const id = req.params.id;

    if (id) query += ` where id_factura = '${id}'  `;
    query += " order by a.id_factura desc";

    db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT })
        .then(data => {
            return res.send({ mensaje: data, perfil: req.session.perfil });
        }).catch(err => {
            res.status(500).send({ message: err.message || "Some error occurred while retrieving tutorials." });
        });
    */

}


async function GetCompras() {
    var query = `select a.id_factura, b.nit, b.nombre, DATE_FORMAT(a.createdAt, '%d/%m/%Y %T')  as fecha, format(a.total, 2) as total, 
    a.estado, upper(c.usuario) as usuario,  
    case 
        when a.estado = 1 then 'ACTIVA'
        when a.estado = 2 then 'ANULADA'
    end as estatus,
    case
        when a.estado = 1 then ''
        when a.estado = 2 then DATE_FORMAT(a.updatedAt, '%d/%m/%Y %T')
    end as confirmacion
    from factura a
    inner join cliente b on a.id_cliente = b.id_cliente
    inner join usuario c on a.id_usuario = c.id_usuario `;

    const id = req.params.id;

    if (id) query += ` where id_factura = '${id}'  `;
    query += " order by a.id_factura desc";

    db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT })
        .then(data => {
            return res.send({ mensaje: data, perfil: req.session.perfil });
        }).catch(err => {
            res.status(500).send({ message: err.message || "Some error occurred while retrieving tutorials." });
        });
}


async function GetVentas() {
    var query = `select a.id_factura, b.nit, b.nombre, DATE_FORMAT(a.createdAt, '%d/%m/%Y %T')  as fecha, format(a.total, 2) as total, 
    a.estado, upper(c.usuario) as usuario,  
    case 
        when a.estado = 1 then 'ACTIVA'
        when a.estado = 2 then 'ANULADA'
    end as estatus,
    case
        when a.estado = 1 then ''
        when a.estado = 2 then DATE_FORMAT(a.updatedAt, '%d/%m/%Y %T')
    end as confirmacion
    from factura a
    inner join cliente b on a.id_cliente = b.id_cliente
    inner join usuario c on a.id_usuario = c.id_usuario `;

    const id = req.params.id;

    if (id) query += ` where id_factura = '${id}'  `;
    query += " order by a.id_factura desc";

    db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT })
        .then(data => {
            return res.send({ mensaje: data, perfil: req.session.perfil });
        }).catch(err => {
            res.status(500).send({ message: err.message || "Some error occurred while retrieving tutorials." });
        });
}



exports.getVentas = function (req, res) {

    var query = `select a.id_factura, b.nit, b.nombre, DATE_FORMAT(a.createdAt, '%d/%m/%Y %T')  as fecha, format(a.total, 2) as total, 
                a.estado, upper(c.usuario) as usuario,  
                case 
                    when a.estado = 1 then 'ACTIVA'
                    when a.estado = 2 then 'ANULADA'
                end as estatus,
                case
                    when a.estado = 1 then ''
                    when a.estado = 2 then DATE_FORMAT(a.updatedAt, '%d/%m/%Y %T')
                end as confirmacion
                from factura a
                inner join cliente b on a.id_cliente = b.id_cliente
                inner join usuario c on a.id_usuario = c.id_usuario `;

    const id = req.params.id;

    if (id) query += ` where id_factura = '${id}'  `;
    query += " order by a.id_factura desc";

    db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT })
        .then(data => {
            return res.send({ mensaje: data, perfil: req.session.perfil });
        }).catch(err => {
            res.status(500).send({ message: err.message || "Some error occurred while retrieving tutorials." });
        });

}

