<template>
  <BaseDialog v-bind="$attrs" v-on="listeners" @submit="onSubmit" @open="onOpen" :loading="loading">
    <el-form class="form" ref="form" v-bind="{ labelWidth: '7em', size: 'small' }" :model="formData" :rules="rules">
      <el-form-item required label="拆除人" prop="dismantlePeople">
        <el-input v-model="formData.dismantlePeople" placeholder="请输入修改人" clearable />
      </el-form-item>
      <el-form-item required label="拆除人电话" prop="dismantlePhone">
        <el-input v-model="formData.dismantlePhone" placeholder="请输入拆除人电话" clearable type="tel" />
      </el-form-item>
      <el-form-item required label="拆除时间" prop="dismantleTime">
        <el-date-picker
          type="datetime"
          v-model="formData.dismantleTime"
          placeholder="请选择拆除时间"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width:100%"
        />
      </el-form-item>
    </el-form>
  </BaseDialog>
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from 'vue-property-decorator'
import BaseDialog from '@/components/BaseDialog/index.vue'
import { ElForm } from 'element-ui/types/form'
import { telAndMobileReg } from '@/utils/constant'
import { IPoint, IPointDismountParams } from '../api/common'

@Component({ name: 'DismountForm', components: { BaseDialog } })
export default class DismountForm extends Vue {
  @Prop({ type: Array, default: () => [] }) selected!: IPoint[]
  @Prop({ type: Boolean, default: () => false }) loading!: boolean
  $refs!: { form: ElForm }
  $listeners!: { open: Function; submit: Function }
  formData: IPointDismountParams = {
    dismantlePeople: '',
    dismantlePhone: '',
    dismantleTime: '',
    id: undefined
  }

  get listeners() {
    const { submit, open, ...rest } = this.$listeners
    return rest
  }

  rules = {
    dismantlePeople: [{ required: true, message: '请输入拆除人姓名', trigger: 'blur' }],
    dismantlePhone: [
      { required: true, message: '请输入拆除人电话' },
      { type: 'string', max: 50, message: '联系方式不能超过50个字符' },
      { pattern: telAndMobileReg(), message: '请输入正确的联系方式', trigger: 'blur' }
    ],
    dismantleTime: [{ required: true, message: '请选择修改时间', trigger: 'change' }]
  }

  onSubmit() {
    this.$refs.form.validate((valid) => {
      if (valid) {
        this.$emit('submit', this.formData)
      }
    })
  }

  onOpen() {
    this.setDefaultValue()
    this.$emit('open')
  }

  setDefaultValue() {
    // console.log(this.$store.state.user)
    const { realName: dismantlePeople = '' } = this.$store.state.user || {}
    const { deviceId: id } = this.selected[0] || {}
    this.formData = {
      ...this.formData,
      id,
      dismantlePeople,
      dismantleTime: this.$moment().format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>
