#ifndef QPAGEDTABLE_H
#define QPAGEDTABLE_H

#include <QWidget>
#include <QList>
#include <QMap>
#include <QVariant>
#include <QFrame>
#include <QMenu>
#include <QTableWidget>
#include <QContextMenuEvent>
#include <QCheckBox>
#include <QRadioButton>
#include <QButtonGroup>

#include "QPagedTablePage.h"
#include "QPagedTableColumn.h"

namespace Ui {
class QPagedTable;
}

class QPagedTable : public QWidget
{
    Q_OBJECT

public:
    explicit QPagedTable(QWidget *parent = nullptr);
    ~QPagedTable();

    enum MultiCheckType {
        NONE,
        RADIO,
        MULTI
    };

    /**
     * 封装API函数
     */
    void initTableWidget();
    void initTableHeader();
    void adjustTableWidth();
    void setColumns(QList<QPagedTableColumn> columnList);
    void setColumns(QList<QMap<QString, QVariant>> colMapList);
    void setDatas(QList<QMap<QString, QVariant>> pagedDataList);
    void setTableProperty(const char* name, QString value);
    void setContextMenuLabels(QStringList menuLabels);
    void setPage(int pageSize, int currentPage, int totalCount, int totalPage);
    void setPageSize(int pageSize);
    void setTotalCount(int totalCount);
    void setPageButtonIcons(QStringList iconUrls);
    void setPageButtonProperty(const char* name, QString value);
    void setCheckBoxClass(QString className);

    void setHeaderHeight(int height);
    void setDataRowHeight(int height);
    void setHighlightHeaderSection(bool highlight);
    void setShowVerticalHeader(bool show);
    void setEditTrigger(int trigger);
    void setSelectionBehavior(int behavior);

    void setMultiCheckType(MultiCheckType type);

    QTableWidget * getTableInstance();
    QList<int> filterCells(QString colName, QString value);
    void setRowSelected(int rowIdx, bool selected);

private:
    Ui::QPagedTable *ui;

    QPagedTablePage page;
    QList<QPagedTableColumn> columns;
    QList<QMap<QString, QVariant>> datas;

    QStringList contextMenuLabels;
    QMenu * menu;
    QList<QAction *> actionItemList;

    QStringList headerLabels;
    int fixedWidth = 0;

    /**
     * 表格属性
     *
     */
    int headerHeight = 50;
    int dataRowHeight = 40;
    bool highlightHeaderSection = false;
    bool showVerticalHeader = false;
    int editTrigger = 0;
    int selectionBehavior = 1;

    int frameStyle = QFrame::NoFrame;
    int frameBorderSize = 0;

    int multiCheck = MultiCheckType::NONE;
    QMap<QString, QString> checkProperty;
    bool doubleClickSelectRow = true;

    QButtonGroup * group;

    void updateTableList();
    QWidget * buildNewCheckWidget();
    QWidget * buildNewRadioWidget();
    QWidget * buildNewLabelWidget(QString text);

private slots:
    void on_btnFirst_clicked();
    void on_btnPre_clicked();
    void on_btnNext_clicked();
    void on_btnLast_clicked();
    void on_table_cellDoubleClicked(int row, int column);

signals:
    void reloadTablePagedData(int currPage);
    void contextMenuActionTrigged(int actionIdx, int rowIdx, int colIdx);
    void tableRowSelected(int rowIdx, bool selected);
};

#endif // QPAGEDTABLE_H
