// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: panel.js,v 1.2 2026/07/23 09:29:57 qvarin Exp $
import Vue from "vue";
import panelapp from "./components/PanelApp.vue";
import InitVue from "@/common/helper/InitVue.js";

InitVue(Vue, {
    urlWebservice: $data.urlWebservice,
    useLoader: true,
});

const root = new Vue({
    el: "#prefillingform_panel_app",
    data: {
        ...$data,
        open: false,
        confirmingClose: false,
    },
    components: { panelapp },
    methods: {
        hasPendingWork() {
            return this.$refs.app ? this.$refs.app.hasPendingWork() : false;
        },
        attemptToggle() {
            if (!this.open) {
                this.open = true;
                return;
            }
            this.attemptClose();
        },
        attemptClose() {
            if (this.hasPendingWork()) {
                this.confirmingClose = true;
                return;
            }
            this.open = false;
        },
        cancelClose() {
            this.confirmingClose = false;
        },
        confirmClose() {
            this.confirmingClose = false;
            this.open = false;
            if (this.$refs.app) {
                this.$refs.app.resetState();
            }
        },
    },
    watch: {
        open(isOpen) {
            const wrapper = document.getElementById("prefillingform_panel");
            if (wrapper) {
                wrapper.classList.toggle("prefillingform-panel-open", isOpen);
            }
            const button = document.getElementById("bt_prefillingform");
            if (button) {
                button.value = this.messages.get(
                    "prefillingform_panel",
                    isOpen ? "prefillingform_button_quit" : "prefillingform_button_open"
                );
            }
        },
    },
});

window.prefillingformPanelToggle = () => root.attemptToggle();
