<template>
  <div>

    <div>
      <div ref="lxHeader">
        <lx-header icon="md-apps" :text="$route.meta.title" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content">

            <div class="searchwrap" style="display: flex;align-items: center;">
              <div>
                <div>
                  <el-input v-model="select.name" placeholder="请输入姓名"></el-input>
                </div>
                <!-- <div>
                  <el-input v-model="select.course_name" placeholder="请输入课程名称"></el-input>
                </div> -->
                <div>
                    <el-select clearable v-model="select.course_id" placeholder="请选择课程">
                      <el-option
                        v-for="item in course_options"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                      </el-option>
                    </el-select>
                </div>
              </div>
              <div>
                <el-button type="primary" size="small" @click="select.page=1,getList()">查询</el-button>
              </div>
            </div>
          </div>
        </lx-header>
      </div>
    </div>

    <div>
      <xy-table :list="list" @pageIndexChange="pageIndexChange" @pageSizeChange="pageSizeChange" :total="total"
        :table-item="table_item">
        <template v-slot:appointments_count>
          <el-table-column align='center' label="已预约次数" header-align="center" width="120">
            <template slot-scope="scope">
              <div @click="showCounts(scope.row.name)" v-if="scope.row.appointments_count>0"
                style="color:#0077CC;text-decoration: underline;cursor: pointer;">{{scope.row.appointments_count}}</div>
              <div v-else>{{scope.row.appointments_count}}</div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:appointments_can>
          <el-table-column align='center' label="预约剩余次数" header-align="center" width="120">
            <template slot-scope="scope">
              <div>
                {{scope.row.appointment_total - scope.row.appointments_count>0?scope.row.appointment_total - scope.row.appointments_count:0}}
              </div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:course_list>
          <el-table-column align='left' label="课程名称" header-align="center">
            <template slot-scope="scope">
              <div v-for="(item,index) in scope.row.courses">
                <div>{{item.name}}</div>
              </div>
            </template>
          </el-table-column>
        </template>

        <template v-slot:btns>
          <el-table-column align='center' label="操作" fixed="right" width="80" header-align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="editCount(scope.row)">调整</el-button>
            </template>
          </el-table-column>
        </template>
      </xy-table>
    </div>
    <addCount ref="addCount" @refresh="getList"></addCount>
    <showCount ref="showCount"></showCount>
  </div>
</template>

<script>
  import addCount from './components/addCount.vue';
  import showCount from './components/showCount.vue';
  import {
    indexStudy
  } from '@/api/student/index.js'
  import {
    index as courseIndex
  } from "@/api/course/index.js"
  export default {
    components: {
      addCount,
      showCount
    },
    data() {
      return {
        select: {
          name: '',
          course_name:'',
          course_id:'',
          page: 1,
          page_size: 10,
        },
        course_options:[{
          label:'进行中',
          value:1
        },{
          label:'其他',
          value:0
        }],
        list: [],
        total:0,
        table_item: [{
          prop: 'course_list',
          label: '课程名称',
          align: 'left',
        },{
          prop: 'username',
          label: '预约人',
          align: 'center',
          width:120
        }, {
          prop: 'appointment_total',
          label: '可预约总数',
          align: 'center',
          width:120
        }, {
          prop: 'appointments_count',
          label: '已预约次数',
          align: 'center',
          width:120
        }, {
          prop: 'appointments_can',
          label: '预约剩余次数',
          align: 'center',
          width:120
        }]

      }
    },
    created() {
      this.getList()
      this.getCourseList()
    },
    methods: {
      pageIndexChange(e) {
        this.select.page = e
        this.getList()
      },
      pageSizeChange(e) {
        this.select.page_size = e
        this.select.page = 1
        this.getList()
      },
      async getCourseList() {
        const res = await courseIndex({
          page: 1,
          page_size: 999,
          sort_name: 'id',
          sort_type: 'DESC',
        })
        this.course_options = res.data
      },
      async getList() {
        const res = await indexStudy({
          page: this.select.page,
          page_size: this.select.page_size,
          name: this.select.name,
          course_name:this.select.course_name,
          course_id: this.select.course_id
        })
        this.list = res.list.data
        this.total = res.list.total
      },
      editCount(row) {
        this.$refs.addCount.setRow(row)
        this.$refs.addCount.isShow = true
      },
      showCounts(name) {
        this.$refs.showCount.setName(name)
        this.$refs.showCount.isShow = true
      },
    }
  }
</script>

<style lang="scss" scoped>
  .searchwrap {
    display: flex;
    align-items: center;

    &>div {
      display: flex;
      align-items: center;

      &>div {

        margin-right: 10px;
      }

    }
  }
</style>
