#ifndef WORKNODEFORMMODEL_H
#define WORKNODEFORMMODEL_H
#include <QAbstractListModel>
#include <QtQml/qqml.h>
#include <QJsonArray>

// 单个表单控件
class FormControl
{
public:
    explicit FormControl() = default;
    ~FormControl() = default;

    QString m_id;
    QString m_label;
    QString m_name;
    QList<QVariant> m_options;       // 目前暂定为QRadio控件类型
    QString m_placeholder;
    bool m_required;
    QString m_requiredMsg;
    QString m_type;
    QString m_value;        // 表单回显值

    QVariant m_result;

    QString m_gridGroupName;
    uint8_t m_gridColumns = 0;
    uint8_t m_gridCount;
    uint8_t m_gridIndex;
    bool m_TextAreaHeight = false;  // Grid中单行存在类似TextArea控件高度的控件，那么该行使用TextArea控件的高度
};

class WorkNodeFormModel : public QAbstractListModel
{
    Q_OBJECT
    QML_SINGLETON
    QML_NAMED_ELEMENT(WorkNodeFormModel)

    Q_PROPERTY(QString modelType READ modelType WRITE setModelType NOTIFY modelTypeChanged)
    Q_PROPERTY(QString formJsonInfo READ formJsonInfo WRITE setFormJsonInfo NOTIFY formJsonInfoChanged)
    Q_PROPERTY(QJsonArray nodeUserList READ nodeUserList WRITE setNodeUserList NOTIFY nodeUserListChanged)
    Q_PROPERTY(int workId READ workId WRITE setWorkId NOTIFY workIdChanged)
    Q_PROPERTY(QString formName READ formName WRITE setFormName NOTIFY formNameChanged)
    Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged)
    Q_PROPERTY(QString nodeProgress READ nodeProgress WRITE setNodeProgress NOTIFY nodeProgressChanged)
private:
    explicit WorkNodeFormModel(QObject *parent=nullptr);
    explicit WorkNodeFormModel() = default;
    ~WorkNodeFormModel() = default;
public:
    enum FormRoles {
        Id = Qt::UserRole + 1,
        Label,
        Name,
        Options,
        Placeholder,
        Required,
        RequiredMessage,
        Type,
        Value,          // 表单回显值

        GroupName,
        GridColumns,
        GridCount,
        GridIndex,
        TextAreaHeight,
    };
    Q_ENUM(FormRoles)

    static WorkNodeFormModel* pInstance;
    static WorkNodeFormModel* instance();
    static WorkNodeFormModel* create(QQmlEngine*, QJSEngine*);

    void append(const FormControl& form);
    void clear();

    Q_INVOKABLE QString getType(int index);

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    virtual QHash<int, QByteArray> roleNames() const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

    QString modelType()
    {
        return this->m_type;
    }
    void setModelType(const QString& modelType)
    {
        m_type = modelType;
    }

    QString formJsonInfo()
    {
        return this->m_formJson;
    }
    void setFormJsonInfo(const QString& formJsonInfo)
    {
        m_formJson = formJsonInfo;
    }

    QString isolationPoints () const {
        return m_isolationPoints;
    }
    void setIsolationPoints(const QString& strIsolationPoints) {
        m_isolationPoints = strIsolationPoints;
    }

    QJsonArray nodeUserList() const {
        return m_nodeUserList;
    }
    void setNodeUserList(const QJsonArray& jArr) {
        m_nodeUserList = jArr;
        emit nodeUserListChanged();
    }

    QJsonArray pointsList() const {
        return m_pointsList;
    }
    void setPointsList(const QJsonArray& jArr) {
        m_pointsList = jArr;
    }

    int workId() const {
        return m_workId;
    }
    void setWorkId(int id) {
        m_workId = id;
    }

    QString formName() const {
        return m_formName;
    }
    void setFormName(const QString& name) {
        m_formName = name;
        emit formNameChanged();
    }

    QString description() const {
        return m_description;
    }
    void setDescription(const QString& desc) {
        m_description = desc;
        emit descriptionChanged();
    }

    QString nodeProgress() const {
        return m_nodeProgress;
    }
    void setNodeProgress(const QString& node) {
        m_nodeProgress = node;
    }

    QString orderNo() const {
        return m_orderNo;
    }
    void setOrderNo(const QString& order) {
        m_orderNo = order;
    }

    QString workerName() const {
        return m_workerName;
    }
    void setWorkerName(const QString& name) {
        m_workerName = name;
    }

    QString nodeName() const {
        return m_nodeName;
    }
    void setNodeNmae(const QString& name) {
        m_nodeName = name;
    }
signals:
    void modelTypeChanged();
    void formJsonInfoChanged();
    void nodeUserListChanged();
    void workIdChanged();
    void formNameChanged();
    void descriptionChanged();
    void nodeProgressChanged();
private:
    QList<FormControl> m_formList;

    QString m_type;

    QString m_formJson;

    QString m_isolationPoints;

    QJsonArray m_nodeUserList;

    QJsonArray m_pointsList;

    int m_workId;

    QString m_formName;
    QString m_description;
    QString m_nodeProgress;

    QString m_workerName;
    QString m_orderNo;
    QString m_nodeName;
};

#endif // WORKNODEFORMMODEL_H
