<template>
  <div>
    <xy-dialog ref="dialog" :width="70" :is-show.sync="isShow" :type="'form'" :title="type === 'add' ? '新增课程' : '编辑课程'"
      :form="form" :rules='rules' @submit="submit">
      <template v-slot:no>
        <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 v-model="form.no" placeholder="请输入场地编号" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:name>
        <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 v-model="form.name" placeholder="请输入场地名称" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:content>
        <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 v-model="form.content" type="textarea" placeholder="请输入场地简介" clearable
              style="width: 100%;"></el-input>
          </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 v-model="form.total" placeholder="请输入场地容纳人数" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:status>
        <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-select v-model="form.status" placeholder="请选择状态" style="width: 100%;">
              <el-option v-for="item in types_status" :key="item.id" :label="item.value" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:use_student>
        <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-select v-model="form.status" placeholder="请选择" style="width: 100%;">
              <el-option v-for="item in false_or_true" :key="item.id" :label="item.value" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>

      <template v-slot:file_ids>
        <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-upload :action="action" multiple class='upload-demo' list-type="picture-card" :file-list="fileList"
              ref="pictureUpload" :auto-upload="true" :on-success="uploadSuccesspublic" :on-remove="uploadRemovepublic">
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </div>
      </template>


    </xy-dialog>
  </div>
</template>

<script>
  import myMixins from "@/mixin/selectMixin.js";
  import {
    show,
    save
  } from "@/api/book/appointment.js"
  export default {
    mixins: [myMixins],
    components: {},
    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',
        form: {
          no: '',
          name: '',
          content: '',
          total: '',
          status: 1,
          use_student: 1,
          file_ids:[]
        },
        action: `${process.env.VUE_APP_UPLOAD_API}`,
        fileList: [],
        rules: {
          name: [{
            required: true,
            message: '请输入场地名称'
          }],
        },
      }
    },
    created() {},
    methods: {
      uploadSuccesspublic(response, file, fileList) {
        this.fileList = fileList
      },
      uploadRemovepublic(file, fileList) {
        this.fileList = fileList
      },
      submit() {
        if (this.id) {
          this.form.id = this.id
        }else{
          this.form.id = ''
        }
        let _files = []
        if (this.fileList.length > 0) {
          this.fileList.map(item => {
            if (item.response) {
              _files.push(item.response.id)
            } else {
              _files.push(item.id)
            }
          })
        }
        this.form.file_ids = _files
        save({
          ...this.form
        }).then(res => {
          this.$message({
            type: 'success',
            message: '保存预约场地成功'
          })
          this.isShow = false
          this.$emit('refresh')
          // this.active = 1

        })
      },
      getDetail() {
        show({
          id: this.id
        }).then(res => {
          this.form = this.base.requestToForm(res, this.form)
          this.fileList = res.files
          this.form.status = res.status?res.status:0
          this.form.use_student = res.use_student?res.use_student:0
        })
      },


    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          }
        } else {
          this.id = ''
          this.fileList = []
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">

</style>
