import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Controls.Fusion
import QtQuick.Dialogs
import "../js/qmldict.js" as JsLib

ColumnLayout
{
    Layout.fillWidth: true
    Layout.fillHeight: true
    spacing: 15
    Label
    {
        text: qsTr("Kunden anlegen")
        horizontalAlignment: Text.AlignHCenter
        Layout.fillWidth: true
        font.pixelSize: 35
    }
    CheckBox
    {
        id: checkAddContact
        text: qsTr("Ansprechpartner hinzufügen")
        Layout.alignment: Qt.AlignRight
        checked: false
        onCheckStateChanged: addContactLayout.visible = checked
    }

    RowLayout
    {
        id: addCustomer
        Layout.fillWidth: true
        Layout.fillHeight: true
        spacing: 45
        GridLayout
        {
            id: businessGrid
            columns: 2
            Layout.fillWidth: true
            Layout.fillHeight: true
            rowSpacing: 9

            Label
            {
                id: lblFirmenName
                text: qsTr("Firmenname")
                Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
            }

            TextField
            {
                property string name: "business"
                id: firmenName
                Layout.fillWidth: true
                Layout.alignment: Qt.AlignVCenter
                onTextChanged: isEmptyField()
                placeholderText: "Pflichtfeld"
                placeholderTextColor: "red"


            }

            Label
            {
                text: qsTr("Straße")
                Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
            }

            TextField
            {
                property string name: "street"
                id: street
                Layout.fillWidth: true
                onTextChanged: isEmptyField()
                placeholderText: "Pflichtfeld"
                placeholderTextColor: "red"
            }
            Label
            {
                text: qsTr("PLZ")
                Layout.alignment: Qt.AlignRight
            }

            ComboBox
            {
                property string name: "postcode"
                id: postcode
                Layout.fillWidth: true
                editable: true
                onCurrentTextChanged: isEmptyField()
                onEditTextChanged: isEmptyField()
                onActivated: currentValue
                model: am
                textRole: "display"
                popup.height: 300
                popup.y: postcode.y + 5 - (postcode.height * 2)
                currentIndex: -1
                onCurrentIndexChanged: city.currentIndex = postcode.currentIndex
            }

            Label
            {
                text: qsTr("Ort")
                Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
            }
            ComboBox
            {
                property string name: "city"
                id: city
                Layout.fillWidth: true
                editable: true
                onEditTextChanged: isEmptyField()
                onCurrentTextChanged: isEmptyField()
                model: am
                textRole: "city"
                popup.height: 300
                popup.y: postcode.y + 5 - (postcode.height * 2)
                currentIndex: -1
                onCurrentIndexChanged: postcode.currentIndex = city.currentIndex
            }

            Label
            {
                text: qsTr("Telefon")
                Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
            }
            TextField
            {
                property string name: "telephone"
                id: telephone
                Layout.fillWidth: true
            }

            Label
            {
                text: qsTr("E-Mail")
                Layout.alignment: Qt.AlignRight
            }
            TextField
            {
                property string name: "email"
                id: email
                Layout.fillWidth: true
            }

            Label
            {
                text: qsTr("Homepage")
                Layout.alignment: Qt.AlignRight
            }
            TextField
            {
                property string name: "homepage"
                id: homepage
                Layout.fillWidth: true
            }

            Label
            {
                text: qsTr("Geschäftsführer")
                Layout.alignment: Qt.AlignRight
            }
            TextField
            {
                property string name: "ceo"
                id: ceo
                Layout.fillWidth: true
            }

            Label
            {
                text: qsTr("USt-IdNr")
                Layout.alignment: Qt.AlignRight
            }
            TextField
            {
                property string name: "taxno"
                id: taxno
                Layout.fillWidth: true
            }
            Label
            {
                text: qsTr("Typ")
                Layout.alignment: Qt.AlignRight
            }
            ComboBox
            {
                property string name: "typeid"
                id: typeid
                Layout.fillWidth: true
                editable: false


            }
            Label
            {
                text: qsTr("Info")
                Layout.alignment: Qt.AlignRight | Qt.AlignTop
            }

            ScrollView
            {
                Layout.fillWidth: true
                Layout.preferredHeight: 100

                TextArea
                {
                    property string name: "customerinfo"
                    id: customerInfo
                    background: Rectangle
                    {
                        color: customerInfo.palette.base
                        border.color: customerInfo.activeFocus? customerInfo.palette.highlight: customerInfo.palette.base
                    }
                }
            }   
        }
        AddContact
        {
            id: addContactLayout
        }
    }
    RowLayout
    {
        Layout.fillHeight: true
        Layout.alignment: Qt.AlignRight
        Button
        {
            text: qsTr("Abbrechen")
            onClicked: appLoader.source = "CustomerTables.qml"

        }
        Button
        {
            id: saveBtn
            text: qsTr("Speichern")
            enabled: false
            onClicked:
            {
                var new_business
                if (!checkAddContact.checked)
                {
                    new_business = JsLib.addBusiness(businessGrid)
                    bm.addBusiness(new_business)
                }
                else
                {
                    new_business = JsLib.addBusiness(businessGrid)
                    var new_contact = JsLib.addBusiness(addContactLayout)
                    bm.addBusiness(new_business)
                    bm.setContact(new_contact)
                }
            }

        }
    }
    Item
    {
        id: spacer3
        Layout.fillHeight: true
    }


    function isEmptyField()
    {
        if (!firmenName.text.trim() || !street.text.trim() || !city.text.trim())
        {
            saveBtn.enabled = false
        }
        else
        {
            if (!postcode.editText.trim() || !postcode.currentText)
                saveBtn.enabled = false
            else
                saveBtn.enabled = true
        }
    }


}
