import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import Qt.labs.qmlmodels

ColumnLayout
{
    property var availableFilters: [""]

    anchors.fill: parent
    spacing: Dimensions.l

    function viewCriterion(criterion)
    {
        business_model.viewCriterion(criterion.text);
    }
    function onObjectContactAdded(added)
    {
        console.log(added)
        if (added) object_model.viewCriterion("")
    }

    Component.onCompleted:
    {
        contact_model.objectContactAdded.connect(onObjectContactAdded)
        contentStack.pop()
    }
    RowLayout
    {
        Layout.fillWidth: true
        spacing: Dimensions.l
        SearchBar
        {
        }
        QuickFilter {
            onSelectedChanged: (name) => {
                business_model.viewCriterion(name)
            }

            model: ListModel {
                ListElement {

                    name: "Alle"
                    text: qsTr("Alle")
                    selected: true
                }
                ListElement {
                    name: "Interessent"
                    text: qsTr("Interessent")
                    selected: false
                }
                ListElement {
                    name: "Kunde"
                    text: qsTr("Kunde")
                    selected: false
                }
                ListElement {
                    name: "Lieferant"
                    text: qsTr("Lieferant")
                    selected: false
                }
                ListElement {
                    name: "Erledigt"
                    text: qsTr("Erledigt")
                    selected: false
                }
            }
        }
        Button
        {
            id: addObjectBtn
            icon.source: "qrc:/images/PlusCircle.svg"
            text: qsTr("Objekt Hinzufügen")
            Layout.alignment: Qt.AlignRight
            onClicked: contentStack.push("AddObject.qml")
        }
    }


    ColumnLayout
    {
        id: tableColumn
        Layout.fillWidth: true
        Layout.fillHeight: true
        Layout.verticalStretchFactor: 1
        clip: true
        // anchors
        // {
        //     top: searchBar.bottom
        //     bottom: parent.bottom
        //     left: parent.left
        //     right: parent.right
        //     topMargin: 15
        // }
        HorizontalHeaderView
        {
            id: horizontalHeaderview
            Layout.fillWidth: true
            implicitHeight: 40
            movableColumns: true //@disable-check M16
            syncView: objectTable

            delegate: Rectangle {
                    color: addObjectBtn.palette.alternateBase
                    border.color: addObjectBtn.palette.base
                    implicitHeight: 40
                    Layout.fillWidth: true
                    implicitWidth: 1
                    Text
                    {
                        text: model.display
                        elide: Text.ElideRight
                        width: parent.width
                        height: parent.height
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        color: addObjectBtn.palette.text
                    }
            }
        }

        TableView
        {
            property real newWidth: 0
            id: objectTable
            z: 1
            // height: tableColumn.height - horizontalHeaderview.height
            Layout.fillHeight: true
            Layout.fillWidth: true
            columnSpacing: 1
            rowSpacing: 2
            model: object_model
            alternatingRows: true
            resizableColumns: true // @disable-check M16
            selectionBehavior: TableView.SelectRows
            ScrollBar.vertical: ScrollBar
            {
                policy: objectTable.contentHeight > objectTable.height ? ScrollBar.AlwaysOn : ScrollBar.AlwaysOff
            }
            selectionModel: ItemSelectionModel
            {
                id: obmodel
                model: objectTable.model
            }

            delegate:Rectangle
            {
                required property bool selected
                required property bool current
                implicitWidth: objectTable.width / objectTable.columns
                implicitHeight: 25
                color: selected
                           ? addObjectBtn.palette.highlight //palette.highlight
                           : (objectTable.alternatingRows && row % 2 !== 0
                           ? addObjectBtn.palette.base // palette.base
                           : addObjectBtn.palette.alternateBase) //palette.alternateBase)

                Text
                {
                    text: (model.display === null || model.display === undefined)? "": model.display
                    elide: Text.ElideRight
                    width: parent.width
                    height: parent.height
                    verticalAlignment: Text.AlignVCenter
                    leftPadding: 9 //@d isable-check M16
                    color: addObjectBtn.palette.text
                }

                MouseArea
                {
                    property bool hovered: false
                    id: mouseArea
                    anchors.fill: parent
                    hoverEnabled: true
                    onDoubleClicked:
                    {
                        contentStack.push("ObjectDetails.qml", {selectedObject: row});
                    }
                    onEntered:
                    {
                        objectTable.selectionModel.select(objectTable.model.index(row, 0), ItemSelectionModel.SelectCurrent | ItemSelectionModel.Rows)
                    }
                }
            }
        }

    }
    Item {
        Layout.fillHeight: true
    }
}
