import { UniversalDataService } from '/js/ApiDataServer/Universal.js?tf=202411131831';
import { AuthDataService } from '/js/ApiDataServer/Auth.js?tf=202411121501';
import { ReportDataService } from '/js/ApiDataServer/Report.js?tf=202501071831';
import { UserDataService } from '/js/ApiDataServer/User.js';
import { RESPONSE_STATUS } from '/js/constants.js';
import { Decorator } from '/js/util/dacorator.js?tf=202506251701';
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,
            user: {
                id: -1
            },
            query: {
                userId: -1,
                keyword: "",
                status: [],
                currentPage: 1,
                pageSize: 20
            },
            list: [],
            pager: {},
            ltStatus: [],
            summary: {
                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");

        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.ltStatus = await UniversalDataService.GetOrderMainStatus();

            v.query.userId = v.user.id;
            v.getSummary();

            v.getList(1);

        } else {
            var data = res.data;
            showErrMsg(data.message, "", "", function () {
                if (data.enMessage == enMessage.NotFound) {
                    v.backToList();
                }
            });
        }
    },
    updated: function () {
        var v = this;
        initAdminutil();
        if (v.isFinishRequest) {
            setTimeout(function () {
                v.isLoading = false;
            }, 100);
        } else {
            v.isLoading = true;
        }
    },
    methods: {
        clearCriteria() {
            var v = this;
            v.query.keyword = "";
            v.query.status = [];
            v.getList(1);
        },
        async getList(pageNum) {
            var v = this;
            v.isLoading = true;
            v.isFinishRequest = false;
            if (pageNum > 0) {
                v.query.currentPage = pageNum;
            }

            let res = await ReportDataService.GetUserOrderList(v.query);
            if (res.status == RESPONSE_STATUS.OK) {
                var data = res.data;
                v.list = data.list;
                v.pager = data.pager;
                v.list.forEach(function (item) {
                    item.createDate = Decorator.ChangeISODateTimeToDate(item.createDatetime);
                    item.details.forEach(function (detail) {
                        var sd = new Date(detail.startDate);
                        var ed = new Date(detail.endDate);
                        if (sd.getFullYear() != ed.getFullYear()) {
                            detail.startDate = Decorator.ChangeISODateTimeToYYMMdd(detail.startDate);
                            detail.endDate = Decorator.ChangeISODateTimeToYYMMdd(detail.endDate);
                        } else {
                            detail.startDate = Decorator.ChangeISODateTimeToMMdd(detail.startDate);
                            detail.endDate = Decorator.ChangeISODateTimeToMMdd(detail.endDate);
                        }
                    });
                });

                v.isLoading = false;
                v.isFinishRequest = true;
            } else {
                var data = res.data;
                if (res.httpStatus === 401) {
                    showErrMsg(data.message, "", "", function () {
                        location.href = "/";
                    });
                } else {
                    showErrMsg(data.message, "", "", null);
                    v.isLoading = false;
                    v.isFinishRequest = true;
                }
            }
        },
        async getSummary() {
            var v = this;
            v.isLoading = true;
            v.isFinishRequest = false;

            let res = await ReportDataService.GetUserOrderSummary(v.query);
            if (res.status == RESPONSE_STATUS.OK) {
                var data = res.data;
                v.summary = data;

                v.isLoading = false;
                v.isFinishRequest = true;
            } else {
                var data = res.data;
                if (res.httpStatus === 401) {
                    showErrMsg(data.message, "", "", function () {
                        location.href = "/";
                    });
                } else {
                    showErrMsg(data.message, "", "", null);
                    v.isLoading = false;
                    v.isFinishRequest = true;
                }
            }
        },
        goPage(pageNum) {
            this.getList(pageNum);
        },
        keychange(event) {
            if (event.keyCode == 13) {
                this.getList(1);
            }
        },
        changeStatus(type, val, ckd) {
            var v = this;

            var isExists = false;
            var lt = [];
            switch (type) {
                case "status":
                    lt = v.query.status;
                    break;
            }

            lt.forEach(function (value, idx) {
                if (value == val) {
                    isExists = true;
                    if (!ckd) {
                        lt.splice(idx, 1);
                    }
                }
            });
            if (ckd && !isExists) {
                lt.push(val);
            }

            v.getList(1);
        },
        checkedList(type, val) {
            var v = this;
            var lt = [];
            switch (type) {
                case "status":
                    lt = v.query.status;
                    break;
            }

            var res = false;
            lt.forEach(function (value, idx) {
                if (value == val) {
                    res = true;
                }
            });
            return res;
        },
    }
});


$(function () {
    setTimeout(function () {
        openLeftMenu('/report/userlist');
    }, 100);
});