import { UniversalDataService } from '/js/ApiDataServer/Universal.js?tf=20251231';
import { OemDataService } from '/js/ApiDataServer/Oem.js?tf=202411191029';
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,
            ltStores: [],
            ltOemMarkets: [],
            orderId: -1,
            packingId: -1,
            list: [],
            packingTitle: ""
        };
    },
    created: async function () {
        this.ltStores = await OrderDataService.GetMarketStoreList(-1);
        this.ltOemMarkets = await OemDataService.GetOemMarketList();
        this.orderId = UJ.getParameterByName("oid");
        this.packingId = UJ.getParameterByName("id");
        this.loadData();
        this.packingTitle = await UniversalDataService.GetPackingTitle();
    },
    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.scheduleDate = Decorator.ChangeISODateTimeToDate(packingObj.scheduleDate);
                    if (packingObj.shippingDate != null) {
                        packingObj.shippingDate = Decorator.ChangeISODateTimeToDate(packingObj.shippingDate);
                    }

                    var c = 0;
                    if (packingObj.orderObj != null) {
                        c += packingObj.orderObj.details.list.length;
                        packingObj.orderObj.details.list.forEach(function (item) {
                            item.startDate = Decorator.ChangeISODateTimeToDate(item.startDate);
                            item.endDate = Decorator.ChangeISODateTimeToDate(item.endDate);
                        });
                    }
                    if (packingObj.oemOrderObj != null) {
                        packingObj.oemOrderObj.oemMarketName = "";
                        v.ltOemMarkets.forEach(function (item) {
                            if (packingObj.oemOrderObj.oemMarketId == item.id) {
                                packingObj.oemOrderObj.oemMarketName = item.name.substring(0, 2);
                            }
                        });
                    }

                    if (c > 2) {
                        packingObj.pageCss = "a4";
                    } else {
                        packingObj.pageCss = "a5";
                    }
                });
            }
        },
        closeAndReloadParent() {
            CloseFancyBox();
        }
    }
});