<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="Recovered"-->
                <!--                        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.CreatedAt"
                        label="创建时间"
                        width="210"
                        :formatter="filterFmtDate"
                        sortable
                >
                </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.ClassSelf)">课程会员</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="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.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"></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">
            <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 {
        ClassAdd,
        ClassEdit,
        ClassListQuery,
        ClassColorEdit,
        ClassStatusEdit,
        VipUserListQuery,
        ClassVisibleStatusEdit,
        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: {
            // 确认添加课程
            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 teacherId = 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: '',
                    teacherId: teacherId,
                    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() : '';
                let param = {
                    token: localStorage.token,
                    shopId: localStorage.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 = '新增课程'
            },
            // 改色
            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 : 0,
                    phone : '',
                    name : '',
                    expDay : '365',
                };
                let postdata = qs.stringify(param);
                VipUserListQuery(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) {
                console.log(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;
                    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.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.dialogLesson = true
                this.dialogTitle = '编辑课程'
            },
            Lessonmember(row) {
                this.dialogTableVisible = true;
                this.gridData = row.Userlist;
            },
            // 查询按钮
            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);
                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);
                    }
                })
            },
            // 导出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 nonTfmtDate(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>
