
// Qt
import QtQuick 2.12

// Project
//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/pages/posttreatment"

/*!
 * \brief   the post treatment disconnect stack screen
 */
StackItem { id: _root
    stackView.initialItem   : null

    enum Step {
        DisconnectPatient   ,
        DrainConsumable     ,
        Count
    }

    property int subStepIndex: PostTreatmentDisconnectStack.Step.DisconnectPatient
    property int    stepIndex: PostTreatmentStack.Disconnect

    signal goToNextStep()

    function setPage(vPage, vCondition) {
        _root.subStepIndex = vPage.stepIndex
        page( vPage , vCondition)
    }

    function initStack() { setPage ( _postTreatmentDisconnectPatient ) }

    function continueClicked() {
        switch ( _root.subStepIndex ) {
        case PostTreatmentDisconnectStack.Step.DisconnectPatient:
            vPostTreatmentAdjustmentPatientDisconnectionConfirm.doConfirm()
            break
        case PostTreatmentDisconnectStack.Step.DrainConsumable:
            goToNextStep ()
            break
        default:
            break
        }
    }

    function backClicked() {
        switch ( _root.subStepIndex ) {
        case PostTreatmentDisconnectStack.Step.DisconnectPatient:
            break
        case PostTreatmentDisconnectStack.Step.DrainConsumable:
            setPage ( _postTreatmentDisconnectPatient )
            break
        default:
            break
        }
    }

    function backEnabled() {
        return _root.subStepIndex === PostTreatmentDisconnectStack.Step.DrainConsumable
    }

    // ----- Screens definitions
    PostTreatmentBase               { id: _postTreatmentDisconnectPatient; stepIndex: PostTreatmentDisconnectStack.Step.DisconnectPatient; subStepName: qsTr("Disconnect Patient BloodLines"); instructionBased: true}
    PostTreatmentDrainConsumables   { id: _postTreatmentDrainConsumables;  stepIndex: PostTreatmentDisconnectStack.Step.DrainConsumable;                            }

    // Confirm Patient Disconnection
    Connections { target: vPostTreatmentAdjustmentPatientDisconnectionConfirm
        function onAdjustmentTriggered          ( vValue ) {
            if ( vPostTreatmentAdjustmentPatientDisconnectionConfirm.adjustment_Accepted ) {
                _postTreatmentDisconnectPatient.reasonText = ""
                setPage ( _postTreatmentDrainConsumables )
            } else {
                _postTreatmentDisconnectPatient.reasonText = vPostTreatmentAdjustmentPatientDisconnectionConfirm.text()
            }
        }
    }

    onVisibleChanged: {
        if (visible) {
            _headerBar.activeStack = stackView
            initStack()
        }
        else {
            stackView.initialItem = null
        }
    }
}
