<template>
  <div>
    <pop-menu ref="popMenu" :menu="menu" :pop-class="popClass" />
    <notice-info ref="noticeInfo" />
    <create-device-label ref="createDeviceLabel" />
  </div>
</template>

<script>
import PopMenu from '@/components/PopMenu';
import NoticeInfo from './dialog/childDialog/childDialog/noticeInfo';
import CreateDeviceLabel from './dialog/createDeviceLabel';
import { mapGetters } from 'vuex';
import { OperationEvent } from '@/scripts/cmdPlugin/OperationHandler';
import { DeviceMenu, OperateMode } from '@/scripts/ConstDic';
import CMD from '@/scripts/cmdPlugin/CommandEnum';
import MenuContextHandler from '@/scripts/cmdPlugin/MenuContextHandler';

export default {
    name: 'SignalMenu',
    components: {
        PopMenu,
        NoticeInfo,
        CreateDeviceLabel
    },
    props: {
        selected: {
            type: Object,
            default: () => {
                return null;
            }
        }
    },
    data() {
        return {
            menu: [],
            popClass: 'chengdou-01__select-option',
            menuNormal: {
                Local: [
                    {
                        label: '始端/终端选择',
                        handler: this.arrangementRoute,
                        cmdType: CMD.Signal.CMD_SIGNAL_SET_ROUTE
                    },
                    {
                        label: '取消进路',
                        handler: this.cancelTrainRoute,
                        cmdType: CMD.Signal.CMD_SIGNAL_CANCEL_ROUTE
                    },
                    {
                        label: '开放自动进路',
                        handler:  this.singalPassModel,
                        cmdType: CMD.Signal.CMD_SIGNAL_SET_CI_AUTO
                    },
                    {
                        label: '关闭自动进路',
                        handler: this.singalCancelPassModel,
                        cmdType: CMD.Signal.CMD_SIGNAL_CANCEL_CI_AUTO
                    },
                    {
                        label: '终端信号封锁',
                        handler: this.lock,
                        cmdType: CMD.Signal.CMD_SIGNAL_BLOCK
                    },
                    {
                        label: '终端信号解封',
                        handler: this.unlock,
                        cmdType: CMD.Signal.CMD_SIGNAL_UNBLOCK
                    },
                    {
                        label: '引导信号',
                        handler: this.guide,
                        cmdType: CMD.Signal.CMD_SIGNAL_ROUTE_GUIDE
                    },
                    {
                        label: '设备标签',
                        handler: '',
                        cmdType: CMD.Signal.CMD_SIGNAL_ROUTE_GUIDE,
                        children: [
                            {
                                label: '创建设备标签',
                                handler: '',
                                cmdType: ''
                            }
                        ]
                    },
                    {
                        label: '模拟',
                        handler: '',
                        cmdType: ''
                    },
                    {
                        label: '选择设备',
                        handler: '',
                        cmdType: ''
                    },
                    {
                        label: '帮助',
                        handler: '',
                        cmdType: ''
                    }
                ],
                Center: [
                    {
                        label: '始端/终端选择',
                        handler: this.arrangementRoute,
                        cmdType: CMD.Signal.CMD_SIGNAL_SET_ROUTE
                    },
                    {
                        label: '取消进路',
                        handler: this.cancelTrainRoute,
                        cmdType: CMD.Signal.CMD_SIGNAL_CANCEL_ROUTE
                    },
                    {
                        label: '开放自动进路',
                        handler:  this.singalPassModel,
                        cmdType: CMD.Signal.CMD_SIGNAL_SET_CI_AUTO
                    },
                    {
                        label: '关闭自动进路',
                        handler: this.singalCancelPassModel,
                        cmdType: CMD.Signal.CMD_SIGNAL_CANCEL_CI_AUTO
                    },
                    {
                        label: '终端信号封锁',
                        handler: this.lock,
                        cmdType: CMD.Signal.CMD_SIGNAL_BLOCK
                    },
                    {
                        label: '终端信号解封',
                        handler: this.unlock,
                        cmdType: CMD.Signal.CMD_SIGNAL_UNBLOCK
                    },
                    {
                        label: '引导信号',
                        handler: this.guide,
                        cmdType: CMD.Signal.CMD_SIGNAL_ROUTE_GUIDE
                    },
                    {
                        label: '设备标签',
                        handler: '',
                        cmdType: CMD.Signal.CMD_SIGNAL_ROUTE_GUIDE,
                        children: [
                            {
                                label: '创建设备标签',
                                handler: this.createDeviceLabel,
                                cmdType: ''
                            }
                        ]
                    },
                    {
                        label: '模拟',
                        handler: '',
                        cmdType: ''
                    },
                    {
                        label: '选择设备',
                        handler: '',
                        cmdType: ''
                    },
                    {
                        label: '帮助',
                        handler: '',
                        cmdType: ''
                    }
                ]
            },
            menuForce: [
                {
                    label: '信号关灯',
                    handler: this.signalClose,
                    cmdType: CMD.Signal.CMD_SIGNAL_CLOSE_SIGNAL
                },
                {
                    label: '设置故障',
                    handler: this.setStoppage,
                    cmdType: CMD.Signal.CMD_SIGNAL_ADD_FAULT
                },
                {
                    label: '取消故障',
                    handler: this.cancelStoppage,
                    cmdType: CMD.Signal.CMD_SIGNAL_REMOVE_FAULT
                }
            ]
        };
    },
    computed: {
        ...mapGetters('training', [
            'mode',
            'operatemode'
        ]),
        ...mapGetters('menuOperation', [
            'buttonOperation'
        ]),
        ...mapGetters('map', [
            'routeList'
        ]),
        group() {
            return this.$route.query.group;
        }
    },
    watch: {
        '$store.state.menuOperation.menuCount': function (val) {
            if (this.$store.getters['menuOperation/checkDialogIsOpen'](DeviceMenu.Signal) && !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 = MenuContextHandler.covert(this.menuNormal);

            // 故障模式菜单列表
            if (this.operatemode === OperateMode.FAULT) {
                this.menu = this.menuForce;
            }
        },
        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();
            }
        },
        // 信号关灯
        signalClose() {
            const step = {
                start: true,
                over: true,
                code: `${this.selected.code}`,
                operation: OperationEvent.Signal.signalClose.menu.operation,
                param: {
                    signalCode: `${this.selected.code}`
                },
                cmdType: CMD.Signal.CMD_SIGNAL_CLOSE_SIGNAL
            };
            this.$store.dispatch('training/nextNew', step).then(({ valid }) => {
                if (valid) {
                    this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
                    this.$refs.routeControl.doShow(step, this.selected);
                }
            }).catch(() => {
                this.$refs.noticeInfo.doShow(step);
            });
        },
        // 设置故障
        setStoppage() {
            const step = {
                start: true,
                over:true,
                code: `${this.selected.code}`,
                cmdType: CMD.Signal.CMD_SIGNAL_ADD_FAULT,
                operation: OperationEvent.Signal.stoppage.menu.operation,
                param: {
                    signalCode: `${this.selected.code}`
                }
            };

            this.$store.dispatch('training/nextNew', step).then(({ valid }) => {
                if (valid) {
                    this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
                } else {
                    this.$refs.noticeInfo.doShow(step);
                }
            }).catch(() => {
                this.$refs.noticeInfo.doShow(step);
            });
        },
        // 取消故障
        cancelStoppage() {
            const step = {
                start: true,
                over:true,
                code: `${this.selected.code}`,
                cmdType: CMD.Signal.CMD_SIGNAL_REMOVE_FAULT,
                operation: OperationEvent.Signal.cancelStoppage.menu.operation,
                param: {
                    signalCode: `${this.selected.code}`
                }
            };

            this.$store.dispatch('training/nextNew', step).then(({ valid }) => {
                if (valid) {
                    this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
                } else {
                    this.$refs.noticeInfo.doShow(step);
                }
            }).catch(() => {
                this.$refs.noticeInfo.doShow(step);
            });
        },
        // 设置进路
        arrangementRoute() {
            const step = {
                start: true,
                code: `${this.selected.code}`,
                operation: OperationEvent.Signal.arrangementRoute.menu.operation,
                param: {
                    signalCode: `${this.selected.code}`
                }
            };

            this.$store.dispatch('training/nextNew', step).then(({ valid }) => {
                if (valid) {
                    this.$store.dispatch('menuOperation/handleBreakFlag', { break: true});
                    const routes = [];
                    this.routeList.forEach(elem => {
                        if (elem.startSignalCode === this.selected.code) {
                            routes.push(elem);
                        }
                    });
                    const routeCode = this.checkRoutes(routes);
                    this.$store.dispatch('menuOperation/pushRequestList', {
                        device: this.selected,
                        operation: {
                            code: OperationEvent.Signal.arrangementRoute.menu.operation,
                            name: '始端/终端选择',
                            cmdType: CMD.Signal.CMD_SIGNAL_SET_ROUTE,
                            param: {
                                routeCode: routeCode
                            }
                        }
                    });
                }
            }).catch(() => {
                this.$refs.noticeInfo.doShow(step);
            });
        },
        checkRoutes(routes) {
            const route = routes.filter(route => {
                return route.greenOpen;
            });
            return route[0].code;
        },
        // 进路引导
        guide() {
            const step = {
                start: true,
                // over:true,
                code: `${this.selected.code}`,
                operation: OperationEvent.Signal.guide.menu.operation,
                param: {
                    signalCode: `${this.selected.code}`
                }
                // cmdType: CMD.Signal.CMD_SIGNAL_ROUTE_GUIDE
            };
            this.$store.dispatch('training/nextNew', step).then(({ valid, response }) => {
                if (valid) {
                    this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
                    this.$store.dispatch('menuOperation/pushRequestList', {device: this.selected, operation: { code: OperationEvent.Signal.guide.menu.operation, name: '引导信号', cmdType:CMD.Signal.CMD_SIGNAL_ROUTE_GUIDE}});
                }
            });
        },
        // 取消进路
        cancelTrainRoute() {
            const step = {
                start: true,
                // over:true,
                code: `${this.selected.code}`,
                operation: OperationEvent.Signal.cancelTrainRoute.menu.operation,
                param: {
                    signalCode: `${this.selected.code}`
                }
                // cmdType: CMD.Signal.CMD_SIGNAL_CANCEL_ROUTE

            };
            this.$store.dispatch('training/nextNew', step).then(({ valid }) => {
                if (valid) {
                    this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
                    this.$store.dispatch('menuOperation/pushRequestList', {device: this.selected, operation: { code: OperationEvent.Signal.cancelTrainRoute.menu.operation, name: '取消进路', cmdType:CMD.Signal.CMD_SIGNAL_CANCEL_ROUTE}});
                }
            });
        },
        // 信号封锁
        lock() {
            const step = {
                start: true,
                // over:true,
                code: `${this.selected.code}`,
                operation: OperationEvent.Signal.lock.menu.operation,
                param: {
                    signalCode: `${this.selected.code}`
                }
                // cmdType: CMD.Signal.CMD_SIGNAL_BLOCK
            };
            this.$store.dispatch('training/nextNew', step).then(({ valid }) => {
                if (valid) {
                    this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
                    this.$store.dispatch('menuOperation/pushRequestList', { device: this.selected, operation: { code: OperationEvent.Signal.lock.menu.operation, name: '终端信号机封锁', cmdType:CMD.Signal.CMD_SIGNAL_BLOCK}});
                }
            }).catch(() => {
                this.$refs.noticeInfo.doShow(step);
            });
        },
        // 信号解封
        unlock() {
            const step = {
                start: true,
                // over:true,
                code: `${this.selected.code}`,
                operation: OperationEvent.Signal.unlock.menu.operation,
                param: {
                    signalCode: `${this.selected.code}`
                }
                // cmdType: CMD.Signal.CMD_SIGNAL_UNBLOCK
            };
            this.$store.dispatch('training/nextNew', step).then(({ valid }) => {
                if (valid) {
                    this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
                    this.$store.dispatch('menuOperation/pushRequestList', { device: this.selected, operation: { code: OperationEvent.Signal.unlock.menu.operation, name: '终端信号机解封', cmdType:CMD.Signal.CMD_SIGNAL_UNBLOCK}});
                }
            });
        },
        // 设置通过模式
        singalPassModel() {
            const operate = {
                start: true,
                code: this.selected.code,
                operation: OperationEvent.Signal.setAutoInterlock.menu.operation,
                param: {
                    signalCode: `${this.selected.code}`
                }
            };
            this.$store.dispatch('training/nextNew', operate).then(({ valid }) => {
                if (valid) {
                    this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
                    this.$store.dispatch('menuOperation/pushRequestList', { device: this.selected, operation: { code: OperationEvent.Signal.setAutoInterlock.menu.operation, name: '开放自动进路', cmdType:CMD.Signal.CMD_SIGNAL_SET_CI_AUTO}});
                }
            });
        },
        // 取消通过模式
        singalCancelPassModel() {
            const operate = {
                start: true,
                code: this.selected.code,
                operation: OperationEvent.Signal.cancelAutoInterlock.menu.operation,
                param: {
                    signalCode: `${this.selected.code}`
                }
            };
            this.$store.dispatch('training/nextNew', operate).then(({ valid }) => {
                if (valid) {
                    this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
                    this.$store.dispatch('menuOperation/pushRequestList', { device: this.selected, operation: { code: OperationEvent.Signal.cancelAutoInterlock.menu.operation, name: '关闭自动进路', cmdType:CMD.Signal.CMD_SIGNAL_CANCEL_CI_AUTO}});
                }
            });
        },
        createDeviceLabel() {
            this.doClose();
            this.$refs.createDeviceLabel.doShow();
        }
    }
};
</script>
