import { UniversalDataService } from '/js/ApiDataServer/Universal.js?tf=202411131831';
import { ReportDataService } from '/js/ApiDataServer/Report.js?tf=202412301706';
import { AuthDataService } from '/js/ApiDataServer/Auth.js?tf=202411121501';
import { RESPONSE_STATUS } from '/js/constants.js';
import { Decorator } from '/js/util/dacorator.js';

var vMain = new Vue({
    el: "#dvMain",
    data: function () {
        return {
            pageTitle: "直客對帳報表",
            authDesc: "報表_直客對帳報表",
            isSystemManager: false,
            isLoading: false,
            isFinishRequest: true,
            StorageKey: "AssistiveRent_Admin_Report_Userlist",
            query: {
                keyword: "",
                status: -1,
                unClosedOnly: 1,
                currentPage: 1,
                pageSize: 20
            },
            list: [],
            pager: {},
            summary: {
                receivableAmt: 0,
                receivedAmt: 0,
                unreceivedAmt: 0,
                refundableAmt: 0,
                refundedAmt: 0,
                pendingRefundAmt: 0
            }
        };
    },
    created: async function () {
        var v = this;
        v.isSystemManager = await AuthDataService.IsSystemManager();

        var s = $("body").getPostDataFromStorage(v.StorageKey);
        if (s.propCount == 0) {
            v.clearCriteria();
        } else {
            v.query.keyword = s.keyword;
            v.query.status = s.status;

            if (s.unclosedonly != null && s.unclosedonly == "on") {
                v.query.unClosedOnly = 1;
            } else {
                v.query.unClosedOnly = -1;
            }

            v.getList(s.currentPage);
        }
    },
    updated: function () {
        var v = this;
        initAdminutil();

        if (v.isFinishRequest) {
            setTimeout(function () {
            $("body").setPostDataToStorage(v.StorageKey);
                v.isLoading = false;
            }, 100);
        } else {
            v.isLoading = true;
        }
    },
    methods: {
        clearCriteria() {
            var v = this;
            $("body").resetStorage(v.StorageKey);

            v.query.keyword = "";
            v.query.status = -1;
            v.query.unClosedOnly = 1;

            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.GetUserList(this.query);
            if (res.status == RESPONSE_STATUS.OK) {
                var data = res.data;
                v.list = data.list;
                v.pager = data.pager;
                v.summary = data.summary;

                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;
                }
            }
        },
        keychange(event) {
            if (event.keyCode == 13) {
                this.getList(1);
            }
        },
        goPage(pageNum) {
            this.getList(pageNum);
        }
    }
});
