
<template>
  <div class="courseManage app-container">
    <div class="search">
      <div>
        <el-date-picker
          v-model="date"
          size="small"
          class="item-width-350"
          value-format="yyyy-MM-dd hh:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
        <el-button
        class="ml10"
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="handleSearch"
      >
        搜索
      </el-button>
      </div>
        

        <el-button class="add-btn" type="primary" size="small" @click="handleAdd">添加</el-button>
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="课程封面"
        prop="publishUserId"
        show-overflow-tooltip
      >
      <template slot-scope="scope">
        <img style="width:80px;height:80px" :src="scope.row.courseImg" alt="" class="icon">
      </template>
      </el-table-column>
      <el-table-column
        prop="courseName"
        align="center"
        label="章节名称"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="courseMsg"
        align="center"
        label="课程简介"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="columnId"
        align="center"
        label="栏目名称"
        show-overflow-tooltip
      >
      <template slot-scope="scope">
        {{scope.row.columnId | filterColumn(that)}}
      </template>
      </el-table-column>

      <el-table-column
        prop="createTime"
        align="center"
        label="状态"
        show-overflow-tooltip
      >
      <template slot-scope="scope">
        <span :class="[scope.row.state == 1?'red':'green']">{{scope.row.state == 1?'隐藏':'显示' }}</span>
      </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        align="center"
        label="发布状态"
        show-overflow-tooltip
      >
      <template slot-scope="scope">
        <span :class="[scope.row.status == 1?'red':'green']">{{scope.row.status == 1?'未发布':'已发布' }}</span>
      </template>
      </el-table-column>
       <el-table-column prop="address" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleDetail(scope.row.id)">查看</el-button>
          <el-button type="text" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="handleStatus(scope.row.id,scope.row.status == 1?2:1)">{{scope.row.status == 1?'立即发布':'取消发布' }}</el-button>
          <el-button type="text" size="small" @click="handleState(scope.row.id,scope.row.state == 1?2:1)">{{scope.row.state == 1?'显示':'隐藏' }}</el-button>
          <el-button type="text" class="red" size="small" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="query.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
  
  <script>
  import {debounce} from '@/utils/index'
  import {courseList,operateByState,operateByStatus,delCourse,getAllColumn} from '@/api/study'
export default {
  name: "courseManage",
  data() {
    return {
        that:this,
      date:'',//时间
      tableData: [],
      options:[],
      loading: false,
      query: {
        "currentPage": 1,
        "pageSize": 10,
        "startDateTime":'',
        "endDateTime":'',
      },
      title: "",
      total: 0,
    };
  },
 watch: {
    // query: {
    //   handler: debounce(function(val) {
    //     this.courseList()
        
    //   }),
    //   deep:true
    // },
    date(val){
      if(val){
        this.query.startDateTime = val[0]
        this.query.endDateTime = val[1]
      }else{
        this.query.startDateTime = ''
        this.query.endDateTime = ''
      }
    }
  },
  filters: {
    filterColumn: function(val,that) {
        let res
        that.options.map(item=>{
            if(val == item.value){
                res = item.label
            }
        })
        return res
    }
  },
  methods: {
    handleSearch(){
      this.query.currentPage = 1
      this.courseList();

    },
    handleAdd(){
      this.$router.push({
        path:'/studyManage/courseManage/addCourse',
      })
    },

    handleStatus(id,status){
      let msg
      if(status == 2){
        msg = '发布'
      }else{
        msg = '取消发布'
      }
      this.$confirm(`此操作将${msg}该数据, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          operateByStatus({id,status}).then(res=>{
            if(res.state == 'Success'){
               this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              });
              this.courseList()
            }
          })
        })
      
    },
    handleState(id,state){
      let msg
      if(state == 2){
        msg = '显示'
      }else{
        msg = '隐藏'
      }
      this.$confirm(`此操作将${msg}该数据, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          operateByState({id,state}).then(res=>{
            if(res.state == 'Success'){
               this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              });
              this.courseList()
            }
          })
        })
      
    },
    // 点击删除
    handleDel(id) {
       this.$confirm(`此操作将删除该数据, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delCourse({id}).then(res=>{
            if(res.state == 'Success'){
               this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              });
              this.search()
            }
          })
        })
    },
    handleDetail(id){
      this.$router.push({
        path:'/studyManage/courseManage/addCourse',
        query:{
          id,
          mode:'detail'
        }
      })
    },
    handleEdit(id){
        this.$router.push({
            path:'/studyManage/courseManage/addCourse',
            query:{
            id,
            mode:'edit'
            }
        })
    },
    handleSizeChange(val) {
      this.query.currentPage = 1;
      this.query.pageSize = val;
   this.courseList()
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
   this.courseList()
    },

    search() {
      this.query.currentPage = 1;
        this.courseList()
    },
    courseList(){
      this.loading = true
      courseList(this.query).then(res=>{
        this.loading = false
        if(res.state == 'Success'){
          this.tableData = res.content.records
          this.total = res.content.total
        }
      })
    },
    // 获取栏目
    getAllColumn() {
      getAllColumn().then((res) => {
        if (res.state == "Success") {
          res.content.map((item) => {
            this.options.push({
              value: item.id,
              label: item.columnName,
            });
          });
        }
      });
    },
  },
  created() {
   this.courseList()
   this.getAllColumn()
  },
};
</script>
  
<style lang="scss" scoped>
.courseManage {
  .search{
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}

</style>
  