<template>
  <el-dialog :title="chooseCourseObj.title" :visible.sync="chooseCourseObj.open" :width="chooseCourseObj.width" @open="init()" append-to-body>
    <el-form ref="auditForm" inline :model="chooseCourseObj.queryParams" label-width="80px">
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          size="small"
          placeholder="请输入课程名称"
          v-model="chooseCourseObj.queryParams.courseName"
          clearable
        />
      </el-form-item>
      <el-form-item label="课程类型:" prop="courseTypeId">
        <el-select
          v-model="chooseCourseObj.queryParams.courseTypeId"
          placeholder="请选择课程类别"
          clearable
          size="small"
          filterable
          default-first-option
          :loading="loadingSelect"
        >
          <el-option
            v-for="item in courseTypeOptions"
            :key="item.courseTypeId"
            :label="item.courseType"
            :value="item.courseTypeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getCourseList">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table class="mt20" :data="chooseCourseObj.list" ref="table" v-loading="chooseCourseObj.loading" max-height="500" border
              row-key="courseId" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :reserve-selection="true"/>
      <el-table-column prop="courseName" width="180" align="center" label="课程名称" />
      <el-table-column prop="courseTypeName" align="center" label="课程类型" />
      <el-table-column prop="teachingMode" align="center" label="授课模式">
        <template slot-scope="scope">
          <div v-if="scope.row.teachingMode === '1'">
            <el-tooltip class="item" effect="dark" content="班课" placement="right">
              <svg-icon icon-class="one-to-many" style="height: 25px;width: 25px;" />
            </el-tooltip>
          </div>
          <div v-if="scope.row.teachingMode === '2'">
            <el-tooltip class="item" effect="dark" content="一对一" placement="right">
              <svg-icon icon-class="one-to-one" style="height: 25px;width: 25px;" />
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="tuitionFee" align="center" label="课程价值/节" >
        <template slot-scope="scope">
          {{ scope.row.tuitionFee }} 元
        </template>
      </el-table-column>
      <el-table-column prop="claFee" align="center" label="教练课时费/节" >
        <template slot-scope="scope">
          {{ scope.row.claFee }} 元
        </template>
      </el-table-column>
      <el-table-column prop="campusIds" align="center" label="开课校区">
        <template slot-scope="scope">
          <div >
            <span v-if="scope.row.campusIds==null">全部校区</span>
            <span v-else>
              {{ scope.row.campusIds.split(',').length }}个校区<br>
              {{ scope.row.storeNames }}
            </span>

          </div>
        </template>
      </el-table-column>
      <el-table-column prop="star" width="200" align="center" label="课程难度">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.star"
            :disabled="true"
            :colors="['#F7BA2A', '#F7BA2A', '#F7BA2A']"
            :void-color="'#C6D1DE'"
            :max="5"
          />
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="chooseCourseObj.total>0"
      :total="chooseCourseObj.total"
      :page.sync="chooseCourseObj.queryParams.pageNum"
      :limit.sync="chooseCourseObj.queryParams.pageSize"
      @pagination="getCourseList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">提 交</el-button>
      <el-button @click="chooseCourseObj.open=false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import ProductCategorySelect from "@/views/components/ProductCategorySelect.vue";
import {listCourseForProduct} from "@/api/pms/product";
import {select as courseTypeSelect} from "@/api/school/sc/course/courseType";

export default {
  components: {ProductCategorySelect},
  name: "CourseSelect",
  data() {
    return {
      loadingSelect: false,
      // 课程类型
      courseTypeOptions: [],
      selectCourseIds: [],
      selectCourse: [],
      chooseCourseObj: {
        loading: false,
        open: false,
        title: "选择课程",
        list: [],
        width: '60%',
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          sale:'1',
          courseTypeId: null,
          courseName: null
        },
        total: 0
      }
    }
  },
  methods: {
    courseTypeList() {
      courseTypeSelect({}).then(response => {
        this.courseTypeOptions = response.data
      })
    },
    submitForm() {
      if (this.selectCourse.length>1){
        this.$message.warning('请选择一条记录进行提交');
        return;
      }
      if (this.selectCourse.length==1){
        this.$emit('onComplete', this.selectCourse[0])
      }
      this.chooseCourseObj.open = false
    },
    async init(chooseCourseId) {
      this.chooseCourseObj.queryParams.pageNum = 1;
      this.chooseCourseObj.queryParams.courseId=chooseCourseId
      await this.getCourseList();
      await this.courseTypeList();
      this.chooseCourseObj.open = true
      this.$nextTick(()=>{
        this.$refs.table.clearSelection()
      })
    },
    judge() {
      this.$nextTick(()=>{
        this.chooseCourseObj.list.forEach(ele => {
          if (this.selectCourse.some(item => item.id === ele.id)) {
            this.$refs.table.toggleRowSelection(ele, true)
          } else {
            this.$refs.table.toggleRowSelection(ele, false)
          }
        });
      })
    },
    handleSelectionChange(selection) {
      this.selectCourse = selection
    },
    async getCourseList() {
      this.chooseCourseObj.loading = true;
      const query = {...this.chooseCourseObj.queryParams, pageNum: undefined, pageSize: undefined};
      const {pageNum, pageSize} = this.chooseCourseObj.queryParams;
      const pageReq = {page: pageNum - 1, size: pageSize};
      await listCourseForProduct(query, pageReq).then(response => {
        this.chooseCourseObj.list = response.rows;
        this.chooseCourseObj.total = response.total;
        this.chooseCourseObj.loading = false;
      });
    },
  }
}
</script>

<style lang="stylus">
.line
  display flex
  align-items center
  margin-bottom: 5px;
  border-bottom 1px dashed #ccc

.line:last-child
  border-bottom 0 !important
  margin-bottom 0 !important
</style>
