"use strict";
Vue.component('ibiz-calendar', {
    template: "\n        <div><div>\n    ",
    props: ['control'],
    data: function () {
        var data = {
            calendar: null,
            http: IBizHttp.getInstance(),
        };
        return data;
    },
    methods: {
        formateDate: function (date) {
            var d = new Date(date);
            var resDate = d.getFullYear() + '-' + this.complement((d.getMonth() + 1)) + '-' + this.complement(d.getDate());
            var resTime = this.complement(d.getHours()) + ':' + this.complement(d.getMinutes()) + ':' + this.complement(d.getSeconds());
            return resDate + " " + resTime;
        },
        complement: function (s) {
            return s < 10 ? '0' + s : s;
        },
        calendarRender: function () {
            var _this_1 = this;
            var _this = this;
            var calendarType = _this.control.getCalendarType();
            var height = _this.control.getHeight();
            var headerRight = [];
            var defaultView = 'dayGridMonth';
            if (Object.is(calendarType, 'MONTH')) {
                headerRight.push('dayGridMonth');
                defaultView = 'dayGridMonth';
            }
            else if (Object.is(calendarType, 'WEEK')) {
                headerRight.push('dayGridWeek');
                defaultView = 'dayGridWeek';
            }
            else if (Object.is(calendarType, 'DAY')) {
                headerRight.push('dayGridDay');
                defaultView = 'dayGridDay';
            }
            else {
                headerRight.push('dayGridMonth');
                headerRight.push('dayGridWeek');
                headerRight.push('dayGridDay');
            }
            var isClicked = false;
            var isDblClicked = false;
            this.calendar = new FullCalendar.Calendar(_this.$el, {
                plugins: ['interaction', 'dayGrid'],
                header: {
                    left: 'prevYear,prev,next,nextYear, today',
                    center: 'title',
                    right: headerRight.join(','),
                },
                eventLimit: true,
                locale: 'zh-cn',
                editable: true,
                defaultView: defaultView,
                height: height,
                businessHours: true,
                events: function (info, successCallback, failureCallback) {
                    var params = {
                        srfctrlid: _this.control.getName(),
                        srfaction: 'fetch',
                    };
                    var url = _this.control.getBackendUrl();
                    _this.http.post(url, params).subscribe(function (data) {
                        if (data.ret == 0) {
                            data.items.forEach(function (item, index) {
                                item.start = item.begintime;
                                item.end = item.endtime;
                                item.title = item.text;
                                item.textColor = item.color;
                                item.color = item.bkcolor;
                            });
                            successCallback(data.items);
                        }
                        else {
                            failureCallback([]);
                        }
                    }, function (error) {
                        failureCallback([]);
                    });
                },
                eventDrop: function (arg) {
                    var event = arg.event;
                    var params = {
                        srfaction: 'update',
                        srfctrlid: _this.control.getName(),
                        srfitemid: event.id,
                        begintime: _this_1.formateDate(event.start),
                        endtime: _this_1.formateDate(event.end),
                    };
                    var url = _this.control.getBackendUrl();
                    _this.http.post(url, params).subscribe(function (data) {
                        if (data.ret == 0) {
                            console.log(data);
                        }
                        else {
                        }
                    }, function (error) {
                    });
                },
                eventClick: function (arg) {
                    if (isClicked) {
                        isDblClicked = true;
                        isClicked = false;
                    }
                    else {
                        isClicked = true;
                    }
                    setTimeout(function () {
                        isClicked = false;
                        isDblClicked = false;
                    }, 200);
                    if (!isDblClicked) {
                        return;
                    }
                    if (arg.event) {
                        _this.control.fire(IBizCalendar.ROWDBLCLICK, [{ srfkey: arg.event.id }]);
                    }
                },
                dateClick: function (arg) {
                    if (isClicked) {
                        isDblClicked = true;
                        isClicked = false;
                    }
                    else {
                        isClicked = true;
                    }
                    setTimeout(function () {
                        isClicked = false;
                        isDblClicked = false;
                    }, 200);
                    if (!isDblClicked) {
                        return;
                    }
                    _this.control.fire(IBizCalendar.ROWDBLCLICKNEWDATA, [{ begintime: arg.dateStr + " 00:00:00" }]);
                },
            });
            this.calendar.render();
        },
    },
    mounted: function () {
        if (!this.calendar) {
            this.calendarRender();
        }
    },
    watch: {
        'control.refreshCount': function (newVal, oldVal) {
            if (this.calendar && newVal > 1) {
                this.calendar.refetchEvents();
            }
        }
    },
});
