// Qt
import QtQuick 2.12

// Project

//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/compounds"
import "qrc:/pages/pretreatment/create"
import "qrc:/pages/treatment/adjustments"

TreatmentAdjustmentBase { id : _root
    property int cellWidth          : Variables.createRxLabelUnitContainerWidth
    property int cellHeight         : Variables.createRxLabelUnitContainerHeight
    property bool editingEnabled    : false

    height          : Variables.smallDialogHeight
    width           : Variables.smallDialogWidth
    titleText       : qsTr("Ultrafiltration Volume (L)")
    x               : Math.round((parent.width - width) / 2)
    y               : Math.round((parent.height - height) / 2)

    onCloseClicked  : _root.close()

    onVisibleChanged: {
        if ( visible ) {
            _estimatedTargetWeight.value = vTreatmentCreate.ufEstimatedTargetWeight
            _preWeight.value             = vTreatmentCreate.ufPreWeight
        }
    }

    Connections { target: vTDOpMode
        function onPreTreatmentChanged() { _root.close() }
    }

    Column { id : _column
        spacing: Variables.defaultMargin * 2

        anchors {
            verticalCenter      : _root.contentItem.verticalCenter
            verticalCenterOffset: Variables.defaultMargin * -3 // better align label with dialog height
            horizontalCenter    : _root.contentItem.horizontalCenter
        }

        LabelUnitValueAdjuster { id: _preWeight
            text            :  qsTr("Pre-Weight")
            width           : _root.cellWidth
            height          : _root.cellHeight
            unitText        : Variables.unitWeight
            valid           : ! vTreatmentCreate.ufPreWeightRejectionReason
            editable        : _root.editingEnabled
            minimum         : vTreatmentRanges.weightMin
            maximum         : vTreatmentRanges.weightMax
            step            : vTreatmentRanges.weightRes
            defaultValue    : vTreatmentRanges.weightDef
            decimal         : Variables.weightPrecision
            value           : vTreatmentCreate.ufPreWeight
            isActive        : true
            onDidChange     : function(vValue) {
                if ( ! _preWeight.valid ) { vTreatmentCreate.ufPreWeightRejectionReason = Variables.noRejectReason }
                value = vValue

                // zero weight if UF volume is entered
                vTreatmentCreate.ultrafiltrationVolume = 0
            }
        }

        LabelUnitValueAdjuster { id: _estimatedTargetWeight
            text            :  qsTr("Estimated Target Weight")
            width           : _root.cellWidth
            height          : _root.cellHeight
            unitText        : Variables.unitWeight
            valid           : ! vTreatmentCreate.ufEstimatedTargetWeightRejectionReason
            editable        : _root.editingEnabled
            minimum         : _preWeight.value
            maximum         : vTreatmentRanges.weightMax
            step            : vTreatmentRanges.weightRes
            defaultValue    : vTreatmentRanges.weightDef
            decimal         : Variables.weightPrecision
            value           : vTreatmentCreate.ufEstimatedTargetWeight
            isActive        : true

            onDidChange     : function(vValue) {
                if ( ! _estimatedTargetWeight.valid ) { vTreatmentCreate.ufEstimatedTargetWeightRejectionReason = Variables.noRejectReason }
                value = vValue

                // zero weight if UF volume is entered
                vTreatmentCreate.ultrafiltrationVolume = 0
            }
        }
    }

    ConfirmButton { id : _confirmButton
        anchors {
            bottom          : _root.contentItem.bottom
            bottomMargin    : Variables.defaultMargin * 4
            horizontalCenter: _root.contentItem.horizontalCenter
        }

        width       : Variables.defaultButtonWidth
        height      : Variables.defaultButtonHeight
        enabled     : _preWeight.isActive && _estimatedTargetWeight.isActive

        anchors {
            top     : undefined
            right   : undefined
            margins : 0
        }

        onClicked   : {
            vTreatmentCreate.ufEstimatedTargetWeight = _estimatedTargetWeight.value
            vTreatmentCreate.ufPreWeight = _preWeight.value
            _root.close()
        }
    }
}
