<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="楼栋id" prop="buildingId">
      <el-input v-model="dataForm.buildingId" placeholder="楼栋id"></el-input>
    </el-form-item>
    <el-form-item label="项目id" prop="projectId">
      <el-input v-model="dataForm.projectId" placeholder="项目id"></el-input>
    </el-form-item>
    <el-form-item label="设施名称" prop="facilityName">
      <el-input v-model="dataForm.facilityName" placeholder="设施名称"></el-input>
    </el-form-item>
    <el-form-item label="设施类型" prop="facilityType">
      <el-input v-model="dataForm.facilityType" placeholder="设施类型"></el-input>
    </el-form-item>
    <el-form-item label="设施型号" prop="model">
      <el-input v-model="dataForm.model" placeholder="设施型号"></el-input>
    </el-form-item>
    <el-form-item label="安装位置" prop="installLocation">
      <el-input v-model="dataForm.installLocation" placeholder="安装位置"></el-input>
    </el-form-item>
    <el-form-item label="安装日期" prop="installDate">
      <el-input v-model="dataForm.installDate" placeholder="安装日期"></el-input>
    </el-form-item>
    <el-form-item label="预计使用寿命" prop="serviceLife">
      <el-input v-model="dataForm.serviceLife" 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/pmfacility.js'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          buildingId: '' ,
          projectId: '' ,
          facilityName: '' ,
          facilityType: '' ,
          model: '' ,
          installLocation: '' ,
          installDate: '' ,
          serviceLife: '' ,
          status: '' ,
          remark: '' ,
        },
        dataRule: {
          buildingId: [
            { required: true, message: '楼栋id  为必填项', trigger: 'blur' }
          ],
          projectId: [
            { required: true, message: '项目id  为必填项', trigger: 'blur' }
          ],
          facilityName: [
            { required: true, message: '设施名称  为必填项', trigger: 'blur' }
          ],
          facilityType: [
            { required: true, message: '设施类型  为必填项', trigger: 'blur' }
          ],
          model: [
            { required: true, message: '设施型号  为必填项', trigger: 'blur' }
          ],
          installLocation: [
            { required: true, message: '安装位置  为必填项', trigger: 'blur' }
          ],
          installDate: [
            { required: true, message: '安装日期  为必填项', trigger: 'blur' }
          ],
          serviceLife: [
            { 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.pmfacilityDetailApi(id).then(function(res) {
              this.dataForm = res;
            }.bind(this))
          }
        }.bind(this))
      },
      // 表单数据提交
      dataSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              api.pmfacilityUpdateApi(this.dataForm).then(function(res) {
                this.$message.success('保存成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            } else {
              api.pmfacilityCreateApi(this.dataForm).then(function(res) {
                this.$message.success('新增成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            }
          }
        })
      }
    }
  }
</script>
