#include "msqlquerymodel.h"

#include <QIcon>
#include <QDebug>
#include <QLocale>

MSqlQueryModel::MSqlQueryModel(QObject *parent) :
    QSqlQueryModel(parent)
{

}


QVariant MSqlQueryModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return false;

    QVariant value = QSqlQueryModel::data(index,role);
    //if(role==Qt::TextColorRole && index.column()==0)
    //    return qVariantFromValue(QColor(Qt::red));


    switch (role)
    {
        case Qt::DisplayRole:
        //case Qt::EditRole:
        {

            switch (index.column())
            {
                case 0:
                {
                    if (false)
                    {
                        switch (QSqlQueryModel::data(index, Qt::DisplayRole).toInt())//(data(index).toInt())
                        {
                            case 0:
                                return tr("Elemento Compuesto");

                            case 1:
                                return tr("Material");

                            case 2:
                                return tr("Mano de Obra");

                            case 3:
                                return tr("Maquinaria");

                            case 4:
                                return tr("Porcentajes");

                            default:
                                break;
                        }
                    }
                    else
                        return QVariant();
                }

                case 1:
                {
                    switch (QSqlQueryModel::data(index, Qt::DisplayRole).toInt())//(data(index).toInt())
                    {
                        case 0:
                            return tr("Servicio");

                        case 1:
                            return tr("Producto");
                    }
                }

                case 4:
                case 5:
                {
                    QLocale locale = QLocale("es_ES");
                    QString s = locale.toString(value.toDouble(), 'f', 2) + " €";
                    return s;
                }

                case 6:
                {
                    return QVariant();
                }

                default:
                    return value;
            }
        }

        case Qt::DecorationRole:
        {
            if(index.column() == 0)
            {
                switch (QSqlQueryModel::data(index, Qt::DisplayRole).toInt())//(data(index).toInt())
                {
                    case 0:
                        return QIcon(":/resources/icons/box.svg");

                    case 1:
                        return QIcon(":/resources/icons/blocks.svg");

                    case 2:
                        return QIcon(":/resources/icons/helmet.svg");

                    case 3:
                        return QIcon(":/resources/icons/gear.svg");

                    case 4:
                        return QIcon(":/resources/icons/percentage.svg");

                    default:
                        break;
                }
            }
            else
                return QVariant();
        }

        case Qt::TextAlignmentRole:
            switch (index.column())
            {
                case 4:
                case 5:
                {
                    return int(Qt::AlignRight | Qt::AlignTop);
                }
                case 6:
                {
                    return int(Qt::AlignCenter | Qt::AlignTop);
                }
                default:
                    return int(Qt::AlignLeft | Qt::AlignTop);
            }

        case Qt::CheckStateRole:

            if (index.column() == 6)
                return (QSqlQueryModel::data(index).toBool() == true) ? Qt::Checked : Qt::Unchecked;
            else
                return QVariant();

        default:
            return value;
    }
}
