<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: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: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 disabled v-model="form.name" placeholder="请输入场地名称" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:real_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.real_name" placeholder="请输入小程序展示名称" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>

      <template v-slot:appointment_type_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-select v-model="form.appointment_type_id" placeholder="请选择场地类型" style="width: 100%;">
              <el-option v-for="item in types_options" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </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 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: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'
              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:door>
        <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>{{form.door}}</div>
          </div>
        </div>
      </template>

      <template v-slot:room>
        <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>{{form.room}}</div>
          </div>
        </div>
      </template> -->

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

<script>
  import myMixins from "@/mixin/selectMixin.js";
  import {
    show,
    save
  } from "@/api/book/appointment.js"
  import {
    index as typeIndex
  } from "@/api/book/type.js"
  export default {
    mixins: [myMixins],
    components: {},
    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',
        form: {
          no: '',
          sort:0,
          name: '',
          real_name:'',
          appointment_type_id:'',
          content: '',
          total: '',
          status: 1,
          use_student: 1,
          dateRange: ['09:00','17:30'],
          file_ids: [],
          start_time:'09:00',
          end_time:'17:30',
          // door:'',
          // room:''
        },
        action: `${process.env.VUE_APP_UPLOAD_API}`,
        types_options:[],
        fileList: [],
        rules: {
          name: [{
            required: true,
            message: '请输入场地名称'
          }],
        },
      }
    },
    created() {
      this.getList()
    },
    methods: {
      async getList() {
        const res = await typeIndex({
          page: 1,
          page_size: 999,
        })
        this.types_options = res.data
      },
      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.sort = res.sort?res.sort:0
          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]:['','']
          // this.form.door = res.door?JSON.parse(res.door):''
          // this.form.room = res.room?JSON.parse(res.room):''
        })
      },


    },
    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>
