<template>
  <div>
    <div class="button-box clearflex fr p20">
      <el-button @click="dialogFormVisible=true" type="primary">新增课程</el-button>
    </div>

    <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
        
      </el-form>
    </div>

    <el-table :data="tableData"  border stripe>
      <el-table-column label="序号" width="70" prop="courseId">

      </el-table-column>
      <el-table-column label="课程名字"  prop="name">
        <template slot-scope="{row}">  
          <el-input v-if="row.edit" class="edit-input" size="small" v-model="row.name" clearable/>
          <span v-else>{{row.name}}</span>
        </template>
      </el-table-column>

      <el-table-column label="课程作者"  prop="marks">
        <template slot-scope="{row}">  
          <el-input v-if="row.edit" type="text" size="small" v-model="row.author" clearable />
          <span v-else>{{row.author}}</span>
        </template>
      </el-table-column>
	  
	  <el-table-column label="课程类型"  prop="marks">
	    <template slot-scope="{row}">  
		  <DropSearch v-if="row.edit" :info="row.type" type="type" @select="row.select" ></DropSearch>
	      <span v-else>{{row.type}}</span>
	    </template>
	  </el-table-column>

    <el-table-column label="学习年度"  prop="marks">
	    <template slot-scope="{row}">  
	      <el-input v-if="row.edit" type="text" size="small" v-model="row.nd" clearable/>
	      <span v-else>{{row.nd}}</span>
	    </template>
	  </el-table-column>
	  
	  <el-table-column label="课程学时"  prop="marks">
	    <template slot-scope="{row}">  
	      <el-input v-if="row.edit" type="text" size="small" v-model="row.xs" clearable/>
	      <span v-else>{{row.xs/10}}</span>
	    </template>
	  </el-table-column>

    
	  
	  <el-table-column label="课程费用"  prop="marks">
	    <template slot-scope="{row}">  
	      <el-input v-if="row.edit" type="text" size="small" v-model="row.fee"  clearable/>
	      <span v-else>{{row.fee?(row.fee/100+'元'):'免费'}}</span>
	    </template>
	  </el-table-column>

    <el-table-column label="学时模板"  prop="marks">
	    <template slot-scope="{row}">  
        <div v-if="row.edit">
	        <el-radio v-model="row.tplId" :label="1">八大员</el-radio>
          <el-radio v-model="row.tplId" :label="2">园林</el-radio>
        </div>
        <span v-else>{{row.tplId==1?'八大员':'园林'}}</span>
	    </template>
	  </el-table-column>
	  
	  <el-table-column label="展示图片"  prop="invoice_img">
		  <template slot-scope="{ row }">
			<UploadImg v-if="row.edit" v-model="row.tb" placeholder="课程图片"   @rmImage="row.tb=''" ></UploadImg>
			<PreviewImg v-else :src="row.tb" title="查看图片" />
		  </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.edit"
            type="success"
            size="small"
            icon="el-icon-circle-check"
            @click="confirmEdit(row, $index)"
          >提交</el-button>
          <el-button
            v-if="row.edit"
            class="cancel-btn"
            size="small"
            icon="el-icon-refresh"
            type="warning"
            @click="cancelEdit(row, $index)"
          >取消</el-button>
          <el-button
            v-if="!row.edit"
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="initEdit(row, $index)"
          >编辑</el-button>
		  
		  <el-button
		    v-if="!row.edit"
		    type="primary"
		    size="small"
		    icon="el-icon-edit"
		    @click="editMedia(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="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <el-dialog title="新增课程" center :visible.sync="dialogFormVisible"  >
      <el-form :model="form" :rules="rules" label-width="120px" ref="elForm">
        <el-form-item  label="课程名字" prop="name" >
          <el-input  v-model="form.name" type="text" class="user-form-item" clearable></el-input>
        </el-form-item>
		
        <el-form-item  label="课程作者" prop="author">
			<el-input  v-model="form.author" type="text" class="user-form-item" clearable></el-input>
        </el-form-item>
		
		<el-form-item  label="课程类型" prop="type">
		  <drop-search :info="form.type" type="type" class="user-form-item" @select="okType"/>
		</el-form-item>
	
		<el-form-item  label="学习年度" prop="xs" >
		  <el-input  v-model="form.nd" type="number" class="user-form-item" clearable></el-input>
		</el-form-item>

		<el-form-item  label="课程总学时" prop="xs" >
		  <el-input  v-model="form.xs" type="number" class="user-form-item" clearable></el-input>
		</el-form-item>
		
		<el-form-item  label="课程费用" prop="xs" >
		  <el-input  v-model="form.fee" type="number" class="user-form-item" clearable></el-input>
		</el-form-item>
		
		<el-form-item  label="课程图片" prop="xs" >
			<UploadImg v-model="form.tb" placeholder="课程图片"   @rmImage="form.tb=''" ></UploadImg>
		</el-form-item>
		
		
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="dialogFormVisible=false">取 消</el-button>
        <el-button @click="addCourse" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  
  </div>
</template>

<script>
import { getcourseList, addCourse} from "@/api/course.js";
import {toDatetime} from "@/utils/date"
import infoList from "@/mixins/infoList";
import { time2str, str2time} from "@/utils/index.js";
import DropSearch from '@/components/select/DropSearch.vue';
import PreviewImg from "@/components/customPic/previewImg.vue";
import  UploadImg from "@/components/upload/image.vue";
import rules from "@/utils/rules"

export default {
  name: "Api",
  data() {
    return {
	  listApi: getcourseList,
      dialogFormVisible: false,
	  dialogMediaVisible: false,
      dialogTitle: "新增广告",
      boolVal:[0,1],
      form: {
		 type:'',
		 xs:0,
		 name:'',
		 tb:'',
		 author:''
	  },
      tableData: [],
      rules: {
        name: rules.defaultFun("请输入课程名字"),
        type: rules.defaultFun("请输入课程分类"),
		author: rules.defaultFun("请输入作者"),
		xs: rules.defaultNum("请输入学时"),
      }
    };
  },
  components:{DropSearch,PreviewImg,UploadImg},
  mixins: [infoList],
  methods: {
    //条件搜索前端看此方法
    onSubmit() {
      this.page = 1;
      this.size = 10;
      this.getTableData();
    },
	editMedia(row, index){
		this.$router.push( {name:'course_info', query:{id: row.courseId}})
	},
    cancelEdit(row, index) {
      row = Object.assign(row, row.temp);
      row.edit = false;
      delete( row["temp"] );
    },
    initEdit(row, index) {
      let form = Object.assign({}, row);
      row.edit = true;
	  row.xs = row.xs/10;
	  row.fee = row.fee/100;
	  row.select = (item)=>{
		  row.type = item.name;
	  }
      row.temp = form;
    },
    confirmEdit( row ) {
      let param = Object.assign( {}, row )
	  param.xs = parseInt(param.xs*10)
	  param.fee = parseInt(param.fee*100)
	  addCourse(param).then((res) => {
	    if( res.code == 200){
			
			Object.assign( row, param)
			row.edit = false;
			delete( row["temp"] )
			this.$message.successMsg("编辑成功", 1);
	    }
	  });
    },
	okType(item){
		this.form.type = item.name
	},
	addCourse() {
	  this.$refs["elForm"].validate((valid) => {
		if (!valid) return;
		let param = Object.assign({}, this.form);
		param.xs  = parseInt( 10*param.xs);
		param.fee = parseInt(param.fee*100)
		addCourse(param).then((res) => {
		  if( res.code == 200){
			this.dialogFormVisible = false
			this.$message.successMsg("新增成功", 1);
			this.getTableData()
		  }
		});
	  })
	}
  },
  filters: {
    toDatetime
  },
  created() {
    this.getTableData();
  }
};
</script>
<style scoped lang="scss">
.el-tag--mini {
  margin-left: 5px;
}
.warning {
  color: #dc143c;
}
</style>
