<!-- 采集点配置-无线 -->
<template>
    <div>
        <!-- <div v-click-outside="closeMenu"> -->
        <!-- 设置树形控件 -->
        <!-- <el-button type="info" plain @click="dialogVisible = true">采集点设置</el-button> -->
        <div v-show="dialogVisible" class="tree_menu">
            <el-tree :data="datas" :props="defaultProps" default-expand-all @node-click="treeNodeClick"></el-tree>
            <!-- 树形控件右键组件 -->
        </div>
        <!-- </div> -->
        <div class="pas_params" v-show="add_passage">
            <div>
                <el-button type="info" plain @click="handleSelect">保存</el-button>
                <!-- <el-menu class="el-menu-demo" mode="horizontal" @select="handleSelect"> -->
                <!-- 添加时只支持一条，不能多次添加 -->
                <!-- <el-menu-item index="1">保存</el-menu-item> -->
                <!-- </el-menu> -->
            </div>
            <el-table ref="singleTable" :data="tableData" style="width: 100%" v-loading="loading">
                <el-table-column type="index">
                </el-table-column>
                <el-table-column property="cjdpz_tagid" label="TagID">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.cjdpz_tagid" placeholder="0X">
                            <el-option v-for="(option, index) in options1" :key="index" :label="option.label"
                                :value="option.value"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column property="cjdpz_mc" label="名称">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.cjdpz_mc" placeholder="0X">
                            <el-option v-for="(option, index) in options2" :key="index" :label="option.label"
                                :value="option.value"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column property="cjdpz_sjq" label="数据区">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.cjdpz_sjq" placeholder="0X">
                            <el-option v-for="(option, index) in options3" :key="index" :label="option.label"
                                :value="option.value"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column property="cjdpz_jcqdz" label="寄存地址(十进制)" width="150px">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.cjdpz_jcqdz" placeholder="1-9999"></el-input>
                    </template>
                </el-table-column>
                <el-table-column property="cjdpz_cjsjlx" label="采集数据类型">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.cjdpz_cjsjlx" placeholder="0X">
                            <el-option v-for="(option, index) in options5" :key="index" :label="option.label"
                                :value="option.value"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column property="cjdpz_zjx" label="自节序">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.cjdpz_zjx" placeholder="0X">
                            <el-option v-for="(option, index) in options6" :key="index" :label="option.label"
                                :value="option.value"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column property="cjdpz_dxsx" label="读写属性">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.cjdpz_dxsx" placeholder="0X">
                            <el-option v-for="(option, index) in options7" :key="index" :label="option.label"
                                :value="option.value"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column property="cjdpz_beilv" label="倍率">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.cjdpz_beilv" placeholder="0X">
                            <el-option v-for="(option, index) in options8" :key="index" :label="option.label"
                                :value="option.value"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>

<script>
import { SaveUserAnswer, GetUserAnswer } from "@/api/user";
export default {
    name: 'DeviceView',

    data() {
        return {
            tableData: [{
                cjdpz_tagid: 'MachinePamHID909',
                cjdpz_mc: '急停状态',
                cjdpz_sjq: "0X (Coil Status)",
                cjdpz_jcqdz: '',
                cjdpz_cjsjlx: 'bool',
                cjdpz_zjx: "Null",
                cjdpz_dxsx: "读写",
                cjdpz_beilv: "1",
            }],
            options1: [{
                value: "MachinePamHID909",
                label: "MachinePamHID909",
            }, {
                value: "MachinePamHID910",
                label: "MachinePamHID910",
            }, {
                value: "MachinePamHID911",
                label: "MachinePamHID911",
            }, {
                value: "MachinePamHID912",
                label: "MachinePamHID912",
            }, {
                value: "MachinePamHID913",
                label: "MachinePamHID913",
            }, {
                value: "MachinePamHID914",
                label: "MachinePamHID914",
            }, {
                value: "MachinePamHID915",
                label: "MachinePamHID915",
            }, {
                value: "MachinePamHID916",
                label: "MachinePamHID916",
            }, {
                value: "MachinePamHID917",
                label: "MachinePamHID917",
            }, {
                value: "MachinePamHID918",
                label: "MachinePamHID918",
            }, {
                value: "MachinePamHID919",
                label: "MachinePamHID919",
            }, {
                value: "MachinePamHID920",
                label: "MachinePamHID920",
            }, {
                value: "MachinePamHID921",
                label: "MachinePamHID921",
            }, {
                value: "MachinePamHID922",
                label: "MachinePamHID922",
            }, {
                value: "MachinePamHID923",
                label: "MachinePamHID923",
            }, {
                value: "MachinePamHID924",
                label: "MachinePamHID924",
            }, {
                value: "MachinePamHID925",
                label: "MachinePamHID925",
            }, {
                value: "MachinePamHID926",
                label: "MachinePamHID926",
            }, {
                value: "MachinePamHID927",
                label: "MachinePamHID927",
            }, {
                value: "MachinePamHID928",
                label: "MachinePamHID928",
            }, {
                value: "MachinePamHID929",
                label: "MachinePamHID929",
            }, {
                value: "MachinePamHID930",
                label: "MachinePamHID930",
            }, {
                value: "MachinePamHID931",
                label: "MachinePamHID931",
            }, {
                value: "MachinePamHID932",
                label: "MachinePamHID932",
            }, {
                value: "MachinePamHID933",
                label: "MachinePamHID933",
            }, {
                value: "MachinePamHID934",
                label: "MachinePamHID934",
            }, {
                value: "MachinePamHID935",
                label: "MachinePamHID935",
            }, {
                value: "MachinePamHID936",
                label: "MachinePamHID936",
            }, {
                value: "MachinePamHID937",
                label: "MachinePamHID937",
            }, {
                value: "MachinePamHID938",
                label: "MachinePamHID938",
            }],
            options2: [{
                value: "急停状态",
                label: "急停状态",
            }, {
                value: "产量",
                label: "产量",
            },
            {
                value: "相对位置X",
                label: "相对位置X",
            },
            {
                value: "相对位置Y",
                label: "相对位置Y",
            },
            {
                value: "相对位置Z",
                label: "相对位置Z",
            },
            {
                value: "实际位置X",
                label: "实际位置X",
            },
            {
                value: "实际位置Y",
                label: "实际位置Y",
            },
            {
                value: "实际位置Z",
                label: "实际位置Z",
            },
            {
                value: "主轴转速",
                label: "主轴转速",
            },
            {
                value: "当前刀具",
                label: "当前刀具",
            },
            {
                value: "预选刀具",
                label: "预选刀具",
            },
            {
                value: "报警信息",
                label: "报警信息",
            },
            {
                value: "驱动单元电流",
                label: "驱动单元电流",
            },
            {
                value: "负载电流",
                label: "负载电流",
            },
            {
                value: "额定电流",
                label: "额定电流",
            },
            {
                value: "运行时间",
                label: "运行时间",
            },
            {
                value: "加工产品用时",
                label: "加工产品用时",
            },
            {
                value: "工作模式",
                label: "工作模式",
            },
            {
                value: "切削速度",
                label: "切削速度",
            },
            {
                value: "任务信息",
                label: "任务信息",
            },
            {
                value: "使能状态",
                label: "使能状态",
            },
            {
                value: "系统状态",
                label: "系统状态",
            },
            {
                value: "执行任务信息",
                label: "执行任务信息",
            },
            {
                value: "进给倍军",
                label: "进给倍军",
            },
            {
                value: "轴速度",
                label: "轴速度",
            },
            {
                value: "轴角度",
                label: "轴角度",
            },
            {
                value: "进给速度",
                label: "进给速度",
            }],
            options3: [{
                value: "0X (Coil Status)",
                label: "0X (Coil Status)"
            },
            {
                value: "1X (Input Status)",
                label: "1X (Input Status)"
            }, {
                value: "3X (Input Registers)",
                label: "3X (Input Registers)"
            },
            {
                value: "4X (Holding Register)",
                label: "4X (Holding Register)"
            }],
            options5: [{
                value: " bool",
                label: "bool"
            },
            {
                value: "bit",
                label: "bit"
            }, {
                value: "uint16",
                label: "uint16"
            },
            {
                value: "int16",
                label: "int16"
            },
            {
                value: "uint32",
                label: "uint32"
            },
            {
                value: "int32",
                label: "int32"
            }, {
                value: "float",
                label: "float"
            },
            {
                value: "double",
                label: "double"
            }],
            options6: [{
                value: " Null",
                label: "Null"
            },
            {
                value: "Big-endian（4321）",
                label: "Big-endian（4321）"
            }, {
                value: "Little-endian（1234）",
                label: "Little-endian（1234）"
            },
            {
                value: "Big-endian byte swap（2143）",
                label: "Big-endian byte swap（2143）"
            },
            {
                value: "Little-endian byte swap（3412）",
                label: "Little-endian byte swap（3412）"
            }],
            options7: [{
                value: "读写",
                label: "读写"
            },
            {
                value: "只读",
                label: "只读"
            }, {
                value: "只写",
                label: "只写"
            }],
            options8: [{
                value: " 0.1",
                label: "0.1"
            },
            {
                value: "1",
                label: "1"
            }, {
                value: "10",
                label: "10"
            },
            {
                value: "100",
                label: "100"
            }],
            labelPosition: "left",
            formLabelWidth: "116px",
            add_passage: false,
            rightMenu: "",
            dialogVisible: true,
            menuShow: false,
            datas: [{
                label: 'Gateway',
                children: [{
                    label: '数据采集',
                    children: [{
                        label: 'I/O点'
                    },
                    {
                        label: '用户点',
                    },
                    {
                        label: '计算点',
                    },
                    {
                        label: '系统点',
                    },
                    ]
                },
                {
                    label: "数据服务",
                    children: [{
                        label: "Modbus"
                    }]
                },
                {
                    label: "IOT",
                    children: [
                        {
                            label: "Mqtt Client"
                        },
                        {
                            label: "Alink"
                        },
                        {
                            label: "Tlink"
                        },
                        {
                            label: "loTDDC"
                        },
                        {
                            label: "Ulink"
                        },
                    ]
                }
                ]
            }],
            defaultProps: {
                children: 'children',
                label: 'label'
            },

        };
    },
    methods: {
        treeNodeClick(key, keyPath) {
            console.log(key);
            console.log(keyPath);
            if (keyPath.id == 10 || key.label == "Mqtt Client") {
                // this.$router.push("./mqttconfig")
            }
            this.add_passage = true;
        },
        getDriveName() {
            this.diag_drive = true;
        },
        async handleSelect() {
            // console.log(this.tableData);
            // console.log("123111");
            let datas = sessionStorage.getItem("DB-IotLite_wx");
            this.tableData = { ...JSON.parse(datas), ...this.tableData[0] };
            let res = await SaveUserAnswer({
                mark: "DB-IotLite_wx",
                user_id: "USER202307301114011710",
                mark_value: JSON.stringify(this.tableData),
            });
            if (res.status == 200) {
                this.$message({
                    message: '保存成功',
                    type: 'success'
                });
            }
            console.log(res);
            // localStorage.setItem("CollectionWifi",true);
            sessionStorage.setItem("DB-IotLite_wx", JSON.stringify(this.tableData));
            console.log(this.tableData);
            // }
        },
        closeMenu(ev) {
            console.log({ ev });
            this.dialogVisible = false;
        },
        async GetUserAnswer() {
            this.loading = true;
            let answer = await GetUserAnswer({
                mark: "DB-IotLite_wx",
                user_id: "USER202307301114011710",
            })
            this.loading = false;
            if (answer.data.state && answer.data.data) {
                console.log(answer);
                let obj = JSON.parse(answer.data.data.mark_value);
                console.log(obj);
                this.tableData[0].cjdpz_tagid = obj.cjdpz_tagid;
                this.tableData[0].cjdpz_mc = obj.cjdpz_mc;
                this.tableData[0].cjdpz_sjq = obj.cjdpz_sjq;
                this.tableData[0].cjdpz_jcqdz = obj.cjdpz_jcqdz;
                this.tableData[0].cjdpz_cjsjlx = obj.cjdpz_cjsjlx;
                this.tableData[0].cjdpz_zjx = obj.cjdpz_zjx;
                this.tableData[0].cjdpz_dxsx = obj.cjdpz_dxsx;
                this.tableData[0].cjdpz_beilv = obj.cjdpz_beilv;
            }
        }
    },
    mounted() {
        this.GetUserAnswer();
    },
    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);
}

/deep/ .pas_params {
    position: absolute;
    left: 362px;
    top: 125px;
    width: 1066px;
    height: 716px;
    line-height: 20px;
    border-radius: 3px;
    background-color: rgba(255, 255, 255, 1);
    // text-align: center;
    border: 1px solid rgba(220, 220, 220, 1);

    .el-button {
        width: 80px;
        height: 30px;
        line-height: 0.5;
    }

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

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

    .el-row {
        height: 40px;
        display: flex;
        align-items: flex-start;

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

        .el-col-9 {
            padding-left: 30px;
        }
    }

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



    .content {
        position: absolute;
        left: 377px;
        top: 69px;
        width: 1220px;
        height: 836px;
        line-height: 20px;
        padding-left: 36px;
        background-color: rgba(255, 255, 255, 1);
        color: rgba(16, 16, 16, 1);
        font-size: 14px;
        border: 1px solid rgba(187, 187, 187, 1);
        display: flex;
        justify-content: flex-start;
        align-items: flex-start;

        .el-form-item {
            margin-bottom: 0px;
        }

        /deep/ .con_left {
            .el-con-5 {
                width: 43px;
            }
        }

        .con_left {
            width: 709px;
            position: relative;

            .con_left_footer {
                position: absolute;
                left: 499px;
                top: 777px;
            }
        }

        .con_right {
            width: 480px;

            .dialog-footer {
                .el-button {
                    width: 80px;
                    height: 30px;
                    line-height: 0.5;
                }
            }
        }
    }
}
</style>