<template>
    <div class="context">
        <div class="panel">
            <el-page-header @back="goBack" content="绑定记录">
            </el-page-header>
            <h5></h5>
            <div class="panel-body" v-if="true">
                <div class="panel_control">
                    <el-row :gutter="20">
                        <!--<el-col :span="4">-->
                        <!--<em>心率带ID:</em>-->
                        <!--<el-input v-model="panel.hrId" placeholder="请输入心率带ID"></el-input>-->
                        <!--</el-col>-->
                        <el-col :span="7">
                            <em>日期:</em>
                            <el-date-picker
                                    v-model="panel.timeScope"
                                    type="daterange"
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期">
                            </el-date-picker>
                        </el-col>
                        <el-col :span="4">
                            <el-button size="" type="primary" @click="query" plain :disabled="serachBtnStatus">查询
                            </el-button>
                        </el-col>
                    </el-row>
                </div>
            </div>
        </div>
        <div class="table">
            <el-table
                    :data="tableData"
                    border
                    is-horizontal-resize
                    :default-sort="{prop: 'date', order: 'descending'}"
                    element-loading-background="rgba(0, 0, 0, 0.8)"
                    class=""
                    @selection-change="handleSelectionChange"
            >

                >
                <el-table-column
                        type="index"
                        label="序号"
                        align="center"

                        width="50">
                </el-table-column>
                <el-table-column
                        prop="UserName"
                        label="绑定用户"
                >
                </el-table-column>
                <el-table-column
                        prop="Sn"
                        label="序列号"
                >
                </el-table-column>
                <el-table-column
                        prop="ClassName"
                        label="课程"
                        sortable
                >
                    <template slot-scope="scope">
                        <span class="lessons"
                              :style="{ background:scope.row.ClassColor }">{{ scope.row.ClassName }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="Base.CreatedAt"
                        label="绑定时间"
                        width="180"
                        :formatter="filterFmtDate"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="Base.UpdatedAt"
                        label="解绑时间"
                        width="180"
                        :formatter="filterFmtDate"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="IsPrivate"
                        label="是否私有"
                        sortable
                >
                    <template slot-scope="scope">
                        <span v-if="scope.row.IsPrivate == 1">私有</span>
                        <span v-if="scope.row.IsPrivate == 2">公共</span>
                    </template>
                </el-table-column>
                <!--<el-table-column-->
                <!--prop="BindStatus"-->
                <!--label="绑定状态"-->
                <!--&gt;-->
                <!--<template slot-scope="scope">-->
                <!--<span v-if="scope.row.BindStatus == 1" style="color: greenyellow">绑定</span>-->
                <!--<span v-if="scope.row.BindStatus == 2" style="color:red;">解绑</span>-->
                <!--</template>-->
                <!--</el-table-column>-->
            </el-table>
            <br>
            <el-pagination
                    background
                    :total="pageination.total"
                    :page-size="pageination.pageItem"
                    @current-change="pageChange"
            ></el-pagination>
        </div>
    </div>
</template>

<script>
    import Global from '../Global.js'
    import {
        HrSensorsBindHistory,
    } from "../api/getApiRes";

    let qs = require('qs');
    export default {
        data() {
            return {
                serachBtnStatus: false,
                dialogVisible: false,//其他dialog
                dialogMemberVisible: false,//新增会员dialog
                dialogLesson: false,//课时调整
                dialogGift: false,//赠送课时调整
                dialogExpTime: false,//有效期调整
                dialogLessonTable: false,//会员课程
                dialogTitle: '新增会员',
                dialogValue: [],
                // panel 配置项目
                panel: {
                    hrId: this.$route.query.hrId,
                    phone: '',
                    classId: '',
                    usercode: '',
                    username: '',
                    timeScope: globalBt2(3),
                    compname: '',
                    keyword: '',
                    USERCODE: '',
                    endType: '',
                    taskstatus: 99,
                    draw: 1,
                    start: 0,
                    recordsTotal: 0,
                    tableData: [],
                    allTableData: [],
                    limit: '10',
                    multipleSort: false,
                    loading: false,
                    fileList: [],
                    multipleSelection: [],
                    detectedmac: '',
                    options: [],
                    time1: globalBt(),
                },
                multipleSelection: [],
                pageination: {
                    pageItem: 100,
                    pageoptions: pageOptions(),
                    total: 100,
                    pageIndex: 1,
                },
                form: {
                    name: '',
                    userCode: '',
                    shopId: '',
                    memberType: 1,
                    lesson: 1,
                    gift: 1,
                    btnType: 0,//0新建，1编辑编辑
                    memo: '',
                    expTime: '',
                    dialogdata: [],//穿梭待选
                    dialogValue: [],//穿梭已选
                },
                memberTypes: [
                    {value: 1, label: '年会员'},
                    {value: 2, label: '充值会员'},
                ],
                tableData: []
            }
        },
        mounted() {
            this.getTableQuery();
        },
        methods: {
            goBack(){
                this.$router.push({
                    path: '/heartEquip', query: {}
                });
            },
            // 表格合计
            getSummaries(param) {
                const {columns, data} = param;
                const sums = [];
                columns.forEach((column, index) => {
                    if (index === 0) {
                        sums[index] = '合计';
                        return;
                    }
                    const values = data.map(item => Number(item[column.property]));
                    if (!values.every(value => isNaN(value)) && index != 0 && index != 1 && index != 2) {
                        sums[index] = values.reduce((prev, curr) => {
                            const value = Number(curr);
                            if (!isNaN(value)) {
                                return prev + curr;
                            } else {
                                return prev;
                            }
                        }, 0);
                        sums[index] += '';
                        sums[index] = parseInt(sums[index]);
                    } else {
                        sums[index] = '';
                    }
                });
                return sums;
            },
            // 关闭所有
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 查询按钮
            query() {
                // 按钮倒计时
                let that = this;
                that.serachBtnStatus = true;
                let totalTime = 2;
                let clock = window.setInterval(() => {
                    totalTime--;
                    if (totalTime < 0) {
                        totalTime = 2;
                        that.serachBtnStatus = false;
                    }
                }, 1000);
                this.getTableQuery();
                this.$message.success('查询完毕');
            },
            clearForm() {
                // clear
                this.form.name = '';
                this.form.userCode = '';
                this.form.shopId = '';
            },
            // 页面数据查询
            getTableQuery() {
                console.log(123);
                let that = this;
                that.loading = true;
                let param = {
                    token: localStorage.token,
                    hrId: this.$route.query.hrId,
                    bt: nonTfmtDatetoLength(that.panel.timeScope[0], 10) + " 00:00:01",
                    et: nonTfmtDatetoLength(that.panel.timeScope[1], 10) + " 23:59:59",
                };
                let postdata = qs.stringify(param);
                HrSensorsBindHistory(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 + '错误码：' + json.Code);
                    }
                })
            },
            // 设置分页数据
            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)
            },
            // 过滤时间
            filterFmtDate(value, row, column) {
                let that = this;
                return nonTfmtDate(column, 11);
            },
        },
        watch: {
            $route(to) {
                if (to.name == 'bindRecord') {
                    this.getTableQuery();
                }
            },
        },
    }
</script>

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

    .context {
        height: 770px;

        overflow-y: scroll;
        display: block;
        margin: 0 auto;
        background-color: #fff !important;
        padding: 30px;
    }

    .panel-body {
        padding: 20px;
        background: #F0F2F5;
    }

    .change {
        width: 100%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
        padding-top: 10px;
        padding-bottom: 10px;
    }

    .change button {
        float: left;
    }

    .change button.pull-right {
        float: right;
    }

    .dialogTitle {
        width: 100%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
        color: #000000;
        font-size: 18px;
        text-align: center;
    }

    .dialogTitle em {
        float: none;
        font-style: normal;
        color: #3799FF;
        margin: 0;
    }

    /deep/ .el-transfer-panel__item .el-checkbox__input {
        left: 40px;
    }

    .dialogFooter {
        width: 90%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
        margin-top: 10px;
    }

    .dialogFooter button {
        float: right;
        margin-left: 10px;
    }

    .dialogContent {
        width: 100%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
    }

    .dialogContent .pull-left {
        width: 30%;
        float: left;
    }

    .dialogContent .pull-right {
        width: 70%;
        float: right;
    }

    .panel /deep/ .el-date-editor--daterange {
        /*width: 260px;*/
    }

    /deep/ .el-col-4 {
        width: 15% !important;
    }

    .panel /deep/ .el-input__inner {
        /*width: 130px;*/
    }

    .panel /deep/ .el-input {
        /*width: 130px;*/
    }

    .panel /deep/ .el-select {
        width: 130px;
    }

    /deep/ .el-date-editor .el-range-separator {
        line-height: 22px;
    }

    .lessons {
        padding: 1px 7px;
        border-radius: 250px;
        float: left;
        color: #000;
    }
</style>
