import { AuthDataService } from '/js/ApiDataServer/Auth.js?tf=202411121501';
import { ReportDataService } from '/js/ApiDataServer/Report.js?tf=202412311245';
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,
            isBindFancybox: true,
            query: {
                startDate: "",
                endDate: "",
                propertyNo: ""
            },
            resObj: {
                list: [],
                ltStatus: []
            },
            stockTitle: "",
            statusItem: {
                list: []
            }
        };
    },
    created: async function () {
        var v = this;
        v.isSystemManager = await AuthDataService.IsSystemManager();

        v.clearCriteria();
    },
    updated: function () {
        var v = this;
        if (!v.isBindFancybox) {
            bindFancybox();
            v.isBindFancybox = true;
        }

        initAdminutil();
        if (v.isFinishRequest) {
            setTimeout(function () {
                v.isLoading = false;
            }, 100);
        } else {
            v.isLoading = true;
        }
    },
    components: {
        'date-picker': DatePicker
    },
    methods: {
        clearCriteria() {
            var v = this;

            var sd = new Date();
            v.query.startDate = sd.format("yyyy-MM-01");
            v.changeDate('start');
            v.query.propertyNo = "";

            v.getList();
        },
        changeDate(type) {
            var v = this;
            if (type == "start") {
                var sd = new Date(v.query.startDate);
                var ed = new Date(sd.getFullYear(), sd.getMonth(), 1);
                v.query.endDate = ed.addMonths(1).addDays(-1).format("yyyy-MM-dd");
            }
        },
        async getList() {
            var v = this;
            v.isLoading = true;
            v.isFinishRequest = false;

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

                v.isLoading = false;
                v.isFinishRequest = true;
                v.isBindFancybox = false;//重新綁定fancybox
            } else {
                var data = res.data;
                if (res.httpStatus === 401) {
                    showErrMsg(data.message, "", "", function () {
                        location.href = "/";
                    });
                } else {
                    showErrMsg(data.message, "", "", null);
                    v.isFinishRequest = true;
                }
            }
        },
        keychange(event) {
            if (event.keyCode == 13) {
                this.getList();
            }
        },
        showList(type, item, status) {
            var v = this;
            if (type == "all") {
                v.stockTitle = status.statusString + "明細";
            } else if (type == "week") {
                v.stockTitle = item.startDate + " ~ " + item.endDate;
            } else {
                v.stockTitle = item.dateString;
            }
            v.statusItem = status;
            v.statusItem.list.forEach(function (item) {
                item.createDatetime = Decorator.ChangeISODateTimeToDate(item.createDatetime);
            });

            setTimeout(function () {
                $(".iframe").fancybox({
                    'transitionIn': 'elastic',
                    'transitionOut': 'elastic',
                    type: "iframe",
                    'speedIn': 600,
                    'speedOut': 200,
                    'autoSize': true,
                    'overlayShow': false,
                    iframe: {
                        scrolling: 'auto',
                        css: {
                            width: '95%',
                            height: '85%',
                            padding: "6px"
                        }
                    }
                });
            }, 50);
        },
        closeList() {
            try {
                parent.$.fancybox.close();
            }
            catch (e) {
                top.$.fancybox.close();
            }
        },
        //匯出報表
        async exportData() {
            var v = this;
            v.isLoading = true;
            v.isFinishRequest = false;
            let res = await ReportDataService.ExportMaintenance(v.query);
            if (res.status != RESPONSE_STATUS.OK) {
                alert('匯出失敗');
                v.isFinishRequest = true;
                v.isLoading = false;
                return;
            } else {
                v.isFinishRequest = true;
                v.isLoading = false;
            }
        }
    }
});
