import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Window
{
    property alias printerDialog: printDialog
    property var printers: null

    id: printDialog
    title: qsTr("PYQCRM - Drucker")
    color: palette.base
    minimumWidth: 300
    maximumWidth: 600
    minimumHeight: 150
    maximumHeight: 150
    ColumnLayout
    {
        spacing: 9
        y: 15
        implicitWidth: parent.width
        RowLayout
        {
            Layout.fillWidth: true
            Layout.leftMargin: 5
            Layout.rightMargin: 5
            Label
            {
                id: printersLabel
                Layout.alignment: Qt.AlignRight
                text: qsTr("Drucker")
            }

            ComboBox
            {
                id: allPrinters
                model: printers
                Layout.fillWidth: true
            }
        }

        RowLayout
        {
            Layout.leftMargin: 5
            Layout.rightMargin: 5
            Layout.fillWidth: true

            Label
            {
                Layout.minimumWidth: printersLabel.width
                Layout.alignment: Qt.AlignRight
                text: qsTr("Kopie")
            }

            SpinBox
            {
                id: copiesSpinBox
                from: 1
                to: 10
                value: 1
            }

            Item
            {
                Layout.fillWidth: true
            }
        }

        RowLayout
        {
            Layout.leftMargin: 5
            Layout.rightMargin: 5
            Layout.fillWidth: true
            CheckBox
            {
                id: colorPrint
                text: qsTr("Farbe")
                Layout.minimumWidth: printersLabel.width
            }

            Item
            {
                Layout.fillWidth: true
            }
        }

        RowLayout
        {
            Layout.leftMargin: 5
            Layout.rightMargin: 5
            Layout.fillWidth: true
            Item
            {
                Layout.fillWidth: true
            }

            Button
            {
                id: printButton
                text: qsTr("Drucken")
                onClicked:
                {
                    var copies = copiesSpinBox.value > 1? copiesSpinBox.value + " copies": "one copy"
                    console.log("Printing ", copies, " using ", allPrinters.currentText);
                    printDialog.close();
                }
            }

            Button
            {
                text: qsTr("Ablehnen")
                onClicked: printDialog.close();
            }
        }

        Item
        {
            Layout.fillHeight: true
        }
    }

    onVisibleChanged:
    {
        copiesSpinBox.value = 1
        colorPrint.checked = true
        if (!sys_printers)
        {
            printers = sys_printers.getPrinters()
            if (sys_printers.getDefaultPrinter())
                allPrinters.currentIndex = allPrinters.indexOfValue(sys_printers.getDefaultPrinter())
        }
    }

    Component.onCompleted:
    {
        if (sys_printers)
        {
            printers = sys_printers.getPrinters()
            if (sys_printers.getDefaultPrinter())
                allPrinters.currentIndex = allPrinters.indexOfValue(sys_printers.getDefaultPrinter())
        }
    }
}
