<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog
    :title="!dataForm.id ? '添加-ADD' : '修改-EDITE'"
    :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-item label="删除标志（0代表存在 2代表删除）" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="删除标志（0代表存在 2代表删除）"></el-input>
    </el-form-item>
    <el-form-item label="创建部门" prop="createDept">
      <el-input v-model="dataForm.createDept" placeholder="创建部门"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="修改人"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="租户ID" prop="tenantId">
      <el-input v-model="dataForm.tenantId" placeholder="租户ID"></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: '' , 
          delFlag: '' , 
          createDept: '' , 
          createBy: '' , 
          createTime: '' , 
          updateBy: '' , 
          updateTime: '' , 
          tenantId: '' 
        },
        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' }
          ],
          delFlag: [
            { required: true, message: '删除标志（0代表存在 2代表删除）  为必填项', trigger: 'blur' }
          ],
          createDept: [
            { required: true, message: '创建部门  为必填项', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人  为必填项', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间  为必填项', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '修改人  为必填项', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间  为必填项', trigger: 'blur' }
          ],
          tenantId: [
            { required: true, message: '租户ID  为必填项', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) { // 初始化表单验证规则
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            api.pmownerDetailApi(id).then(res => {
                this.dataForm = res;
            })
          }
        })
      },
      // 表单数据提交
      dataSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (dataForm.id) {
              api.pmownerUpdateApi(this.dataForm).then(res =>{
                  this.$message.success('保存成功')

                  this.visible = false
                  this.$emit('refreshDataList')
              });
            } else {
              api.pmownerCreateApi(this.dataForm).then(res =>{
                  this.$message.success('新增成功')

                  this.visible = false
                  this.$emit('refreshDataList')
              });
            }
          
          }
        })
      }
    }
  }
</script>
