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

ColumnLayout {
    property var availableFilters: ["Name", "Adresse", "PLZ", "Ort"]

    function viewCriterion(criterion)
    {
        business_model.viewCriterion(criterion.text);
    }



    anchors.fill: parent
    spacing: Dimensions.l

    Component.onCompleted: 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: addCustomer

            Layout.alignment: Qt.AlignRight
            icon.source: "qrc:/images/PlusCircle.svg"
            text: qsTr("Kunde Hinzufügen")
            onClicked: contentStack.push("AddCustomer.qml")
        }
    }
    ColumnLayout
    {
        Layout.fillWidth: true
        Layout.fillHeight: true
        Layout.verticalStretchFactor: 1
        clip: true

        HorizontalHeaderView
        {
            id: horizontalHeader

            Layout.fillWidth: true
            implicitHeight: 40
            movableColumns: true //@disable-check M16
            syncView: customerTable

            delegate: Rectangle
            {
                Layout.fillWidth: true
                border.color: addCustomer.palette.base
                color: addCustomer.palette.alternateBase
                implicitHeight: 40
                implicitWidth: 1

                Text
                {
                    color: addCustomer.palette.text
                    elide: Text.ElideRight
                    height: parent.height
                    horizontalAlignment: Text.AlignHCenter
                    text: model.display
                    verticalAlignment: Text.AlignVCenter
                    width: parent.width
                }
            }
        }
        TableView {
            id: customerTable

            property real newWidth: 0

            Layout.fillHeight: true
            Layout.fillWidth: true
            alternatingRows: true
            columnSpacing: 1
            model: business_model
            resizableColumns: true
            rowSpacing: 2
            selectionBehavior: TableView.SelectRows
            z: 1

            ScrollBar.vertical: ScrollBar {
                policy: customerTable.contentHeight > customerTable.height ? ScrollBar.AlwaysOn : ScrollBar.AlwaysOff
            }
            delegate: Rectangle {
                required property bool current
                required property bool selected

                color: selected ? addCustomer.palette.highlight //palette.highlight
                 : (customerTable.alternatingRows && row % 2 !== 0 ? addCustomer.palette.base // palette.base
                     : addCustomer.palette.alternateBase) //palette.alternateBase)
                implicitHeight: 25
                implicitWidth: customerTable.width / customerTable.columns

                Text {
                    color: addCustomer.palette.text
                    elide: Text.ElideRight
                    height: parent.height
                    leftPadding: 9
                    text: model.display == null ? "" : model.display // @disable-check M126
                    verticalAlignment: Text.AlignVCenter
                    width: parent.width
                }
                MouseArea {
                    id: mouseArea

                    property bool hovered: false

                    anchors.fill: parent
                    hoverEnabled: true

                    onDoubleClicked: {
                        business_model.onRowClicked(row);
                        contentStack.push("CustomerDetails.qml", {
                            selectedClient: row
                        });
                    }
                    onEntered: {
                        customerTable.selectionModel.select(customerTable.model.index(row, 0), ItemSelectionModel.SelectCurrent | ItemSelectionModel.Rows);
                    }
                }
            }
            selectionModel: ItemSelectionModel {
                id: selModel

                model: customerTable.model
            }
        }

    }
    Item {
        Layout.fillHeight: true
    }
}
