<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog
      :title="!dataForm.id ? '添加' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible"
      :append-to-body="true"
  >
    <!-- 新增和修改表单 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="80px">
    <el-form-item label="租户姓名" prop="tenantName">
      <el-input v-model="dataForm.tenantName" placeholder="租户姓名"></el-input>
    </el-form-item>
    <el-form-item label="租户类型(个人、企业)" prop="tenantType">
      <el-input v-model="dataForm.tenantType" placeholder="租户类型(个人、企业)"></el-input>
    </el-form-item>
    <el-form-item label="租户身份证" prop="idCard">
      <el-input v-model="dataForm.idCard" placeholder="租户身份证"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phome">
      <el-input v-model="dataForm.phome" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="工作单位" prop="company">
      <el-input v-model="dataForm.company" 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/pmtenant.js'
  export default {

    // tenant_status
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          tenantName: '' ,
          tenantType: '' ,
          idCard: '' ,
          phome: '' ,
          company: '' ,
          status: '' ,
          remark: '' ,
          ownerId: '',
        },
        dataRule: {
          tenantName: [
            { required: true, message: '租户姓名  为必填项', trigger: 'blur' }
          ],
          tenantType: [
            { required: true, message: '租户类型(个人、企业)  为必填项', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '租户身份证  为必填项', trigger: 'blur' }
          ],
          phome: [
            { required: true, message: '联系电话  为必填项', trigger: 'blur' }
          ],
          company: [
            { required: true, message: '工作单位  为必填项', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态  为必填项', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注  为必填项', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id, ownerId) { // 初始化表单验证规则
        this.dataForm.id = id || 0
        this.dataForm.ownerId = ownerId || ''
        this.visible = true
        this.$nextTick(function() {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            api.pmtenantDetailApi(id).then(function(res) {
              this.dataForm = res;
            }.bind(this))
          } else if (ownerId) {
            this.dataForm.ownerId = ownerId;
          }
        }.bind(this))
      },
      // 表单数据提交
      dataSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              api.pmtenantUpdateApi(this.dataForm).then(function(res) {
                this.$message.success('保存成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            } else {
              api.pmtenantCreateApi(this.dataForm).then(function(res) {
                this.$message.success('新增成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            }
          }
        })
      }
    }
  }
</script>
