import QtQuick
import QtQuick.Controls.impl
import QtQuick.Layouts
import TeroStyle

ColumnLayout {
    readonly property int fieldM: 235
    readonly property int fieldS: 110
    readonly property bool valid: emailAddress.acceptableInput && firstName.acceptableInput && houseNumber.acceptableInput && lastName.acceptableInput && mobileNumber.acceptableInput && phoneNumber.acceptableInput && salutation.acceptableInput&& street.acceptableInput && title.acceptableInput && zipCode.acceptableInput
    readonly property var value: QtObject {
        readonly property string emailAddress: emailAddress.text ?? ""
        readonly property string firstName: firstName.text ?? ""
        readonly property string houseNumber: houseNumber.text ?? ""
        readonly property string lastName: lastName.text ?? ""
        readonly property string mobileNumber: mobileNumber.text ?? ""
        readonly property string phoneNumber: phoneNumber.text ?? ""
        readonly property string salutation: salutation.text ?? ""
        readonly property string street: street.text ?? ""
        readonly property string title: title.currentText
        readonly property int zipCode: zipCode.currentIndex
    }

    function setValue(value) {
        title.currentIndex = value.title ?? 0;
        firstName.text = value.firstName ?? "";
        lastName.text = value.lastName ?? "";
        street.text = value.street ?? "";
        houseNumber.text = value.houseNumber ?? "";
        zipCode.currentIndex = value.zipCode ?? -1;
        phoneNumber.text = value.phoneNumber ?? "";
        mobileNumber.text = value.mobileNumber ?? "";
        emailAddress.text = value.emailAddress ?? "";
        salutation.text = value.salutation ?? 0;
    }

    spacing: Dimensions.l

    IconLabel {
        color: Colors.foreground
        font: Typography.h2
        icon.color: Colors.foreground
        icon.height: Typography.h2.pixelSize
        icon.source: "qrc:/images/UserCircle"
        icon.width: Typography.h2.pixelSize
        spacing: Dimensions.m
        text: qsTr("Stammdaten")
    }
    RowLayout {
        spacing: Dimensions.m

        Field {
            label: qsTr("Anrede")

            ComboBox {
                id: title

                implicitWidth: fieldM
                model: [qsTr("Keine Angabe"), qsTr("Herr"), qsTr("Frau")]

                onCurrentTextChanged: {
                    switch (title.currentIndex) {
                    case 1:
                        salutation.text = "Sehr geehrter Herr ";
                        break;
                    case 2:
                        salutation.text = "Sehr geehrte Frau ";
                        break;
                    default:
                        salutation.text = "Guten Tag ";
                    }
                }
            }
        }
        Field {
            label: qsTr("Vorname")
            mandatory: true

            TextField {
                id: firstName

                implicitWidth: fieldM
                placeholderText: qsTr("Max")

                validator: NotEmptyValidator {
                }
            }
        }
        Field {
            label: qsTr("Nachname")
            mandatory: true

            TextField {
                id: lastName

                implicitWidth: fieldM
                placeholderText: qsTr("Mustermann")

                validator: NotEmptyValidator {
                }
            }
        }
    }
    RowLayout {
        spacing: Dimensions.m

        Field {
            label: qsTr("Straße")

            TextField {
                id: street

                implicitWidth: fieldM
                placeholderText: qsTr("Musterstraße")
            }
        }
        Field {
            label: qsTr("Hausnummer")

            TextField {
                id: houseNumber

                implicitWidth: fieldS
                placeholderText: qsTr("1a")
            }
        }
        Field {
            label: qsTr("PLZ")

            ComboBox {
                id: zipCode

                currentIndex: -1
                editable: true
                implicitWidth: fieldS
                model: address_model
                textRole: "display"

                onActivated: currentValue
                onCurrentIndexChanged: city.currentIndex = zipCode.currentIndex
            }
        }
        Field {
            label: qsTr("Ort")

            ComboBox {
                id: city

                currentIndex: -1
                editable: true
                implicitWidth: fieldM
                model: address_model
                textRole: "city"
            }
        }
    }
    IconLabel {
        color: Colors.foreground
        font: Typography.h2
        icon.color: Colors.foreground
        icon.height: Typography.h2.pixelSize
        icon.source: "qrc:/images/Phone"
        icon.width: Typography.h2.pixelSize
        spacing: Dimensions.m
        text: qsTr("Kontakt")
    }
    RowLayout {
        spacing: Dimensions.m

        Field {
            label: qsTr("Telefonnummer")

            TextField {
                id: phoneNumber

                implicitWidth: fieldM
                placeholderText: "+49 1234 567890"

                validator: OptionalPhoneNumberValidator {
                }
            }
        }
        Field {
            label: qsTr("Mobil")

            TextField {
                id: mobileNumber

                implicitWidth: fieldM
                placeholderText: "+49 123 4567891011"

                validator: OptionalPhoneNumberValidator {
                }
            }
        }
        Field {
            label: qsTr("E-Mail Adresse")

            TextField {
                id: emailAddress

                implicitWidth: fieldM
                placeholderText: "tero@example.org"

                validator: OptionalEmailAddressValidator {
                }
            }
        }
        Field {
            label: qsTr("Briefanrede")

            TextField {
                id: salutation

                implicitWidth: fieldM
            }
        }
    }
}
