import { UniversalDataService } from '/js/ApiDataServer/Universal.js?tf=20251231';
import { PackingDataService } from '/js/ApiDataServer/Packing.js?tf=202411201826';
import { OrderDataService } from '/js/ApiDataServer/Order.js';
import { RESPONSE_STATUS } from '/js/constants.js';
import { enMessage } from '/js/constants.js';
import { Decorator } from '/js/util/dacorator.js';
import { UJ } from '/js/util/uj.js';

var vMain = new Vue({
    el: "#dvMain",
    data: function () {
        return {
            isLoading: false,
            isFinishRequest: false,
            orderId: -1,
            packingId: -1,
            list: [],
            customerLessor: "",
            storeSupplier: ""
        };
    },
    created: async function () {
        this.orderId = UJ.getParameterByName("oid");
        this.packingId = UJ.getParameterByName("id");
        this.loadData();
        this.customerLessor = await UniversalDataService.GetContractCustomerLessor();
        this.storeSupplier = await UniversalDataService.GetContractStoreSupplier();
    },
    updated: function () {
        var v = this;

        if (v.isFinishRequest) {
            setTimeout(function () {
                v.isLoading = false;
            }, 100);
        } else {
            v.isLoading = true;
        }
    },
    methods: {
        async loadData() {
            var v = this;
            v.isFinishRequest = false;

            if (v.packingId != null && v.packingId > 0) {
                v.list = [];
                var res = await PackingDataService.Get(v.packingId);
                if (res.status == RESPONSE_STATUS.OK) {
                    v.list.push(res.data);
                    v.isFinishRequest = true;

                } else {
                    var data = res.data;
                    showErrMsg(data.message, "", "", function () {
                        if (data.enMessage == enMessage.NotFound) {
                            v.closeAndReloadParent();
                        }
                    });
                }
            } else {
                var res = await OrderDataService.GetPrint(v.orderId);
                if (res.status == RESPONSE_STATUS.OK) {
                    v.list = res.data;
                    v.isFinishRequest = true;

                } else {
                    var data = res.data;
                    showErrMsg(data.message, "", "", function () {
                        if (data.enMessage == enMessage.NotFound) {
                            v.closeAndReloadParent();
                        }
                    });
                }
            }

            if (v.list != null && v.list.length > 0) {
                v.list.forEach(function (packingObj) {
                    packingObj.orderObj.details.list.forEach(function (item) {
                        item.startDate = Decorator.ChangeISODateTimeToTwDate(item.startDate);
                    });
                });
            }
        },
        closeAndReloadParent() {
            CloseFancyBox();
        }
    }
});
