import { AuthDataService } from '/js/ApiDataServer/Auth.js?tf=202411121501';
import { OemDataService } from '/js/ApiDataServer/Oem.js?tf=202411191029';
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 {
            pageTitle: "[代工清消收送] 庫存異動記錄",
            authDesc: "代工_輔具管理、代工_庫存管理、代工_輔具清消維修記錄、訂單_回收清消維修報廢、訂單_出貨管理",
            isSystemManager: false,
            isLoading: false,
            isFinishRequest: false,
            showCloseBTN: false,
            mainObj: {
                id: -1
            },
            stock: {
                id: -1,
                productId: -1,
                propertyNo: "",
                status: -1,
                statusString: ""
            },
            query: {
                stockId: -1,
                currentPage: 1,
                pageSize: 20
            },
            list: [],
            pager: {}
        };
    },
    created: async function () {
        var v = this;
        v.isSystemManager = await AuthDataService.IsSystemManager();
        v.stock.id = UJ.getParameterByName("id");
        v.stock.productId = UJ.getParameterByName("mid");
        v.query.stockId = v.stock.id;

        var res = await OemDataService.GetProduct(v.stock.productId);
        if (res.status == RESPONSE_STATUS.OK) {
            v.mainObj = res.data;
            v.resetItem();
        } else {
            var data = res.data;
            showErrMsg(data.message, "", "", function () {
                if (data.enMessage == enMessage.NotFound) {
                    v.closeAndReloadParent();
                }
            });
        }
    },
    updated: function () {
        var v = this;
        initAdminutil();

        if (v.isFinishRequest) {
            setTimeout(function () {
                v.isLoading = false;
            }, 100);
        } else {
            v.isLoading = true;
        }
    },
    methods: {
        async resetItem() {
            var v = this;
            if (v.stock.id != null && v.stock.id > 0) {
                v.isFinishRequest = false;

                var res = await OemDataService.GetStockItrm(v.stock.id);
                if (res.status == RESPONSE_STATUS.OK) {
                    v.stock = res.data;
                    v.stock.createDate = Decorator.ChangeServerDateTimeToISODateTime(v.stock.createDatetime);
                    v.stock.modifyDate = Decorator.ChangeServerDateTimeToISODateTime(v.stock.modifyDatetime);
                    v.getList(1);
                    v.isFinishRequest = true;
                    v.isLoading = false;
                } else {
                    var data = res.data;
                    showErrMsg(data.message, "", "", function () {
                        v.closeAndReloadParent();
                    });
                }
            } else {
                v.isFinishRequest = true;
                v.isLoading = false;
            }
        },
        async getList(pageNum) {
            var v = this;
            v.isLoading = true;
            if (pageNum > 0) {
                v.query.currentPage = pageNum;
            }
            let res = await OemDataService.GetStockHistory(this.query);
            if (res.status == RESPONSE_STATUS.OK) {
                var data = res.data;
                v.list = data.list;
                v.list.forEach(function (item) {
                    item.createDate = Decorator.ChangeServerDateTimeToISODateTime(item.createDatetime);
                });
                v.pager = data.pager;
                v.isLoading = false;
            } else {
                var data = res.data;
                if (res.httpStatus === 401) {
                    showErrMsg(data.message, "", "", function () {
                        v.closeAndReloadParent();
                    });
                } else {
                    showErrMsg(data.message, "", "", null);
                    v.isFinishRequest = true;
                }
            }
        },
        closeAndReloadParent() {
            CloseAndReloadParent();
        }
    }
});

