<template>
    <div class="context">
        <div class="panel">
            <h5>课程管理</h5>
        </div>
        <div class="change">
            <el-button type="primary" @click="addLesson">新增课程</el-button>
            <el-button @click="lessonStudenChange">增删课程会员</el-button>
            <el-button @click="delList">删除</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)"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="课程名称"
                >
                </el-table-column>
                <el-table-column
                        prop="vipType"
                        label="是否通用"
                        width="110"
                        align="center"
                        sortable
                >
                    <template slot-scope="scope">
                        <span v-if="scope.row.vipType == 1">是</span>
                        <span v-if="scope.row.vipType == 0">否</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="Recovered"
                        label="消耗课时"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="Recovered"
                        label="建议上课人数"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="Recovered"
                        label="课程会员人数"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="wxVisible"
                        label="微信可见"
                >
                    <template slot-scope="scope">
                        <el-switch
                                v-model="scope.row.wxVisible"
                                active-value="1"
                                inactive-value="0"
                                active-color="#409EFF"
                                inactive-color="#D9D9D9"
                                @change=changeWechat($event,scope.row)
                        >
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="color"
                        label="课程颜色"
                        width="110"
                >
                    <template slot-scope="scope">
                        <el-color-picker v-model="scope.row.color"
                                         @change="changeColor(scope.row.color,scope.row)"></el-color-picker>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="create"
                        label="创建时间"
                        width="210"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="备注">
                    <template slot-scope="scope">
                        <el-popover
                                placement="top"
                                title="标题"
                                width="200"
                                trigger="hover"
                                :content="scope.row.name">
                            <span slot="reference"
                                  v-if="scope.row.name.length > 6">{{scope.row.name.substr(0,6)}} ....</span>
                        </el-popover>
                        <span v-if="scope.row.name.length <= 6">{{scope.row.name}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="Status"
                        label="操作"
                >
                    <template slot-scope="scope">
                        <el-button type="text" @click="editLesson(scope.row)">修改</el-button>
                        <el-button type="text" @click="Lessonmember(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="新增课程" :visible.sync="dialogLesson" width="1200px">
            <div>
                <div class="pull-left">
                    <el-form ref="form" :model="form" label-width="160px">
                        <el-form-item label="课程名称">
                            <el-input v-model="form.currName"></el-input>
                        </el-form-item>
                        <el-form-item label="消耗课时">
                            <el-input-number v-model="form.consumeHour" :min="0" :max="99999"
                                             label="（天）"></el-input-number>
                        </el-form-item>
                        <el-form-item label="建议上课人数">
                            <el-input-number v-model="form.topLimit" :min="0" :max="99999"
                                             label="（天）"></el-input-number>
                        </el-form-item>
                        <el-form-item label="微信可见">
                            <el-switch
                                    v-model="form.wxvisible"
                                    active-value="1"
                                    inactive-value="0"
                                    active-color="#409EFF"
                                    inactive-color="#D9D9D9">
                            </el-switch>
                        </el-form-item>
                        <el-form-item label="是否通用">
                            <el-switch
                                    v-model="form.wxvisible"
                                    active-value="1"
                                    inactive-value="0"
                                    active-color="#409EFF"
                                    inactive-color="#D9D9D9">
                            </el-switch>
                        </el-form-item>
                        <el-form-item label="课程颜色">
                            <el-color-picker v-model="form.currColor"></el-color-picker>
                        </el-form-item>
                        <el-form-item label="备注">
                            <el-input v-model="form.memo"></el-input>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="pull-right">
                    <el-form ref="form" :model="form" label-width="160px">
                        <el-form-item label="课程会员人数">
                            <el-transfer  filterable v-model="form.dialogValue" :data="form.dialogdata"></el-transfer>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <div class="dialogFooter">
                <el-button type="primary" size="small" @click="confirmAddLesson">确定</el-button>
                <el-button size="small" @click="dialogLesson = false">取消</el-button>
            </div>
        </el-dialog>

        <el-dialog title="增删课程会员" :visible.sync="dialogVisible">
            <div class="dialogTitle">
                <span>基础功能</span>
            </div>
            <div>
                <el-transfer  filterable  v-model="dialogValue" :data="form.dialogdata"></el-transfer>
            </div>
            <div class="dialogFooter">
                <el-button type="primary" size="small">确定</el-button>
                <el-button @click=" dialogVisible = false" size="small">取消</el-button>
            </div>
        </el-dialog>

        <el-dialog title="课程会员" :visible.sync="dialogTableVisible">
            <el-table :data="gridData">
                <el-table-column property="name" label="姓名" width="200"></el-table-column>
                <el-table-column property="userCode" label="手机"></el-table-column>
                <el-table-column property="vipType" label="会员类型">
                    <template slot-scope="scope">
                        <span v-if="scope.row.vipType == 1">年费会员</span>
                        <span v-if="scope.row.vipType == 2">充值会员</span>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>

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

    let qs = require('qs');
    export default {
        data() {
            return {
                dialogLesson: false,//新增课程
                dialogVisible: false,
                dialogTableVisible: false,
                dialogdata: [],
                gridData: [],
                dialogValue: [],
                // panel 配置项目
                panel: {
                    usercode: '',
                    username: '',
                    compname: '',
                    keyword: '',
                    USERCODE: '',
                    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: '已完成'},
                    ],
                    time1: globalBt(),
                },
                form: {
                    currName: "",
                    consumeHour: 0,
                    topLimit: 0,
                    wxvisible: 1,
                    currColor: "",
                    memo: "",
                    dialogdata: [],//穿梭待选
                    dialogValue: [],//穿梭已选
                },
                multipleSelection: [],
                pageination: {
                    pageItem: 100,
                    pageoptions: pageOptions(),
                    total: 100,
                    pageIndex: 1,
                },
                tableData: []
            }
        },
        mounted() {
            this.panelSelect();
            this.getTableQuery();
        },
        methods: {
            // 确认添加课程
            confirmAddLesson() {
                let that = this;

                // checkNum
                if (!that.form.currName) {
                    this.$message.error('错了哦，课程名称不能为空');
                    return false
                }
                if (that.form.currName.length > 20) {
                    this.$message.error('错了哦，课程名称字数超过20个字');
                    return false
                }
                if (that.form.memo) {
                    if (that.form.memo.length > 200) {
                        this.$message.error('错了哦，备注字数超过200个字');
                        return false
                    }
                }

                let param = {
                    token: localStorage.token,
                    currName: that.form.currName,
                    consumeHour: that.form.consumeHour,
                    topLimit: that.form.topLimit,
                    wxvisible: that.form.wxvisible,
                    currColor: that.form.currColor,
                    memo: that.form.memo,
                };
                let postdata = qs.stringify(param);
                testSelect(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        // 关闭弹窗
                        that.dialogLesson = false;
                        // 重载列表
                        that.getTableQuery();
                        that.$message({
                            showClose: true,
                            message: '会员课程调整成功！',
                            type: 'success'
                        });
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            // 新增课程
            addLesson() {
                // clear
                this.form.currName = '';
                this.form.consumeHour = '';
                this.form.topLimit = '';
                this.form.wxvisible = 1;
                this.form.currColor = "";
                this.form.memo = "";
                this.form.dialogValue = "";

                this.dialogLesson = true;
            },
            // 改色
            changeColor(c, row) {
                let that = this;
                let param = {
                    token: localStorage.token,
                    id: row.id,//
                    color: c,//
                };
                let postdata = qs.stringify(param);
                testTable(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);
                    }
                })
            },
            // 删除
            delList() {
                let that = this;
                // checkNum
                if (!this.multipleSelection.length) {
                    that.$message({
                        showClose: true,
                        message: '错了哦，需要先选中至少一条记录',
                        type: 'error'
                    });
                    return false
                }
                if (this.multipleSelection.length != 1) {
                    that.$message({
                        showClose: true,
                        message: '错了哦，只能选中一条记录',
                        type: 'error'
                    });
                    return false
                }

                let detectorid = that.multipleSelection[0].Id;

                let param = {
                    token: localStorage.token,
                    detectorid: detectorid,
                    status: 9,//0禁用1启用9删除
                };
                let postdata = qs.stringify(param);

                this.$confirm('此操作将永久删除该课程, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    testSelect(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: '已取消删除'
                    });
                });
            },
            // 加载选项
            panelSelect() {
                let that = this;
                let param = {
                    token: localStorage.token,
                };
                let postdata = qs.stringify(param);
                testSelect(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        that.dialogdata = json.Rs;
                        that.form.dialogdata = json.Rs;
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            // 微信可见与否
            changeWechat(e, row) {
                let that = this;
                let param = {
                    token: localStorage.token,
                    id: row.id,//
                    wxVisible: e,//
                };
                let postdata = qs.stringify(param);
                testTable(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        that.$message({
                            showClose: true,
                            message: '当前用户微信已' + json.Memo,
                            type: 'success'
                        });
                        // 重载列表
                        that.getTableQuery();
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            // 增删会员课程
            lessonStudenChange() {
                if (!this.multipleSelection.length) {
                    this.$message({
                        showClose: true,
                        message: '错了哦，需要先选中至少一条记录',
                        type: 'error'
                    });
                    return false
                }
                if (this.multipleSelection.length != 1) {
                    this.$message({
                        showClose: true,
                        message: '错了哦，只能选中一条记录',
                        type: 'error'
                    });
                    return false
                }
                console.log(123);
                let row = this.multipleSelection[0];
                this.dialogValue = row.dialogValue;
                this.dialogVisible = true
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            clearForm(){
                // clear
                this.form.currName = '';
                this.form.consumeHour = 0;
                this.form.topLimit = 0;
                this.form.wxvisible = 1;
                this.form.currColor = '';
                this.form.memo = '';
            },
            // 修改
            editLesson(row){
                let that = this;
                this.clearForm();

                this.form.currName = row.name;
                this.form.consumeHour = row.Recovered;
                this.form.topLimit = row.Recovered;
                this.form.wxvisible = row.wxVisible;
                this.form.currColor = row.color;
                this.form.memo = row.memo;
                this.form.dialogValue = row.dialogValue;
                this.form.btnType = 1; //修改

                this.dialogLesson = true
                this.dialogTitle = '修改课程'
            },
            Lessonmember(row){
                this.dialogTableVisible = true;
                this.gridData = row.members;

            },
            // 查询按钮
            query() {
                // this.getTableQuery();
                this.$message.success('查询完毕');
            },
            // 页面数据查询
            getTableQuery() {
                let that = this;
                // this.getGetChildRegionSelect(0, 1);
                that.loading = true;
                // 查询检测设备。上级区域id，区域id必传。regionid传0，查询supregionid对应所有子区域的检测设备。 如果supregionid，regionid都传0，默认查询企业ID下所有检测设备
                let param = {
                    token: localStorage.token,
                    supregionid: 0,//
                    regionid: this.panel.regionid,//
                    comid: 1,//
                    tagname: that.panel.tagname,//标签名
                    start: 1,//
                    tableMax: 9999,//
                };
                let postdata = qs.stringify(param);
                testTable(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);
                    }
                })
            },
            // 导出excel
            btnExpAll() {
                let that = this;
                let url = headapi + '?ctl=ajax&mod=czgl&act=czcx_excel';//获取
                let bt = globaltime2String(that.panel.time1[0]);
                let et = globaltime2String(that.panel.time1[1]);
                let usercode = that.panel.usercode;
                window.location = url + '&bt=' + bt + '&et=' + et + '&usercode=' + usercode;
            },
            // 设置分页数据
            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 globalfmtDate(column, 11);
            },
            // 过滤金额
            filterMoney(value, row, column) {
                let that = this;
                return parseFloat(column).toFixed(2);
            },
        },
    }
</script>

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

    .context {
        overflow: hidden;
        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 span {
        width: 169px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        color: #fff;
        background: #3799FF;
        border-radius: 250px;
        font-size: 18px;
        overflow: hidden;
        display: block;
        margin: 0 auto;
        margin-bottom: 30px;
    }

    .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;
    }

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

    .pull-right {
        width: 70%;
        float: right;
    }
</style>
