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

GridLayout {
    id: personalData

    function checkPersonalField() {
        return (firstname.text.trim() && lastname.text.trim() && street.text.trim() && houseno.text.trim() && (postcode.editText.trim() || postcode.currentText.trim()) && (city.editText.trim() || city.currentText.trim()) && birthday.text.trim() && phonenumber.text.trim() && cellphone.text.trim() && email.text.trim() && jobdescription.text.trim() && contractstart.text.trim() && contractend.text.trim() && briefAnrede.text.trim());
    }
    function requiredField() {
        const pf = "Pflichtfeld";
        street.placeholderText = pf;
        phonenumber.placeholderText = pf;
        cellphone.placeholderText = pf;
        email.placeholderText = pf;
        jobdescription.placeholderText = pf;
        contractstart.placeholderText = pf;
        contractend.placeholderText = pf;
        briefAnrede.placeholderText = pf;
        houseno.placeholderText = pf;
    }

    columns: 4

    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Anrede")
    }
    ComboBox {
        id: title

        property string name: "title"

        Layout.columnSpan: 3
        Layout.fillWidth: true
        editable: false
        model: [qsTr("Keine Angabe"), qsTr("Herr"), qsTr("Frau")]

        onCurrentTextChanged: {
            switch (title.currentIndex) {
            case 1:
                briefAnrede.text = "Sehr geehrter Herr ";
                break;
            case 2:
                briefAnrede.text = "Sehr geehrte Frau ";
                break;
            default:
                briefAnrede.text = "Guten Tag ";
            }
        }
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Vorname*")
    }
    TextField {
        id: firstname

        property string name: "firstname"

        Layout.columnSpan: 3
        Layout.fillWidth: true

        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Nachname*")
    }
    TextField {
        id: lastname

        property string name: "lastname"

        Layout.columnSpan: 3
        Layout.fillWidth: true

        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Straße")
    }
    TextField {
        id: street

        property string name: "street"

        Layout.fillWidth: true
        placeholderTextColor: "red"

        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Nr.")
    }
    TextField {
        id: houseno

        property string name: "houseno"

        Layout.fillWidth: true
        placeholderTextColor: "red"

        validator: RegularExpressionValidator {
            regularExpression: /([0-9a-zA-Z\-]{1,6})/
        }

        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("PLZ")
    }
    ComboBox {
        id: postcode

        property string name: "postcode"

        Layout.fillWidth: true
        currentIndex: -1
        editable: true
        model: address_model
        popup.height: 300
        textRole: "display"

        validator: PostcodeValidator {
        }

        onActivated: currentValue
        onCurrentIndexChanged: city.currentIndex = postcode.currentIndex
        onCurrentTextChanged: checkFields()
        onEditTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Ort")
    }
    ComboBox {
        id: city

        property string name: "city"

        Layout.fillWidth: true
        currentIndex: -1
        editable: true
        model: address_model
        popup.height: 300
        textRole: "city"

        onCurrentTextChanged: checkFields()
        onEditTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Geburtsname")
    }
    TextField {
        id: birthname

        property string name: "birthname"

        Layout.columnSpan: 3
        Layout.fillWidth: true

        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Geburtsdatum*")
    }
    TextField {
        id: birthday

        property string name: "birthday"

        Layout.columnSpan: 3
        Layout.fillWidth: true

        validator: RegularExpressionValidator {
            regularExpression: /((^|)(0[1-9]{1}|[1-2]{1}[0-9]{1}|3[0-1]))\.((^|)(0[1-9]{1}|1[0-2]{1}))\.((^|)(196[0-9]{1}|19[7-9]{1}[0-9]{1}|20[0-9]{2}))/
        }

        Keys.onPressed: event => {
            if (event.key !== Qt.Key_Backspace) {
                var len = birthday.length;
                var bd = birthday.text;
                if (len === 2 || len === 5)
                    birthday.text = bd + ".";
            }
        }
        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Geburtsort*")
    }
    TextField {
        id: placeofbirth

        property string name: "placeofbirth"

        Layout.columnSpan: 3
        Layout.fillWidth: true

        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Telefonnummer")
    }
    TextField {
        id: phonenumber

        property string name: "phone"

        Layout.columnSpan: 3
        Layout.fillWidth: true
        placeholderTextColor: "red"

        validator: RegularExpressionValidator {
            regularExpression: /([+0-9]{1})([0-9]{1,17})/
        }

        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Mobil")
    }
    TextField {
        id: cellphone

        property string name: "mobile"

        Layout.columnSpan: 3
        Layout.fillWidth: true
        placeholderTextColor: "red"

        validator: RegularExpressionValidator {
            regularExpression: /([+0-9]{1})([0-9]{1,17})/
        }

        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("E-Mail")
    }
    TextField {
        id: email

        property string name: "email"

        Layout.columnSpan: 3
        Layout.fillWidth: true
        placeholderText: qsTr("beispiel@domain.de")
        placeholderTextColor: "red"

        validator: EmailAddressValidator {
        }

        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Familienstand")
    }
    ComboBox {
        id: maritalstatus

        property string name: "maritalstatus"

        Layout.columnSpan: 3
        Layout.fillWidth: true
        editable: false
        model: [qsTr("ledig"), qsTr("verheiratet"), qsTr("verwitwet"), qsTr("geschieden")]
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Stundenlohn")
    }
    TextField {
        id: salary

        property string name: "salary"

        Layout.columnSpan: 3
        Layout.fillWidth: true
        placeholderTextColor: "red"

        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Jobbeschreibung")
    }
    TextField {
        id: jobdescription

        property string name: "jobdesc"

        Layout.columnSpan: 3
        Layout.fillWidth: true
        placeholderTextColor: "red"

        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Vertragsbeginn")
    }
    TextField {
        id: contractstart

        property string name: "contractstart"

        Layout.columnSpan: 3
        Layout.fillWidth: true
        placeholderTextColor: "red"

        validator: RegularExpressionValidator {
            regularExpression: /((^|)(0[1-9]{1}|[1-2]{1}[0-9]{1}|3[0-1]))\.((^|)(0[1-9]{1}|1[0-2]{1}))\.((^|)(196[0-9]{1}|19[7-9]{1}[0-9]{1}|20[0-9]{2}))/
        }

        Keys.onPressed: event => {
            if (event.key !== Qt.Key_Backspace) {
                var len = contractstart.length;
                var bd = contractstart.text;
                if (len === 2 || len === 5)
                    contractstart.text = bd + ".";
            }
        }
        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Vertragsende")
    }
    TextField {
        id: contractend

        property string name: "contractend"

        Layout.columnSpan: 3
        Layout.fillWidth: true
        placeholderTextColor: "red"

        validator: RegularExpressionValidator {
            regularExpression: /((^|)(0[1-9]{1}|[1-2]{1}[0-9]{1}|3[0-1]))\.((^|)(0[1-9]{1}|1[0-2]{1}))\.((^|)(196[0-9]{1}|19[7-9]{1}[0-9]{1}|20[0-9]{2}))/
        }

        Keys.onPressed: event => {
            if (event.key !== Qt.Key_Backspace) {
                var len = contractend.length;
                var bd = contractend.text;
                if (len === 2 || len === 5)
                    contractend.text = bd + ".";
            }
        }
        onTextChanged: checkFields()
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Arbeitszeiten Tage")
    }
    ComboBox {
        id: workdays

        property string name: "workdays"

        Layout.fillWidth: true
        model: ["1", "2", "3", "4", "5", "6", "7"]
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Stunden")
    }
    ComboBox {
        id: workhours

        property string name: "workhours"

        Layout.fillWidth: true
        model: ["1", "2", "3", "4", "5", "6", "7", "8"]
    }
    Label {
        Layout.alignment: Qt.AlignRight
        text: qsTr("Briefanrede")
    }
    TextField {
        id: briefAnrede

        property string name: "formofaddress"

        Layout.columnSpan: 3
        Layout.fillWidth: true
        placeholderTextColor: "red"

        onTextChanged: checkFields()
    }
    Item {
        Layout.columnSpan: 4
        Layout.fillHeight: true
    }
}
