<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="form"
      label-width="100px"
      @submit.native.prevent
      style="max-width: 680px"
    >
      <el-form-item label="姓名" prop="real_name">
        <el-input v-model.trim="ruleForm.real_name"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model.trim="ruleForm.mobile"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">确认修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as api from '@/api/my'

export default {
  name: 'MyInfo',
  data() {
    return {
      ruleForm: {
        real_name: '',
        mobile: ''
      },
      rules: {
        real_name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
      }
    }
  },
  methods: {
    getUser() {
      api.getUser().then(response => {
        const data = response.data
        this.ruleForm.real_name = data.realname
        this.ruleForm.mobile = data.mobile
      })
    },
    // 提交
    handleSubmit() {
      this.$refs.form.validate().then(this.handleRequest)
    },
    // 接口请求
    handleRequest() {
      this.loading = true
      api.updateUser(this.ruleForm).then(response => {
        response.code === 0 ? this.handleSuccess(response) : this.$message.error(response.msg)
        this.loading = false
      })
    },
    // 成功
    handleSuccess(response) {
      this.$alert('基本信息修改成功', {
        type: 'success',
        confirmButtonText: '返回报名系统',
        callback: action => {
          this.$router.push('/my')
        }
      })
    }
  },
  beforeMount() {
    this.getUser()
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-input {
  max-width: 270px;
}
</style>
