import { UniversalDataService } from '/js/ApiDataServer/Universal.js?tf=20251231';
import { OemDataService } from '/js/ApiDataServer/Oem.js?tf=202411191029';
import { RecycleDataService } from '/js/ApiDataServer/Recycle.js?tf=202411271944';
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: [],
            recycleObj: {
                id: -1,
                orderObj: {
                    details: {
                        list: [],
                        deposit: 0
                    }
                }
            },
            cashOnDelivery: {
                info: "",
                charge: 0
            },
            recycleTitle: ""
        };
    },
    created: async function () {
        this.recycleObj.id = UJ.getParameterByName("id");
        this.ltStores = await OrderDataService.GetMarketStoreList(-1);
        this.ltOemMarkets = await OemDataService.GetOemMarketList();
        this.loadData();
        this.recycleTitle = await UniversalDataService.GetRecycleTitle();
    },
    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;

            var res = await RecycleDataService.Get(v.recycleObj.id);
            if (res.status == RESPONSE_STATUS.OK) {
                v.recycleObj = res.data.recycleObj;
                v.cashOnDelivery = res.data.cashOnDelivery;

                v.recycleObj.scheduleDate = Decorator.ChangeISODateTimeToDate(v.recycleObj.scheduleDate);

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

                if (c > 2) {
                    v.recycleObj.pageCss = "a4";
                } else {
                    v.recycleObj.pageCss = "a5";
                }

                v.isFinishRequest = true;

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