<template>
  <a-drawer
    title="修改资料"
    :maskClosable="false"
    width=650
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="profileEditVisible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <!--<a-form-item label='邮箱' v-bind="formItemLayout">
        <a-input
          v-decorator="[
          'email',
          {rules: [
            { type: 'email', message: '请输入正确的邮箱' },
            { max: 50, message: '长度不能超过50个字符'}
          ]}
        ]"/>
      </a-form-item>-->
      <a-form-item label="手机" v-bind="formItemLayout">
        <a-input
          v-decorator="['phoneNumber', {rules: [
            { pattern: '^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$', message: '请输入正确的手机号'}
          ]}]"/>
      </a-form-item>
      <!--<a-form-item label='部门' v-bind="formItemLayout">
        <a-tree-select
          :allowClear="true"
          :dropdownStyle="{ maxHeight: '220px', overflow: 'auto' }"
          :treeData="deptTreeData"
          @change="onDepartmentChange"
          :value="userDept">
        </a-tree-select>
      </a-form-item>-->
      <!--<a-form-item label='性别' v-bind="formItemLayout">
        <a-radio-group
          v-decorator="[
            'ssex',
            {rules: [{ required: true, message: '请选择性别' }]}
          ]">
          <a-radio value="01">男</a-radio>
          <a-radio value="02">女</a-radio>
          <a-radio value="03">保密</a-radio>
        </a-radio-group>
      </a-form-item>-->
      <a-form-item label='描述' v-bind="formItemLayout">
        <a-textarea
          :rows="4"
          v-decorator="[
          'description',
          {rules: [
            { max: 100, message: '长度不能超过100个字符'}
          ]}]">
        </a-textarea>
      </a-form-item>
    </a-form>
    <div class="drawer-bottom-button">
      <a-popconfirm title="确定放弃修改？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-drawer>
</template>
<script>
import {mapState, mapMutations} from 'vuex'

const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}
export default {
  props: {
    profileEditVisible: {
      default: false
    }
  },
  data () {
    return {
      formItemLayout,
      form: this.$form.createForm(this),
      deptTreeData: [],
      userDept: [],
      authUserId: '',
      authRoleId: '',
      status: '',
      userName: '',
      loading: false,
      phoneNumber: ''
    }
  },
  computed: {
    ...mapState({
      currentUser: state => state.account.user
    })
  },
  methods: {
    ...mapMutations({
      setUser: 'account/setUser'
    }),
    onClose () {
      this.loading = false
      this.form.resetFields()
      this.$emit('close')
    },
    setFormValues ({...user}) {
      this.authUserId = user.authUserId
      let fields = ['email', 'ssex', 'description', 'phoneNumber']
      Object.keys(user).forEach((key) => {
        if (fields.indexOf(key) !== -1) {
          this.form.getFieldDecorator(key)
          let obj = {}
          obj[key] = user[key]
          this.form.setFieldsValue(obj)
        }
      })
      if (user.departmentId) {
        this.userDept = [user.departmentId]
      }
      this.status = user.status
      this.authRoleId = user.authRoleId
      this.userName = user.userName
    },
    onDepartmentChange (value) {
      this.userDept = value
    },
    handleSubmit () {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true
          let user = this.form.getFieldsValue()
          user.authUserId = this.authUserId
          user.departmentId = this.userDept
          user.authRoleId = this.authRoleId
          user.status = this.status
          user.userName = this.userName
          user.phoneNumber = this.phoneNumber

          this.$putBodyRequest('api/v1/authUser/updateAuthUserProfile', {
            ...user
          }).then((r) => {
            this.loading = false
            this.$emit('success')
            // 更新其state 查询用户的信息
            this.$get(`api/v1/authUser/${user.userName}`).then((r) => {
              this.setUser(r.data)
            })
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  },
  watch: {
    profileEditVisible () {
      if (this.profileEditVisible) {
        //请求部门数据
        /*this.$get('dept').then((r) => {
          this.deptTreeData = r.data.rows.children
        })*/
      }
    }
  }
}
</script>
