<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <el-input 
      v-model="query.courseName" 
      clearable placeholder="输入搜索内容" 
      style="width: 200px;" 
      class="filter-item" 
      @keyup.enter.native="crud.toQuery" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="课程等级" prop="level">
            <el-select v-model="form.level">
              <el-option v-for="item in levelList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="课程分类" prop="categoryIds">
            <el-cascader
              v-model="form.categoryIds"
              :options="categoryList"
              :show-all-levels="false"
              :props="{ checkStrictly: true }"
              @change="handleChange">
            </el-cascader>
          </el-form-item>
          <el-form-item label="课程图片" prop="imageInput">
            <picUpload v-model="form.imageInput"></picUpload>
          </el-form-item>
          <el-form-item label="课程名称" prop="courseName">
            <el-input v-model="form.courseName" />
          </el-form-item>
          <el-form-item label="课程简介" prop="courseIntroduce">
            <el-input type="textarea" v-model="form.courseIntroduce" />
          </el-form-item>
          <el-form-item label="课程天数" prop="dayCount">
            <el-input v-model="form.dayCount" placeholder="请填写课程天数" @input="countChange"></el-input>
          </el-form-item>
          <el-form-item label="课程表" v-if="form.courseScheduleList.length>0" prop="courseContent">
            <el-input 
            type="textarea"
            style="margin-bottom:10px;" 
            v-for="(item,index) in form.courseScheduleList" 
            :key="index"
            :placeholder="'请输入第'+ item.day + '天的课程内容'"
            v-model="item.courseContent"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="submit">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('id')" prop="id" label="id" width="75"/>
        <el-table-column v-if="columns.visible('courseName')" prop="courseName" label="课程名称"/>
        <el-table-column prop="imageArr" label="课程封面">
          <template slot-scope="scope">
            <a :href="scope.row.imageArr[0]" style="color: #42b983" target="_blank">
              <img :src="scope.row.imageArr[0]" alt="点击打开" style="width:38px;height:38px;border-radius:4px;">
            </a>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('categoryName')" prop="categoryName" label="分类名称" />
        <el-table-column v-if="columns.visible('courseIntroduce')" prop="courseIntroduce" label="课程简介" :show-overflow-tooltip="true"/>
        <el-table-column ref="table" prop="dayCount" label="课程天数"></el-table-column>
        <el-table-column v-permission="['admin','cyCourse:del']" label="操作"  width="260px"  align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudCyCourse from '@/api/cyCourse'
import crudCourseMaster from "@/api/CourseMaster";
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import picUpload from '@/components/pic-upload'
import MaterialList from "@/components/material"
import { initData } from '@/api/data'
import { parseTime } from '@/utils/index'

// crud交由presenter持有
const defaultCrud = CRUD({ 
  title: '课程模块', 
  url: 'api/cyCourse', 
  sort: 'id,desc', 
  isModel:1,
  crudMethod: { ...crudCyCourse }
})
const defaultForm = { 
  id: null, 
  masters:[],
  categoryIds: null, 
  level:null,
  courseName: '', 
  categoryName: null, 
  courseIntroduce: null, 
  seatArrange: 1,
  chargeType: 0, 
  dayCount:'',
  courseCharge: null, 
  createTime: null, 
  updateTime: null, 
  deleted: null, 
  createUser: null, 
  updateUser: null,
  imageInput: '',
  imageArr: [],
  courseScheduleList:[],
  }
export default {
  name: 'courseManage',
  components: {picUpload, pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      courseTime:null,
      signTime:null,
      permission: {
        add: ['admin', 'cyCourse:add'],
        edit: ['admin', 'cyCourse:edit'],
        view: ['admin', 'cyCourse:view'],
        del: ['admin', 'cyCourse:del']
      },
      categoryList: [],
      rules: {
        courseName:[
          { required: true, message: '请填写课程名称', trigger: 'blur' }
        ],
        level: [
          { required: true, message: '请选择课程等级', trigger: 'blur' }
        ],
        categoryIds: [
          { required: true, message: '请选择课程分类', trigger: 'blur' }
        ],
        courseIntroduce: [
          { required: true, message: '请填写课程简介', trigger: 'blur' }
        ],
        dayCount: [
          { required: true, message: '请填写课程天数', trigger: 'blur' }
        ],
      },
      detailDialog:false,
      detail:{},
      signUpData:[],
      masterList:[],
      showCreateSeat:false,
      seatForm:{
        rowNum:'',
        colNum:'',
        courseId:''
      },
      levelList:[]
    }
  },
  
  watch: {
  },
  created(){
    this.getCategory()
    this.getCourseLevel()
  },
  mounted(){
    // console.log(crudCourseMaster)
    this.$http('api/CourseMaster','get').then((res)=>{
      this.masterList = res.content
    })
  },
  methods: {
    getCourseLevel(){
      crudCyCourse.getCourseLevel().then((res)=>{
        this.levelList = res.content
      })
    },
    createSeat(id){
      this.seatForm.courseId = id;
      this.showCreateSeat = true;
    },
    submitSetSeat(){
      console.log(this.seatForm)
      let data = {
        colNum: this.seatForm.colNum,
        rowNum: this.seatForm.rowNum,
        courseId: this.seatForm.courseId,
      }
      crudCyCourse.courseSeat(this.seatForm).then((res)=>{
        this.$message({
          type:'success',
          message:'操作成功！'
        }).catch((res)=>{
          console.log(res)
        })
      })
    },
    cancelSubmit(){
      this.showCreateSeat = false;
      this.seatForm = {
        rowNum:'',
        colNum:'',
        courseId:'',
      }
    },
    handleChange(value){
      console.log(value)
      // this.form.categoryId = value;
    },
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    getCategory(){
      initData('/api/CourseCategory',this.getQueryParams()).then((res)=>{
        // console.log(res)
        res.content.forEach((item)=>{
          item.label = item.categoryName
          item.value = item.id
          item.children = item.categoryList
          item.categoryList.forEach((child)=>{
            child.label = child.categoryName
            child.value = child.id
            child.children = child.categoryList
          })
        })
        this.categoryList = res.content   
      })
    },
    getQueryParams: function() {
      return {
        page: 0,
        size: 200,
        sort: ['id,desc'],
      }
    },
    countChange(){
      let num = this.form.dayCount;
      for(let i = 0; i < num; i++){
        this.form.courseScheduleList.push({day:i+1, courseContent:''})
      }
    },
    submit(){
      this.form.imageArr = [this.form.imageInput]
      this.form.isModel = 1
      this.crud.submitCU()
    },

  }
}


</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
  .detail-box{
    width: 100%;
    font-size: 16px;
    padding: 24px;
    box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.18);
    border-radius: 12px;
  }
  .el-row{
    margin: 24px 0;
  }
  .title-w{
    font-size:19px;
    font-weight: bold;
    margin: 32px 0;
    color: #000;
  }
</style>
