#pragma once
#include <QTableView>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include "QxModelView.h"
#include "OrmModelBase.h"
#include "TableView.h"

#define SELECTED_VIEW_DEFAULT_WIDTH		200					// 选择区默认宽度

class MultipleTableView : public TableView
{
    Q_OBJECT

    enum class SelectedViewState {
        Normal,
        Max,
        Min
    };

public:
    MultipleTableView(QWidget* parent = nullptr);

    void setMultipleSelect(bool value);

    uint getPkFieldIndex() const;

    void setPkFieldIndex(const uint &value);

	void hideSelectTv(bool hide = true);

    template<typename T>
	void setModel(qx::QxModel<T, OrmModelBase>* model)
    {
        //model->setMultiSelect(true);
        mainModel = model;
        QTableView::setModel(model);
        if (twSelected != nullptr)
        {
			selectedModel = new qx::QxModel<T, OrmModelBase>(this);
            twSelected->setModel(selectedModel);
			connect(model, &OrmModelBase::checkStateChanged, this, &MultipleTableView::checkStateChanged, Qt::UniqueConnection);
        }
    }

    qx::IxCollection *getSelectedRecord();

     void  selectRecord(int row);

     void  unselectRecord(int row);

     /************************************
     	*  @Method:    isSelected
     	*  @ Returns:   int	 已被选择的记录所在视图中的行数，返回-1 则model中不存在
     	*  @ Parameter: const qx::IxModel * model
     	*  @ Parameter: void * pOwner  实体类对象指针
     ************************************/
     int isSelected(const qx::IxModel* model, void *pOwner);

protected:
     void initSelectView();

private slots:
    void checkStateChanged(Qt::CheckState state, int row);
    void expandSelectedView();
    void collapseSelectedView();
    void removeRow();

protected:
	void resizeEvent(QResizeEvent* event) override;

protected:
    QWidget* wgtContainer;
	QHBoxLayout* layout;
    QTableView          *twSelected;
    QLabel              *lblCounter;
    QPushButton         *btnExpand;
    QPushButton         *btnCollapse;
    SelectedViewState	selectedViewState;
    bool				multipleSelect;
    bool                inited;
    OrmModelBase           *mainModel;
    uint                pkFieldIndex;
    qx::IxModel* selectedModel;
};

