<template>
  <div>
    <xy-dialog ref="dialog" :width="50" :is-show.sync="isShow" :type="'form'" title="预约次数调整" :form="form" :rules='rules'
      @submit="submit">
      <template v-slot:course_id>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>所属课程：
          </div>
          <div class="xy-table-item-content">
            <div v-if="type==='editor'">
              {{form.course?form.course.name:''}}
            </div>
            <div v-else>
              <el-select v-model="form.course_id" style="width:100%" 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>
      </template>
      <template v-slot:user_id>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>姓名：
          </div>
          <div class="xy-table-item-content">
            <div v-if="type==='editor'">
              {{form.user?form.user.name:''}}
            </div>
            <div v-else>
              <el-select v-model="form.user_id" style="width:100%"
                 filterable remote reserve-keyword
                 placeholder="输入姓名查找人员"
                :remote-method="remoteMethod" :loading="loading">
                <el-option v-for="item in user_options" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </div>
          </div>
        </div>
      </template>
      <template v-slot:total>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>可预约次数：
          </div>
          <div class="xy-table-item-content">
            <el-input type="number" style="width:100%" v-model="form.total"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:start_time>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>开始日期：
          </div>
          <div class="xy-table-item-content">
            <el-date-picker v-model="form.start_time" style="width:100%" format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期">
            </el-date-picker>
          </div>
        </div>
      </template>
      <template v-slot:end_time>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>结束日期：
          </div>
          <div class="xy-table-item-content">
            <el-date-picker v-model="form.end_time" style="width:100%" format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期">
            </el-date-picker>
          </div>
        </div>
      </template>

    </xy-dialog>

  </div>
</template>

<script>
  // import {
  //   save
  // } from "@/api/student/index.js"
  import {
    index as indexCourse
  } from "@/api/course/index.js"
  import {
    indexStudy
  } from '@/api/student/index.js'
  import {
    save
  } from '@/api/book/count.js'
  export default {
    components: {

    },
    data() {
      return {
        isShow: false,
        type:'add',
        user_options:[],
        course_options:[],
        loading:false,
        form: {
          course_id:'',
          user_id:"",
          total: '',
          start_time: '',
          end_time: '',
        },

        rules: {
          total: [{
            required: true,
            message: '请填可预约次数'
          }]
        }
      }
    },
    created() {
      this.getCourseList()
    },
    methods: {
      async getCourseList() {
        const res = await indexCourse({
          page: 1,
          page_size: 999,
          show_relation: ['type_detail'],
          sort_name: 'start_date',
          sort_type: 'DESC',
        })
        this.course_options = res.data
      },
      remoteMethod(query) {
        if (query !== '') {
          this.loading = true;
          this.getUserList(query)
        } else {
          this.user_options = [];
        }
      },
      async getUserList(query) {
        const res = await indexStudy({
          page: 1,
          page_size: 99,
          name: query
        })
        this.user_options = res.list.data
        if(this.user_options.length===0){
          this.$Message.warning("当前搜索没有学员")
        }
        this.loading = false
      },
      submit() {
        save({
          ...this.form
        }).then(res => {
          this.$message({
            type: 'success',
            message: '调整成功'
          })
          this.isShow = false
          this.$emit('refresh')
        })
      },

      setRow(row) {
        this.form = this.base.requestToForm(row, this.form)
      }
    },
    watch: {
      isShow(newVal) {
        if (newVal) {

        } else {
          this.form={
            course_id:'',
            user_id:"",
            total: '',
            start_time: '',
            end_time: '',
          }
          this.type='add'
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">

</style>
