<template>
  <div>

    <div class="search-term">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
            <DropSearch  type="media" @select="selectMedia"></DropSearch>
        </el-form-item>
		
		<el-form-item>
		    <el-button @click="dialogFormVisible=true" type="primary">新增课程</el-button>
		</el-form-item>
      </el-form>
    </div>
    <el-table :data="list.slice( (page-1)*size, page*size)"  border stripe>
      <el-table-column label="序号" width="70" prop="mediaId">

      </el-table-column>
      <el-table-column label="课程名字"  prop="name">
      </el-table-column>

      <el-table-column label="视频时长"  prop="duration">
        <template slot-scope="{row}">  
          <span>{{ row.duration/3600 |toFiexd }}小时</span>
        </template>
      </el-table-column>
	  
	  <el-table-column label="视频学时"  prop="xs">
	    <template slot-scope="{row, $index}">  
	      <span>{{row.xs/10|toFiexd}}学时</span>
	    </template>
	  </el-table-column>
	  
      
      <el-table-column
        align="center"
        width="160px"
        label="操作"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row, $index}">
		  <el-button
		    v-if="row.deleted"
		    type="danger"
		    size="small"
		    @click="dropMedia(row, $index)"
		  >取消移除</el-button>
		  
		 <el-button
		   v-else
		   type="primary"
		   size="small"
		   icon="el-icon-edit"
		   @click="dropMedia(row, $index)"
		 >移除</el-button>
		  
        </template>
		
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="size"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="list.length"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
	
  </div>
</template>

<script>
import DropSearch from '@/components/select/DropSearch.vue';
import { getCourseMediaList, deleteCourseMedia} from "@/api/course.js";
import PreviewImg from "@/components/customPic/previewImg.vue";
export default {
  name: "Api",
  data() {
    return {
		page:1,
		size:10,
		list:[],
		form:{}
    }
  },
  components:{PreviewImg,DropSearch},
  props:{
	info: {
		type: Object,
		default:{courseId:0}
	 }
  },
  watch:{
	'info.courseId'(){
		console.log("load")
		this.getTableData()
	}  
  },
  filters:{
	toFiexd(val){
		return val.toFixed(1)
	}  
  },
  methods: {
	  selectMedia(row){
		this.form = row;
		 
	  },
	  getTableData( ){
		this.page = 1
		let param = {id: this.info.courseId}
		getCourseMediaList(param).then(res=>{
			console.log( res.data )
			this.list = res.data
		})
	  },
	  dropMedia(row, index){
		let param ={mediaId: row.mediaId}
		deleteCourseMedia( param).then(res=>{
			 if( res.code == 200){
				this.$message.successMsg("移除成功", 1)
				row.deleted = true
			 }
		}) 
	  },
	  handleCurrentChange(page  ){
		  this.page = page
	  },
	  handleSizeChange(size){
		  this.size = size
	  }
	  
  },
  created() {
    this.getTableData();
  }
};
</script>
<style scoped lang="scss">
.el-tag--mini {
  margin-left: 5px;
}
.warning {
  color: #dc143c;
}
</style>
