<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">
                    <el-row :gutter="20">
                        <el-col :span="4">
                            <em>Mac:</em>
                            <el-input class="macContianer" v-model="panel.detectedmac" placeholder="请输入白名单Mac"></el-input>
                        </el-col>
                        <el-col :span="4">
                            <em>关键词:</em>
                            <el-input v-model="panel.keywords" placeholder="请输入关键词"></el-input>
                        </el-col>
                        <el-col :span="6">
                            <em>检测时间:</em>
                            <el-date-picker
                                    v-model="panel.time1"
                                    type="daterange"
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期">
                            </el-date-picker>
                        </el-col>
                        <el-col :span="4">
                            <el-button size="small" type="primary" @click="query">查询</el-button>
                        </el-col>
                    </el-row>
                </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=""
                stripe
        >
            <el-table-column
                    type="index"
                    label="序号"
                    width="60"
                    sortable
            >
            </el-table-column>
            <el-table-column
                    prop="Name"
                    label="设备"
                    width="420"
            >
            </el-table-column>
            <el-table-column
                    prop="Mac"
                    label="Mac地址"
                    width="160"
            >
            </el-table-column>

            <el-table-column
                    prop="Regionname"
                    label="位置"
                    width="260"
            >
            </el-table-column>
            <el-table-column
                    prop="CreateTime"
                    label="标注时间"
            >
                <template slot-scope="scope">
                    {{scope.row.CreateTime |fmtDate}}
                </template>
            </el-table-column>
            <el-table-column
                    prop="Usercode"
                    label="标注者"
            >
            </el-table-column>
            <el-table-column
                    prop="Memo"
                    label="备注"
            >
            </el-table-column>
            <el-table-column
                    prop="Id"
                    label="操作"
                    width="120"
            >
                <template slot-scope="scope">
                    <el-button size="small" type="primary" @click="remevoWhite(scope.row.Id)">移出白名单</el-button>
                </template>
            </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 {
        WhiteListQuery,
        RemoveFromWhiteList
    } from '../api/getApiRes.js'

    let qs = require('qs');
    export default {
        data() {
            return {
                tabIndex: 2,
                tabs: [
                    {name: '手机信号记录', url: 'phoneSign'},
                    {name: 'WiFi信号记录', url: 'wifiSign'},
                    {name: '白名单', url: 'white'},
                ],
                // panel 配置项目
                panel: {
                    usercode: '',
                    username: '',
                    compname: '',
                    keywords: '',
                    USERCODE: '',
                    detectedmac: '',
                    taskstatus: 99,
                    options: [
                        {value: 99, label: '全部'},
                        {value: 1, label: '进行中'},
                        {value: 2, label: '已完成'},
                    ],
                    detOptions: [],
                    detidstr: 0,
                    time1: globalBt3(2),
                },
                pageination: {
                    pageItem: 10,
                    pageoptions: pageOptions(),
                    total: 300,
                    pageIndex: 1,
                },
                draw: 1,
                start: 0,
                recordsTotal: 0,
                tableData: [],
                allTableData: [],
                limit: '10',
                multipleSort: false,
                loading: true,
                fileList: [],
                multipleSelection: [],
                detectedmac: '',
            }
        },
        mounted() {
            this.getTableQuery();
        },
        methods: {
            // 跳转tab页面
            goTab(url) {
                this.$router.push({path: url});
            },
            // 查询按钮
            query() {
                this.getTableQuery();
                this.$message.success('查询完毕');
            },
            // 页面数据查询
            getTableQuery() {
                // 获取探测器列表
                let that = this;
                that.loading = true;
                let param = {
                    token: localStorage.token,
                    detectedmac: that.panel.detectedmac,//被检测设备Mac
                    keywords: that.panel.keywords,//关键字
                    bt: globaltime2String(that.panel.time1[0]) + ' 00:00:01',//开始时间
                    et: globaltime2String(that.panel.time1[1]) + ' 23:59:59',//结束时间
                    start: 1,//
                    tableMax: 9999,//
                };
                let postdata = qs.stringify(param);
                WhiteListQuery(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        that.loading = false;
                        if (json.Rs) {
                            that.allTableData = json.Rs;
                            that.recordsTotal = json.Rs.length;
                        } else {
                            that.allTableData = [];
                            that.recordsTotal = 0;
                        }

                        // 设置分页数据
                        that.setPaginations();
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            // 设置分页数据
            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)
            },
            // 过滤金额
            filterMoney(value, row, column) {
                return parseFloat(column).toFixed(2);
            },
            filterSignal(val, row, column) {
                //127信号是无效强度状态
                return column = column == 127 ? '- -' : column;
            },
            remevoWhite(Id) {
                let that = this;
                this.$confirm('此操作将从白名单移出该设备, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let param = {
                        token: localStorage.token,
                        knowndeviceid : Id,
                    };
                    let postdata = qs.stringify(param);
                    RemoveFromWhiteList(postdata).then(res => {
                        let json = res;
                        if (json.Code == 0) {
                            that.$message({
                                showClose: true,
                                message: '白名单移出成功！',
                                type: 'success'
                            });
                            that.getTableQuery();
                        } else {
                            that.$message.error(json.Memo);
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        },
        filters: {
            // 过滤时间
            filterFmtDate(column) {
                return nonTfmtDate(column, 16);
            },
            fmtDate: function (value) {
                let res = '';
                if (!value) {
                    return '---';
                } else {
                    res = new Date(+new Date(value) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
                    return res;
                }
            }
        }
    }
</script>

<style scoped>
    @import "../assets/css/panel.css";

    .tabs ul {
        width: 492px;
        overflow: hidden;
        display: block;
        margin: 5px auto;
        list-style: none;
        padding-left: 0;
    }

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

    /deep/ .el-table__empty-text {
        color: #015B9E;
    }
    .panel /deep/ .macContianer .el-input__inner {
        width: 170px;
    }

</style>
