<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 :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <!-- <el-form-item label="昵称" prop="nickname">
          <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
      </el-form-item> -->
       <el-form-item label="真实姓名" prop="realName">
          <span >{{dataForm.realName}}</span>
      </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <span >{{dataForm.mobile}}</span>
      </el-form-item>
      <el-form-item label="身份证号" prop="identityNo">
          <span >{{dataForm.identityNo}}</span>
      </el-form-item>
      <el-form-item label="是否党员">
          <el-select v-model="dataForm.partyFlag" placeholder="请选择" >
            <el-option
              v-for="item in partyFlagOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="扫码网格" >
          <span >{{dataForm.street}} - </span>
          <span >{{dataForm.community}} - </span>
          <span >{{dataForm.grid}}</span>
      </el-form-item>
      <el-form-item label="居民住址" prop="address">
          <el-input v-model="dataForm.address" placeholder="居民住址"></el-input>
      </el-form-item>
      <template>
          <span>是否通过审核</span>&nbsp;&nbsp;
          <el-radio v-model="radio" label="1">是</el-radio>
          <el-radio v-model="radio" label="0">否</el-radio>
      </template>
          <el-form-item label="注册时间" prop="registerTime">
          <el-input v-model="dataForm.registerTime" placeholder="注册时间"></el-input>
      </el-form-item>
          <el-form-item label="头像" prop="faceImg">
          <el-input v-model="dataForm.faceImg" placeholder="头像"></el-input>
      </el-form-item>
          <el-form-item label="性别(女性-female，男性-male)" prop="sex">
          <el-input v-model="dataForm.sex" placeholder="性别(女性-female，男性-male)"></el-input>
      </el-form-item>
          <el-form-item label="生日" prop="birthday">
          <el-input v-model="dataForm.birthday" 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="telephone">
          <el-input v-model="dataForm.telephone" placeholder="电话"></el-input>
      </el-form-item>
          <el-form-item label="邮编" prop="zipCode">
          <el-input v-model="dataForm.zipCode" placeholder="邮编"></el-input>
      </el-form-item>
          <el-form-item label="职业" prop="profession">
          <el-input v-model="dataForm.profession" placeholder="职业"></el-input>
      </el-form-item>
          <el-form-item label="爱好" prop="hobbies">
          <el-input v-model="dataForm.hobbies" placeholder="爱好"></el-input>
      </el-form-item>
          <el-form-item label="个性签名" prop="userSign">
          <el-input v-model="dataForm.userSign" placeholder="个性签名"></el-input>
      </el-form-item>
          <el-form-item label="邀请码" prop="invitationCode">
          <el-input v-model="dataForm.invitationCode" placeholder="邀请码"></el-input>
      </el-form-item>
          <el-form-item label="最近登录时间" prop="lastLoginTime">
          <el-input v-model="dataForm.lastLoginTime" placeholder="最近登录时间"></el-input>
      </el-form-item>
          <el-form-item label="最近登录IP" prop="lastLoginIp">
          <el-input v-model="dataForm.lastLoginIp" placeholder="最近登录IP"></el-input>
      </el-form-item>
          <el-form-item label="最近登录位置经度" prop="lastLongitude">
          <el-input v-model="dataForm.lastLongitude" placeholder="最近登录位置经度"></el-input>
      </el-form-item>
          <el-form-item label="最近登录位置维度" prop="lastLatitude">
          <el-input v-model="dataForm.lastLatitude" placeholder="最近登录位置维度"></el-input>
      </el-form-item>
          <el-form-item label="身份证号" prop="identityNo">
          <el-input v-model="dataForm.identityNo" placeholder="身份证号"></el-input>
      </el-form-item>
          <el-form-item label="微信OPENID" prop="wxOpenId">
          <el-input v-model="dataForm.wxOpenId" placeholder="微信OPENID"></el-input>
      </el-form-item>
          <el-form-item label="是否是党员(0-否，1-是)" prop="partyFlag">
          <el-input v-model="dataForm.partyFlag" placeholder="是否是党员(0-否，1-是)"></el-input>
      </el-form-item>
          <el-form-item label="注册方式（wx：微信注册）" prop="registerWay">
          <el-input v-model="dataForm.registerWay" placeholder="注册方式（wx：微信注册）"></el-input>
      </el-form-item>
          <el-form-item label="用户来源(wp:公众号)" prop="registerSource">
          <el-input v-model="dataForm.registerSource" placeholder="用户来源(wp:公众号)"></el-input>
      </el-form-item>
          <el-form-item label="手机号所属省份" prop="phoneProvince">
          <el-input v-model="dataForm.phoneProvince" placeholder="手机号所属省份"></el-input>
      </el-form-item>
          <el-form-item label="手机号所属城市" prop="phoneCity">
          <el-input v-model="dataForm.phoneCity" placeholder="手机号所属城市"></el-input>
      </el-form-item>
          <el-form-item label="手机号所属运营商" prop="phoneCarrier">
          <el-input v-model="dataForm.phoneCarrier" placeholder="手机号所属运营商"></el-input>
      </el-form-item>
          <el-form-item label="用户积分" prop="points">
          <el-input v-model="dataForm.points" placeholder="用户积分"></el-input>
      </el-form-item>
          <el-form-item label="邀请人ID" prop="inviteUserId">
          <el-input v-model="dataForm.inviteUserId" placeholder="邀请人ID"></el-input>
      </el-form-item>
          <el-form-item label="乐观锁" prop="revision">
          <el-input v-model="dataForm.revision" placeholder="乐观锁"></el-input>
      </el-form-item>
          <el-form-item label="网格ID" prop="gridId">
          <el-input v-model="dataForm.gridId" placeholder="网格ID"></el-input>
      </el-form-item>
          <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="dataForm.createdBy" placeholder="创建人"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createdTime">
          <el-input v-model="dataForm.createdTime" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="更新人" prop="updatedBy">
          <el-input v-model="dataForm.updatedBy" placeholder="更新人"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updatedTime">
          <el-input v-model="dataForm.updatedTime" placeholder="更新时间"></el-input>
      </el-form-item>
          <el-form-item label="删除标记" prop="delFlag">
          <el-input v-model="dataForm.delFlag" 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>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        nickname: '',
        mobile: '',
        password: '',
        registerTime: '',
        faceImg: '',
        sex: '',
        birthday: '',
        email: '',
        telephone: '',
        zipCode: '',
        profession: '',
        hobbies: '',
        userSign: '',
        invitationCode: '',
        lastLoginTime: '',
        lastLoginIp: '',
        lastLongitude: '',
        lastLatitude: '',
        realName: '',
        identityNo: '',
        address: '',
        wxOpenId: '',
        registerWay: '',
        registerSource: '',
        phoneProvince: '',
        phoneCity: '',
        phoneCarrier: '',
        points: '',
        inviteUserId: '',
        revision: '',
        gridId: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: '',
        delFlag: '',
        partyFlag: ''
      },
      radio: '0',
      partyFlagOptions: [{
        id: '0',
        name: '不是'
      }, {
        id: '1',
        name: '是'
      }, {
        id: '1',
        name: '全部'
      }]
    }
  },
  computed: {
    dataRule () {
      return {
        nickname: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        registerTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        faceImg: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sex: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        birthday: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        email: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        telephone: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        zipCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        profession: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        hobbies: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        userSign: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        invitationCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        lastLoginTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        lastLoginIp: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        lastLongitude: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        lastLatitude: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        realName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        identityNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        address: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        wxOpenId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        partyFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        registerWay: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        registerSource: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        phoneProvince: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        phoneCity: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        phoneCarrier: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        points: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        inviteUserId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        revision: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        gridId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        delFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/app-user/user/${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']('/epdc/user/', 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>
