/*!
 *
 * 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    HeaderbarPrescription.qml
 * \author  (last)      Nico Ramirez
 * \date    (last)      11-Sep-2025
 * \author  (original)  Nico Ramirez
 * \date    (original)  11-Sep-2025
 *
 */

// Qt
import QtQuick 2.12

// Project
import "qrc:/globals"
import "qrc:/components"
import "qrc:/pages/pretreatment/create"

ModalDialog { id: _root
    readonly property bool isConfirmed: vTreatmentCreate.parametersConfirmed
    readonly property bool isFirstTab : _headerbarPrescriptionContent.currentIndex === PreTreatmentCreateContent.PatientPrescription

    padding : Variables.defaultMargin
    onOpened: _headerbarPrescriptionContent.canEdit( ! ( vTreatmentCreate.parametersValidated || vTreatmentCreate.parametersConfirmed ) )

    onIsConfirmedChanged: if (isConfirmed) { _root.close() }

    Text { id: descriptiveText
        anchors {
            top         : parent.top
//            topMargin           : -10
            left        : parent.left
            leftMargin  : Variables.defaultMargin * 5
        }
        text            : qsTr("[Original Prescribed Parameters]")
        visible         :  vTDOpMode.inTreatment
        color           : Colors.mainTreatmentOrange
        font.pixelSize  : 24
    }

    PreTreatmentCreateContent { id: _headerbarPrescriptionContent
        anchors {
            top         : parent.top
            topMargin   : Variables.defaultMargin
            left        : parent.left
            leftMargin  : Variables.defaultMargin * 3
            right       : parent.right
            rightMargin : anchors.leftMargin
            bottom      : parent.bottom
            bottomMargin: anchors.leftMargin
        }
    }

    CloseButton { id : _closeButton
        anchors {
            right   : parent.right
            left    : undefined
            margins : 0
        }

        onClicked : _root.close()
    }

    ConfirmButton { id : _confirmButton
        anchors {
            top             : undefined
            bottom          : parent.bottom
            bottomMargin    : 5
            rightMargin     : Variables.defaultMargin * 3
        }
        enabled     : _root.isFirstTab ? true : _headerbarPrescriptionContent.confirmReady()
        text.text   : _root.isFirstTab ? qsTr("Next") :
                                         vTreatmentCreate.parametersValidated ? qsTr("Confirm") :
                                                                                qsTr("Validate")
        visible     : ! vTDOpMode.inTreatment && ! isConfirmed

        onClicked   : {
            if ( _root.isFirstTab ) {
                _headerbarPrescriptionContent.currentIndex = PreTreatmentCreateContent.TreatmentSettings
                return
            }

            if ( vTreatmentCreate.parametersValidated ) {
                _headerbarPrescriptionContent.confirm()
                _root.close()
            }
            else { _headerbarPrescriptionContent.validate() }
        }
    }
}
