import { __decorate } from "tslib";
import { Vue, Component } from 'vue-property-decorator';
import { Subject } from 'rxjs';
import { Util, ViewTool, ModelTool, LogUtil, throttle } from '@ibizstudio/runtime';
import { MDControlBase } from './md-control-base';
import { GlobalService } from '@ibizstudio/runtime';
import { AppCalendarService } from '../ctrl-service';
import { ContextMenu } from '../components/common/context-menu/context-menu';
import { AppDefaultContextMenu } from '../components/control/app-default-contextmenu/app-default-contextmenu';
import { AppViewLogicService } from '../app-service';
import moment from 'moment';
import { VueLifeCycleProcessing, Watch } from '../decorators';
/**
 * 应用日历部件基类
 *
 * @export
 * @class CalendarControlBase
 * @extends {MDControlBase}
 */
let CalendarControlBase = class CalendarControlBase extends MDControlBase {
    constructor() {
        super(...arguments);
        /**
         * 日历部件样式名
         *
         * @public
         * @type {any[]}
         * @memberof CalendarControlBase
         */
        this.calendarClass = 'calendar';
        /**
         * 日历默认打开样式
         *
         * @type {string}
         * @memberof CalendarControlBase
         */
        this.defaultView = 'dayGridMonth';
        /**
         * this引用
         *
         * @type {any}
         * @memberof CalendarControlBase
         */
        this.thisRef = this;
        /**
         * 组件key值（用于刷新日历）
         *
         * @type {any}
         * @memberof CalendarControlBase
         */
        this.UUKey = Util.createUUID();
        /**
         * 设置头部显示
         *
         * @public
         * @type {}
         * @memberof CalendarControlBase
         */
        this.header = {
            left: 'prev,next today gotoDate',
            center: 'title',
            right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek',
        };
        /**
         * 按钮文本集合
         *
         * @public
         * @type {}
         * @memberof CalendarControlBase
         */
        this.buttonText = {
            today: this.$t('app.calendar.today'),
            month: this.$t('app.calendar.month'),
            week: this.$t('app.calendar.week'),
            day: this.$t('app.calendar.day'),
            list: this.$t('app.calendar.list'),
        };
        /**
         * 自定义按钮集合
         *
         * @public
         * @type {}
         * @memberof CalendarControlBase
         */
        this.customButtons = {
            gotoDate: {
                text: this.$t('app.calendar.gotodate'),
                click: this.openDateSelect,
            },
        };
        /**
         * 模态显示控制变量
         *
         * @public
         * @type boolean
         * @memberof CalendarControlBase
         */
        this.modalVisible = false;
        /**
         * 跳转日期
         *
         * @public
         * @type Date
         * @memberof CalendarControlBase
         */
        this.selectedGotoDate = new Date();
        /**
         * 有效日期范围
         *
         * @public
         * @type {}
         * @memberof CalendarControlBase
         */
        this.validRange = {
            start: '0000-01-01',
            end: '9999-12-31',
        };
        /**
         * 默认加载日期
         *
         * @public
         * @type {}
         * @memberof CalendarControlBase
         */
        this.defaultDate = Util.dateFormat(new Date());
        /**
         * 快速工具栏项
         *
         * @type {Array<any>}
         * @memberof CalendarControlBase
         */
        this.quickToolbarItems = [];
        /**
         * 快速工具栏模型
         *
         * @type {*}
         * @memberof CalendarControlBase
         */
        this.quickToolbarModels = {};
        /**
         * 日程事件集合
         *
         * @public
         * @type {any[]}
         * @memberof CalendarControlBase
         */
        this.events = [];
        /**
         * 日历项上下文菜单集合
         *
         * @type {string[]}
         * @memberof CalendarControlBase
         */
        this.actionModel = {
        //TODO待补充
        };
        /**
         * 日历样式类型
         *
         * @public
         * @type {string}
         * @memberof CalendarControlBase
         */
        this.calendarType = '';
        /**
         * 图例显示控制
         *
         * @public
         * @type {any}
         * @memberof CalendarControlBase
         */
        this.isShowlegend = {};
        /**
         * 是否已经选中第一条数据
         *
         * @type {boolean}
         * @memberof CalendarControlBase
         */
        this.isSelectFirst = false;
        /**
         * 事件id
         *
         * @type {string}
         * @memberof CalendarControlBase
         */
        this.eventid = '';
        /**
         * 事件类型map
         *
         * @type {any}
         * @memberof CalendarControlBase
         */
        this.eventKey = new Map();
        /**
         * 查询参数缓存
         *
         * @public
         * @type {any}
         * @memberof CalendarControlBase
         */
        this.searchArgCache = {};
    }
    /**
     * 监听部件动态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof CalendarControlBase
     */
    onDynamicPropsChange(newVal, oldVal) {
        super.onDynamicPropsChange(newVal, oldVal);
    }
    /**
     * 监听部件参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof CalendarControlBase
     */
    onStaticPropsChange(newVal, oldVal) {
        this.isSelectFirstDefault = newVal.isSelectFirstDefault ? true : false;
        super.onStaticPropsChange(newVal, oldVal);
    }
    /**
     * 部件模型数据初始化
     *
     * @memberof CalendarControlBase
     */
    async ctrlModelInit(args) {
        var _a, _b, _c;
        await super.ctrlModelInit();
        if (!(this.Environment && this.Environment.isPreviewMode)) {
            this.service = new AppCalendarService(this.controlInstance, this.context);
            this.appEntityService = await new GlobalService().getService(this.appDeCodeName, this.context);
        }
        this.initShowLegend();
        this.initActionModel();
        this.initQuickToolbar();
        this.initEventKey();
        this.calendarType = (_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.calendarStyle;
        this.ctrlParams = (_c = (_b = this.controlInstance) === null || _b === void 0 ? void 0 : _b.getPSControlParam()) === null || _c === void 0 ? void 0 : _c.ctrlParams;
        if (Object.is(this.calendarType, 'WEEK')) {
            this.defaultView = 'timeGridWeek';
        }
        else if (Object.is(this.calendarType, 'DAY')) {
            this.defaultView = 'timeGridDay';
        }
    }
    /**
     * 部件模型数据加载
     *
     * @memberof CalendarControlBase
     */
    async ctrlModelLoad() {
        var _a, _b, _c, _d, _e, _f;
        const calendarItems = this.controlInstance.getPSSysCalendarItems() || [];
        if (calendarItems.length > 0) {
            for (const item of calendarItems) {
                await ((_c = (_b = (_a = item.getPSAppDataEntity) === null || _a === void 0 ? void 0 : _a.call(item)) === null || _b === void 0 ? void 0 : _b.fill) === null || _c === void 0 ? void 0 : _c.call(_b));
                await ((_f = (_e = (_d = item.getNavPSAppView) === null || _d === void 0 ? void 0 : _d.call(item)) === null || _e === void 0 ? void 0 : _e.fill) === null || _f === void 0 ? void 0 : _f.call(_e));
            }
        }
    }
    /**
     * 日历视图部件初始化
     *
     * @memberof CalendarControlBase
     */
    ctrlInit() {
        super.ctrlInit();
        if (Object.is(this.calendarType, 'TIMELINE')) {
            this.searchEvents();
        }
        else {
            this.setButtonText();
        }
    }
    viewStateAction(tag, action, data) {
        if (!Object.is(tag, this.name)) {
            return;
        }
        super.viewStateAction(tag, action, data);
    }
    /**
     * 部件挂载
     *
     * @memberof CalendarControlBase
     */
    ctrlMounted() {
        var _a;
        super.ctrlMounted();
        let appCalendar = this.$refs[(_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.codeName];
        if (appCalendar) {
            let api = appCalendar.getApi();
            api.updateSize();
        }
    }
    /**
     * 初始化日历项上下文菜单集合
     *
     * @memberof CalendarControlBase
     */
    initActionModel() {
        const calendarItems = this.controlInstance.getPSSysCalendarItems() || [];
        let tempModel = {};
        if (calendarItems.length > 0) {
            calendarItems.forEach((item) => {
                var _a;
                const contextMenuItems = ((_a = item.getPSDEContextMenu()) === null || _a === void 0 ? void 0 : _a.getPSDEToolbarItems()) || [];
                if (contextMenuItems.length > 0) {
                    contextMenuItems.forEach((toolbarItem) => {
                        this.initActionModelItem(toolbarItem, item, tempModel);
                    });
                }
            });
        }
        this.actionModel = {};
        Object.assign(this.actionModel, tempModel);
    }
    /**
     * 初始化上下菜单项
     *
     * @param toolbarItem
     * @param item
     * @param tempModel
     * @memberof CalendarControlBase
     */
    initActionModelItem(toolbarItem, item, tempModel) {
        let tempItem = {
            name: toolbarItem.name,
            nodeOwner: item.itemType,
        };
        if (toolbarItem.itemType == 'DEUIACTION') {
            const uiAction = toolbarItem.getPSUIAction();
            if (uiAction) {
                Object.assign(tempItem, {
                    type: item.itemType,
                    tag: uiAction.id,
                    visabled: true,
                    disabled: false,
                    actiontarget: uiAction.actionTarget,
                    noprivdisplaymode: uiAction.noPrivDisplayMode,
                    dataaccaction: uiAction.dataAccessAction,
                });
            }
        }
        tempModel[`${item.itemType}_${toolbarItem.name}`] = tempItem;
        //TODO YY 上下文菜单子项
        // if(toolbarItem.getPSDEToolbarItems?.length > 0){
        //     for(let toolBarChild of toolbarItem.getPSDEToolbarItems){
        //         this.initActionModelItem(toolBarChild,item,tempModel)
        //     }
        // }
    }
    /**
     * 初始化快速工具栏
     *
     * @memberof CalendarControlBase
     */
    initQuickToolbar() {
        var _a;
        const quickToolbaItems = ((_a = ModelTool.findPSControlByType('QUICKTOOLBAR', this.controlInstance.getPSControls() || [])) === null || _a === void 0 ? void 0 : _a.getPSDEToolbarItems()) || [];
        if (quickToolbaItems.length > 0) {
            let items = quickToolbaItems.filter((item) => {
                return item.itemType == 'DEUIACTION';
            });
            this.quickToolbarItems = [...items];
        }
        this.initQuickToolbarItemModel();
    }
    /**
     * 初始化快速工具栏模型
     *
     * @memberof CalendarControlBase
     */
    initQuickToolbarItemModel() {
        const items = this.quickToolbarItems;
        let models = {};
        if (items.length > 0) {
            items.forEach((_item) => {
                var _a, _b;
                const item = _item;
                const uiAction = item.getPSUIAction();
                let model = {};
                Object.assign(model, {
                    name: (_a = item.name) === null || _a === void 0 ? void 0 : _a.toLowerCase(),
                    actiontarget: 'NONE',
                    disabled: false,
                    visabled: true,
                    type: item.itemType,
                    noprivdisplaymode: uiAction === null || uiAction === void 0 ? void 0 : uiAction.noPrivDisplayMode,
                    dataaccaction: uiAction === null || uiAction === void 0 ? void 0 : uiAction.dataAccessAction,
                    uiaction: {
                        tag: (uiAction === null || uiAction === void 0 ? void 0 : uiAction.uIActionTag) ? uiAction.uIActionTag : uiAction.id ? uiAction.id : '',
                        target: uiAction === null || uiAction === void 0 ? void 0 : uiAction.actionTarget,
                    },
                });
                Object.assign(models, { [`${(_b = item.name) === null || _b === void 0 ? void 0 : _b.toLowerCase()}`]: model });
            });
        }
        this.quickToolbarModels = {};
        Object.assign(this.quickToolbarModels, models);
    }
    /**
     * 初始化事件map对象
     *
     * @memberof CalendarControlBase
     */
    initEventKey() {
        const calendarItems = this.controlInstance.getPSSysCalendarItems() || [];
        if (calendarItems.length > 0) {
            calendarItems.forEach(calendarItem => {
                var _a, _b;
                let eventKey = ((_b = (_a = ModelTool.getAppEntityKeyField(calendarItem === null || calendarItem === void 0 ? void 0 : calendarItem.getPSAppDataEntity())) === null || _a === void 0 ? void 0 : _a.codeName) === null || _b === void 0 ? void 0 : _b.toLowerCase()) || '';
                this.eventKey.set(calendarItem.itemType, eventKey);
            });
        }
    }
    /**
     * 初始化图例显示控制
     *
     * @memberof CalendarControlBase
     */
    initShowLegend() {
        if (Object.keys(this.isShowlegend).length > 0) {
            return;
        }
        const calendarItems = this.controlInstance.getPSSysCalendarItems() || [];
        if (calendarItems.length > 0) {
            calendarItems.forEach((item) => {
                this.isShowlegend[item.itemType] = true;
            });
        }
    }
    /**
     * 图例点击事件
     *
     * @param {string} itemType 日历项类型
     * @return {*}
     * @memberof CalendarControlBase
     */
    legendTrigger(itemType) {
        const eventDisabled = this.$el.getElementsByClassName('event-disabled').length;
        if (Object.keys(this.isShowlegend).length != 1 && eventDisabled == Object.keys(this.isShowlegend).length - 1 && this.isShowlegend[itemType]) {
            return;
        }
        this.isShowlegend[itemType] = !this.isShowlegend[itemType];
        this.refresh();
    }
    /**
     * 面板数据变化处理事件
     * @param {any} item 当前数据
     * @param {any} $event 面板事件数据
     *
     * @memberof CalendarControlBase
     */
    onPanelDataChange(item, $event) {
        Object.assign(item, $event, { rowDataState: 'update' });
    }
    /**
     * 搜索获取日程事件
     *
     * @param {*} [fetchInfo] 日期信息
     * @param {*} [successCallback] 成功回调
     * @return {*}
     * @memberof CalendarControlBase
     */
    searchEvents(fetchInfo, successCallback) {
        var _a;
        if (this.Environment && this.Environment.isPreviewMode) {
            this.events = [];
            return;
        }
        // 处理请求参数
        let start = fetchInfo && fetchInfo.start ? Util.dateFormat(fetchInfo.start) : null;
        let end = fetchInfo && fetchInfo.end ? Util.dateFormat(fetchInfo.end) : null;
        let arg = { query_start: start, query_end: end };
        if (fetchInfo && fetchInfo.query) {
            Object.assign(arg, { query: fetchInfo.query });
        }
        const parentdata = {};
        this.$emit('ctrl-event', { controlname: (_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.name, action: 'beforeload', data: parentdata });
        Object.assign(arg, parentdata);
        let tempViewParams = parentdata.viewparams ? parentdata.viewparams : {};
        Object.assign(tempViewParams, Util.deepCopy(this.viewparams ? this.viewparams : ''));
        Object.assign(arg, { viewparams: tempViewParams });
        // 处理events数据
        let _this = this;
        let handleEvents = () => {
            if (_this.isSelectFirstDefault) {
                _this.calendarClass = 'calendar select-first-calendar';
                _this.isSelectFirst = true;
            }
            let filterEvents = this.events.filter((event) => {
                return _this.isShowlegend[event.itemType];
            });
            if (successCallback) {
                successCallback(filterEvents);
            }
        };
        if (JSON.stringify(arg) === JSON.stringify(this.searchArgCache)) {
            handleEvents();
            return;
        }
        else {
            this.searchArgCache = arg;
        }
        let tempContext = Util.deepCopy(this.context);
        const post = this.service.search(this.loadAction, tempContext, arg, this.showBusyIndicator);
        post.then((response) => {
            if (!response || response.status !== 200) {
                this.$throw(response, 'searchEvents');
                return;
            }
            // 默认选中第一项
            this.events = response.data;
            this.ctrlEvent({
                controlname: this.name,
                action: 'load',
                data: this.events,
            });
            handleEvents();
            this.scheduleSort();
        }, (response) => {
            this.$throw(response, 'searchEvents');
        });
    }
    /**
     * 日期点击事件
     *
     * @param {*} $event 日期信息
     * @memberof CalendarControlBase
     */
    onDateClick($event) {
        let date = $event.date;
        let datestr = $event.dateStr;
    }
    /**
     * 时间轴点击事件
     *
     * @param {*} item 项数据
     * @memberof CalendarControlBase
     */
    onTimeLineClick(item) {
        this.events.forEach((event) => {
            event.srfchecked = 0;
        });
        item.srfchecked = Number(!item.srfchecked);
        this.onEventClick(item, true);
        this.$forceUpdate();
    }
    /**
     * 处理日历日程选中样式
     *
     * @param {*} item 项数据
     * @memberof CalendarControlBase
     */
    handleEventSelectStyle($event) {
        var _a, _b;
        const calendarApi = (_b = this.$refs[(_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.codeName]) === null || _b === void 0 ? void 0 : _b.getApi();
        if (!calendarApi) {
            return;
        }
        const findApis = (id) => {
            if (!id) {
                return [];
            }
            return calendarApi.getEvents().filter((event) => {
                return event.extendedProps.curdata[this.getEventKey(event.extendedProps)] === id;
            });
        };
        const eventId = $event.event.extendedProps.curdata[this.getEventKey($event.event.extendedProps)];
        const eventApis = findApis(eventId);
        eventApis.forEach((api) => {
            const classNames = [...api.classNames];
            if (classNames.findIndex((className) => {
                return className == 'selected-event';
            }) === -1) {
                classNames.push('selected-event');
            }
            api.setProp('classNames', classNames);
        });
        if ((this.isSelectFirstDefault && this.eventid != eventId) || !this.isSelectFirstDefault) {
            const _eventApis = findApis(this.eventid);
            _eventApis.forEach((api) => {
                const _temp = [...api.classNames];
                const index = _temp.findIndex((className) => {
                    return className == 'selected-event';
                });
                if (index !== -1) {
                    _temp.splice(index, 1);
                    api.setProp('classNames', _temp);
                }
            });
        }
        this.eventid = this.eventid && this.eventid == eventId && !this.isSelectFirstDefault ? '' : eventId;
    }
    /**
     * 日程点击事件
     *
     * @param {*} $event calendar事件对象或event数据
     * @param {*} isOriginData true：$event是原始event数据，false：是组件
     * @param {*} $event timeline事件对象
     * @memberof CalendarControlBase
     */
    async onEventClick($event, isOriginData = false, $event2) {
        var _a;
        // 处理event数据
        let event = {};
        if (isOriginData) {
            event = Util.deepCopy($event);
        }
        else {
            event = Object.assign({ title: $event.event.title, start: $event.event.start, end: $event.event.end }, $event.event.extendedProps);
        }
        // 点击选中样式
        let JSelement = null;
        if (!isOriginData && $event.el) {
            JSelement = $event.el;
        }
        else if (isOriginData && $event2 && $event2.currentTarget) {
            JSelement = $event2.currentTarget;
        }
        if (JSelement) {
            this.calendarClass = 'calendar';
        }
        const eventId = event.curdata[this.getEventKey(event)];
        let tempItem = this.events.find((_item) => {
            return _item.curdata[this.getEventKey(_item)] == eventId;
        });
        if (this.eventid && this.eventid == eventId) {
            if (tempItem)
                tempItem.curdata.srfchecked = 0;
        }
        else {
            if (tempItem)
                tempItem.curdata.srfchecked = 1;
        }
        this.selections = this.eventid && this.eventid == eventId ? [] : [event];
        this.handleEventSelectStyle($event);
        // 处理上下文数据
        let _this = this;
        let view;
        let _context = Object.assign({}, this.context);
        let _viewparams = Object.assign({ start: event.start, end: event.end }, this.viewparams);
        const item = (this.controlInstance.getPSSysCalendarItems() || []).find((_item) => {
            return _item.itemType == event.itemType;
        });
        if (item) {
            const codeName = ((_a = item.getPSAppDataEntity()) === null || _a === void 0 ? void 0 : _a.codeName).toLowerCase();
            if (codeName) {
                _context[codeName] = event[codeName];
            }
            view = this.getEditView(item);
        }
        await (view === null || view === void 0 ? void 0 : view.fill());
        // 导航栏中不需要打开视图，只要抛出选中数据
        this.$emit('ctrl-event', { controlname: this.controlInstance.name, action: 'selectionchange', data: this.selections });
        if (this.isSelectFirstDefault) {
            return;
        }
        // 根据打开模式打开视图
        if (!view) {
            return;
        }
        else if (Object.is(view.openMode, 'INDEXVIEWTAB') || Util.isEmpty(view.openMode)) {
            const deResParameters = [];
            const parameters = [];
            await this.processingParameter(this.context, view, deResParameters, parameters);
            const routePath = ViewTool.buildUpRoutePath(_this.$route, this.context, deResParameters, parameters, [Util.deepCopy(_context)], _viewparams);
            _this.$router.push(routePath);
        }
        else {
            let container = new Subject();
            if (Object.is(view.openMode, 'POPOVER')) {
                container = _this.$apppopover.openPop(isOriginData ? $event2 : $event.jsEvent, view, Util.deepCopy(_context), _viewparams);
            }
            else if (Object.is(view.openMode, 'POPUPMODAL')) {
                container = _this.$appmodal.openModal(view, Util.deepCopy(_context), _viewparams);
            }
            else if (view.openMode.startsWith('DRAWER')) {
                container = _this.$appdrawer.openDrawer(view, Util.getViewProps(_context, _viewparams));
            }
            container.subscribe((result) => {
                container.complete();
                if (!result || !Object.is(result.ret, 'OK')) {
                    return;
                }
                // 刷新日历
                _this.refresh();
            });
        }
    }
    /**
     * 整合参数
     *
     * @memberof AppFuncService
     */
    async processingParameter(context, appView, deResParameters, parameters) {
        let params = [];
        if (appView.getPSAppDataEntity()) {
            let result = appView.getPSAppDataEntity();
            await result.fill();
            if (!result) {
                console.error('未找到应用实体');
                return;
            }
            if ((appView.openMode && (appView.openMode == 'INDEXVIEWTAB' || appView.openMode == 'POPUPAPP' || appView.openMode == '')) || !appView.openMode) {
                params = [
                    {
                        pathName: Util.srfpluralize(result.codeName).toLowerCase(),
                        parameterName: result.codeName.toLowerCase(),
                    },
                    { pathName: 'views', parameterName: appView.getPSDEViewCodeName().toLowerCase() },
                ];
            }
            else {
                params = [
                    {
                        pathName: Util.srfpluralize(result.codeName).toLowerCase(),
                        parameterName: result.codeName.toLowerCase(),
                    },
                ];
            }
        }
        else {
            params = [{ pathName: 'views', parameterName: appView.name.toLowerCase() }];
        }
        Object.assign(parameters, params);
    }
    /**
     * 日历刷新
     *
     * @param {*} [args] 额外参数
     * @memberof CalendarControlBase
     */
    refresh(args) {
        var _a;
        if (Object.is(this.calendarType, 'TIMELINE')) {
            this.searchArgCache = {};
            this.searchEvents();
        }
        else if (this.ctrlParams) {
            let calendarTimeLine = this.$refs['appCalendarTimeline'];
            if (calendarTimeLine) {
                calendarTimeLine.refetchEvents();
            }
        }
        else {
            let calendarApi = this.$refs[(_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.codeName].getApi();
            calendarApi.refetchEvents().then(() => {
                this.UUKey = Util.createUUID();
            });
        }
        this.$forceUpdate();
    }
    /**
     * 日程拖动事件
     *
     * @param {*} $event 事件信息
     * @memberof CalendarControlBase
     */
    onEventDrop($event) {
        var _a;
        if (this.isSelectFirstDefault) {
            return;
        }
        let arg = {};
        let _context = Object.assign({}, this.context);
        arg.start = Util.dateFormat($event.event.start);
        arg.end = Util.dateFormat($event.event.end);
        let itemType = $event.event._def.extendedProps.itemType;
        const item = (this.controlInstance.getPSSysCalendarItems() || []).find((_item) => {
            return _item.itemType == itemType;
        });
        if (item) {
            const codeName = ((_a = item.getPSAppDataEntity()) === null || _a === void 0 ? void 0 : _a.codeName).toLowerCase();
            if (codeName) {
                arg[codeName] = $event.event._def.extendedProps[codeName];
                _context[codeName] = $event.event._def.extendedProps[codeName];
            }
        }
        Object.assign(arg, { viewparams: this.viewparams });
        let tempContext = Util.deepCopy(this.context);
        this.onControlRequset('onEventDrop', tempContext, arg);
        const post = this.service.update(itemType, tempContext, arg, this.showBusyIndicator);
        post.then((response) => {
            this.onControlResponse('onEventDrop', response);
            if (!response || response.status !== 200) {
                this.$throw(response, 'onEventDrop');
                return;
            }
        }, (response) => {
            this.onControlResponse('onEventDrop', response);
            this.$throw(response, 'onEventDrop');
        });
    }
    /**
     * 快速工具栏菜单项点击
     *
     * @param {*} tag 菜单项标识
     * @param {*} $event 事件源
     * @memberof CalendarControlBase
     */
    itemClick(tag, $event) {
        AppViewLogicService.getInstance().executeViewLogic(`calendar_quicktoolbar_${tag}_click`, $event, this, {}, this.controlInstance.getPSAppViewLogics() || []);
    }
    /**
     * 时间点击
     *
     * @param {*} $event 当前时间
     * @param {*} jsEvent 原生事件对象
     * @returns
     * @memberof CalendarControlBase
     */
    onDayClick($event, jsEvent) {
        let _this = this;
        let content = this.renderBarMenu;
        const container = _this.$apppopover.openPopover(jsEvent, content, 'left-end', true, 103, _this.quickToolbarItems.length * 34);
    }
    /**
     * 计算节点右键权限
     *
     * @param {*} data 日历项数据
     * @param {*} appEntityName 应用实体名称
     * @returns
     * @memberof CalendarControlBase
     */
    async computeNodeState(data, appEntityName) {
        let service = await new GlobalService().getService(appEntityName, this.context);
        if (this.copyActionModel && Object.keys(this.copyActionModel).length > 0) {
            if (service['Get'] && service['Get'] instanceof Function) {
                let tempContext = Util.deepCopy(this.context);
                tempContext[appEntityName.toLowerCase()] = data[appEntityName.toLowerCase()];
                let targetData = await service.Get(tempContext, {});
                ViewTool.calcTreeActionItemAuthState(targetData.data, this.copyActionModel, this.appUIService);
                return this.copyActionModel;
            }
            else {
                LogUtil.warn(this.$t('app.warn.geterror'));
                return this.copyActionModel;
            }
        }
        return this.copyActionModel;
    }
    /**
     * 计算日历日程样式
     *
     * @param info
     */
    handleEventStyle(info) {
        var _a, _b, _c, _d;
        if (!info) {
            return;
        }
        const calendarApi = (_b = this.$refs[(_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.codeName]) === null || _b === void 0 ? void 0 : _b.getApi();
        if (!calendarApi) {
            return;
        }
        const data = info.event.extendedProps.curdata;
        let calendarItem = (this.controlInstance.getPSSysCalendarItems() || []).find((_item) => {
            return _item.itemType == data.itemType;
        });
        const cssName = (_d = (_c = calendarItem === null || calendarItem === void 0 ? void 0 : calendarItem.getPSSysCss) === null || _c === void 0 ? void 0 : _c.call(calendarItem)) === null || _d === void 0 ? void 0 : _d.cssName;
        const calendarEvents = calendarApi.getEvents().filter((event) => {
            return event.extendedProps.curdata[this.getEventKey(event.extendedProps)] === data[this.getEventKey(info.event.extendedProps)];
        });
        if (cssName && calendarEvents.length > 0) {
            calendarEvents.forEach((event) => {
                const classNames = [...event.classNames];
                if (classNames.findIndex((className) => {
                    return className == cssName;
                }) === -1) {
                    classNames.push(cssName);
                    event.setProp('classNames', classNames);
                }
            });
        }
    }
    /**
     * 事件绘制回调
     *
     * @param {*} info 信息
     * @memberof CalendarControlBase
     */
    eventRender(info) {
        if (this.isSelectFirstDefault && this.isSelectFirst) {
            this.isSelectFirst = false;
            this.onEventClick(info);
        }
        this.handleEventStyle(info);
        let data = Object.assign({ title: info.event.title, start: info.event.start, end: info.event.end }, info.event.extendedProps);
        info.el.addEventListener('contextmenu', (event) => {
            var _a;
            this.copyActionModel = {};
            Object.values(this.actionModel).forEach((item) => {
                if (Object.is(item.nodeOwner, data.itemType)) {
                    this.copyActionModel[item.name] = item;
                }
            });
            if (Object.keys(this.copyActionModel).length === 0) {
                return;
            }
            let dataMapping = (this.controlInstance.getPSSysCalendarItems() || []).find((_item) => {
                return _item.itemType == data.itemType;
            });
            const appDECodeName = (_a = dataMapping.getPSAppDataEntity()) === null || _a === void 0 ? void 0 : _a.codeName;
            if (!appDECodeName) {
                return;
            }
            this.computeNodeState(data, appDECodeName).then((result) => {
                let flag = false;
                if (Object.values(result).length > 0) {
                    flag = Object.values(result).some((item) => {
                        return item.visabled === true;
                    });
                }
                if (flag) {
                    let props = { data: data, renderContent: this.renderContextMenu };
                    let component = ContextMenu;
                    const vm = new Vue({
                        render(h) {
                            return h(component, { props });
                        },
                    }).$mount();
                    document.body.appendChild(vm.$el);
                    const comp = vm.$children[0];
                    comp.showContextMenu(event.clientX, event.clientY);
                }
            });
        });
    }
    /**
     * 绘制右键菜单
     *
     * @param {*} event
     * @returns
     * @memberof CalendarControlBase
     */
    renderContextMenu(event) {
        if (!event || !event.itemType) {
            return null;
        }
        this.selections = [event];
        const calendarItems = this.controlInstance.getPSSysCalendarItems() || [];
        if (calendarItems.length > 0) {
            let item = calendarItems.find((_item) => {
                return _item.itemType == event.itemType;
            });
            const contextMenu = item.getPSDEContextMenu();
            if (contextMenu && contextMenu.controlType == 'CONTEXTMENU') {
                let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(contextMenu, event);
                targetCtrlParam.dynamicProps.contextMenuActionModel = this.copyActionModel;
                return this.$createElement(targetCtrlName, { props: targetCtrlParam, ref: contextMenu.name, on: targetCtrlEvent });
            }
        }
        return null;
    }
    /**
     * 绘制快速工具栏项
     *
     * @returns
     * @memberof CalendarControlBase
     */
    renderBarMenu() {
        return (<div class='calendar-popover'>
        <dropdown class='quick-toolbar' trigger='custom' visible={true} on-on-click={($event, $event2) => throttle(this.itemClick, [$event, $event2], this)}>
          <dropdown-menu slot='list'>
            {this.quickToolbarItems.map((item) => {
                var _a;
                return (<dropdown-item name={item.name} v-show={this.quickToolbarModels[item.name].visabled} disabled={this.quickToolbarModels[item.name].disabled}>
                  {item.showIcon && item.getPSSysImage ? <i class={(_a = item.getPSSysImage) === null || _a === void 0 ? void 0 : _a.cssClass}></i> : null}
                  {item.showCaption ? <span title={item.tooltip}>{item.caption}</span> : null}
                </dropdown-item>);
            })}
          </dropdown-menu>
        </dropdown>
      </div>);
    }
    /**
     * 部件事件
     * @param ctrl 部件
     * @param action  行为
     * @param data 数据
     *
     * @memberof CalendarControlBase
     */
    onCtrlEvent(controlname, action, data) {
        var _a;
        if (action == 'contextMenuItemClick') {
            AppViewLogicService.getInstance().executeViewLogic(`${controlname}_${data === null || data === void 0 ? void 0 : data.data}_click`, undefined, this, {}, ((_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.getPSAppViewLogics()) || []);
        }
        else if (action == 'panelDataChange') {
            this.onPanelDataChange(data.item, data.data);
        }
        else {
            this.ctrlEvent({ controlname, action, data });
        }
    }
    /**
     * 设置按钮文本
     *
     * @public
     * @memberof CalendarControlBase
     */
    setButtonText() {
        (this.buttonText.today = this.$t('app.calendar.today')),
            (this.buttonText.month = this.$t('app.calendar.month')),
            (this.buttonText.week = this.$t('app.calendar.week')),
            (this.buttonText.day = this.$t('app.calendar.day')),
            (this.buttonText.list = this.$t('app.calendar.list'));
        this.customButtons.gotoDate.text = this.$t('app.calendar.gotodate');
    }
    /**
     * 监听语言变化
     *
     * @public
     * @memberof CalendarControlBase
     */
    onLocaleChange(newval, val) {
        this.setButtonText();
    }
    /**
     * 打开时间选择模态
     *
     * @public
     * @memberof CalendarControlBase
     */
    openDateSelect() {
        this.modalVisible = true;
    }
    /**
     * 跳转到指定时间
     *
     * @public
     * @memberof CalendarControlBase
     */
    gotoDate() {
        var _a;
        let appCalendar = this.$refs[(_a = this.controlInstance) === null || _a === void 0 ? void 0 : _a.codeName];
        let api = appCalendar === null || appCalendar === void 0 ? void 0 : appCalendar.getApi();
        if (api) {
            api.gotoDate(this.selectedGotoDate);
        }
    }
    /**
     * 获取事件key
     *
     * @param {*} event 事件对象
     * @return {*}
     * @memberof CalendarControlBase
     */
    getEventKey(event) {
        if ((event === null || event === void 0 ? void 0 : event.itemType) && this.eventKey.has(event.itemType)) {
            return this.eventKey.get(event.itemType);
        }
        return '';
    }
    /**
     * 获取编辑视图信息
     *
     * @param {string} deName 视图名称
     * @return {*}
     * @memberof CalendarControlBase
     */
    getEditView(item) {
        var _a, _b;
        let parentModel = this.controlInstance.parentModel;
        if (parentModel && ((_b = (_a = parentModel === null || parentModel === void 0 ? void 0 : parentModel.getPSAppViewRefs) === null || _a === void 0 ? void 0 : _a.call(parentModel)) === null || _b === void 0 ? void 0 : _b.length)) {
            const viewRef = parentModel.getPSAppViewRefs().find((element) => {
                return element.id.endsWith(`@${item.id}`);
            });
            return viewRef.getRefPSAppView();
        }
    }
    /**
     * 计算部件所需参数
     *
     * @param {*} controlInstance 部件模型对象
     * @param {*} item 日历项数据
     * @returns
     * @memberof CalendarControlBase
     */
    computeTargetCtrlData(controlInstance, item) {
        const { targetCtrlName, targetCtrlParam, targetCtrlEvent } = super.computeTargetCtrlData(controlInstance);
        Object.assign(targetCtrlParam.dynamicProps, {
            navdatas: [item === null || item === void 0 ? void 0 : item.curdata],
        });
        Object.assign(targetCtrlParam.staticProps, {
            transformData: this.transformData,
            opendata: this.opendata,
            newdata: this.newdata,
            refresh: this.refresh,
        });
        targetCtrlEvent['ctrl-event'] = ({ controlname, action, data }) => {
            this.onCtrlEvent(controlname, action, { item: item.curdata, data: data });
        };
        return { targetCtrlName, targetCtrlParam, targetCtrlEvent };
    }
    /**
     * 日程排序
     *
     * @memberof CalendarControlBase
     */
    scheduleSort() {
        if (Object.is(this.calendarType, 'TIMELINE') && this.events.length > 0) {
            this.events.sort((a, b) => {
                const x = a.start;
                const y = b.start;
                return moment(x).isAfter(y) ? -1 : moment(x).isBefore(y) ? 1 : 0;
            });
            // 默认选中第一项
            if (this.isSelectFirstDefault) {
                this.onTimeLineClick(this.events[0]);
            }
        }
    }
};
__decorate([
    Watch('$i18n.locale')
], CalendarControlBase.prototype, "onLocaleChange", null);
CalendarControlBase = __decorate([
    Component({
        components: {
            'app-default-contextmenu': AppDefaultContextMenu,
        },
    }),
    VueLifeCycleProcessing()
], CalendarControlBase);
export { CalendarControlBase };
