import { AuthDataService } from '/js/ApiDataServer/Auth.js?tf=202411121501';
import { ReportDataService } from '/js/ApiDataServer/Report.js?tf=202412301940';
import { UserDataService } from '/js/ApiDataServer/User.js';
import { OrderDataService } from '/js/ApiDataServer/Order.js';
import { RESPONSE_STATUS } from '/js/constants.js';
import { Decorator } from '/js/util/dacorator.js';
import { enMessage } from '/js/constants.js';
import { UJ } from '/js/util/uj.js';

var vMain = new Vue({
    el: "#dvMain",
    data: function () {
        return {
            pageTitle: "直客對帳報表-訂單",
            authDesc: "報表_直客對帳報表",
            isSystemManager: false,
            isLoading: false,
            isFinishRequest: true,
            isBindFancybox: true,
            user: {
                id: -1
            },
            orderNum: "",
            ltStores: [],
            orderObj: {
                detail: {
                    list: [],
                    freightage: 0,
                    subTotal: 0,
                    consumableFee: 0,
                    deposit: 0,
                    depositRefund: 0,
                    suppliesTotal: 0,
                    additionsTotal: 0,
                    total: 0
                },
                other: {
                    list: [],
                    freightage: 0,
                    repair: 0,
                    total: 0
                },
                delivery: {
                    storeId: 0
                },
                takeback: {
                    storeId: 0
                },
                summary: {
                    charge: {
                        list: []
                    },
                    refund: {
                        list: []
                    },
                    receivableAmt: 0,
                    receivedAmt: 0,
                    unreceivedAmt: 0,
                    refundableAmt: 0,
                    refundedAmt: 0,
                    pendingRefundAmt: 0
                }
            }
        };
    },
    created: async function () {
        var v = this;
        v.isSystemManager = await AuthDataService.IsSystemManager();
        v.user.id = UJ.getParameterByName("uid");
        v.orderNum = UJ.getParameterByName("onum");
        v.ltStores = await OrderDataService.GetMarketStoreList(-1);

        var res = await UserDataService.Get(v.user.id);
        if (res.status == RESPONSE_STATUS.OK) {
            v.user = res.data;
            v.user.createDate = Decorator.ChangeISODateTimeToDate(v.user.createDatetime);

            v.resetItem();

        } else {
            var data = res.data;
            showErrMsg(data.message, "", "", function () {
                if (data.enMessage == enMessage.NotFound) {
                    v.backToList();
                }
            });
        }
    },
    updated: function () {
        var v = this;
        if (!v.isBindFancybox) {
            bindFancybox();
            v.isBindFancybox = true;
        }

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

            var res = await ReportDataService.GetUserOrders(v.orderNum);
            if (res.status == RESPONSE_STATUS.OK) {
                v.orderObj = res.data;
                v.orderObj.detail.list.forEach(function (item) {
                    item.startDate = Decorator.ChangeISODateTimeToDate(item.startDate);
                    item.endDate = Decorator.ChangeISODateTimeToDate(item.endDate);
                });
                if (v.orderObj.other != null) {
                    v.orderObj.other.list.forEach(function (item) {
                        item.repaireDate = Decorator.ChangeISODateTimeToDate(item.repaireDate);
                        item.paymentDatetime = Decorator.ChangeISODateTimeToDate(item.paymentDatetime);
                    });
                }
                if (v.orderObj.paymentRecords != null) {
                    v.orderObj.paymentRecords.forEach(function (item) {
                        item.paymentDate= Decorator.ChangeISODateTimeToDate(item.paymentDate);
                    });
                }
                v.isBindFancybox = false;//重新綁定fancybox
                v.isFinishRequest = true;
                v.isLoading = false;

            } else {
                var data = res.data;
                showErrMsg(data.message, "", "", function () {
                    if (data.enMessage == enMessage.NotFound) {
                        v.backToList();
                    } else {
                        location.href = "userlist";
                    }
                });
            }
        },
        trCss(item) {
            if (item.enStatus == 700) {
                if (Decorator.ChangeStringToDate(item.endDate).getTime() < new Date().setHours(0, 0, 0, 0)) {
                    return "bg-expired";
                } else {
                    return "bg-running";
                }
            }
            if (item.enStatus == 800) return "bg-running";
            if (item.enStatus == 900) return "bg-closed";
            if (item.enStatus == 9999) return "bg-closed";
            return "";
        },
        backToList() {
            var v = this;
            location.href = "userorders?uid=" + v.user.id;
        }
    }
});
