/*!
 *
 * Copyright (c) 2021-2025 Diality Inc. - All Rights Reserved.
 * \copyright
 * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN
 * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER.
 *
 * \file    PreTreatmentCreateContent.qml
 * \author  (last)      Nico Ramirez
 * \date    (last)      21-Aug-2025
 * \author  (original)  Nico Ramirez
 * \date    (original)  21-Aug-2025
 *
 */

// Qt
import QtQuick 2.15
import QtQuick.Controls 2.15

//  Qml
import "qrc:/globals"
import "qrc:/components"
import "qrc:/compounds"
import "qrc:/dialogs"
import "qrc:/pages/pretreatment"

Item { id: _root
    readonly property int cellWidth     : width  / 2 - ( rowSpacing / 2 )
    readonly property int cellHeight    : ( _contentGrid.height ) / 7 - ( columnSpacing )
    readonly property int rowSpacing    : Variables.defaultMargin * 3
    readonly property int columnSpacing : 15
    readonly property bool isValidated  : vTreatmentCreate.parametersValidated
    readonly property bool heparinFeatured: vSettings.heparinSyringePump

    readonly property string editRx     : qsTr( "Edit Rx"    )
    readonly property string selectRx   : qsTr( "Select Rx"  )
    readonly property string clearAll   : qsTr( "Clear All"  )

    property bool editingEnabled        : true

    onIsValidatedChanged : canEdit ( ! isValidated )

    function canEdit  (state) { editingEnabled = state && ! vTDOpMode.inTreatment }

    function activateAndRefresh() {
        refreshAll () // needs to be first
        vTreatmentCreate.bloodFlowRateSet                   = true
        vTreatmentCreate.dialysateFlowRateSet               = true
        vTreatmentCreate.treatmentDurationSet               = true
        vTreatmentCreate.heparinDispensingRateSet           = true
        vTreatmentCreate.heparinBolusVolumeSet              = true
        vTreatmentCreate.heparinStopTimeSet                 = true
        vTreatmentCreate.dialysateTempSet                   = true
        vTreatmentCreate.salineBolusVolumeSet               = true
        vTreatmentCreate.bloodPressureMeasureIntervalSet    = true
        vTreatmentCreate.acidConcentrateSet                 = true
        vTreatmentCreate.bicarbonateConcentrateSet          = true
        vTreatmentCreate.dialyzerTypeSet                    = true
    }

    function refreshAll () {
        _bloodFlowRateControl           .refresh()
        _dialysateFlowRateControl       .refresh()
        _durationControl                .refresh()
        _heparinDispensingRateControl   .refresh()
        _heparinBolusVolumeControl      .refresh()
        _heparinStopTimeControl         .refresh()
        _dialysateTemperatureControl    .refresh()
        _salineBolusVolumeControl       .refresh()
    }

    function clear () {
        vTreatmentCreate.patientID  = ""
        _bloodFlowRateControl           .clear()
        _dialysateFlowRateControl       .clear()
        _durationControl                .clear()
        _heparinDispensingRateControl   .clear()
        _heparinBolusVolumeControl      .clear()
        _heparinStopTimeControl         .clear()
        _acidConcentrateComboBox        .clear()
        _bicarbonateConcentrateComboBox .clear()
        _dialyzerTypeComboBox           .clear()
        _dialysateTemperatureControl    .clear()
        _salineBolusVolumeControl       .clear()
        _bpMeasurementIntervalControl   .clear()
        clearErrors()
        canEdit (true)
    }

    function clearErrors() {
        vTreatmentCreate.bloodFlowRateRejectReason                  = Variables.noRejectReason
        vTreatmentCreate.dialysateFlowRateRejectReason              = Variables.noRejectReason
        vTreatmentCreate.treatmentDurationRejectReason              = Variables.noRejectReason
        vTreatmentCreate.heparinDispensingRateRejectReason          = Variables.noRejectReason
        vTreatmentCreate.heparinBolusVolumeRejectReason             = Variables.noRejectReason
        vTreatmentCreate.heparinStopTimeRejectReason                = Variables.noRejectReason
        vTreatmentCreate.acidConcentrateRejectReason                = Variables.noRejectReason
        vTreatmentCreate.bicarbonateConcentrateRejectReason         = Variables.noRejectReason
        vTreatmentCreate.dialyzerTypeRejectReason                   = Variables.noRejectReason
        vTreatmentCreate.dialysateTempRejectReason                  = Variables.noRejectReason
        vTreatmentCreate.salineBolusVolumeRejectReason              = Variables.noRejectReason
        vTreatmentCreate.bloodPressureMeasureIntervalRejectReason   = Variables.noRejectReason
    }

    function confirmReady () {
        return  _bloodFlowRateControl           .isActive     && _bloodFlowRate           .valid  &&
                _dialysateFlowRateControl       .isActive     && _dialysateFlowRate       .valid  &&
                _durationControl                .isActive     && _duration                .valid  &&
                ( _root.heparinFeatured ? _heparinDispensingRateControl   .isActive     && _heparinDispensingRate   .valid  &&
                _heparinBolusVolumeControl      .isActive     && _heparinBolusVolume      .valid  &&
                _heparinStopTimeControl         .isActive     && _heparinStopTime         .valid : true ) &&
                _acidConcentrateComboBox        .isActive     &&
                _bicarbonateConcentrateComboBox .isActive     &&
                _dialyzerTypeComboBox           .isActive     &&
                _dialysateTemperatureControl    .isActive     && _dialysateTemperature    .valid  &&
                _salineBolusVolumeControl       .isActive     && _salineBolusVolume       .valid  &&
                _bpMeasurementIntervalControl   .isActive
    }

    function validate () {
        vTreatmentCreate                    .patientID = _pretreatmentPatientIDEntry.text
        vPostTreatmentAdjustmentTreatmentLog.patientID = _pretreatmentPatientIDEntry.text // store for the TreatmentLog

        vTreatmentCreate.arterialPressureLimitWindow = vTreatmentRanges.arterialPressureLimitWindowDef  // these parameters don't have a user option on create treatment, so sending the default.
        vTreatmentCreate.venousPressureLimitWindow   = vTreatmentRanges.venousPressureLimitWindowDef    // these parameters don't have a user option on create treatment, so sending the default.
        vTreatmentCreate.venousPressureLimitAsymtrc  = vTreatmentRanges.venousPressureLimitAsymtrcDef   // these parameters don't have a user option on create treatment, so sending the default.
        vTreatmentCreate.rinsebackFlowRate           = vTreatmentRanges.rinsebackFlowRateDef            // these parameters don't have a user option on create treatment, so sending the default.
        vTreatmentCreate.doValidation       (        )
    }

    function confirm () { vTreatmentCreate.doConfirm() }

    Connections{ target: vTreatmentCreate
        function onDidValidationPass            ( ) {
            vPostTreatmentAdjustmentTreatmentLog.heparinDispensingRateOff = vTreatmentCreate.heparinDispensingRateOff   = ! vTreatmentCreate.heparinDispensingRate
            vPostTreatmentAdjustmentTreatmentLog.heparinBolusVolumeOff    = vTreatmentCreate.heparinBolusVolumeOff      = ! vTreatmentCreate.heparinBolusVolume
            activateAndRefresh() // refresh values on validation
            clearErrors()
        }
    }

    Connections {  target: _acidConcentrateAdjustment
        function onAccepted () {
            vTreatmentCreate.acidConcentrate      = _acidConcentrateComboBox.find(_acidConcentrateAdjustment.adjustment)
            _acidConcentrateComboBox.currentIndex = vTreatmentCreate.acidConcentrate
        }
    }

    MouseArea { // click outside to remove active focus and lower keyboard
        anchors.fill            : parent
        propagateComposedEvents : true
        onClicked               : focus = true   // grab focus here
    }

    Text { id: _prescriptionTitle
        anchors {
            top         : parent.top
            topMargin   : Variables.defaultMargin
            left        : parent.left
        }
        text            : qsTr("Prescription")
        color           : Colors.textMain
        font.pixelSize  : Fonts.fontPixelTitle
        font.weight     : Font.Medium
    }

    BaseComboBox { id: _prescriptionMenu
        anchors {
            left                : _prescriptionTitle.right
            verticalCenter      : _prescriptionTitle.verticalCenter
            verticalCenterOffset: 5
        }

        model                   : ! vTreatmentCreate.parametersValidated ?  [ _root.selectRx,   _root.clearAll                      ]:
                                                                            [ _root.editRx,     _root.selectRx,     _root.clearAll  ]
        height                  : 50
        width                   : 75
        dropDownWidth           : 250
        delegateHeight          : 65
        iconAnchors.rightMargin : Variables.defaultMargin * 2
        displayText             : ""
        iconSource              : "qrc:/images/iMenuDot"
        backgroundColor         : Colors.transparent
        visible                 : ! vTDOpMode.inTreatment

        onActivated: {
            const selectedItem = model[currentIndex]

            // only send  AdjustParametersConfirmRequestData::eCancel when validated
            // Telling FW user is canceling confirm treatment parameters
            if ( vTreatmentCreate.parametersValidated ) { vTreatmentCreate.doCancel() }

            if      ( selectedItem === _root.editRx     )   { /*All parameters set to edit if parameters are validated */   }
            else if ( selectedItem === _root.selectRx   )   { print("TODO: Open and Select from Prescription Manager")      }
            else if ( selectedItem === _root.clearAll   )   { _root.clear()                                                 }
            else                                            {  /*Unknown state */                                           }
        }
    }

    Grid { id: _contentGrid
        anchors {
            top         : _prescriptionTitle.bottom
            topMargin   : Variables.defaultMargin * 2
            left        : _root.left
            right       : _root.right
            bottom      : parent.bottom
        }
        // Flow intentially set o keep top row aligned. To organize with Heparin Defeature
        flow            : Grid.LeftToRight
        rows            : _root.heparinFeatured ? 7 : 6
        columns         : 2
        rowSpacing      :_root.columnSpacing
        columnSpacing   :  _root.rowSpacing

        Item { id: _patientIDItem
            height  : _root.cellHeight
            width   : _root.cellWidth

            LabelUnitContainer { id: _patientID
                anchors.verticalCenter          : parent.verticalCenter
                text                            : qsTr("Patient ID")
                height                          : cellHeight - Variables.defaultMargin
                contentArea.anchors.leftMargin  : 120

                contentItem : TextEntry { id: _pretreatmentPatientIDEntry
                    clip                        : true
                    textInput.width             : parent.width - Variables.defaultMargin * 4
                    text                        : vTreatmentCreate.patientID
                    anchors.centerIn            : parent
                    textInput.maximumLength     : 20  // LEAHI-PRS-236
                    textInput.rightPadding      : Variables.defaultMargin
                    textInput.leftPadding       : Variables.defaultMargin * 14
                    textInput.inputMethodHints  : Qt.ImhPreferLowercase
                    textInput.echoMode          : TextInput.Normal
                    textInput.validator         : RegExpValidator { regExp: Variables.regExp_PatientID }
                    line.visible                : false
                    enabled                     : _root.editingEnabled
                    onEditingFinished           : vTreatmentCreate.patientID = text

                    Text { id: _patientIDPlaceHolderText
                        text                : Variables.emptyEntry
                        anchors.fill        : parent
                        rightPadding        : 125
                        horizontalAlignment : Text.AlignRight
                        font.pixelSize      : Fonts.fontPixelValueControl
                        color               : Colors.offWhite
                        visible             :   _pretreatmentPatientIDEntry.textInput.text.length === 0 &&
                                              ! _pretreatmentPatientIDEntry.textInput.activeFocus
                    }
                }
            }
        }

        Row { id: _qrRow
            height          : _root.cellHeight
            spacing         : Variables.defaultMargin

            QRCode { id: _qrCode
                anchors.verticalCenter      : parent.verticalCenter
                anchors.verticalCenterOffset: -5
                qrcode          : vTreatmentCreate.txCode
                clear           : ! _root.visible
            }

            Text { id: _txCode
                anchors.bottom  : _qrCode.bottom
                text            : vTreatmentCreate.txCode ? qsTr("Tx Code: ") + vTreatmentCreate.txCode : " "
                color           : "#DBE9FA"
                font.pixelSize  : Fonts.fontPixelButton
            }
        }

        LabelUnitContainer { id: _bloodFlowRate
            text        : qsTr("Blood Flow Rate")
            unitText    : Variables.unitTextFlowRate
            valid       : ! vTreatmentCreate.bloodFlowRateRejectReason

            contentItem : ValueAdjuster { id: _bloodFlowRateControl
                editable        : _root.editingEnabled
                minimum         : vTreatmentRanges.bloodFlowRateMin
                maximum         : vTreatmentRanges.bloodFlowRateMax
                step            : vTreatmentRanges.bloodFlowRateRes
                defaultValue    : vTreatmentRanges.bloodFlowRateDef
                value           : vTreatmentCreate.bloodFlowRate
                isActive        : vTreatmentCreate.bloodFlowRateSet
                onDidActiveChange: function(vState) {  vTreatmentCreate.bloodFlowRateSet = vState }
                onDidChange     : function(vValue) {
                    if ( ! _bloodFlowRate.valid ) { vTreatmentCreate.bloodFlowRateRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.bloodFlowRate = vValue
                }
            }
        }

        LabelUnitContainer { id: _acidConcentrate
            text            : qsTr("Acid Concentrate")
            showEdit        : _root.editingEnabled
            onEditClicked   : _acidConcentrateAdjustment.open()
            valid           : ! vTreatmentCreate.acidConcentrateRejectReason

            contentItem : BaseComboBox { id: _acidConcentrateComboBox
                anchors.rightMargin : Variables.defaultMargin * 2
                anchors.leftMargin  : anchors.rightMargin
                anchors.topMargin   : Variables.defaultMargin / 2
                anchors.bottomMargin: anchors.topMargin
                isActive            : vTreatmentCreate.acidConcentrateSet
                enabled             : _root.editingEnabled
                currentIndex        : vTreatmentCreate.acidConcentrate
                model               : vTreatmentRanges.acidConcentrateOptions
                onClear             : {
                    vTreatmentRanges.doClearAcidConcentrate(vTreatmentCreate.acidConcentrateSet)
                    vTreatmentCreate.acidConcentrateSet = false
                }

                onActivated         : {
                    if ( ! _acidConcentrate.valid ) { vTreatmentCreate.acidConcentrateRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.acidConcentrate    = _acidConcentrateComboBox.currentIndex
                    vTreatmentCreate.acidConcentrateSet = true
                }
            }
        }

        LabelUnitContainer { id: _dialysateFlowRate
            text        : qsTr("Dialysate Flow Rate")
            unitText    : Variables.unitTextFlowRate
            valid       : ! vTreatmentCreate.dialysateFlowRateRejectReason

            contentItem : ValueAdjuster { id: _dialysateFlowRateControl
                editable        : _root.editingEnabled
                minimum         : vTreatmentRanges.dialysateFlowRateMin
                maximum         : vTreatmentRanges.dialysateFlowRateMax
                step            : vTreatmentRanges.dialysateFlowRateRes
                defaultValue    : vTreatmentRanges.dialysateFlowRateDef
                value           : vTreatmentCreate.dialysateFlowRate
                isActive        : vTreatmentCreate.dialysateFlowRateSet
                onDidActiveChange: function(vState) {  vTreatmentCreate.dialysateFlowRateSet = vState }
                onDidChange     :  function(vValue) {
                    if ( ! _dialysateFlowRate.valid ) { vTreatmentCreate.dialysateFlowRateRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.dialysateFlowRate = vValue
                }
            }
        }

        LabelUnitContainer { id: _bicarbonateConcentrate
            text        : qsTr("Bicarbonate Concentrate")
            valid       : ! vTreatmentCreate.bicarbonateConcentrateRejectReason

            contentItem : BaseComboBox { id: _bicarbonateConcentrateComboBox
                anchors.rightMargin : Variables.defaultMargin * 2
                anchors.leftMargin  : anchors.rightMargin
                anchors.topMargin   : Variables.defaultMargin / 2
                anchors.bottomMargin: anchors.topMargin
                isActive            : vTreatmentCreate.bicarbonateConcentrateSet
                enabled             : _root.editingEnabled
                currentIndex        : vTreatmentCreate.bicarbonateConcentrate
                model               : vTreatmentRanges.bicarbonateConcentrateOptions
                onClear             : vTreatmentCreate.bicarbonateConcentrateSet = false
                onActivated         : {
                    if ( ! _bicarbonateConcentrate.valid ) { vTreatmentCreate.bicarbonateConcentrateRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.bicarbonateConcentrate    = _bicarbonateConcentrateComboBox.currentIndex
                    vTreatmentCreate.bicarbonateConcentrateSet = true
                }
            }
        }

        LabelUnitContainer { id: _duration
            text        : qsTr("Duration")
            unitText    : Variables.unitTextDuration
            valid       : ! vTreatmentCreate.treatmentDurationRejectReason

            contentItem : ValueAdjuster { id: _durationControl
                editable        : _root.editingEnabled
                minimum         : vTreatmentRanges.treatmentDurationMin
                maximum         : vTreatmentRanges.treatmentDurationMax
                step            : vTreatmentRanges.treatmentDurationRes
                defaultValue    : vTreatmentRanges.treatmentDurationDef
                value           : vTreatmentCreate.treatmentDuration
                isActive        : vTreatmentCreate.treatmentDurationSet
                onDidActiveChange: function(vState) {  vTreatmentCreate.treatmentDurationSet = vState }
                onDidChange     : function(vValue) {
                    if ( ! _duration.valid ) { vTreatmentCreate.treatmentDurationRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.treatmentDuration = vValue

                    // set heparin time to clear when set
                    if ( _heparinStopTimeControl.enabled ) { _heparinStopTimeControl.clear() }
                }
            }
        }

        LabelUnitContainer { id: _dialyzerType
            text        : qsTr("Dialyzer Type")
            valid       : ! vTreatmentCreate.dialyzerTypeRejectReason

            contentItem : BaseComboBox { id: _dialyzerTypeComboBox
                anchors.rightMargin : Variables.defaultMargin * 2
                anchors.leftMargin  : anchors.rightMargin
                anchors.topMargin   : Variables.defaultMargin / 2
                anchors.bottomMargin: anchors.topMargin
                isActive            : vTreatmentCreate.dialyzerTypeSet
                enabled             : _root.editingEnabled
                currentIndex        : vTreatmentCreate.dialyzerType
                model               : vTreatmentRanges.dialyzerTypeOptions
                onClear             : vTreatmentCreate.dialyzerTypeSet = false
                onActivated         : {
                    if ( ! _dialyzerType.valid ) { vTreatmentCreate.dialyzerTypeRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.dialyzerType    = _dialyzerTypeComboBox.currentIndex
                    vTreatmentCreate.dialyzerTypeSet = true
                }
            }
        }

        LabelUnitContainer { id: _heparinBolusVolume
            text        : qsTr("Heparin Bolus Volume")
            unitText    : Variables.unitTextFluid
            valid       : ! vTreatmentCreate.heparinBolusVolumeRejectReason
            visible     : _root.heparinFeatured

            contentItem : ValueAdjuster { id: _heparinBolusVolumeControl
                editable        : _root.editingEnabled
                minimum         : vTreatmentRanges.heparinBolusVolumeMin
                maximum         : vTreatmentRanges.heparinBolusVolumeMax
                step            : vTreatmentRanges.heparinBolusVolumeRes
                defaultValue    : vTreatmentRanges.heparinBolusVolumeDef
                value           : vTreatmentCreate.heparinBolusVolume
                decimal         : Variables.heparinPrecision
                canOff          : true
                isActive        : vTreatmentCreate.heparinBolusVolumeSet
                onDidActiveChange: function(vState) {  vTreatmentCreate.heparinBolusVolumeSet = vState }
                onDidChange     : function(vValue) {
                    if ( ! _heparinBolusVolume.valid ) { vTreatmentCreate.heparinBolusVolumeRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.heparinBolusVolume = vValue
                }
            }
        }

        LabelUnitContainer { id: _bpMeasurementInterval
            text        : qsTr("Vitals Interval")
            unitText    : Variables.unitTextDuration
            valid       : ! vTreatmentCreate.bloodPressureMeasureIntervalRejectReason

            contentItem : BaseComboBox { id: _bpMeasurementIntervalControl
                anchors.rightMargin : Variables.defaultMargin * 2
                anchors.leftMargin  : anchors.rightMargin
                anchors.topMargin   : Variables.defaultMargin / 2
                anchors.bottomMargin: anchors.topMargin
                isActive            : vTreatmentCreate.bloodPressureMeasureIntervalSet
                enabled             : _root.editingEnabled
                currentIndex        : vTreatmentRanges.bloodPressureMeasureInterval.indexOf(vTreatmentCreate.bloodPressureMeasureInterval.toString())
                model               : vTreatmentRanges.bloodPressureMeasureInterval
                canOff              : true
                centerHorizontally  : true

                onClear             : {
                    vTreatmentCreate.bloodPressureMeasureIntervalSet = false
                }

                onActivated         : {
                    if ( ! _bpMeasurementInterval.valid ) {
                        vTreatmentCreate.bloodPressureMeasureIntervalRejectReason = Variables.noRejectReason
                    }
                    vTreatmentCreate.bloodPressureMeasureInterval    = vTreatmentRanges.bloodPressureMeasureInterval[_bpMeasurementIntervalControl.currentIndex]
                    vTreatmentCreate.bloodPressureMeasureIntervalSet = true
                }
            }
        }

        LabelUnitContainer { id: _heparinDispensingRate
            text        : qsTr("Heparin Dispensing Rate")
            unitText    : Variables.unitTextDispensingRate
            valid       : ! vTreatmentCreate.heparinDispensingRateRejectReason
            visible     : _root.heparinFeatured

            contentItem : ValueAdjuster { id: _heparinDispensingRateControl
                editable        : _root.editingEnabled
                minimum         : vTreatmentRanges.heparinDispensingRateMin
                maximum         : vTreatmentRanges.heparinDispensingRateMax
                step            : vTreatmentRanges.heparinDispensingRateRes
                defaultValue    : vTreatmentRanges.heparinDispensingRateDef
                value           : vTreatmentCreate.heparinDispensingRate
                decimal         : Variables.heparinPrecision
                canOff          : true
                isActive        : vTreatmentCreate.heparinDispensingRateSet
                onDidActiveChange: function(vState) {  vTreatmentCreate.heparinDispensingRateSet = vState }
                onDidChange     : function(vValue) {
                    if ( ! _heparinDispensingRate.valid ) { vTreatmentCreate.heparinDispensingRateRejectReason = Variables.noRejectReason }
                    // set heparin time to clear when set OFF to value
                    if ( ! vTreatmentCreate.heparinDispensingRate && vValue ) { _heparinStopTimeControl.clear() }

                    vTreatmentCreate.heparinDispensingRate = vValue

                    // if set to OFF set heparin stop time 0
                    if ( ! vTreatmentCreate.heparinDispensingRate ) {
                        vTreatmentCreate.heparinStopTimeSet = true
                        vTreatmentCreate.heparinStopTime = 0
                    }
                }

                onIsActiveChanged: {
                    if ( ! vTreatmentCreate.heparinDispensingRate && isActive ) {
                        vTreatmentCreate.heparinStopTimeSet = true
                        vTreatmentCreate.heparinStopTime    = 0
                    }
                }
            }
        }

        LabelUnitContainer { id: _dialysateTemperature
            text        : qsTr("Dialysate Temperature")
            unitText    : Variables.unitTextTemperature
            valid       : ! vTreatmentCreate.dialysateTempRejectReason

            contentItem : ValueAdjuster { id: _dialysateTemperatureControl
                editable        : _root.editingEnabled
                minimum         : vTreatmentRanges.dialysateTempMin
                maximum         : vTreatmentRanges.dialysateTempMax
                step            : vTreatmentRanges.dialysateTempRes
                defaultValue    : vTreatmentRanges.dialysateTempDef
                value           : vTreatmentCreate.dialysateTemp
                decimal         : Variables.dialysateTempPrecision
                isActive        : vTreatmentCreate.dialysateTempSet
                onDidActiveChange: function(vState) {  vTreatmentCreate.dialysateTempSet = vState }
                onDidChange     : function(vValue) {
                    if ( ! _dialysateTemperature.valid ) { vTreatmentCreate.dialysateTempRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.dialysateTemp = vValue
                }
            }
        }

        LabelUnitContainer { id: _heparinStopTime
            text        : qsTr("Heparin Delivery Duration")
            unitText    : Variables.unitTextDuration
            valid       : ! vTreatmentCreate.heparinStopTimeRejectReason
            visible     : _root.heparinFeatured

            contentItem : ValueAdjuster { id: _heparinStopTimeControl
                editable        : _root.editingEnabled
                minimum         : vTreatmentRanges.heparinStopTimeMin
                maximum         : _durationControl.value
                step            : _durationControl.step
                defaultValue    : _durationControl.value
                value           : vTreatmentCreate.heparinStopTime
                enabled         : vTreatmentCreate.heparinDispensingRate
                canOff          : true
                isActive        : vTreatmentCreate.heparinStopTimeSet
                onDidActiveChange: function(vState) {  vTreatmentCreate.heparinStopTimeSet = vState }
                onDidChange     : function(vValue) {
                    if ( ! _heparinStopTime.valid ) { vTreatmentCreate.heparinStopTimeRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.heparinStopTime = vValue
                }
            }
        }

        LabelUnitContainer { id: _salineBolusVolume
            text        : qsTr("Saline Bolus Volume")
            unitText    : Variables.unitTextFluid
            valid       : ! vTreatmentCreate.salineBolusVolumeRejectReason

            contentItem : ValueAdjuster { id: _salineBolusVolumeControl
                editable        : _root.editingEnabled
                minimum         : vTreatmentRanges.salineBolusVolumeMin
                maximum         : vTreatmentRanges.salineBolusVolumeMax
                step            : vTreatmentRanges.salineBolusVolumeRes
                defaultValue    : vTreatmentRanges.salineBolusVolumeDef
                value           : vTreatmentCreate.salineBolusVolume
                isActive        : vTreatmentCreate.salineBolusVolumeSet
                onDidActiveChange: function(vState) {  vTreatmentCreate.salineBolusVolumeSet = vState }
                onDidChange     : function(vValue) {
                    if ( ! _salineBolusVolume.valid ) { vTreatmentCreate.salineBolusVolumeRejectReason = Variables.noRejectReason }
                    vTreatmentCreate.salineBolusVolume = vValue
                }
            }
        }
    }
}
