<template>
  <el-dialog title="修改服务时间" :visible.sync="open" width="800px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="服务时间段" prop="serviceTime">
          <el-input  @focus="getLayDate" id="inputUI" v-model="form.serviceTime"></el-input>
        </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :disabled="disabled" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {auditEntApi,getEntInfoByIdApi,updateServiceTimeApi} from "@/api/admin/ent/ent"
  export default {
    data () {
      return {
        open: false,
        disabled: false,
        type: '',
        form: {
          serviceTime: "",
          endDate: '',
          startDate: ''
        },
        rules: {
          serviceTime: [
            {required: true, message: '请设置服务时间段', trigger: 'change'}
          ]
        }
      }
    },
    methods: {
      init(id) {
        this.open = true;
        this.form.id = id;
        if(this.form.id) {
          this.getData()
        }
      },
      getData() {
        getEntInfoByIdApi(this.form.id).then(res => {
          this.form = res.data || {}
          if(this.form.startDate && this.form.endDate) {
            this.form.serviceTime = this.form.startDate + ' 至 ' + this.form.endDate
          }else {
            this.form.serviceTime = ""
          }
        })
      },
      submitForm() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.disabled = true
            if(this.form.serviceTime) {
              this.form.startDate = this.form.serviceTime.split('至')[0].trim()
              this.form.endDate = this.form.serviceTime.split('至')[1].trim()
            }else {
              this.form.startDate = ''
              this.form.endDate = ''
            }
            updateServiceTimeApi(this.form).then(res => {
              this.disabled = false;
              if(res.code == 200) {
                this.$message({
                  message: '服务时间修改成功',
                  type: 'success'
                });
              }
              this.open = false;
              this.$emit('refreshData');
            })
          }
        })
      },
      cancel() {
        this.open = false
      },
      getLayDate(){
        let _self = this
          laydate.render({
            elem: '#inputUI', // 输入框的id属性，用来绑定到dom
            type: 'date',
            range: true,
            done: (value) => {
              _self.form.serviceTime = value; // 将选中的值赋给组件中的变量
            }
          })
        }
    },
    watch: {
      "form.serviceTime"(val) {
        if(val) {
          let arr = val.split('至')
          this.form.startDate = arr[0].trim()
          this.form.endDate = arr[1].trim()
        }else {
          this.form.startDate = ''
          this.form.endDate = ''
        }
      }
    }
  }
</script>

<style>
</style>
