<!-- 办理报名等选择课程 -->
<template>
  <div>
    <el-dialog title="选择课程" :visible.sync="open" width="700px" class="compact">
      <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="75px" class="compact">
        <el-form-item label="课程类型:" prop="courseTypeId">
          <el-select
            v-model="queryParams.courseTypeId"
            placeholder="请选择课程类别"
            clearable
            size="small"
            filterable
            default-first-option
            :loading="loadingSelect"
            @change="handleQuery()"
          >
            <el-option
              v-for="item in courseTypeOptions"
              :key="item.courseTypeId"
              :label="item.courseType"
              :value="item.courseTypeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="收费模式:" prop="cardType">
          <el-select
            v-model="queryParams.cardType"
            placeholder="请选择收费模式"
            clearable
            size="small"
            filterable
            default-first-option
            :loading="loadingSelect"
          >
            <el-option
              v-for="item in chargeTypeOptions"
              :key="item.chargeType"
              :label="item.chargeTypeName"
              :value="item.chargeType"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="24" class="text-right">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table ref="table" v-loading="loading" v-table-load-more="loadMore" :data="dataList" height="320" @select="handleSelect" @select-all="handleSelectAll">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="courseName" align="center" label="卡项名称">
          <template slot-scope="scope">
            <span>
<!--              <el-tag v-if="scope.row.studentCourseId" size="mini" closable>续费</el-tag>-->
              {{ scope.row.cardName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="courseTypes" width="120" header-align="center" label="课程类别" >
          <template slot-scope="scope" >
            {{scope.row.courseTypesName}}
          </template>
        </el-table-column>
        <el-table-column prop="restrictedCourses" width="200" header-align="center"  label="限制课程">
          <template slot-scope="scope" >
            <div v-if="scope.row.restrictedCourses=='1'">
              全部允许<br>
            </div>
            <div v-if="scope.row.restrictedCourses=='2'">
              <div>
                <ul>
                  <li v-for="item in scope.row.courses" :key="item.id">
                    {{ item.courseType }} - {{item.courseNames}}
                  </li>
                </ul>
              </div>

            </div>
          </template>
        </el-table-column>
        <el-table-column prop="restrictedTeacher" width="120" header-align="center" label="限制教练" >
          <template slot-scope="scope" >
            {{scope.row.restrictedTeacher=='1'?'全部允许':scope.row.teachersName}}
          </template>
        </el-table-column>
        <el-table-column prop="count" width="180" header-align="center" label="总天数/次数/时长" >
          <template slot-scope="scope" >
            总天数/有效期：{{ scope.row.days?scope.row.days+'天':'未定义'}}<br>
            课程总次数：{{ scope.row.count?scope.row.count+'次':'未定义'}}<br>
            应付金额：{{ scope.row.totalFee?scope.row.totalFee+'元':'未定义'}}
          </template>
        </el-table-column>
        <template slot="append">
          <div v-if="!hasMore" class="append-table-loading">没有更多数据</div>
          <div v-else-if="loading" class="append-table-loading">加载中</div>
        </template>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <div class="flex space-between">
          <div class="footer-left-txt">
<!--            已选择 {{ chooseCourseIdList.length }} 门课程-->
          </div>
          <div class="align-right">
            <el-button :loading="loading" type="primary" @click="chooseCourse">确定选择</el-button>
            <el-button @click="open = false">取 消</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listMemberCard  } from '@/api/school/sc/memberCardType'
import { select as courseTypeSelect } from '@/api/school/sc/course/courseType'

export default {
  data() {
    return {
      open: false,
      loading: true,
      loadingSelect: false,
      // 总条数
      total: 0,
      // 表格树数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cardType: undefined,
        courseTypeId: undefined,
        sale: '1'
      },
      // 课程类型
      courseTypeOptions: [],
      // 收费模式
      chargeTypeOptions: [{
        chargeType: 'count',
        chargeTypeName: '按次数'
      }, {
        chargeType: 'days',
        chargeTypeName: '按周期'
      }, {
        chargeType: 'total_fee',
        chargeTypeName: '按储值'
      }],
      chooseCardTypeId: [],
      hasMore: false
    }
  },
  methods: {
    initData() {
      this.dataList = []
      this.getList()
    },
    courseTypeList() {
      courseTypeSelect({}).then(response => {
        this.courseTypeOptions = response.data
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      listMemberCard(this.queryParams).then(response => {
        this.dataList = [
          ...this.dataList,
          ...response.data.rows
        ]
        this.total = response.data.total
        this.loading = false

        this.hasMore = response.data.current < response.data.pages

        this.$nextTick(() => {
          this.dataList.forEach(row => {
            if (this.chooseCardTypeId.indexOf(row.cardTypeId) !== -1) {
              this.$refs.table.toggleRowSelection(row, true)
            }
          })
        })
      })
    },
    loadMore() {
      if (this.hasMore) {
        this.queryParams.pageNum = this.queryParams.pageNum + 1
        this.getList()
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.dataList = []
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelect(selection, row) {
      const idx = this.chooseCardTypeId.indexOf(row.cardTypeId)
      console.log('chooseCardTypeId',this.chooseCardTypeId)
      if (idx !== -1) {
        this.chooseCardTypeId.splice(idx, 1)
      } else {
        this.chooseCardTypeId.push(row.cardTypeId)
      }
    },
    handleSelectAll(selection) {
      this.chooseCardTypeId = selection.map(item => item.cardTypeId)
    },
    chooseCourse() {
      if (this.chooseCardTypeId.length>1){
        this.msgError('只能提交1项，请确认选择数量！')
        return ;
      }
      this.$emit('chooseComplete', {
        chooseCardTypeIdList: this.chooseCardTypeId,
      })
      this.open = false
    }
  }
}
</script>
<style lang="scss" scoped>
  .text-right {
    text-align: right;
  }
  .append-table-loading {
    text-align: center;
    color: #848484;
    padding: 5px;
    font-size: 13px;
  }
</style>
