#ifndef FORMBUDGET_H
#define FORMBUDGET_H

#include "formbase.h"

#include "dbutils.h"
#include <QWidget>


class QString;
class TreeItem;
class QSqlQuery;
class QAbstractItemModel;
class TreeModel;

namespace Ui
{
class formBudget;
}

class formBudget : public formBase
{
    Q_OBJECT

public:
    explicit formBudget(QString aID = "", int aEditMode = 0, QWidget *parent = nullptr);
    ~formBudget() override;

    //void openDocument(QString id);
    void newDocument() override;
    void openDocument(QString id) override;
    void save() override;
    bool needsave() override;
    void setEditMode(bool aMode) override;
    void closeDocument() override;
private slots:
    void on_buttonInsertRow_released();
    void on_buttonInsertChild_released();
    void on_buttonPercent_released();
    void on_buttonRemoveRow_released();
    void on_buttonMaterials_released();
    void on_buttonMachinery_released();
    void on_buttonManpower_released();
    void on_buttonValidate_released();
    void on_buttonSave_released();
    void on_tabWidget_currentChanged(int index);
    void onAnyChange();
    void updateMargin();
    void onModelStructureChanged();

private:
    TreeModel *m_treeModel;
    Ui::formBudget *ui;
    DbUtils m_dbUtils;
    bool m_changed;

    void setLineType(QString type, QModelIndex index);
    void setCellText(QString val, QModelIndex index, int col);
    bool InsertElement(QString ID, QModelIndex index);
    void saveModel(QSqlQuery qry, QString aCommand, QAbstractItemModel *aModel, int aLevel);
    void setupTreeView();
    void setupDelegates();
    void setupConnections();
    void initializeDocument();
    void onModelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
    void insertRow();
    void insertChild();
    void removeRow();
    void insertRowWithType(const QString& type);


protected:
    virtual void focusInEvent(QFocusEvent *event) override;
    virtual void focusOutEvent(QFocusEvent *event) override;
};

#endif // FORMBUDGET_H
