<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body @close="cancel">
    <el-form ref="dataForm" :rules="rules" :model="doorForm" label-well-code="right" label-width="100px">
      <el-row type="flex" justify="center">
        <el-col :span="18">
          <el-form-item label="门禁编号" prop="doorCode">
            <el-input v-model.trim="doorForm.doorCode" disabled type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="18">
          <el-form-item label="门禁名称" prop="doorName">
            <el-input v-model.trim="doorForm.doorName" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="18">
          <el-form-item label="区域" prop="areaid">
            <area-select-tree v-model="doorForm.areaid" :need-top="false" :dept-show="true" placeholder="区域"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="18">
          <el-form-item label="描述" prop="description">
            <el-input v-model.trim="doorForm.description" :maxlength="20" type="text" placeholder="必填" show-word-limit/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addDoor, updateDoor, getDoorCode } from '@/api/door'
import DeptSelect from '../../components/DeptSelect/index'
import AreaSelectTree from '@/components/AreaSelect/areaSelectTree'

export default {
  name: 'EditDoor',
  components: { DeptSelect, AreaSelectTree },
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      doorForm: {
        id: '', // id
        doorCode: '', // 门禁编码
        doorName: '', // 门禁名称
        areaid: '', // 权属单位
        description: '', // 描述
        openStatus: '2' // 门禁状态
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      }, // 表头显示标题
      rules: {
        doorCode: [{ required: true, message: '门禁编号不能为空', trigger: ['blur', 'change'] }],
        doorName: [{ required: true, message: '门禁名称不能为空', trigger: ['blur', 'change'] }],
        areaid: [{ required: true, message: '单位/部门必选', trigger: ['blur', 'change'] }]
      }, // 前端校验规则
      isEditMode: false,
      deptShowTop: false, // 权属单位下拉是否显示顶级
      deptShow: true
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.resetForm()
        getDoorCode().then(response => {
          this.doorForm.doorCode = response.data
        })
        this.isEditMode = false
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改，将row中数据填写到输入框中
        this.doorForm = {
          id: row.id,
          doorCode: row.doorCode, // 门禁编码
          doorName: row.doorName, // 门禁名称
          areaid: row.areaid, // 权属单位
          description: row.description, // 描述
          openStatus: row.openStatus // 门禁状态
        }
        this.isEditMode = true
      }
    },
    // 重置表单
    resetForm() {
      this.doorForm = {
        id: '', // id
        doorCode: '', // 门禁编码
        doorName: '', // 门禁名称
        areaid: '', // 权属单位
        description: '' // 描述
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.doorForm)
        if (valid) {
          addDoor(this.doorForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功，是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                getDoorCode().then(response => {
                  this.doorForm.doorCode = response.data
                })
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
              })
            }
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateDoor(this.doorForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
</style>
