<template>
    <div class="content">
        <div class="tabs">
            <ul>
                <li v-for="(tab,i) in tabs" @click="goTab(tab.url)" :class="{'active':tabIndex == i}">
                    {{tab.name}}
                </li>
            </ul>
        </div>
        <div class="panel">
            <div class="panel-body">
                <div class=" panel_control">
                    <div class="pline">
                        <em>MAC地址:</em>
                        <el-input v-model="panel.mac" placeholder=""></el-input>
                    </div>
                    <div class="pline">
                        <em>设备类型 :</em>
                        <el-select v-model="panel.deviceType">
                            <el-option
                                    v-for="item in panel.deviceTypeOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </div>
                    <div class="pline">
                        <em>安全等级 :</em>
                        <el-select v-model="panel.level">
                            <el-option
                                    v-for="item in panel.levelOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </div>
                    <div class="pline">
                        <em>探测器 :</em>
                        <el-select v-model="panel.detectorId">
                            <el-option
                                    v-for="item in panel.detectorIdOptions"
                                    :key="item.Id"
                                    :label="item.TagName"
                                    :value="item.Id">
                            </el-option>
                        </el-select>
                    </div>
                    <div class="pline">
                        <em>时间范围</em>
                        <el-date-picker
                                v-model="panel.time1"
                                type="daterange"
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期">
                        </el-date-picker>
                    </div>
                    <div class="pline">
                        <el-button size="medium" type="primary" @click="query">查询</el-button>
                    </div>
                </div>
            </div>
        </div>
        <el-table
                :data="tableData"
                is-horizontal-resize
                :default-sort="{prop: 'date', order: 'descending'}"
                v-loading="loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                class=""
                @selection-change="handleSelectionChange"
                stripe
                ref="multipleTable"
        >
            <el-table-column
                    type="index"
                    label="序号"
                    width="60"
                    sortable
            >
            </el-table-column>
            <el-table-column
                    prop="Mac"
                    label="Mac"
            >
            </el-table-column>
            <el-table-column
                    prop="Oem"
                    label="品牌"
                    width="140"
                    align="center"
                    sortable
            >
                <template slot-scope="scope">
                    <el-tooltip class="item" effect="dark" :content="scope.row.Oem" placement="top-start">
                            <span v-if="scope.row.Oem">
                                    {{ scope.row.Oem.length > 9 ?  scope.row.Oem.substr(0,9)+'...' : scope.row.Oem }}
                            </span>
                        <span v-else>
                                未知
                            </span>

                    </el-tooltip>
                </template>
            </el-table-column>
            <el-table-column
                    prop="DetectorTag"
                    label="探测器"
                    sortable
            >
            </el-table-column>
            <el-table-column
                    prop="DeviceType"
                    label="设备类型"
                    :formatter="filterType"
                    sortable
            >
            </el-table-column>
            <el-table-column
                    prop="BeginTs"
                    label="开始探测"
                    width="180"
                    :formatter="filterTime"
                    sortable
            >
            </el-table-column>
            <el-table-column
                    prop="EndTs"
                    label="结束探测"
                    width="180"
                    :formatter="filterTime"
                    sortable
            >
            </el-table-column>
            <el-table-column
                    prop="Down"
                    label="下载"
                    sortable
            >
            </el-table-column>
            <el-table-column
                    prop="Up"
                    label="上传"
                    sortable
            >
            </el-table-column>
            <el-table-column
                    prop="Level"
                    label="安全等级"
                    sortable
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.Level == 1">安全</span>
                    <span v-if="scope.row.Level == 2" class="yellow">可疑</span>
                    <span v-if="scope.row.Level == 3" class="red">可疑</span>
                    <span v-if="scope.row.Level == 4" class="red">危险</span>
                </template>
            </el-table-column>
            <el-table-column
                    prop="Rssi"
                    label="Rssi"
                    sortable
            >
            </el-table-column>
            <el-table-column
                    prop="Ssid"
                    label="Ssid"
                    sortable
            >
                <template slot-scope="scope">
                    <el-tooltip class="item" effect="dark" :content="scope.row.Ssid" placement="top-start">
                            <span v-if="scope.row.Ssid">
                                    {{ scope.row.Ssid.length > 9 ?  scope.row.Ssid.substr(0,9)+'...' : scope.row.Ssid }}
                            </span>
                        <span v-else>
                                未知
                            </span>

                    </el-tooltip>
                </template>
            </el-table-column>
            <el-table-column
                    prop="StaCount"
                    label="连接设备数"
                    sortable
            >
            </el-table-column>
        </el-table>
        <br>
        <el-pagination
                background
                :total="pageination.total"
                :page-size="pageination.pageItem"
                @current-change="pageChange"
        ></el-pagination>
    </div>
</template>

<script>
    import Global from '../Global.js'
    import {
        DetectorList,
        DetectorLogQuery,
    } from '../api/getApiRes.js'

    let qs = require('qs');
    export default {
        data() {
            return {
                tabIndex: 0,
                tabs: [
                    {name: '探测记录管理', url: 'record'},
                ],
                // panel 配置项目
                panel: {
                    mac: '',
                    deviceType: '',
                    level:'',
                    detectorId: '',
                    time1: globalBt3(1),
                    deviceTypeOptions: AllTypeOption(),
                    levelOptions: levelOptions(),
                    detectorIdOptions: [],
                },
                pageination: {
                    pageItem: 100,
                    pageoptions: pageOptions(),
                    total: 500,
                    pageIndex: 1,
                },
                draw: 1,
                start: 0,
                recordsTotal: 0,
                tableData: [],
                allTableData: [],
                limit: '10',
                multipleSort: false,
                loading: true,
                fileList: [],
                multipleSelection: [],
            }
        },
        mounted() {
            this.getTableQuery();
        },
        methods: {
            // 页面数据查询
            getTableQuery() {
                let that = this;
                that.loading = true;
                // 拉取探测器列表
                this.getDectorSelect();
                let bt = parseInt(new Date(that.panel.time1[0]).getTime() / 1000);
                let et = parseInt(new Date(that.panel.time1[1]).getTime() / 1000);
                let param = {
                    token: localStorage.token,
                    detectorId:this.panel.detectorId,//
                    mac: this.panel.mac,//
                    deviceType: that.panel.deviceType,//
                    level: that.panel.level,//标签名
                    bt: bt,//
                    et: et,//
                    limit: this.start + 100,//
                    offset: this.start,
                };
                let postdata = qs.stringify(param);
                DetectorLogQuery(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        that.loading = false;
                        if (json.Rs) {
                            that.allTableData = json.Rs;
                            that.recordsTotal = json.AllCount;
                        } else {
                            that.allTableData = [];
                            that.recordsTotal = 0;
                        }
                        // 设置分页数据
                        that.setPaginations();
                    } else {
                        that.$message.error(json.Memo + '，错误代码：' + json.Code);
                    }
                })
            },
            // 获取探测器列表
            getDectorSelect() {
                let that = this;
                let param = {
                    token: localStorage.token,
                };
                let postdata = qs.stringify(param);
                DetectorList(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        if (!json.Rs) {
                            that.$message.error('探测器列表为空，请先添加有效探测器');
                            return false
                        }
                        that.panel.detectorIdOptions = json.Rs;
                        that.panel.detectorIdOptions.unshift({Id: '', TagName: "全部", DisplayName: "全部"});
                    } else {
                        that.$message.error(json.Memo + '，错误代码：' + json.Code);
                    }
                })
            },
            // 查询按钮
            query() {
                this.draw = 1;
                this.start = 0;
                this.pageination.pageIndex = 1;
                this.getTableQuery();
                this.$message.success('查询完毕');
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 设置分页数据
            setPaginations() {
                // 分页属性
                let that = this;
                that.pageination.total = that.recordsTotal;
                // 默认分页
                that.tableData = that.allTableData.filter((item, index) => {
                    return index < that.pageination.pageItem;
                });
            },
            // 每页显示数量
            handleSizeChange() {
                let that = this;
                that.tableData = that.allTableData.filter((item, index) => {
                    return index < that.pageination.pageItem;
                });
                that.draw = that.pageination.pageItem;
                // that.getTableQuery();
            },
            // 翻页
            pageChange(pageIndex) {
                let that = this;
                // 获取当前页
                let index = that.pageination.pageItem * (pageIndex - 1);
                // 数据总数
                let nums = that.pageination.pageItem * pageIndex;
                // 容器
                let tables = [];
                for (var i = index; i < nums; i++) {
                    if (that.allTableData[i]) {
                        tables.push(that.allTableData[i])
                    }
                    this.tableData = tables;
                }
                that.start = index * that.draw;
                that.getTableQuery();
            },
            // 自动排序
            sortChange(params) {
                console.log(params)
            },
            filterTime(val, row, column) {
                return nonTfmtDate(column, 10);
            },
            // 过滤设备类型
            filterType(value, row, column) {
                return filterAllType(column);
            },
        },
    }
</script>

<style scoped>
    @import "../assets/css/panel.css";
    .tabs ul {
        width: 168px;
        float: left;
    }

    table span {
        cursor: pointer;
    }

    .down_btn {
        float: left;
    }

    .content {
        width: 98%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
        padding-left: 20px;
    }

    .selectStyle {
        width: 100%;
        color: #005EA2;
    }

    .selectStyle ::placeholder {
        color: #005EA2;
    }

    /deep/ .modal .el-select .el-input .el-select__caret {
        position: relative;
        top: 35px;
    }

    .imgContianer {
        width: 100%;
        height: 340px;
        overflow: hidden;
        display: block;
        margin: 0 auto;
    }

    .imgContianer img {
        width: 100%;
        height: 100%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
    }

    .pointers {
        position: relative;
        bottom: 340px;
        width: 100%;
        height: 100%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
    }

    .imgContianer i {
        position: relative;
        width: 10px;
        height: 10px;
        float: left;
        border-radius: 250px;
        background: #FFDD00;
    }

    /deep/ .modal-dialog {
        height: 80px;
        margin-top: 20px;
    }

    /deep/ textarea {
        color: #6DC1FF;
    }

    /deep/ .el-table__empty-text {
        color: #015B9E;
    }

    /deep/ .el-input.is-disabled .el-input__inner {
        background-color: #015B9E;
    }

    .red {
        color: red;
    }

    .yellow {
        color: #fd0;
    }
</style>
