<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
      if (!params.is_backend) params.is_backend = '2'
      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: 'is_admin', type: 'select', class: 'c-2', options: this.$dictData.sys_yesno, },
          { label: '手机号', key: 'phone', rules: 1 },
          { label: '昵称', key: 'nickname', rules: 1 },
          { label: '后台登录', rules: 1, key: 'is_backend', type: 'select', class: 'c-2', options: this.$dictData.sys_yesno, },
          { label: '系统角色', key: 'rolesIds', type: 'selectRemote', multiple: true,
            remoteParams: { skey: 'name', api: `base.admroleslist`, opKey: 'name', opVal: 'id' },
            remoteOptions: remoteOptionsRoles
          },
          { label: '楼盘角色', key: 'manage_type', type: 'select', class: 'c-2', options: this.$dictData.manage_type, },
          { label: '关联楼盘', key: 'estate_id', type: 'selectRemote',
            remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
            remoteOptions: [{ keyRO: params.estate_name, valRO: params.estate_id }]
          },
        ]
      } else {
        this.formData = [
          // { label: '超级管理员', key: 'is_admin', type: 'select', class: 'c-2', options: this.$dictData.sys_yesno, },
          { label: '手机号', key: 'phone', rules: 1 },
          { label: '昵称', key: 'nickname', rules: 1 },
          { label: '密码', key: 'password', rules: 1 },
          { label: '后台登录', rules: 1, key: 'is_backend', type: 'select', class: 'c-2', options: this.$dictData.sys_yesno, },
          { label: '系统角色', key: 'rolesIds', type: 'selectRemote', multiple: true,
            remoteParams: { skey: 'name', api: `base.admroleslist`, opKey: 'name', opVal: 'id' },
            remoteOptions: remoteOptionsRoles
          },
          { label: '楼盘角色', key: 'manage_type', type: 'select', class: 'c-2', options: this.$dictData.manage_type, },
          { label: '关联楼盘', key: 'estate_id', type: 'selectRemote',
            remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
            remoteOptions: [{ keyRO: params.estate_name, valRO: params.estate_id }]
          },
        ]
      }
      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(',')
            delete newForm.rolesIds
            if (this.curObj.id) {
              newForm.id = this.curObj.id
              newForm.user_type = this.curObj.user_type || 'sys'
            } else {
              newForm.user_type = 'sys'
            }
            this.$emit('close', newForm)
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    },
  }
}
</script>
<style lang="scss" scoped>
</style>