<template>
  <div>
    <pop-menu ref="popMenu" :menu="menu" />
    <switch-control ref="switchControl" />
    <switch-un-lock ref="switchUnLock" />
    <speed-limit-control ref="speedLimitControl" />
    <notice-info ref="noticeInfo" />
  </div>
</template>

<script>
import PopMenu from '@/components/PopMenu';
import SwitchControl from './dialog/switchControl';
import SwitchUnLock from './dialog/switchUnLock';
import SpeedLimitControl from './dialog/speedLimitControl';
import NoticeInfo from './dialog/childDialog/childDialog/noticeInfo';
import { mapGetters } from 'vuex';
import {menuOperate, commitOperate} from './utils/menuOperate';
import CMD from '@/scripts/cmdPlugin/CommandEnum';
import { OperationEvent } from '@/scripts/cmdPlugin/OperationHandler';
import { DeviceMenu, OperateMode } from '@/scripts/ConstDic';
import MenuContextHandler from '@/scripts/cmdPlugin/MenuContextHandler';

export default {
    name: 'SwitchMenu',
    components: {
        PopMenu,
        SwitchControl,
        SwitchUnLock,
        SpeedLimitControl,
        NoticeInfo
    },
    props: {
        selected: {
            type: Object,
            default() {
                return null;
            }
        }
    },
    data() {
        return {
            menu: [],
            menuNormal: {
                Local: [
                ],
                Center: [
                    // {
                    //     label: '设置临时限速',
                    //     handler: this.setSpeed,
                    //     cmdType: CMD.Section.CMD_SECTION_SET_LIMIT_SPEED
                    // },
                ]
            },
            menuForce: [
                {
                    label: '设置故障',
                    handler: this.setStoppage,
                    cmdType: CMD.Switch.CMD_SWITCH_ADD_FAULT
                },
                {
                    label: '取消故障',
                    handler: this.cancelStoppage,
                    cmdType: CMD.Switch.CMD_SWITCH_REMOVE_FAULT
                }
            ]
        };
    },
    computed: {
        ...mapGetters('training', [
            'mode',
            'operatemode'
        ]),
        ...mapGetters('menuOperation', [
            'buttonOperation'
        ])
    },
    watch: {
        '$store.state.menuOperation.menuCount': function (val) {
            if (this.$store.getters['menuOperation/checkDialogIsOpen'](DeviceMenu.Switch) && !this.buttonOperation) {
                this.doShow(this.$store.state.menuOperation.menuPosition);
            } else {
                this.doClose();
            }
        },
        '$store.state.menuOperation.selectedCount': function(val) {
            if (this.buttonOperation && this.$store.state.menuOperation.selected._type == 'Switch') {
                this.operationHandler(this.buttonOperation, this.$store.state.menuOperation.selected);
            }
        }
    },
    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();
            }
        },
        // 设置故障
        setStoppage() {
            const operate = {
                start: true,
                code: this.selected.code,
                operation: OperationEvent.Switch.stoppage.menu.operation,
                param: {
                    switchCode: this.selected.code
                }
            };

            this.mouseCancelState(this.selected);
            this.$store.dispatch('training/nextNew', 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,
                code: this.selected.code,
                operation: OperationEvent.Switch.cancelStoppage.menu.operation,
                param: {
                    switchCode: this.selected.code
                }
            };

            this.mouseCancelState(this.selected);
            this.$store.dispatch('training/nextNew', operate).then(({ valid }) => {
                if (valid) {
                    this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
                } else {
                    this.$refs.noticeInfo.doShow(operate);
                }
            }).catch(() => {
                this.$refs.noticeInfo.doShow(operate);
            });
        },
        operationHandler(buttonOperation, selectType) {
            switch (buttonOperation) {
            case OperationEvent.Switch.locate.button.operation: {
                // 道岔总定
                if (!selectType.normalPosition && selectType.reversePosition) {
                    this.locate(selectType);
                }
                break;
            }
            case OperationEvent.Switch.reverse.button.operation: {
                // 道岔总反
                if (selectType.normalPosition && !selectType.reversePosition) {
                    this.reverse(selectType);
                }
                break;
            }
            case OperationEvent.Switch.lock.button.operation: {
                // 道岔单锁
                if (!selectType.singleLock) {
                    this.lock(selectType);
                }
                break;
            }
            case OperationEvent.Switch.unlock.button.operation: {
                // 道岔解锁
                if (selectType.singleLock) {
                    this.unlock(selectType);
                }
                break;
            }
            case OperationEvent.Switch.block.button.operation: {
                // 道岔封锁
                if (!selectType.blockade) {
                    this.block(selectType);
                }
                break;
            }
            case OperationEvent.Switch.unblock.button.operation: {
                // 道岔解封
                if (selectType.blockade) {
                    this.unblock(selectType);
                }
                break;
            }
            case OperationEvent.Switch.turnoutForce.button.operation: {
                // 道岔强扳
                this.switchTurnoutForce(selectType);
                break;
            }
            }
        },
        // 单操到定位
        locate(selectType) {
            commitOperate(menuOperate.Switch.locate, {switchCode:selectType.code}, 3).then(({valid, operate})=>{
                // this.$refs.switchControl.doShow(data.operate, this.selected);
            }).catch(error=>{
                this.$refs.noticeInfo.doShow({}, error.message);
            });
        },
        // 单操到反位
        reverse(selectType) {
            commitOperate(menuOperate.Switch.reverse, {switchCode:selectType.code}, 3).then(({valid, operate})=>{
                // this.$refs.switchControl.doShow(data.operate, this.selected);
            }).catch(error=>{
                this.$refs.noticeInfo.doShow({}, error.message);
            });
        },
        // 道岔单锁
        lock(selectType) {
            commitOperate(menuOperate.Switch.lock, {switchCode:selectType.code}, 3).then(({valid, operate})=>{
                // this.$refs.switchControl.doShow(data.operate, this.selected);
            }).catch(error=>{
                this.$refs.noticeInfo.doShow({}, error.message);
            });
        },
        // 道岔解锁
        unlock(selectType) {
            commitOperate(menuOperate.Switch.unlock, {switchCode:selectType.code}, 3).then(({valid, operate})=>{
                // this.$refs.switchUnLock.doShow(data.operate, this.selected);
            }).catch(error=>{
                this.$refs.noticeInfo.doShow({}, error.message);
            });
        },
        // 道岔封锁
        block(selectType) {
            commitOperate(menuOperate.Switch.block, {switchCode:selectType.code}, 3).then(({valid, operate})=>{
                // this.$refs.switchControl.doShow(data.operate, this.selected);
            }).catch(error=>{
                this.$refs.noticeInfo.doShow({}, error.message);
            });
        },
        // 道岔解封
        unblock(selectType) {
            commitOperate(menuOperate.Switch.unblock, {switchCode:selectType.code}, 3).then(({valid, operate})=>{
                // this.$refs.switchUnLock.doShow(data.operate, this.selected);
            }).catch(error=>{
                this.$refs.noticeInfo.doShow({}, error.message);
            });
        },
        // 道岔强扳
        switchTurnoutForce(selectType) {
            commitOperate(menuOperate.Switch.turnoutForce, {switchCode:selectType.code}, 3).then(({valid, operate})=>{
            }).catch(error=>{
                this.$refs.noticeInfo.doShow({}, error.message);
            });
        },
        // 设置临时限速
        // setSpeed() {
        //     const operate = {
        //         start: true,
        //         code: this.selected.code,
        //         operation: OperationEvent.Switch.setSpeed.menu.operation,
        //         param: {
        //             switchCode: this.selected.code
        //         }
        //     };
        //     this.$store.dispatch('training/nextNew', operate).then(({ valid }) => {
        //         if (valid) {
        //             this.$store.dispatch('menuOperation/handleBreakFlag', { break: true });
        //             this.$refs.speedLimitControl.doShow(operate, this.selected);
        //         }
        //     });
        // },
        undeveloped() {
            this.doClose();
            this.$alert('实现中......', '提示', {
                confirmButtonText: '确定',
                callback: action => {
                }
            });
        }
    }
};
</script>
