import { StoreDataService } from '/js/ApiDataServer/Store.js';
import { AuthDataService } from '/js/ApiDataServer/Auth.js?tf=202411121501';
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,
            store: {
                id: -1
            },
            query: {
                storeId: -1,
                keyword: "", 
                propertyNo: "",
                currentPage: 1,
                pageSize: 20
            },
            list: [],
            pager: {}
        };
    },
    created: async function () {
        var v = this;
        v.isSystemManager = await AuthDataService.IsSystemManager();
        v.store.id = UJ.getParameterByName("sid");

        var res = await StoreDataService.Get(v.store.id);
        if (res.status == RESPONSE_STATUS.OK) {
            v.store = res.data;

            v.query.storeId = v.store.id;
         
            v.getList(1);

            v.isFinishRequest = true;

        } else {
            var data = res.data;
            showErrMsg(data.message, "", "", function () {
                if (data.enMessage == enMessage.NotFound) {
                    UJ.BackToList();
                }
            });
        }
    },
    updated: function () {
        var v = this;
        initAdminutil();
        setTimeout(function () {
            v.isLoading = false;
        }, 100);
    },
    methods: {
        clearCriteria() {
            var v = this;
            v.query.keyword = "";
            v.query.propertyNo = "";
            v.getList(1);
        },
        async getList(pageNum) {
            var v = this;
            v.isLoading = true;
            if (pageNum > 0) {
                v.query.currentPage = pageNum;
            }
            let res = await StoreDataService.GetOrderList(v.query);
            v.list = res.list;
            v.list.forEach(function (item) {
                item.createDate = Decorator.ChangeISODateTimeToDate(item.createDatetime);
                item.details.list.forEach(function (detail) {
                    var sd = new Date(detail.startDate);
                    var ed = new Date(detail.finalEndDate);
                    if (sd.getFullYear() != ed.getFullYear()) {
                        detail.startDateShort = Decorator.ChangeISODateTimeToYYMMdd(detail.startDate);
                        detail.endDateShort = Decorator.ChangeISODateTimeToYYMMdd(detail.finalEndDate);
                    } else {
                        detail.startDateShort = Decorator.ChangeISODateTimeToMMdd(detail.startDate);
                        detail.endDateShort = Decorator.ChangeISODateTimeToMMdd(detail.finalEndDate);
                    }
                });
            });
            v.pager = res.pager;
            v.isLoading = false;
        },
        goPage(pageNum) {
            this.getList(pageNum);
        },
        viewUrl(item) {
            return "/order/orderlist?onum=" + item.orderNum;
        },
        keychange(event) {
            if (event.keyCode == 13) {
                this.getList(1);
            }
        },
        detailStatusCss(obj) {
            if (obj.enStatus == 700 || obj.enStatus == 800) {
                if (Decorator.ChangeStringToDate(obj.finalEndDate).getTime() < new Date().setHours(0, 0, 0, 0)) {
                    return "bg-expired";
                } else {
                    return "bg-running";
                }
            }
            if (obj.enStatus == 900) return "bg-closed";
            if (obj.enStatus == 9999) return "bg-closed";
            return "";
        },
        orderCss(order) {
            if (order.enStatus == 500) return "order-pause";
            if (order.enStatus <= 600) return "order-pending";
            return "";
        }
    }
});


$(function () {
    setTimeout(function () {
        openLeftMenu('/store/list');
    }, 100);
});