<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 }
      const disabled = false
      let remoteOptionsRoles = []
      if (params.role_arr && params.role_arr.length > 0) {
        params.rolesIds = params.role_arr.map(item => {
          remoteOptionsRoles.push({ keyRO: item.name, valRO: Number(item.id) })
          return Number(item.id)
        })
      } else {
        params.rolesIds = []
      }
      if (this.curObj.id) {
        this.formData = [
          { label: '账号', key: 'username', rules: 1 },
          { label: '昵称', key: 'nickname', rules: 1 },
          { label: '角色', key: 'rolesIds', type: 'selectRemote', multiple: true,
            remoteParams: { skey: 'name', api: `base.admroleslist`, opKey: 'name', opVal: 'id' },
            remoteOptions: remoteOptionsRoles
          },
          { label: '邮箱', key: 'email' },
          { label: '头像', key: 'avatar', type: 'upload' },
        ]
      } else {
        this.formData = [
          { label: '账号', key: 'username', rules: 1 },
          { label: '昵称', key: 'nickname', rules: 1 },
          { label: '密码', key: 'password', rules: 1 },
          { label: '角色', key: 'rolesIds', type: 'selectRemote', multiple: true,
            remoteParams: { skey: 'name', api: `base.admroleslist`, opKey: 'name', opVal: 'id' },
            remoteOptions: remoteOptionsRoles
          },
          { 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
            let newForm = { ...oldform }
            newForm.roles = newForm.rolesIds.join(',')
            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>
