#pragma once
#include <QSqlQueryModel>
#include <QStringList>

class InvoiceModel : public QSqlQueryModel
{
    Q_OBJECT
    Q_PROPERTY(QString filter  READ filter   WRITE setFilter NOTIFY filterChanged)
    Q_PROPERTY(QString dbPath  READ dbPath   CONSTANT)
    Q_PROPERTY(bool    dbOpen  READ dbOpen   NOTIFY dbOpenChanged)

public:
    explicit InvoiceModel(QObject *parent = nullptr);

    QString filter() const { return m_filter; }
    QString dbPath() const { return m_dbPath; }
    bool    dbOpen() const { return m_dbOpen; }

    void setFilter(const QString &f);

    Q_INVOKABLE bool    isMandatoryColumn(int col) const;
    Q_INVOKABLE QString columnName(int col) const;
    Q_INVOKABLE void    openInvoicesDir() const;
    Q_INVOKABLE void    refresh();
    Q_INVOKABLE bool    exportRows(const QVariantList &rows, const QString &destUrl, const QString &format);
    Q_INVOKABLE QString folderPath(int row) const;
    Q_INVOKABLE void    sortByColumnName(const QString &colName, int order);

signals:
    void filterChanged();
    void dbOpenChanged();

private:
    void loadConfig();
    void openDatabase();
    void rebuildQuery();

    QString         m_filter;
    QString         m_dbPath;
    QString         m_invoicesDir;
    QStringList     m_mandatoryFields;
    QStringList     m_columnNames;
    bool            m_dbOpen      = false;
    QString         m_sortColName;
    Qt::SortOrder   m_sortOrder   = Qt::AscendingOrder;
    QString      m_mandantennr;
    QString      m_anwendernr;
    QString      m_buchschl;
    QString      m_belegart;
};
