<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="facilityId">
      <el-input v-model="dataForm.facilityId" placeholder="设施id"></el-input>
    </el-form-item>
    <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="maintenanceType">
      <el-input v-model="dataForm.maintenanceType" placeholder="维保类型"></el-input>
    </el-form-item>
    <el-form-item label="维保周期" prop="cycleDays">
      <el-input v-model="dataForm.cycleDays" placeholder="维保周期"></el-input>
    </el-form-item>
    <el-form-item label="下次执行时间" prop="nextExecTime">
      <el-input v-model="dataForm.nextExecTime" placeholder="下次执行时间"></el-input>
    </el-form-item>
    <el-form-item label="维保内容" prop="maintenanceContent">
      <el-input v-model="dataForm.maintenanceContent" placeholder="维保内容"></el-input>
    </el-form-item>
    <el-form-item label="执行部门" prop="executorDept">
      <el-input v-model="dataForm.executorDept" 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/pmfacilitymaintenanceplan.js'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          facilityId: '' ,
          buildingId: '' ,
          projectId: '' ,
          facilityName: '' ,
          maintenanceType: '' ,
          cycleDays: '' ,
          nextExecTime: '' ,
          maintenanceContent: '' ,
          executorDept: '' ,
          remark: '' ,
        },
        dataRule: {
          facilityId: [
            { required: true, message: '设施id  为必填项', trigger: 'blur' }
          ],
          buildingId: [
            { required: true, message: '楼栋id  为必填项', trigger: 'blur' }
          ],
          projectId: [
            { required: true, message: '项目id  为必填项', trigger: 'blur' }
          ],
          facilityName: [
            { required: true, message: '设施名称  为必填项', trigger: 'blur' }
          ],
          maintenanceType: [
            { required: true, message: '维保类型  为必填项', trigger: 'blur' }
          ],
          cycleDays: [
            { required: true, message: '维保周期  为必填项', trigger: 'blur' }
          ],
          nextExecTime: [
            { required: true, message: '下次执行时间  为必填项', trigger: 'blur' }
          ],
          maintenanceContent: [
            { required: true, message: '维保内容  为必填项', trigger: 'blur' }
          ],
          executorDept: [
            { 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.pmfacilitymaintenanceplanDetailApi(id).then(function(res) {
              this.dataForm = res;
            }.bind(this))
          }
        }.bind(this))
      },
      // 表单数据提交
      dataSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              api.pmfacilitymaintenanceplanUpdateApi(this.dataForm).then(function(res) {
                this.$message.success('保存成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            } else {
              api.pmfacilitymaintenanceplanCreateApi(this.dataForm).then(function(res) {
                this.$message.success('新增成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            }
          }
        })
      }
    }
  }
</script>
