
var App = Vue.createApp({
    data() {
        return {
            queueList: [],
            printerList: [],
            queueEditor: {
                id: null,
                name: "",
                printerId: null
            }
        }
    },
    methods: {
        EditQueue(QueueID, ShowModal = true)
        {
            let QueueData = this.queueList.find((_Queue) => {
                return _Queue.id == QueueID;
            });

            this.queueEditor.id = QueueData.id;
            this.queueEditor.name = QueueData.name;
            this.queueEditor.printerId = QueueData.printerId;

            if (ShowModal) {
                $('#QueueSettingsModal').modal('show');
            }
        },
        CreateQueue()
        {
            this.queueEditor.id = null;
            this.queueEditor.name = "";
            this.queueEditor.printerId = null;

            $('#QueueSettingsModal').modal('show');
        },
        async SaveQueue()
        {
            if (this.queueEditor.id == null)
            {
                await LabelApi.POST('queue', {
                    name: this.queueEditor.name,
                    printerId: this.queueEditor.printerId,
                });

                GetQueueList();
            }
            else
            {
                await LabelApi.PUT('queue', {
                    id: this.queueEditor.id,
                    name: this.queueEditor.name,
                    printerId: this.queueEditor.printerId,
                });

                GetQueueList();
            }

            $('#QueueSettingsModal').modal('hide');
        },
        async DeleteQueue(QueueID, Confirm = false)
        {
            this.EditQueue(QueueID, false);
            
            if (Confirm) {
                $('#QueueDeleteModal').modal('hide');

                await LabelApi.DELETE('queue', {
                    id: QueueID
                });

                GetQueueList();
            } else {
                $('#QueueDeleteModal').modal('show');
            }
        },
        GetPrinterById(PrinterID) {
            if (PrinterID != null) {
                return this.printerList.find((Printer) => {
                    return Printer.id == PrinterID;
                }).name;
            } else {
                return "Kein Drucker";
            }
        }
    }
}).mount('main');

async function GetQueueList() {
    let QueueList = await LabelApi.GET('queues');
    App.queueList = QueueList;
}

async function GetPrinterList() {
    let PrinterList = await LabelApi.GET('printers');
    App.printerList = PrinterList;
}

GetPrinterList();
GetQueueList();
