﻿<template>
  <div>
    <pop-menu ref="popMenu" :menu="menu" />
    <notice-info ref="noticeInfo" />
    <train-delete ref="trainDelete" />
    <train-define ref="trainDefine" />
    <train-move ref="trainMove" />
    <train-edit ref="trainEdit" />
    <train-set-plan ref="trainSetPlan" />
    <train-add-plan ref="trainAddPlan" />
    <train-move-evently ref="trainMoveEvently" />
    <train-delete-plan ref="trainDeletePlan" />
    <train-set-head ref="trainSetHead" />
    <train-set-work ref="trainSetWork" />
    <train-flag ref="trainFlag" />
  </div>
</template>

<script>
import PopMenu from '@/components/PopMenu';
import NoticeInfo from './dialog/childDialog/childDialog/noticeInfo';
import { mapGetters } from 'vuex';
import { OperateMode } from '@/scripts/ConstDic';
import { MapDeviceType, DeviceMenu } from '@/scripts/ConstDic';
import { MenuDisabledState, menuConvert, trainMenuFiltration } from './utils/menuItemStatus';
import TrainDelete from './dialog/trainDelete';
import TrainDefine from './dialog/trainDefine';
import TrainMove from './dialog/trainMove';
import TrainEdit from './dialog/trainEdit';
import TrainSetPlan from './dialog/trainSetPlan';
import TrainAddPlan from './dialog/trainAddPlan';
import TrainMoveEvently from './dialog/trainMoveEvently';
import TrainDeletePlan from './dialog/trainDeletePlan';
import TrainSetHead from './dialog/trainSetHead';
import TrainSetWork from './dialog/trainSetWork';
import TrainFlag from './dialog/trainFlag';
import { mouseCancelState } from './utils/menuItemStatus';
import { OperationEvent } from '@/scripts/cmdPlugin/OperationHandler';
import MenuContextHandler from '@/scripts/cmdPlugin/MenuContextHandler';
import CMD from '@/scripts/cmdPlugin/CommandEnum';

export default {
	name: 'MenuTrain',
	components: {
		PopMenu,
		NoticeInfo,
		TrainDelete,
		TrainDefine,
		TrainMove,
		TrainEdit,
		TrainSetPlan,
		TrainAddPlan,
		TrainMoveEvently,
		TrainDeletePlan,
		TrainSetHead,
		TrainSetWork,
		TrainFlag
	},
	props: {
		selected: {
			type: Object,
			default() {
				return null;
			}
		}
	},
	data() {
		return {
			menu: [],
			menuNormal: {
				Local: [
					// {
					// 	label: '定义车组号',
					// 	handler: this.addTrainId,
					// 	disabledCallback: MenuDisabledState.Train.addTrainId,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '删除车组号',
					// 	handler: this.delTrainId,
					// 	disabledCallback: MenuDisabledState.Train.delTrainId,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '修改车组号',
					// 	handler: this.editTrainId,
					// 	disabledCallback: MenuDisabledState.Train.editTrainId,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '移动车组号',
					// 	handler: this.moveTrainId,
					// 	disabledCallback: MenuDisabledState.Train.moveTrainId,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	type: 'separator'
					// },
					// {
					// 	label: '设置计划车',
					// 	handler: this.setPlanTrain,
					// 	disabledCallback: MenuDisabledState.Train.setPlanTrain,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '设置头码车',
					// 	handler: this.setHeadTrain,
					// 	disabledCallback: MenuDisabledState.Train.setHeadTrain,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '设置人工车',
					// 	handler: this.setWorkTrain,
					// 	disabledCallback: MenuDisabledState.Train.setWorkTrain,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	type: 'separator'
					// },
					// {
					// 	label: '添加计划车',
					// 	// handler: this.undeveloped,
					// 	handler: this.addPlanTrain,
					// 	// disabledCallback: MenuDisabledState.Train.moveTrainId,
					// 	disabledCallback: MenuDisabledState.Train.addPlanTrain,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '删除计划车',
					// 	// handler: this.undeveloped,
					// 	handler: this.deletePlanTrain,
					// 	disabledCallback: MenuDisabledState.Train.moveTrainId,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '平移计划车',
					// 	// handler: this.undeveloped,
					// 	handler: this.moveEventlyTrain,
					// 	disabledCallback: MenuDisabledState.Train.moveEventlyTrain,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	type: 'separator'
					// },
					// {
					// 	label: '标记重点列车',
					// 	handler: this.trainToFlag,
					// 	disabledCallback: MenuDisabledState.Train.trainToFlag,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '取消标记重点列车',
					// 	handler: this.trainUnFlag,
					// 	disabledCallback: MenuDisabledState.Train.trainUnFlag,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	type: 'separator'
					// },
					// {
					// 	label: '列车信息',
					// 	handler: this.undeveloped,
					// 	disabledCallback: MenuDisabledState.Train.moveTrainId,
					// 	auth: { station: true, center: true }
					// }
					{
                        label: '新建计划车',
                        handler: this.addPlanTrain,
                        cmdType: CMD.TrainWindow.CMD_Train_Init_Plan
                    },
				],
				Center: [
					// {
					// 	label: '定义车组号',
					// 	handler: this.addTrainId,
					// 	disabledCallback: MenuDisabledState.Train.addTrainId,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '删除车组号',
					// 	handler: this.delTrainId,
					// 	disabledCallback: MenuDisabledState.Train.delTrainId,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '修改车组号',
					// 	handler: this.editTrainId,
					// 	disabledCallback: MenuDisabledState.Train.editTrainId,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '移动车组号',
					// 	handler: this.moveTrainId,
					// 	disabledCallback: MenuDisabledState.Train.moveTrainId,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	type: 'separator'
					// },
					// {
					// 	label: '设置计划车',
					// 	handler: this.setPlanTrain,
					// 	disabledCallback: MenuDisabledState.Train.setPlanTrain,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '设置头码车',
					// 	handler: this.setHeadTrain,
					// 	disabledCallback: MenuDisabledState.Train.setHeadTrain,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '设置人工车',
					// 	handler: this.setWorkTrain,
					// 	disabledCallback: MenuDisabledState.Train.setWorkTrain,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	type: 'separator'
					// },
					// {
					// 	label: '添加计划车',
					// 	// handler: this.undeveloped,
					// 	handler: this.addPlanTrain,
					// 	// disabledCallback: MenuDisabledState.Train.moveTrainId,
					// 	disabledCallback: MenuDisabledState.Train.addPlanTrain,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '删除计划车',
					// 	// handler: this.undeveloped,
					// 	handler: this.deletePlanTrain,
					// 	disabledCallback: MenuDisabledState.Train.moveTrainId,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '平移计划车',
					// 	// handler: this.undeveloped,
					// 	handler: this.moveEventlyTrain,
					// 	disabledCallback: MenuDisabledState.Train.moveTrainId,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	type: 'separator'
					// },
					// {
					// 	label: '标记重点列车',
					// 	handler: this.trainToFlag,
					// 	disabledCallback: MenuDisabledState.Train.trainToFlag,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	label: '取消标记重点列车',
					// 	handler: this.trainUnFlag,
					// 	disabledCallback: MenuDisabledState.Train.trainUnFlag,
					// 	auth: { station: true, center: true }
					// },
					// {
					// 	type: 'separator'
					// },
					// {
					// 	label: '列车信息',
					// 	handler: this.undeveloped,
					// 	disabledCallback: MenuDisabledState.Train.moveTrainId,
					// 	auth: { station: true, center: true }
					// },
					{
                        label: '新建计划车',
                        handler: this.addPlanTrain,
                        cmdType: CMD.TrainWindow.CMD_Train_Init_Plan
                    },
				]
			},
			menuForce: [
				{
					label: '设置通信故障',
					handler: this.setStoppage,
					disabledCallback: MenuDisabledState.Train.setStoppage
				},
				{
					label: '取消通信故障',
					handler: this.cancelStoppage,
					disabledCallback: MenuDisabledState.Train.cancelStoppage
				}
			],
			menuSpeed: [
				{
					label: '确认运行至前方站',
					handler: this.limitSpeed,
					disabledCallback: MenuDisabledState.Train.limitSpeed
				}
			]
		};
	},
	computed: {
		...mapGetters('training', [
			'mode',
			'operatemode'
		]),
		...mapGetters('menuOperation', [
			'buttonOperation'
		])
	},
	watch: {
		'$store.state.menuOperation.menuCount': function () {
			if (this.$store.getters['menuOperation/checkDialogIsOpen'](DeviceMenu.Train) && !this.buttonOperation) {
				this.doShow(this.$store.state.menuOperation.menuPosition);
			} else {
				this.doClose();
			}
		}
	},
	methods: {
		clickEvent() {
			const self = this;
			window.onclick = function (e) {
				self.doClose();
			};
		},
		initMenu() {
			// 编辑模式菜单列表
			// this.menu = trainMenuFiltration(this.menuNormal);
			this.menu = MenuContextHandler.covert(this.menuNormal);
			// if (this.operatemode === OperateMode.ADMIN) {
			// 	this.menu = [...this.menu, ...this.menuForce, ...this.menuSpeed];
			// }

			// 故障模式菜单列表
			if (this.operatemode === OperateMode.FAULT) {
				this.menu = [...this.menuForce, ...this.menuSpeed];
			}

			// this.menu = menuConvert(this.menu);
		},
		doShow(point) {
			this.clickEvent();
			this.initMenu();

			if (this.$refs && this.$refs.popMenu && this.menu && this.menu.length) {
				this.$refs.popMenu.resetShowPosition(point);
			}
		},
		doClose() {
			if (this.$refs && this.$refs.popMenu) {
				this.$refs.popMenu.close();
				// this.$store.dispatch('map/setTrainWindowShow', false);
			}
		},
		// 设置故障
		setStoppage() {
			const operate = {
				start: true,
				send: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.stoppage.menu.operation
			};

			mouseCancelState(this.selected);
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
				} else {
					this.$refs.noticeInfo.doShow(operate);
				}
			}).catch(() => {
				this.$refs.noticeInfo.doShow(operate);
			});
		},
		// 取消故障
		cancelStoppage() {
			const operate = {
				start: true,
				send: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.cancelStoppage.menu.operation
			};

			mouseCancelState(this.selected);
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
				} else {
					this.$refs.noticeInfo.doShow(operate);
				}
			}).catch(() => {
				this.$refs.noticeInfo.doShow(operate);
			});
		},
		// 限速行驶
		limitSpeed() {
			const operate = {
				start: true,
				send: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.limitSpeed.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
				} else {
					this.$refs.noticeInfo.doShow(operate);
				}
			}).catch(() => {
				this.$refs.noticeInfo.doShow(operate);
			});
		},
		undeveloped() {
			this.doClose();
			this.$alert('实现中......', '提示', {
				confirmButtonText: '确定',
				callback: action => {
				}
			});
		},
		// 添加列车识别号
		addTrainId() {
			const operate = {
				start: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.addTrainId.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainDefine.doShow(operate, this.selected);
				}
			});
		},
		// 删除列车识别号
		delTrainId() {
			const operate = {
				start: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.delTrainId.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainDelete.doShow(operate, this.selected);
				}
			});
		},
		// 移动列车识别号
		moveTrainId() {
			const operate = {
				start: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.moveTrainId.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainMove.doShow(operate, this.selected);
				}
			});
		},
		// 修改列车识别号
		editTrainId() {
			const operate = {
				start: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.editTrainId.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainEdit.doShow(operate, this.selected);
				}
			});
		},
		// 设置计划车
		setPlanTrain() {
			const operate = {
				start: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.editTrainId.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainSetPlan.doShow(operate, this.selected);
				}
			});
		},
		// 添加计划车
		addPlanTrain() {
			const operate = {
                start: true,
                code: this.selected.code,
                operation: OperationEvent.Train.createPlanTrain.menu.operation,
                param: {
                    sectionCode: this.$store.state.map.trainWindowSectionCode
                }
            };
			this.$store.dispatch('training/nextNew', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainAddPlan.doShow(operate, this.selected);
				}
			});
		},
		// 平移计划车
		moveEventlyTrain() {
			const operate = {
				start: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.moveEventlyTrainId.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainMoveEvently.doShow(operate, this.selected);
				}
			});
		},
		// 删除计划车
		deletePlanTrain() {
			const operate = {
				start: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.deletePlanTrainId.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainDeletePlan.doShow(operate, this.selected);
				}
			});
		},
		// 设置头码车
		setHeadTrain() {
			const operate = {
				start: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.editTrainId.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainSetHead.doShow(operate, this.selected);
				}
			});
		},
		// 设置人工车
		setWorkTrain() {
			const operate = {
				start: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.editTrainId.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainSetWork.doShow(operate, this.selected);
				}
			});
		},
		// 标记重点车
		trainToFlag() {
			const operate = {
				start: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.editTrainId.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainFlag.doShow(operate, this.selected);
				}
			});
		},
		// 取消标记重点车
		trainUnFlag() {
			const operate = {
				start: true,
				code: this.selected.code,
				type: MapDeviceType.Train.type,
				label: MapDeviceType.Train.label,
				operation: OperationEvent.Train.editTrainId.menu.operation
			};
			this.$store.dispatch('training/next', operate).then(({ valid }) => {
				if (valid) {
					this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
					this.$refs.trainFlag.doShow(operate, this.selected);
				}
			});
		}
	}
};
</script>
