<template>
  <div class="user">
    <el-form ref="searchFrom" :model="searchFrom" label-width="80px">
      <el-row>
        <el-col :span="4">
          <el-form-item label="课程标题">
            <el-input v-model="searchFrom.title"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="课程类型">
            <el-select v-model="searchFrom.type" placeholder="请选择">
              <el-option value="" label="请选择"></el-option>
              <el-option value="0" label="月课"></el-option>
              <el-option value="1" label="日课"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button type="primary" plain @click="getUser">搜索</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="4" :offset="8">
          <el-button type="success" plain @click="add" v-if="!$store.state.readonly" style="float: right">新增课程</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      :data="userList"
      style="width: 100%">
      <el-table-column
        prop="title"
        label="课程名">
      </el-table-column>
      <el-table-column
        label="课程类型">
        <template slot-scope="scope">
          {{scope.row.type | lessonType}}
        </template>
      </el-table-column>
      <el-table-column
        prop="theme_num"
        label="主题数">
      </el-table-column>
      <el-table-column
        prop="sing_num"
        label="歌曲数">
      </el-table-column>
      <el-table-column
        prop="item_num"
        label="实体包数量">
      </el-table-column>
      <el-table-column
        v-if="!$store.state.readonly"
        width="250"
        label="操作">
        <template slot-scope="scope">
          <!--<el-button size="mini" plain type="primary" @click="detail(scope.row)">-->
            <!--查看详情-->
          <!--</el-button>-->
          <el-button size="mini" plain type="warning" @click="edit(scope.row)" v-if="!$store.state.readonly">
            编辑
          </el-button>
          <el-button size="mini" plain type="danger" @click="delTeacher(scope.row)" v-if="$store.state.deletePermission && !$store.state.readonly">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <page :total="total" :limit="limit" @pageChange="onPageChange"/>
    <dialog-com v-if="dialogObj.show" :dialogObj="dialogObj"  @changeShow="changeShow" @reflash="getUser"/>
  </div>
</template>

<script>
  import {getLessonApi,deleteLessonAPI} from "../../service/api";
  import page from '../framework/page'
  import dialogCom from './dialog'
  import {LESSONTYPE} from "../../util/wordbook";

  export default {
    name: "index",
    data(){
      return {
        searchFrom:{
          title:'',
          type:''
        },
        userList:[],
        total:0,
        nowPage:1,
        limit: 10,
        dialogObj:{
          type:0,
          show:false,
          id:''
        },
      }
    },
    components:{
      page,
      dialogCom
    },
    filters:{
      lessonType(value){
        return LESSONTYPE[value]
      }
    },
    mounted(){
      this.getUser()
    },
    methods:{
      onPageChange(val){
        this.nowPage = val
        this.getUser()
      },
      getUser(){
        this.searchFrom.page = this.nowPage;
        let json = {
          limit: this.limit,
          page: this.nowPage
        }
        if (this.searchFrom.title) {
            json.title = this.searchFrom.title
        }
        if (this.searchFrom.type) {
          json.type = this.searchFrom.type
        }
        getLessonApi(json).then(res=>{
          this.userList = res.list;
          this.total = res.total
        })
      },
      edit(data){
        this.dialogObj = {
          type:1,
          show:true,
          id:data.id,
          title:'编辑课程'
        };
      },
      add(){
        this.dialogObj = {
          type:0,
          show:true,
          id:'',
          title:'新增课程'
        }
      },
      detail(data){
        this.dialogObj.id = data.id;
        this.dialogObj.type = 1;
        this.dialogObj.show = true
      },
      delTeacher(data){
        this.$confirm('此操作将删除该课程?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteLessonAPI(data.id).then(res=>{
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getUser()
          });
        });
      },
      changeShow(data){
        this.dialogObj.show=data
      }

    }
  }
</script>

<style scoped lang="less">
  @import "../../util/public";
  .user{
    height: 100%;
    overflow: auto;
    padding: 20px 0;
    .btn-content{
      text-align: center;
    }
  }
</style>
