<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: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:introduce>
        <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.introduce" type="textarea" placeholder="请输入简介" clearable
              style="width: 100%;"></el-input>
          </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 style="width:100%" @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:is_book>
        <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.is_book" 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:floor>
        <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.floor" 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:sort>
        <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.sort" type="number" placeholder="请输入排序" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:image_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">
            <el-upload :action="action" multiple class='upload-demo' accept=".PNG,.jpg,.jpeg,.bmp,.svg"
              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>
      <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">
            <my-tinymce v-if="showTinymce" @input="saveContent" :value="form.content"></my-tinymce>
          </div>
        </div>
      </template>
    </xy-dialog>
  </div>
</template>

<script>
  import myMixins from "@/mixin/selectMixin.js";
  import {
    show,
    save
  } from "@/api/book/type.js"
  export default {
    mixins: [myMixins],
    components: {},
    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',
        showTinymce: false,
        form: {
          name: '',
          introduce: '',
          dateRange: ['09:00', '17:30'],
          is_book: 0,
          floor: '',
          total: '',
          sort: 0,
          image_id: [],
          start_time: '09:00',
          end_time: '17:30',
          content: ''
        },
        action: `${process.env.VUE_APP_UPLOAD_API}`,
        fileList: [],
        rules: {
          name: [{
            required: true,
            message: '请输入类型名称'
          }],
        },
      }
    },
    created() {},
    methods: {
      saveContent(e) {
        this.form.content = e
      },
      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.image_id = _files
        save({
          ...this.form
        }).then(res => {
          this.$message({
            type: 'success',
            message: '保存场地类型成功'
          })
          this.isShow = false
          this.$emit('refresh')
        })
      },
      getDetail() {
        show({
          id: this.id
        }).then(res => {
          this.form = this.base.requestToForm(res, this.form)
          this.fileList = res.image
          this.form.dateRange = res.start_time ? [res.start_time, res.end_time] : ['', ''],
            this.form.sort = res.sort ? res.sort : 0
          this.form.is_book = res.is_book ? res.is_book : 0
          this.showTinymce = true
        })
      },


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

  }
</script>

<style scoped lang="scss">
  ::v-deep .content,
  ::v-deep .image_id {
    flex-basis: 100%;
  }
</style>
