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

ColumnLayout
{
    property var new_object: null
    //property alias checkAddContact: checkAddContact
    Layout.fillWidth: true
    Layout.fillHeight: true
    spacing: 15
    Label
    {
        text: qsTr("Objekt anlegen")
        horizontalAlignment: Text.AlignHCenter
        Layout.fillWidth: true
        font.pixelSize: 35
    }
    CheckBox
    {
        id: checkAddObjectContact
        text: qsTr("Ansprechpartner hinzufügen")
        Layout.alignment: Qt.AlignRight
        checked: false
        onCheckStateChanged:
        {
            checkFields()
        }
    }

    RowLayout
    {
        id: addObject
        Layout.fillWidth: true
        Layout.fillHeight: true
        spacing: 45

        Frame
        {
            Layout.alignment: Qt.AlignTop
            Layout.fillWidth: true
            AddNewObject
            {
                id: newObject
                width: parent.width
            }
        }

        ObjectAddOns
        {
            id: addObjectLayout
            visible: checkAddObjectContact.checked
        }
    }
    RowLayout
    {
        Layout.fillHeight: true
        Layout.alignment: Qt.AlignRight
        Button
        {
            text: qsTr("Abbrechen")
            onClicked: contentStack.pop()
        }
        Button
        {
            property var new_object: null
            id: saveBtn
            text: qsTr("Speichern")
            enabled: false
            onClicked:
            {
                new_object = JsLib.parseForm(newObject)
                new_object['lift'] = new_object['lift'] === 'Ja' ? 1 : 0
                new_object['mezzanin'] = new_object['mezzanin'] === 'Ja' ? 1 : 0
                object_model.addObject(new_object)
            }
        }
    }
    Item
    {
        id: spacer3
        Layout.fillHeight: true
    }

    Component.onCompleted:
    {
        //object_model.objectAdded.connect(onObjectAdded)
        //contact_model.objectContactAdded.connect(onObjectContact)
    }


    Connections
    {
        target: object_model

        function onObjectIdReady()
        {
            var obj_id = arguments[0]
            if (checkAddObjectContact.checked && obj_id)
            {
                var new_objecto = addObjectLayout.getForm()
                contact_model.addObjectContact(new_objecto, obj_id)
                object_model.viewCriterion("Alle")
            }

            contentStack.pop()
        }
    }

    // function onObjectAdded(added, oid)
    // {
    //     if (!added)
    //         console.log(qsTr("Fehler beim Objekt-Anlegen!"))
    //     if (checkAddObjectContact.checked && oid)
    //     {
    //         var new_objecto = addObjectLayout.getForm()

    //         contact_model.addObjectContact(new_objecto, oid)
    //     }
    //     else appLoader.source = "ObjectTable.qml"
    // }

    // function onObjectContact(added)
    // {
    //     if (!added)
    //         console.log(qsTr("Fehler beim Objekt-Kontakt-Anlegen!"))
    //     else
    //     {
    //         //object_model.viewCriterion("Alle")
    //         appLoader.source = "ObjectTable.qml"
    //     }
    // }

    function checkFields()
    {
        if(checkAddObjectContact.checked)
        {
            if(!newObject.checkObjectField() || !addObjectLayout.contactPerson.contacts || !addObjectLayout.contactPerson.contacts.length)
                saveBtn.enabled = false
            else
                saveBtn.enabled = true
        }
        else if (!newObject.checkObjectField())
            saveBtn.enabled = false
        else
            saveBtn.enabled = true
    }
}
