<template>
  <div>
    <xy-dialog ref="dialog" :is-show.sync="isShow" type="form" :title="type === 'add' ? '新增时段' : '编辑时段'" :form="form"
      :rules="rules" @submit="submit">
      <template v-slot:start_time>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>开始时间：
          </div>
          <div class="xy-table-item-content">
                <el-time-picker
                  v-model="form.start_time"
                  format="HH:mm"
                  value-format="HH:mm"
                  placeholder="选择开始时间">
                </el-time-picker>
          </div>
        </div>
      </template>
      <template v-slot:end_time>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>结束时间：
          </div>
          <div class="xy-table-item-content">
               <el-time-picker
                  v-model="form.end_time"
                  format="HH:mm"
                  value-format="HH:mm"
                  placeholder="选择结束时间">
                </el-time-picker>
          </div>
        </div>
      </template>


    </xy-dialog>


  </div>
</template>

<script>
  import {
    show,
    save
  } from "@/api/resource/visittime.js"
  export default {
    components: {},
    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',

        form: {
          start_time:"",
          end_time:""
        },
        rules:{
          start_time: [
            {
              required: true,
              message: "请选择开始时间",
            },
          ],
          end_time: [
            {
              required: true,
              message: "请选择结束时间",
            },
          ]
        }
      }
    },
    created() {
    },
    methods: {

      async getDetail() {
        const res = await show({
          id: this.id
        })
        this.form = res
      },
      submit() {
        let that = this
        if (this.type === 'editor') {
          this.form.id = this.id
        }else{
          this.form.id = ""
        }
        save({
          ...that.form
        }).then(res => {
          this.$successMessage(this.type, '时段')
          this.isShow = false
          this.$emit('refresh')
        })
      }
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          }
        } else {
          this.id = ''
          this.type = ''
          this.$refs['dialog'].reset()
        }
      }
    }

  }
</script>

<style scoped lang="scss">
  .xy-table-item-label {
    width: 160px;
  }

  .img__delete {
    transform: scale(0.8, 0.8);

    position: absolute;
    top: 4px;
    right: 4px;
  }
</style>
