<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>
            <el-button @click="query">刷新</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
                        type="index"
                        label="序号"
                        width="50">
                </el-table-column>
                <el-table-column
                        prop="ClassSelf.ClassName"
                        label="课程名称"
                >
                </el-table-column>
                <el-table-column
                        prop="ClassSelf.ClassType"
                        label="是否通用"
                        width="110"
                        align="center"
                        sortable
                >
                    <template slot-scope="scope">
                        <span v-if="scope.row.ClassSelf.ClassType == 1">是</span>
                        <span v-if="scope.row.ClassSelf.ClassType == 0">否</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="ClassSelf.ShopId"
                        label="课程类型"
                        width="110"
                        align="center"
                        sortable
                >
                    <template slot-scope="scope">
                        <span v-if="scope.row.ClassSelf.ShopId != 0">占用课时</span>
                        <span v-if="scope.row.ClassSelf.ShopId == 0">不占课时</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="ClassSelf.ConsumeHour"
                        label="消耗课时"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="ClassSelf.TopLimit"
                        label="建议上课人数"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="ClassSelf.WxVisible"
                        label="微信可见"
                >
                    <template slot-scope="scope">
                        <el-switch
                                v-model="scope.row.ClassSelf.WxVisible"
                                :active-value="1"
                                :inactive-value="0"
                                active-color="#409EFF"
                                inactive-color="#D9D9D9"
                                @change=changeWechat($event,scope.row.ClassSelf)
                        >
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="ClassSelf.ClassColor"
                        label="课程颜色"
                        width="110"
                >
                    <template slot-scope="scope">
                        <el-color-picker v-model="scope.row.ClassSelf.ClassColor"
                                         @change="changeColor(scope.row.ClassSelf.ClassColor,scope.row.ClassSelf)"></el-color-picker>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="ClassSelf.Memo"
                        label="备注">
                    <template slot-scope="scope">
                        <el-popover
                                placement="top"
                                title="标题"
                                width="200"
                                trigger="hover"
                                :content="scope.row.ClassSelf.Memo">
                            <span slot="reference"
                                  v-if="scope.row.ClassSelf.Memo.length > 6">{{scope.row.ClassSelf.Memo.substr(0,6)}} ....</span>
                        </el-popover>
                        <span v-if="scope.row.ClassSelf.Memo.length <= 6">{{scope.row.ClassSelf.Memo}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="ClassSelf.ClassId"
                        label="操作"
                >
                    <template slot-scope="scope">
                        <el-button type="text" @click="editLesson(scope.row.ClassSelf)">编辑</el-button>
                        <el-button type="text" @click="Lessonmember(scope.row.Userlist)">课程会员</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="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.ClassName"></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.classType"
                                    :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.allUse"
                                    :active-value="0"
                                    :inactive-value="1"
                                    active-color="#409EFF"
                                    inactive-color="#D9D9D9">
                            </el-switch>
                        </el-form-item>
                        <el-form-item label="课程颜色">
                            <el-color-picker v-model="form.ClassColor"></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"
                                         :titles="['全部会员','已选会员']"></el-transfer>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <div class="dialogFooter">
                <el-button type="primary" size="small" v-if="form.btnType == 0" @click="confirmAddLesson">确定</el-button>
                <el-button type="primary" size="small" v-if="form.btnType == 1" @click="confirmEditLesson">确定
                </el-button>
                <el-button size="small" @click="dialogLesson = false">取消</el-button>
            </div>
        </el-dialog>

        <el-dialog title="增删课程会员" :visible.sync="dialogVisible" width="650px">
            <div class="dialogTitle">
                <span>基础功能</span>
            </div>
            <div>
                <el-transfer filterable v-model="dialogValue" :data="form.dialogdata"
                             :titles="['会员列表', '已添加会员']"></el-transfer>
            </div>
            <div class="dialogFooter">
                <el-button type="primary" size="small" @click="cofirmClassVipuserEdit">确定</el-button>
                <el-button @click=" dialogVisible = false" size="small">取消</el-button>
            </div>
        </el-dialog>

        <el-dialog title="课程会员" :visible.sync="dialogTableVisible" style="overflow-y: scroll">
            <el-table :data="gridData">
                <el-table-column property="Name" label="姓名" width="200"></el-table-column>
                <el-table-column property="Phone" 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-column property="ExpTime" label="有效期" :formatter="filterFmtDate"></el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>

<script>
    import Global from '../Global.js'
    import {
        ClassAdd,
        ClassEdit,
        ClassListQuery,
        ClassColorEdit,
        ClassStatusEdit,
        VipUserListQuery,
        ClassVisibleStatusEdit,
        ClassVipuserQuery,
        ClassVipuserEdit,
        testTable,
        testSelect
    } from "../api/getApiRes";

    let qs = require('qs');
    export default {
        data() {
            return {
                dialogLesson: false,//新增课程
                dialogVisible: false,
                dialogTableVisible: false,
                dialogTitle: '',
                dialogdata: [],
                gridData: [],
                dialogValue: [],
                allTableData: [],
                // panel 配置项目
                panel: {
                    usercode: '',
                    username: '',
                    compname: '',
                    keyword: '',
                    USERCODE: '',
                    taskstatus: 99,
                    tableData: [],
                    allTableData: [],
                    draw: 1,
                    start: 0,
                    recordsTotal: 0,
                    limit: '10',
                    multipleSort: false,
                    loading: false,
                    fileList: [],
                    multipleSelection: [],
                    detectedmac: '',
                    options: [
                        {value: 99, label: '全部'},
                        {value: 1, label: '进行中'},
                        {value: 2, label: '已完成'},
                    ],
                    time1: globalBt(),
                },
                form: {
                    ClassName: "",
                    ConsumeHour: 0,
                    TopLimit: 0,
                    wxVisible: 1,
                    classType: 1,
                    ClassColor: "#ffffff",
                    teacherId: "",
                    classId: "",
                    allUse: "",
                    Memo: "",
                    dialogdata: [],//穿梭待选
                    dialogValue: [],//穿梭已选
                },
                multipleSelection: [],
                pageination: {
                    pageItem: 100,
                    pageoptions: pageOptions(),
                    total: 100,
                    pageIndex: 1,
                },
                tableData: []
            }
        },
        mounted() {
            this.panelSelect();
            this.getTableQuery();
        },
        methods: {
            // 课程会员增删
            cofirmClassVipuserEdit() {
                let that = this;
                let userlist = that.dialogValue.toString();
                let param = {
                    token: localStorage.token,
                    classId: this.form.classId,
                    userlist: userlist,
                };
                let postdata = qs.stringify(param);
                ClassVipuserEdit(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        that.$message({
                            showClose: true,
                            message: '接口修改成功！',
                            type: 'success'
                        });
                        that.dialogVisible = false
                        that.getTableQuery();
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            // 确认添加课程
            confirmAddLesson() {
                let that = this;

                // checkNum
                if (!that.form.ClassName) {
                    this.$message.error('错了哦，课程名称不能为空');
                    return false
                }
                if (that.form.ClassName.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 userlist = that.form.dialogValue.toString();
                let param = {
                    token: localStorage.token,
                    shopId: localStorage.shopId,
                    className: that.form.ClassName,
                    consumeHour: that.form.ConsumeHour,
                    topLimit: that.form.TopLimit,
                    classType: that.form.classType,
                    wxVisible: that.form.wxVisible,
                    classColor: that.form.ClassColor,
                    userlist: userlist,
                    teacherId: 0,//预留
                    memo: that.form.Memo,
                };
                let postdata = qs.stringify(param);
                ClassAdd(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);
                    }
                })
            },
            confirmEditLesson() {
                let that = this;
                // checkNum
                if (!that.form.ClassName) {
                    this.$message.error('错了哦，课程名称不能为空');
                    return false
                }
                if (that.form.ClassName.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 teacherId = that.form.dialogValue ? that.form.dialogValue.toString() : '';
                // 通用是0，非通用是shopid
                let shopId = that.form.allUse == 0 ? 0 : localStorage.ShopId;
                let param = {
                    token: localStorage.token,
                    shopId: shopId,
                    classId: that.form.classId,
                    className: that.form.ClassName,
                    consumeHour: that.form.ConsumeHour,
                    topLimit: that.form.TopLimit,
                    classType: that.form.classType,
                    wxVisible: that.form.wxVisible,
                    classColor: that.form.ClassColor,
                    teacherId: teacherId,
                    memo: that.form.Memo,
                };
                let postdata = qs.stringify(param);
                ClassEdit(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() {
                this.form.btnType = 0; //新增
                // clear
                this.form.ClassName = '';
                this.form.ConsumeHour = 0;
                this.form.TopLimit = 0;
                this.form.wxVisible = 1;
                this.form.ClassColor = "#ffffff";
                this.form.Memo = "";
                this.form.dialogValue = [];
                this.dialogLesson = true;
                this.dialogTitle = '新增课程'
                this.panelSelect();
            },
            // 改色
            changeColor(c, row) {
                let that = this;
                let param = {
                    token: localStorage.token,
                    classId: row.ClassId,//
                    classColor: c,//
                };
                let postdata = qs.stringify(param);
                ClassColorEdit(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 row = that.multipleSelection[0];
                console.log(row);
                let param = {
                    token: localStorage.token,
                    classId: row.ClassSelf.ClassId,
                    status: 9,//0禁用1启用9删除
                };
                let postdata = qs.stringify(param);

                this.$confirm('此操作将永久删除该课程, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    ClassStatusEdit(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,
                    vipType: '',
                    start: 1,
                    expDay: '',
                    tableMax: 9999,
                };
                let postdata = qs.stringify(param);
                VipUserListQuery(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        // that.dialogdata = json.Rs;
                        that.form.dialogdata = turnResToOption(json.Rs)
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            // 微信可见与否
            changeWechat(e, row) {
                let that = this;
                let param = {
                    token: localStorage.token,
                    classId: row.ClassId,//
                    wxVisible: e,//
                };
                let postdata = qs.stringify(param);
                ClassVisibleStatusEdit(postdata).then(res => {
                    let json = res;
                    let text = parseInt(e) == 1 ? '可见' : '不可见';
                    if (json.Code == 0) {
                        that.$message({
                            showClose: true,
                            message: '当前用户微信已' + text,
                            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
                }
                this.panelSelect();
                let row = this.multipleSelection[0];
                let userArrary = [];
                row.Userlist.map(function (item) {
                    userArrary.push(item.Id)
                })
                this.form.classId = row.ClassSelf.ClassId;
                this.dialogValue = userArrary;
                this.dialogVisible = true
                // 读取左侧会员列表
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            clearForm() {
                // clear
                this.form.ClassName = '';
                this.form.ConsumeHour = 0;
                this.form.TopLimit = 0;
                this.form.wxVisible = 1;
                this.form.ClassColor = '';
                this.form.Memo = '';
            },
            // 编辑
            editLesson(row) {
                let that = this;
                this.clearForm();
                this.form.btnType = 1; //编辑
                this.form.ClassName = row.ClassName;
                this.form.ConsumeHour = row.ConsumeHour;
                this.form.TopLimit = row.TopLimit;
                this.form.wxVisible = row.WxVisible;
                this.form.ClassColor = row.ClassColor;
                this.form.Memo = row.Memo;
                this.form.dialogValue = row.teacherId;
                this.form.classId = row.ClassId
                this.form.ShopId = row.ShopId;
                this.form.allUse = row.ShopId;
                this.dialogLesson = true
                this.dialogTitle = '编辑课程'
            },
            Lessonmember(Userlist) {
                this.dialogTableVisible = true;
                this.gridData = Userlist;
            },
            // 查询按钮
            query() {
                this.getTableQuery();
                this.$message.success('查询完毕');
            },
            // 页面数据查询
            getTableQuery() {
                let that = this;
                that.loading = true;
                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);
                ClassListQuery(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();
            },
            // 过滤时间
            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 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;
    }

    /deep/ .el-transfer-panel__item .el-checkbox__input {
        left: 15px;
    }
</style>
