<template>
    <div class="context">
        <div class="panel">
            <h5>区域管理</h5>
        </div>
        <div class="change">
            <el-button @click="addMember" type="primary" size="mini">新增区域</el-button>
            <el-button @click="editList" size="mini">编辑区域</el-button>
            <!--<el-button @click="delList" type="danger">删除区域</el-button>-->
        </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" @current-change="clickChange"
            >
                >
                <el-table-column label="选择" width="55">
                    <template slot-scope="scope">
                        <el-radio v-model="tableRadio" :label="scope.row"><i></i></el-radio>
                    </template>
                </el-table-column>
                <el-table-column label="序号" type="index" width="50" align="center">
                    <template scope="scope">
                        <span>{{(start) + scope.$index + 1}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="Name"
                        label="区域名称"
                        width="240">
                </el-table-column>
                <el-table-column
                        prop="Base.CreatedAt"
                        label="创建日期"
                        :formatter="filterFmtDate"
                        width="180"
                        sortable
                >
                </el-table-column>
                <!--<el-table-column-->
                <!--prop="Status"-->
                <!--label="状态"-->
                <!--width="180">-->
                <!--&lt;!&ndash;1：启用，8：暂停，9：删除&ndash;&gt;-->
                <!--<template slot-scope="scope">-->
                <!--<span v-if="scope.row.Status == 1" style="color: rgb(103, 194, 58)">已启用</span>-->
                <!--<span v-if="scope.row.Status == 8" style="color: #E6A23C">已暂停</span>-->
                <!--<span v-if="scope.row.Status == 9" style="color: red">已删除</span>-->
                <!--</template>-->
                <!--</el-table-column>-->
                <el-table-column
                        prop="id"
                        label="操作"
                >
                    <template slot-scope="scope">
                        <!--<el-button type="success" v-if="scope.row.Status == 8" size="mini"-->
                        <!--@click="pauseRow(scope.row,1)">-->
                        <!--启用-->
                        <!--</el-button>-->
                        <!--<el-button type="warning" v-if="scope.row.Status == 1" size="mini"-->
                        <!--@click="pauseRow(scope.row,8)">-->
                        <!--暂停-->
                        <!--</el-button>-->
                        <el-button type="danger" size="mini"
                                   @click="pauseRowDel(scope.row)">
                            删除
                        </el-button>
                        <el-button type="text" @click="goPage(scope.row)">设备管理</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>

        <el-dialog :title="dialogTitle" :visible.sync="dialogMemberVisible" width="650px">
            <div class="dialogContent">
                <div class="">
                    <el-form ref="form" :model="form" label-width="160px">
                        <el-form-item label="区域名称">
                            <el-input v-model="form.name"></el-input>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <div class="dialogFooter">
                <el-button type="primary" size="small" v-if="form.btnType == 0" @click="confirmMember">确定</el-button>
                <el-button type="primary" size="small" v-if="form.btnType == 1" @click="confirmEditMember">确定
                </el-button>
                <el-button size="small" @click="dialogMemberVisible = false">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import Global from '../../Global.js'
    import {
        testTable,
        testSelect,
        QueryShopVenue,
        AddShopVenue,
        EditShopVenue,
        ShopVenueStatusEdit,
    } from "../../api/getApiRes";

    let qs = require('qs');
    export default {
        data() {
            return {
                dialogVisible: false,//其他dialog
                dialogMemberVisible: false,//新增区域dialog
                dialogLesson: false,//课时调整
                dialogGift: false,//赠送课时调整
                dialogExpTime: false,//有效期调整
                dialogLessonTable: false,//区域课程
                dialogTitle: '新增区域',
                dialogValue: [],
                tableRadio: [],
                start: 0,
                draw: 1,
                // panel 配置项目
                panel: {
                    name: '',
                    phone: '',
                    compname: '',
                    keyword: '',
                    USERCODE: '',
                    endType: '',
                    taskstatus: 99,
                    draw: 1,
                    start: 0,
                    recordsTotal: 0,
                    tableData: [],
                    allTableData: [],
                    limit: '10',
                    multipleSort: false,
                    loading: false,
                    fileList: [],
                    multipleSelection: [],
                    detectedmac: '',
                    options: [
                        {value: 99, label: '全部'},
                        {value: 1, label: '进行中'},
                        {value: 2, label: '已完成'},
                    ],
                    endTypeOptions: [
                        {value: 99, label: '全部'},
                        {value: 30, label: '近一个月'},
                        {value: 7, label: '近一周'},
                        {value: 1, label: '当日'},
                    ],
                    time1: globalBt(),
                },
                multipleSelection: [],
                pageination: {
                    pageItem: 10,
                    pageoptions: pageOptions(),
                    total: 100,
                    pageIndex: 1,
                },
                form: {
                    name: '',
                    userCode: '',
                    shopId: '',
                    teacherId: 0,
                    memberType: 1,
                    lesson: 1,
                    gift: 1,
                    btnType: 0,//0新建，1编辑编辑
                    memo: '',
                    phone: '',
                    expTime: '',
                    dialogdata: [],//穿梭待选
                    dialogValue: [],//穿梭已选
                },
                memberTypes: [
                    {value: 1, label: '年区域'},
                    {value: 2, label: '充值区域'},
                ],
                tableData: []
            }
        },
        mounted() {
            this.getTableQuery();
        },
        methods: {
            // 设备回收
            // takeBack(row) {
            //     let that = this;
            //     this.$confirm('是否回收用户' + row.name + '的区域？', '回收操作', {
            //         confirmButtonText: '确定',
            //         cancelButtonText: '取消',
            //         type: 'warning'
            //     }).then(() => {
            //         let param = {
            //             token: localStorage.token,
            //             Id: row.Id,
            //             status: 0,
            //         };
            //         let postdata = qs.stringify(param);
            //         testTable(postdata).then(res => {
            //             let json = res;
            //             if (json.Code == 0) {
            //                 that.$message({
            //                     showClose: true,
            //                     message: row.name + '回收成功！',
            //                     type: 'success'
            //                 });
            //                 // table 重载
            //                 that.getTableQuery();
            //             } else {
            //                 that.$message.error(json.Memo + ' 错误码：' + json.Code);
            //             }
            //         })
            //     }).catch(() => {
            //         this.$message({
            //             type: 'info',
            //             message: '已取消回收'
            //         });
            //     });
            // },
            // 删除
            pauseRowDel(row) {
                let that = this;
                let param = {
                    key: localStorage.ServiceKey,
                    shopId: localStorage.ServiceId,
                    svId: row.SvId,
                    status: 9,// 1：启用 8：暂停 9：删除
                };
                let postdata = qs.stringify(param);
                this.$confirm('此操作将永久删除该区域设备, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    ShopVenueStatusEdit(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 + ' 错误码：' + json.Code);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 禁用
            pauseRow(row, status) {
                let that = this;
                let param = {
                    key: localStorage.ServiceKey,
                    shopId: localStorage.ServiceId,
                    svId: row.SvId,
                    status: status,
                };
                let postdata = qs.stringify(param);
                ShopVenueStatusEdit(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        that.$message({
                            showClose: true,
                            message: '执行成功！',
                            type: 'success'
                        });
                        // table 重载
                        that.getTableQuery();
                    } else {
                        that.$message.error(json.Memo + ' 错误码：' + json.Code);
                    }
                })
            },
            goPage(row) {
                this.$router.push({
                    path: '/heartLog', query: {
                        name: row.Name,
                        svId: row.SvId
                    }
                });
            },
            // 编辑
            editList() {
                let that = this;
                this.clearForm();
                console.log(this.tableRadio);
                if (this.tableRadio.length == 0) {
                    this.$message.error("请先选中一条记录");
                    return false
                }
                let row = this.tableRadio;
                this.form.shopid = row.ShopId;
                this.form.userId = row.Id;
                this.form.name = row.Name;
                this.form.userCode = row.Usercode;
                this.form.password = '';
                this.form.phone = row.Phone;
                this.form.memo = row.Memo;
                this.form.svId = row.SvId;
                this.form.adminType = row.AdminType;
                this.dialogVisible = true;
                this.dialogTitle = '编辑区域';
                this.form.btnType = 1;
                this.dialogMemberVisible = true
            },
            clickChange(item) {
                this.tableRadio = item
            },
            // 选择课程后，获取当前课程的建议上课人数和课时消耗
            getRowTop(row) {
                let that = this;
                console.log(row);
                let param = {
                    token: localStorage.token,
                    classId: row.ClassId,
                };
                let postdata = qs.stringify(param);
                ClassDetailOne(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        row.TopLimit = json.Rs.TopLimit;
                        row.ConsumeHour = json.Rs.ConsumeHour;
                    } else {
                        that.$message.error(json.Memo + ' 错误码：' + json.Code);
                    }
                })
            },
            // 编辑
            editMember(row) {
                this.clearForm();
                this.form.name = row.Name;
                this.form.phone = row.Phone;
                this.form.shopId = row.ShopId;
                this.form.teacherId = row.TeacherId;
                this.form.btnType = 1;
                this.dialogMemberVisible = true;
                this.dialogTitle = '编辑区域'
            },
            // 禁用
            // pauseRow(row) {
            //     let that = this;
            //     this.$confirm('是否禁用用户' + row.name + '？', '禁用操作', {
            //         confirmButtonText: '确定',
            //         cancelButtonText: '取消',
            //         type: 'warning'
            //     }).then(() => {
            //         let param = {
            //             token: localStorage.token,
            //             Id: row.Id,
            //             status: 0,
            //         };
            //         let postdata = qs.stringify(param);
            //         testTable(postdata).then(res => {
            //             let json = res;
            //             if (json.Code == 0) {
            //                 that.$message({
            //                     showClose: true,
            //                     message: row.name + '禁用成功！',
            //                     type: 'success'
            //                 });
            //                 // table 重载
            //                 that.getTableQuery();
            //             } else {
            //                 that.$message.error(json.Memo + ' 错误码：' + json.Code);
            //             }
            //         })
            //     }).catch(() => {
            //         this.$message({
            //             type: 'info',
            //             message: '已取消禁用'
            //         });
            //     });
            // },
            // 启用
            // runRow(row) {
            //     let that = this;
            //     this.$confirm('是否启用用户' + row.name + '？', '启用操作', {
            //         confirmButtonText: '确定',
            //         cancelButtonText: '取消',
            //         type: 'warning'
            //     }).then(() => {
            //         let param = {
            //             token: localStorage.token,
            //             Id: row.Id,
            //             status: 1,
            //         };
            //         let postdata = qs.stringify(param);
            //         testTable(postdata).then(res => {
            //             let json = res;
            //             if (json.Code == 0) {
            //                 that.$message({
            //                     showClose: true,
            //                     message: row.name + '启用成功！',
            //                     type: 'success'
            //                 });
            //                 // table 重载
            //                 that.getTableQuery();
            //             } else {
            //                 that.$message.error(json.Memo + ' 错误码：' + json.Code);
            //             }
            //         })
            //     }).catch(() => {
            //         this.$message({
            //             type: 'info',
            //             message: '已取消启用'
            //         });
            //     });
            // },
            // 关闭所有
            allDialogClose() {
                this.dialogVisible = false;
                this.dialogGift = false;
                this.dialogLesson = false;
                this.dialogExpTime = false;
                this.dialogLessonTable = false;
            },
            // 确认提交新增区域
            confirmMember() {
                let that = this;
                // checkNum
                if (!that.form.name) {
                    this.$message.error('错了哦，区域名称不能为空');
                    return false
                }
                if (that.form.name.length > 16) {
                    this.$message.error('错了哦，区域名称不能超过16个字符');
                    return false
                }

                let param = {
                    key: localStorage.ServiceKey,
                    shopId: localStorage.ServiceId,
                    name: this.form.name
                };
                let postdata = qs.stringify(param);
                AddShopVenue(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        // 关闭弹窗
                        that.dialogMemberVisible = false;
                        // 重载列表
                        that.getTableQuery();
                        that.$message({
                            showClose: true,
                            message: '区域添加成功！',
                            type: 'success'
                        });
                    } else {
                        that.$message.error(json.Memo + ' 错误码：' + json.Code);
                    }
                })
            },
            confirmEditMember() {
                let that = this;
                // checkNum
                if (!that.form.name) {
                    this.$message.error('错了哦，区域名不能为空');
                    return false
                }
                if (that.form.name.length > 16) {
                    this.$message.error('错了哦，区域名字数超过16个字');
                    return false
                }
                let param = {
                    key: localStorage.ServiceKey,
                    shopId: localStorage.ServiceId,
                    svId: this.form.svId,
                    name: this.form.name,
                };
                let postdata = qs.stringify(param);
                EditShopVenue(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        // 关闭弹窗
                        that.dialogMemberVisible = false;
                        // 重载列表
                        that.getTableQuery();
                        that.$message({
                            showClose: true,
                            message: '区域信息编辑成功！',
                            type: 'success'
                        });
                    } else {
                        that.$message.error(json.Memo + ' 错误码：' + json.Code);
                    }
                })
            },
            // 确认提交课时
            confirmLesson() {
                let that = this;
                // checkNum

                let param = {
                    token: localStorage.token,
                    userCode: that.form.userCode,
                    lesson: that.form.lesson,
                };
                let postdata = qs.stringify(param);
                testSelect(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        // 关闭弹窗
                        that.dialogVisible = false;
                        // 重载列表
                        that.getTableQuery();
                        that.$message({
                            showClose: true,
                            message: '课时调整成功！',
                            type: 'success'
                        });
                    } else {
                        that.$message.error(json.Memo + ' 错误码：' + json.Code);
                    }
                })
            },
            // 新增区域
            addMember() {
                this.clearForm();
                this.dialogMemberVisible = true;
                this.btnType = 0;
                this.dialogTitle = '新增区域'
            },
            // 删除
            delList() {
                let that = this;
                if (this.tableRadio.length == 0) {
                    this.$message.error("请先选中一条记录");
                    return false
                }
                let param = {
                    key: localStorage.ServiceKey,
                    shopId: localStorage.ServiceId,
                    svId: this.tableRadio.SvId,
                    status: 9,// 1：启用 8：暂停 9：删除
                };
                let postdata = qs.stringify(param);

                this.$confirm('此操作将永久删除该区域, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    ShopVenueStatusEdit(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 + ' 错误码：' + json.Code);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 查询按钮
            query() {
                this.getTableQuery();
                this.$message.success('查询完毕');
            },
            clearForm() {
                // clear
                this.form.name = '';
                this.form.svId = '';
                this.form.phone = '';
                this.form.userCode = '';
                this.form.shopId = '';
            },
            // 页面数据查询
            getTableQuery() {
                let that = this;
                that.loading = true;
                let param = {
                    key: localStorage.ServiceKey,
                    shopId: localStorage.ServiceId,
                };
                let postdata = qs.stringify(param);
                QueryShopVenue(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 {
                        if (!localStorage.ServiceKey) {
                            that.$message.error('还未与心率系统对接，请联系管理员');
                        } 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);
            },
        },
    }
</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;
    }
</style>
