<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form disabled :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="姓名" prop="name">
          <el-input v-model="dataForm.name" 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="gender">
              <el-radio-group v-model="dataForm.gender">
                  <el-radio :label="'0'">{{ $t('user.gender1') }}</el-radio>
                  <el-radio :label="'1'">{{ $t('user.gender0') }}</el-radio>
              </el-radio-group>
      </el-form-item>
          <el-form-item label="手机号" prop="mobile">
          <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
          <el-form-item label="人员类型" prop="type">
              <el-radio-group v-model="dataForm.gender">
                  <el-radio :label="'0'">{{ '访客' }}</el-radio>
                  <el-radio :label="'1'">{{ '租客' }}</el-radio>
              </el-radio-group>
      </el-form-item>
          <el-form-item label="来访事由" prop="visitReason">
          <el-input type="textarea" v-model="dataForm.visitReason" placeholder="来访事由"></el-input>
      </el-form-item>
          <el-form-item label="头像" prop="faceImg">
              <el-image :src="dataForm.faceImg" @click="imgShow(dataForm.faceImg)" style="width: 100px; height: 100px" />
      </el-form-item>
          <el-form-item label="小区" prop="residential">
          <el-input v-model="dataForm.residential" placeholder="小区"></el-input>
      </el-form-item>
          <el-form-item label="楼栋" prop="building">
          <el-input v-model="dataForm.building" placeholder="楼栋"></el-input>
      </el-form-item>
          <el-form-item label="单元" prop="unit">
          <el-input v-model="dataForm.unit" placeholder="单元"></el-input>
      </el-form-item>
          <el-form-item label="房屋" prop="room">
          <el-input v-model="dataForm.room" placeholder="房屋"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
<!--      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>-->
    </template>
      <el-dialog :visible.sync="dialogVisible" :modal="false">
          <el-image width="100%" :src="dialogImageUrl" alt=""/>
      </el-dialog>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dialogVisible: false,
      dialogImageUrl: '',
      dataForm: {
        id: '',
        name: '',
        idCard: '',
        gender: '',
        mobile: '',
        type: '',
        state: '',
        reason: '',
        delFlag: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: '',
        customerId: '',
        visitReason: '',
        faceImg: '',
        residential: '',
        building: '',
        unit: '',
        room: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 查看大图
    imgShow(url){
      this.dialogImageUrl = url
      this.dialogVisible = true
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/pli/power/visitVisitor/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/pli/power/visitVisitor/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
