<template>
    <div>
        <pop-menu ref="popMenu" :menu="menu"></pop-menu>
        <choose-role ref="chooseRole" @setDriver="setDriver"></choose-role>
    </div>
</template>

<script>
    import PopMenu from '@/components/PopMenu';
    import ChooseRole from './chooseRole';
    import { putUserRoles } from '@/api/chat';
    import { DeviceMenu } from '@/scripts/ConstDic';
    import { UrlConfig } from '@/router/index';

    export default {
        name: 'operateMenu',
        components: {
            PopMenu,
            ChooseRole
        },
        props: {
            group: {
                type: String
            },
            point: {
                type: Object,
                required: true
            },
            selected: {
                type: Object,
                required: true
            },
            driverList: {
                type: Array,
                required: true
            },
            driverMapDict: {
                type: Object,
                required: true
            },
        },
        data() {
            return {
                menuShow: false,
                menu: [
                    {
                        label: '设置司机',
                        handler: this.chooseDriver
                    },
                    {
                        label: '取消司机',
                        handler: this.cancelDriver
                    }
                ]
            }
        },
        watch: {
            '$store.state.menuOperation.menuCount': function (val) {
                if (this.$store.getters['menuOperation/checkDialogIsOpen'](DeviceMenu.SetDriver)) {
                    this.doShow(this.$store.state.menuOperation.menuPosition);
                } else {
                    this.doClose();
                }
            }
        },
        mounted() {
            this.closeEvent();
        },
        methods: {
            closeEvent() {
                let 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;
            },
            chooseDriver() {
                let arrs = Object.values(this.driverMapDict);
                let list = this.driverList.filter(elem => {
                    let ret = true;
                    arrs.forEach(item => {
                        if (item.id == elem.id) {
                            ret = false;
                        }
                    });
                    return ret;
                });

                this.$refs.chooseRole.doShow({ title: '设置列车', list: list });
            },
            async setDriver(obj) {
                if (obj && this.selected) {
                    let params = [{
                        id: obj.id,
                        nickName: obj.name,
                        userRole: 'Driver',
                        stationCode: '',
                        trainCode: this.selected._code
                    }];

                    await putUserRoles(params, this.group);
                }
            },
            async cancelDriver() {
                let data = this.driverMapDict[this.selected._code];
                if (data) {
                    let params = [{
                        id: data.id,
                        nickName: data.name,
                        userRole: 'Driver',
                        stationCode: '',
                        trainCode: ''
                    }];

                    await putUserRoles(params, this.group);
                }
            },
            refresh() {
                this.$emit('refresh');
            }
        }
    }
</script>
