<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:dateRange>
        <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-time-picker @change="changeDateRange" value-format="HH:mm" format="HH:mm" is-range v-model="form.dateRange" range-separator="至"
              start-placeholder="开始时间" end-placeholder="结束时间" placeholder="选择时间范围">
            </el-time-picker>
          </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,
          dateRange: ['08:30','17:30'],
          file_ids: [],
          start_time:'08:30',
          end_time:'17:30'
        },
        action: `${process.env.VUE_APP_UPLOAD_API}`,
        fileList: [],
        rules: {
          name: [{
            required: true,
            message: '请输入场地名称'
          }],
        },
      }
    },
    created() {},
    methods: {
      changeDateRange(e) {
        console.log(e)
        if (e) {
          this.form.start_time = e[0]
          this.form.end_time = e[1]
        } else {
          this.form.start_time = ''
          this.form.end_time = ''
        }
      },
      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
          this.form.dateRange = res.start_time?[res.start_time,res.end_time]:['','']
        })
      },


    },
    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">
  ::v-deep .file_ids{
    flex-basis: 100%;
  }
</style>
