<!-- 串口通道 -->
<template>
    <div>
        <!-- 设置树形控件 -->
        <el-button type="info" plain @click="dialogVisible = true">设备设置</el-button>
        <div class="tree_menu" v-show="dialogVisible">
            <el-tree :data="datas" :props="defaultProps" @node-contextmenu="rightClick"
                @node-click="treeNodeClick"></el-tree>
            <!-- 树形控件右键组件 -->
            <div v-show="menuShow" class="tree_rightmenu" :style="{ ...rightMenu }">

                <el-menu class="el-menu-vertical-demo" @select="selectMenuNode" default-active="#0078FE">

                    <el-menu-item id="menuitem" index="1" style="font-size: 12px;">
                        <span slot="title">新建设备</span>
                    </el-menu-item>
                    <el-menu-item index="2" style="font-size: 12px;">
                        <span slot="title">删 除</span>
                    </el-menu-item>
                    <el-menu-item index="3" style="font-size: 12px;">
                        <span slot="title">取 消</span>
                    </el-menu-item>
                </el-menu>
            </div>
        </div>
        <div class="pas_params">
            <el-dialog title="设备属性" :visible.sync="add_passage" width="382px">
                <el-form :label-position="labelPosition" :rules="rules" ref="ruleForm">
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="设备ID:" :label-width="formLabelWidth" prop="dev_id">
                                <el-input v-model="input1" placeholder="" style="width: 200px;height:30px"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="设备名称:" :label-width="formLabelWidth" prop="dev_name">
                                <el-input v-model="input1" placeholder="" style="width: 200px;height:30px"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="设备地址:" :label-width="formLabelWidth" prop="dev_address">
                                <el-input v-model="input1" placeholder="" style="width: 200px;height:30px"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item>
                                <el-checkbox v-model="checked">禁用地址</el-checkbox>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="基地址选择:">
                                <el-switch v-model="value1" :active-value="value1">
                                </el-switch>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="add_passage = false">保 存</el-button>
                        <el-button @click="add_passage = false">取 消</el-button>

                    </span>
            </el-dialog>

        </div>
    </div>
</template>

<script>
export default {
    name: 'DeviceView',

    data() {
        return {
            value1: 1,
            checked: "",
            labelPosition: "left",
            formLabelWidth: "116px",
            input1: 192,
            add_passage: false,
            rightMenu: "",
            dialogVisible: false,
            menuShow: false,
            datas: [{
                label: '一级 1',
                children: [{
                    label: '二级 1-1',
                    children: [{
                        label: '三级 1-1-1'
                    }]
                }]
            }, {
                label: '一级 2',
                children: [{
                    label: '二级 2-1',
                    children: [{
                        label: '三级 2-1-1'
                    }]
                }, {
                    label: '二级 2-2',
                    children: [{
                        label: '三级 2-2-1'
                    }]
                }]
            }, {
                label: '一级 3',
                children: [{
                    label: '二级 3-1',
                    children: [{
                        label: '三级 3-1-1'
                    }]
                }, {
                    label: '二级 3-2',
                    children: [{
                        label: '三级 3-2-1'
                    }]
                }]
            }],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            rules: {
                dev_id: [
                    { required: true, message: '请输入设备ID', trigger: 'blur' },
                ],
                dev_name: [
                    { required: true, message: '请输入设备名称', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                dev_address: [
                    { required: true, message: '请输入设备地址', trigger: 'blur' },
                ],
            },
        };
    },

    mounted() {

    },

    methods: {
        rightClick(e, data, node) {
            this.menuShow = false
            this.menuShow = true
            e.preventDefault() //关闭浏览器右键默认事件
            this.rightMenu = { top: e.pageY + 'px', left: e.pageX + 'px' }
            document.addEventListener('click', (ev) => {
                ev.stopImmediatePropagation()
                if (ev.target !== document.querySelector('.el-menu-item.is-active')) {
                    this.foo()
                }
            })
        },
        // 点击时候的判断,判断是否为新增设备
        selectMenuNode(index, indexPath) {
            console.log(index);
            if (index == 1) {
                this.add_passage = true;
            }
        },
        foo() {
            // 取消鼠标监听事件 菜单栏
            this.menuShow = false
            document.removeEventListener('click', this.foo) // 关掉监听，
        },
        treeNodeClick() {
            this.foo()
        },
        getDriveName() {
            this.diag_drive = true;
        },
        closeMenu(ev) {
            console.log({ ev });
            this.dialogVisible = false;
        }
    },
    directives: {
        'click-outside': {
            bind: function (el, binding, vnode) {
                console.log('el', el);
                console.log("binding", binding);
                el.event = function (event) {
                    // 检查点击是否发生在节点之内（包括子节点）
                    if (!(el == event.target || el.contains(event.target))) {
                        // 如果没有，则触发调用
                        // 若绑定值为函数，则执行
                        // 这里我们可以通过钩子函数中的 vnode.context，来获取当前组件的作用域
                        if (typeof vnode.context[binding.expression] == 'function') {
                            vnode.context[binding.expression](event)
                        }
                    }
                }
                // 绑定事件
                // 设置为true，代表在DOM树中，注册了该listener的元素，会先于它下方的任何事件目标，接收到该事件。
                document.body.addEventListener('click', el.event, true)
            },
            unbind: function (el) {
                // 解绑事件
                document.body.removeEventListener('click', el.event, true)
            },
        },
    },
};
</script>

<style lang="less" scoped>
.tree_menu {
    position: absolute;
    width: 286px;
    height: 753px;
    left: 64px;
    top: 104px;
    width: 286px;
    height: 753px;
    line-height: 20px;
    border-radius: 3px;
    background-color: rgba(255, 255, 255, 1);
    text-align: center;
    border: 1px solid rgba(220, 220, 220, 1);
}

.tree_rightmenu {
    position: fixed;
    width: 120px;
    border: 1px solid rgba(0, 0, 0, 0.15);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
    z-index: 1000;
}

.super_parmams {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 668px;

    i {
        width: 24px;
        height: 24px;
        display: inline-block;
    }
}

/deep/ .pas_params {
    .el-col-12:last-child {
        padding-right: 150px;
    }

    .el-col-12:first-child {
        padding-right: 42px;
    }

    .el-input__inner {
        height: 30px;
        text-align: left;
        padding: 0px 8px;
    }

    .el-row {
        display: flex;
        align-items: flex-end;

        .el-col-1 {
            text-align: center;
            line-height: 20px;
            height: 20px;
        }

        .el-col-6 {
            height: 40px;
            top: 5px;
            left: 77px;
            position: relative;
            width: 50px;
        }
    }
}

.super_con {
    .el-col-12:last-child {
        padding-right: 28px;
    }

    .el-col-12:first-child {
        padding-right: 42px;
    }

    .el-input__inner {

        text-align: left;
        padding: 0 8px;
    }

    .el-row {
        display: flex;
        align-items: flex-end;

        .el-col-4 {
            padding-left: 5px;
            width: 14px;
            text-align: center;
            line-height: 4px;
            height: 20px;
        }
    }
}
</style>