<template>
  <div>
      <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="请选择收费模式"
            size="small"
            filterable
            default-first-option
            :loading="loadingSelect"
            @change="handleQuery()"
          >
            <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"
                @row-click="handleRowClick"
                border
                :header-cell-style="{'text-align': 'center'}"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="courseName" width="300" align="center" label="卡项名称">
          <template slot-scope="scope">
            <span>
              {{ scope.row.cardName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="courseTypes" width="300" header-align="center" label="课程类别" >
          <template slot-scope="scope" >
            {{scope.row.courseTypesName}}
          </template>
        </el-table-column>
        <el-table-column prop="restrictedCourses" width="300" 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="300" 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="300" 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>
</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: 'count',
        courseTypeId: undefined,
        sale: '1'
      },
      // 课程类型
      courseTypeOptions: [],
      // 收费模式
      chargeTypeOptions: [{
        chargeType: 'count',
        chargeTypeName: '按次数'
      }, {
        chargeType: 'days',
        chargeTypeName: '按周期'
      }, {
        chargeType: 'total_fee',
        chargeTypeName: '按储值'
      }],
      chooseCardTypeId: [],
      chooseCardTypes: [],
      hasMore: false
    }
  },
  created() {
    this.initData()
    this.courseTypeList()
  },
  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.chooseCardTypeId=[]
        this.chooseCardTypes=[]
      })
    },
    loadMore() {
      if (this.hasMore) {
        this.queryParams.pageNum = this.queryParams.pageNum + 1
        this.getList()
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.queryParams.cardType==undefined ||this.queryParams.cardType==''){
        this.msgError('请选择收费模式！')
        return;
      }
      this.queryParams.pageNum = 1
      this.dataList = []
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelect(selection, row) {
      const idx = this.chooseCardTypeId.indexOf(row.cardTypeId)
      if (idx !== -1) {
        this.chooseCardTypeId.splice(idx, 1)
        this.chooseCardTypes.splice(idx, 1)
      } else {
        this.chooseCardTypeId.push(row.cardTypeId)
        this.chooseCardTypes.push(row)
      }
    },
    handleRowClick(row) {
      // 关键：调用表格的toggleRowSelection方法切换选中状态
      this.$refs.table.toggleRowSelection(row);
      this.handleSelect('',row)
      // 可选：如果需要获取当前所有选中的行，可添加以下代码
      // const selectedRows = this.$refs.tableRef.selection;
      // console.log('当前选中的行：', selectedRows);
    }
  }
}
</script>
<style lang="scss" scoped>
.text-right {
  text-align: right;
}
.append-table-loading {
  text-align: center;
  color: #848484;
  padding: 5px;
  font-size: 13px;
}
</style>
