<template>
  <div>
    <pop-menu ref="popMenu" :menu="menu" />
  </div>
</template>

<script>
import { DeviceMenu } from '@/scripts/ConstDic';
import PopMenu from '@/components/PopMenu';

export default {
	name: 'TrainingOperateMenu',
	components: {
		PopMenu
	},
	props: {
		point: {
			type: Object,
			required: true
		},
		node: {
			type: Object,
			required: true
		}
	},
	data() {
		return {
			menuShow: false,
			menu: [],
			menuChapte: [
				{
					label: '更新章节',
					handler: this.updateChapter
				},
				{
					label: '创建章节',
					handler: this.createChapterInChapter
				}
			],
			menuLesson: [
				{
					label: '创建章节',
					handler: this.createChapter
				},
				{
					label: '编辑课程',
					handler: this.editLesson
				}

			]
		};
	},
	watch: {
		node: function (val, old) {
			if (val && val.data) {
				switch (val.data.type) {
				case 'Lesson':
					this.menu = this.menuLesson;
					break;
				case 'Chapter':
					this.menu = this.menuChapte;
					break;
				}
			}
		},
		'$store.state.menuOperation.menuCount': function (val) {
			if (this.$store.getters['menuOperation/checkDialogIsOpen'](DeviceMenu.Lesson)) {
				this.doShow(this.$store.state.menuOperation.menuPosition);
			} else {
				this.doClose();
			}
		}
	},
	mounted() {
		this.closeEvent();
	},
	methods: {
		closeEvent() {
			const self = this;
			window.onclick = function (e) {
				self.doClose();
			};
		},
		doShow(point) {
			this.closeEvent();
			if (this.$refs && this.$refs.popMenu) {
				this.$refs.popMenu.resetShowPosition(point);
			}
			this.menuShow = true;
		},
		doClose() {
			if (this.$refs && this.$refs.popMenu) {
				this.$refs.popMenu.close();
			}
			this.menuShow = false;
		},
		updateChapter() {
			this.$emit('changeRouter', {event: '03', node: this.node});
		},
		createChapter() {
			this.$emit('changeRouter', {event: '02', node: this.node});
		},
		editLesson() {
			this.$emit('changeRouter', {event: '01', node: this.node});
		},
		createChapterInChapter() {
			this.$emit('changeRouter', {event: '04', node: this.node});
		},
		refresh(filterSelect) {
			this.$emit('refresh', filterSelect);
		}
	}
};
</script>
