<!-- 驳回原因客户姓名输入 -->
<template>
  <a-form ref="form" v-bind="formItemLayout" :form="form">
    <a-form-item label="用户姓名">
      <a-input
        :rows="4"
        v-decorator="[
          'name',
          {
            rules: [
              { required: true, message: '请输入添加姓名!', type: 'string' },
            ]
          }
        ]"
        placeholder="请输入添加姓名!" />
    </a-form-item>
    <a-form-item label="手机号码">
      <a-input
        :maxLength="11"
        v-decorator="[
          'username',
          {
            rules: [
              { required: true, message: '请输入添加手机号码!', type: 'string' },
            ]
          }
        ]"
        placeholder="请输入添加手机号码!" />
    </a-form-item>
    <a-form-item label="初始密码">
      <a-input
        :maxLength="6"
        v-decorator="[
          'password',
          {
            rules: [
              { required: false, message: '默认密码111111 !', type: 'string' },
            ]
          }
        ]"
        placeholder="默认密码111111 !" />
    </a-form-item>
  </a-form>
</template>

<script>
import { addPreDepartUser, editDepartsUser, getDefaultUserInfo } from '@/api/promissions'
import moment from 'moment'
export default {
  name: 'EditPromissions',
  data: () => ({
    formItemLayout: {
      labelCol: { span: 6 },
      wrapperCol: { span: 14 }
    },
    loading: false,
    permissions: [],
    users: [],
    defaultRoleKey: '', // 默认role 的 key
    defaultRoleName: '', // 默认显示下拉姓名
    defaultParentsKey: '', // 默认Parents 的 key
    defaultParentsName: '' // 默认显示下拉姓名
  }),
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  mounted () {
    this.getBase('')
    getDefaultUserInfo(this.item.staff_id).then(res => {
      this.$nextTick(() => {
        this.form.setFieldsValue({
          name: res.name,
          username: res.username
          // role: res.type.key
          // address: res.address
        })
        this.defaultRoleKey = res.type.key
        this.defaultRoleName = res.type.text
        this.defaultParentsKey = res.parent_id
      })
      this.getBase(res.type.key)
    }).catch(err => {
      console.log(err)
    })
  },
  methods: {
    moment,
    getBase (key) {
      addPreDepartUser({ key: key }).then(res => {
        if (key === '') {
          this.permissions = res.permission
        } else {
          this.users = res.users
          const index = res.users.findIndex(item => item.staff_id === this.defaultParentsKey)
          if (index >= 0) {
            this.defaultParentsName = res.users[index].name + '(' + res.users[index].type + ')' + res.users[index].username
          }
        }
      })
    },
    handleRoleChange (value) {
      this.getBase(value + '')
      this.defaultParentsKey = ''
      this.defaultParentsName = ''
      this.defaultRoleKey = ''
      this.defaultRoleName = ''
      this.form.setFieldsValue({
        parent_id: ''
      })
    },
    onOk () {
      return new Promise(resolve => {
        this.form.validateFields((errors, values) => {
          const data = {
            name: values.name,
            password: values.password || '111111',
            username: values.username,
            role: this.defaultRoleKey || values.role,
            parent_id: this.defaultParentsKey || values.parent_id
          }
          if (!errors) {
            editDepartsUser(this.item.staff_id, data).then((res) => {
              this.$notification.success({
                message: '成功',
                description: '修改成功'
              })
              resolve(true)
            }).catch((err) => {
              this.$notification.error({
                message: '失败',
                description: err.message
              })
              resolve(false)
            })
          }
        })
      })
    }
  }
}
</script>

<style scoped>
.flexrow {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}
</style>
