import { AuthDataService } from '/js/ApiDataServer/Auth.js?tf=202411121501';
import { PackingSaleOrderDataService } from '/js/ApiDataServer/PackingSaleOrder.js';
import { RESPONSE_STATUS } from '/js/constants.js';
import { StorageKey } from '/js/constants.js?tf=202412292016';
import { UJ } from '/js/util/uj.js';

var vMain = new Vue({
    el: "#dvMain",
    data: function () {
        return {
            pageTitle: "佳醫銷售出貨單-待出貨地址清單",
            authDesc: "訂單_出貨管理",
            isSystemManager: false,
            isLoading: false,
            isFinishRequest: false,
            showCloseBTN: false,
            memberObj: {},
            orderId: -1,
            query: {
                keyword: "",
                currentPage: 1,
                pageSize: 5
            },
            list: [],
            pager: {},
            checkedIdx: -1,
            selectedItem: {}
        };
    },
    created: async function () {
        var v = this;
        v.isSystemManager = await AuthDataService.IsSystemManager();
        v.orderId = UJ.getParameterByName("oid");

        v.clearCriteria();
    },
    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.getList(1);
        },
        async getList(pageNum) {
            var v = this;
            v.isFinishRequest = false;
            v.isLoading = true;
            if (pageNum > 0) {
                v.query.currentPage = pageNum;
            }

            let res = await PackingSaleOrderDataService.GetOrderAddress(v.query);
            if (res.status == RESPONSE_STATUS.OK) {
                var data = res.data;
                v.list = data.list;
                v.pager = data.pager;

                v.pager = data.pager;
                v.isFinishRequest = true;
            } else {
                var data = res.data;
                if (res.httpStatus === 401) {
                    showErrMsg(data.message, "", "", function () {
                        v.closeAndReloadParent();
                    });
                } else {
                    showErrMsg(data.message, "", "", null);
                    v.isFinishRequest = true;
                }
            }
        },
        keychange(event) {
            if (event.keyCode == 13) {
                this.getList(1);
            }
        },
        goPage(pageNum) {
            this.getList(pageNum);
        },
        selected(item, idx) {
            var v = this;
            v.checkedIdx = idx;
            v.selectedItem = item;
        },
        confirmAddress() {
            var v = this;
            UJ.SaveStorage(StorageKey.packingCreateOrderAddressInfo + v.orderId + "_", v.selectedItem);
            CloseAndReloadParent();
        },
        closeAndReloadParent() {
            UJ.ResetStorage(StorageKey.packingCreateOrderAddressInfo);
            CloseAndReloadParent();
        }
    }
});
