<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog
      :title="!dataForm.id ? '添加' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <!-- 新增和修改表单 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="80px">
    <el-form-item label="业主姓名" prop="ownerName">
      <el-input v-model="dataForm.ownerName" placeholder="业主姓名"></el-input>
    </el-form-item>
    <el-form-item label="业主类型" prop="ownerType">
      <el-input v-model="dataForm.ownerType" placeholder="业主类型"></el-input>
    </el-form-item>
    <el-form-item label="业主身份证" prop="ownerIdcard">
      <el-input v-model="dataForm.ownerIdcard" placeholder="业主身份证"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="电子邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="电子邮箱"></el-input>
    </el-form-item>
    <el-form-item label="紧急联系人" prop="emergencyContact">
      <el-input v-model="dataForm.emergencyContact" placeholder="紧急联系人"></el-input>
    </el-form-item>
    <el-form-item label="紧急联系电话" prop="emergencyPhone">
      <el-input v-model="dataForm.emergencyPhone" placeholder="紧急联系电话"></el-input>
    </el-form-item>
    <el-form-item label="业主状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="业主状态"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import * as api from '@/api/pmowner.js'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          ownerName: '' , 
          ownerType: '' , 
          ownerIdcard: '' , 
          phone: '' , 
          email: '' , 
          emergencyContact: '' , 
          emergencyPhone: '' , 
          status: '' , 
          remark: '' , 
        },
        dataRule: {
          ownerName: [
            { required: true, message: '业主姓名  为必填项', trigger: 'blur' }
          ],
          ownerType: [
            { required: true, message: '业主类型  为必填项', trigger: 'blur' }
          ],
          ownerIdcard: [
            { required: true, message: '业主身份证  为必填项', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系电话  为必填项', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '电子邮箱  为必填项', trigger: 'blur' }
          ],
          emergencyContact: [
            { required: true, message: '紧急联系人  为必填项', trigger: 'blur' }
          ],
          emergencyPhone: [
            { required: true, message: '紧急联系电话  为必填项', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '业主状态  为必填项', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注  为必填项', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) { // 初始化表单验证规则
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(function() {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            api.pmownerDetailApi(id).then(function(res) {
              this.dataForm = res;
            }.bind(this))
          }
        }.bind(this))
      },
      // 表单数据提交
      dataSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              api.pmownerUpdateApi(this.dataForm).then(function(res) {
                this.$message.success('保存成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            } else {
              api.pmownerCreateApi(this.dataForm).then(function(res) {
                this.$message.success('新增成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            }
          }
        })
      }
    }
  }
</script>
