#pragma once

#include <QSqlQueryModel>
#include <QSqlRecord>
#include "charcode.h"
// 默认分页大小
#define PAGE_SIZE	20	

// 计算页数
#define CALC_PAGE_COUNT(totalCount) ceil(totalCount*1.0/PAGE_SIZE)

class ModelBase : public QSqlQueryModel
{
	Q_OBJECT

public:
	ModelBase(QObject *parent);
	~ModelBase();

	bool getMultiSelect() const { return multiSelect; }
	void setMultiSelect(bool val) { multiSelect = val; }

public:
	void setQuery(const QSqlQuery &query);

	const quint32 getTotalCount() { return totalCount; }

	const quint32 getPageCount() { return CALC_PAGE_COUNT(totalCount); };

signals:
	void checkStateChanged(Qt::CheckState state, const QSqlRecord &record);

protected:
	QVariant headerData(int section, Qt::Orientation orientation, int role) const;
	bool setData(const QModelIndex &, const QVariant &, int);
	Qt::ItemFlags flags(const QModelIndex &) const;
	QVariant data(const QModelIndex &index, int role) const;

protected:
	QMap<int, Qt::CheckState> checkStateMap;
	QVector<QPair<QString, QString>> fields;
	quint32 totalCount;
	bool multiSelect;
};
