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

Item {
    anchors.fill: parent
    property var availableFilters: ["Name", "Adresse", "PLZ", "Ort"]
    id: test

    SearchBar
    {
        id:searchBar
        anchors.margins: 9
    }

    Button
    {
        id: addBusinessBtn
        icon.source: "qrc:/images/addbusiness.svg"
        icon.color: "olive"
        anchors.right: parent.right
        flat: true
        onClicked: appLoader.source = "AddCustomer.qml"
    }


    ColumnLayout
    {
        id: tableColumn
        anchors
        {
            top: searchBar.bottom
            bottom: parent.bottom
            left: parent.left
            right: parent.right
        }

        RowLayout
        {
            RadioButton
            {

                checked: true
                text: qsTr("Alle")
            }
            RadioButton
            {

                text: qsTr("Interessent")
            }
            RadioButton
            {

                text: qsTr("Kunden")
            }
            RadioButton
            {

                text: qsTr("Lieferant")
            }
            RadioButton
            {

                text: qsTr("Erledigt")
            }

        }

        HorizontalHeaderView
        {
            id: horizontalHeader
            Layout.fillWidth: true
            implicitHeight: 40
            movableColumns: true //@disable-check M16
            syncView: testTable

            delegate: Rectangle {
                    color: addBusinessBtn.palette.alternateBase
                    border.color: addBusinessBtn.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: addBusinessBtn.palette.text
                    }
            }
        }

        TableView
        {
            //property var customWidths: [0.2, 0.5, 0.3, 05, 0.2, 0.2]
            property real newWidth: 0
            id: testTable
            Layout.fillHeight: true
            Layout.fillWidth: true
            columnSpacing: 1
            rowSpacing: 2
            model: bm
            alternatingRows: true
            resizableColumns: true // @disable-check M16
            selectionBehavior: TableView.SelectRows
            selectionModel: ItemSelectionModel
            {
                id: selModel
                model: testTable.model
            }

            // columnWidthProvider: function(column)
            // {
                // switch (column)
                // {
                //     case 0: return width * 0.2;
                //     case 1: return width * 0.5;
                //     case 2: return width * 0.3;
                //     default: return width / model.columnCount();
                // }
                //return customWidths[column] * width;
                //return tableColumn.content.implicitWidth // model.columnCount()
            //      newWidth = columnWidth(column)
            //      return newWidth
            // }

            Timer
            {
                id: redrawTable
                running: true
                interval: 1
                repeat: false
                onTriggered:
                {
                    testTable.forceLayout();
                }
            }



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

                Text
                {
                    text: model.display
                    elide: Text.ElideRight
                    width: parent.width
                    height: parent.height
                    verticalAlignment: Text.AlignVCenter
                    leftPadding: 9 //@d isable-check M16
                    color: addBusinessBtn.palette.text
                }

                MouseArea
                {
                    property bool hovered: false
                    id: mouseArea
                    anchors.fill: parent
                    hoverEnabled: true
                    onClicked:
                    {
                        bm.onRowClicked(row)
                    }
                    onEntered:
                    {
                        testTable.selectionModel.select(testTable.model.index(row, 0), ItemSelectionModel.SelectCurrent | ItemSelectionModel.Rows)
                    }
                }
            }

            onContentWidthChanged:
            {
                //console.log("Model changed!!")
                redrawTable.start()
            }
        }

        Item
        {
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
    }
}




