<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="this.curObj && this.curObj.id ? '编辑用户' : '添加用户'"
      :fullscreen="false"
      width="470px"
      custom-class="xl-dialog"
      center
    >
      <base-form ref="ruleForm" :data="formData" :is-inline="false" label-width="110px">
        <div slot="footer">
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
          <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
        </div>
      </base-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: {},
  mixins: [...mixins],
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      isShowMap: false
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.getDef()
      }
    },
  },
  methods: {
    getDef() {
      const params = { ...this.curObj }
      if (params.avatar) params.avatar = `${params.domain}${params.avatar}?url=${params.avatar}`
      const disabled = false
      this.formData = [
        { label: '账号', key: 'username', rules: 1 },
        { label: '昵称', key: 'nickname', rules: 1 },
        { label: '密码', key: 'password', rules: 1 },
        { label: '邮箱', key: 'email' },
        { label: '头像', key: 'avatar', type: 'upload' },
      ]
      this.setDefaultValue(params)
    },
    close(str) {
      if (str === 'confirm') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            const newForm = { ...oldform }
            if (newForm.avatar && newForm.avatar.indexOf('?') > -1) {
              const imgArr = newForm.avatar.split('?')
              const queryArr = imgArr[1].split('&')
              queryArr.forEach(q =>{
                const curQArr = q.split('=')
                if (curQArr[0] === 'url') {
                  newForm.avatar = curQArr[1]
                }
              })
            }
            if (this.curObj.id) newForm.id = this.curObj.id
            this.$emit('close', newForm)
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    },
  }
}
</script>
<style lang="scss" scoped>
</style>
