<template>
    <div>
        <el-form :inline="true" class="demo-form-inline">
            <el-form-item>
                <Area @click="handleNodeClick" ref="area"></Area>
            </el-form-item>

            <el-form-item label="终端名称:">
                <el-input v-model="name" placeholder="请输入终端名称"></el-input>
            </el-form-item>

            <el-form-item label="设备状态:">
                <el-select v-model="equipmentStatus" placeholder="请选择设备状态">
                    <el-option
                        v-for="item in equipmentStatusData"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="运行状态:">
                <el-select v-model="runningState" placeholder="请选择运行状态">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="在线" value="1"></el-option>
                    <el-option label="离线" value="0"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" class="ml10" icon="el-icon-search" @click="researchFn">查询</el-button>
                <el-button type="primary" icon="el-icon-refresh" @click="Reset">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table
            stripe
            :data="communicationData.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
            :height="height"
            border
            style="width: 100%"
            v-loading="isLoading"
            header-cell-class-name="table_header"
            :row-class-name="rowClassName"
        >
            <el-table-column type="index" width="80" label="序号" align="center">
                <template scope="scope">
                    <span>{{ (currentPage - 1) * pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <template v-for="(item, index) in tableHeaderCommunication">
                <el-table-column
                    :prop="item.prop"
                    :label="item.label"
                    :key="index"
                    :min-width="item.width"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
            </template>
            <el-table-column prop="runStatus" label="运行状态" align="center">
                <template scope="scope">
                    <span>{{ scope.row.runStatus == 1 ? '在线' : '离线' }}</span>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            align="right"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[20, 30, 40, 50, 100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="communicationData.length"
            class="mt10"
        ></el-pagination>
    </div>
</template>

<script>
import { dicData } from '@/assets/commjs/dictypeData'
import Area from '@/views/components/Area/Area.vue'
import { getRStatDevSignalList } from '@/api/Management-operation/mochaITOM'
export default {
    components: { Area },
    data() {
        return {
            id: JSON.parse(window.sessionStorage.getItem('Info')).deptId,
            vh: '',
            height: null,
            isLoading: false,
            name: '', //终端名称
            equipmentStatus: '', //设备状态
            runningState: '', //运行状态
            //设备状态数据
            equipmentStatusData: [],
            //在线状态数据
            runningStateData: [],

            //全网水平评价表头
            tableHeaderCommunication: [
                { prop: 'orgName', label: '单位', width: '220' },
                { prop: 'terminalId', label: '终端编号', width: '240' },
                { prop: 'terminalName', label: '终端名称' , width: '200' },
                { prop: 'deviceStatus', label: '设备状态' },
                // { prop: "runStatus", label: "运行状态" },
                { prop: 'ip', label: 'IP地址',width: '120'  },
                { prop: 'port', label: '端口' },
                { prop: 'updateTime', label: '最后通讯时间', width: '200'  }
            ],
            communicationData: [],
            device: '',
            currentPage: 1, // 当前页码
            pageSize: 20 // 每页的数据条数
        }
    },
    created() {
        this.equipmentStatusData = dicData('Dev_Status', [], 1)

        this.runningStateData = dicData('Run_Status', [], 1)
        this.communication()
    },
    mounted() {
        this.setHeight()
        window.addEventListener('resize', this.setHeight)

        // this.device = window.devicePixelRatio;
        this.height = window.sessionStorage.getItem('appheight') - 165
    },
    beforeDestroy() {
        window.removeEventListener('resize', this.setHeight)
    },
    methods: {
        setHeight() {
            this.height = window.sessionStorage.getItem('appheight') - 165
        },
        rowClassName({ row, rowIndex }) {
            row.number = rowIndex + 1
        },
        //获取主网运行指标统计数据
        communication() {
            this.isLoading = true
            getRStatDevSignalList({
                deviceName: this.name,
                deviceStatus: this.equipmentStatus,
                id: this.id,
                runStatus: this.runningState
            }).then(res => {
                this.isLoading = false
                this.communicationData = res.data
                res.data.forEach(e => {
                    this.equipmentStatusData.forEach(i => {
                        if (i.id == e.deviceStatus) {
                            e.deviceStatus = i.name
                        }
                    })
                })
                res.data.forEach(e => {
                    this.runningStateData.forEach(i => {
                        if (i.id == e.runStatus) {
                            e.runStatus = i.name
                        }
                    })
                })
            })
        },
        // 切换选项
        handleNodeClick(data) {
            this.id = data.id
        },
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.pageSize = val
            // this.communication();
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.currentPage = val
            // this.communication();
        },
        //查询
        researchFn() {
            this.communication()
        },
        //重置
        Reset() {
            this.$refs.area.form.valueTitle = JSON.parse(window.sessionStorage.getItem('Info')).deptName
            this.name = ''
            this.equipmentStatus = ''
            this.runningState = ''
        }
    }
}
</script>
<style lang="less" scoped>
@import url('../../../../styles/comStyle.less');
</style>
