<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog
      :title="!dataForm.deptId ? '添加' : '修改'"
      :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="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父部门id"></el-input>
    </el-form-item>
    <el-form-item label="祖级列表" prop="ancestors">
      <el-input v-model="dataForm.ancestors" placeholder="祖级列表"></el-input>
    </el-form-item>
    <el-form-item label="部门名称" prop="deptName">
      <el-input v-model="dataForm.deptName" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="显示顺序" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="显示顺序"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="leader">
      <el-input v-model="dataForm.leader" 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="isWorkArea">
      <el-input v-model="dataForm.isWorkArea" placeholder="是否作业区"></el-input>
    </el-form-item>
    <el-form-item label="部门状态（0正常 1停用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="部门状态（0正常 1停用）"></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/sysdept.js'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          deptId: 0,
          parentId: '' ,
          ancestors: '' ,
          deptName: '' ,
          orderNum: '' ,
          leader: '' ,
          phone: '' ,
          email: '' ,
          isWorkArea: '' ,
          status: '' ,
          remark: '' ,
        },
        dataRule: {
          parentId: [
            { required: true, message: '父部门id  为必填项', trigger: 'blur' }
          ],
          ancestors: [
            { required: true, message: '祖级列表  为必填项', trigger: 'blur' }
          ],
          deptName: [
            { required: true, message: '部门名称  为必填项', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '显示顺序  为必填项', trigger: 'blur' }
          ],
          leader: [
            { required: true, message: '负责人  为必填项', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系电话  为必填项', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱  为必填项', trigger: 'blur' }
          ],
          isWorkArea: [
            { required: true, message: '是否作业区  为必填项', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '部门状态（0正常 1停用）  为必填项', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注  为必填项', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) { // 初始化表单验证规则
        this.dataForm.deptId = id || 0
        this.visible = true
        this.$nextTick(function() {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.deptId) {
            api.sysdeptDetailApi(id).then(function(res) {
              this.dataForm = res;
            }.bind(this))
          }
        }.bind(this))
      },
      // 表单数据提交
      dataSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.deptId) {
              api.sysdeptUpdateApi(this.dataForm).then(function(res) {
                this.$message.success('保存成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            } else {
              api.sysdeptCreateApi(this.dataForm).then(function(res) {
                this.$message.success('新增成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            }
          }
        })
      }
    }
  }
</script>
