<template>
  <el-form :model="form" ref="form" :rules="rules">
    <el-form-item label="姓名" label-position="right" prop="name">
      <el-input v-model.trim="form.name" />
    </el-form-item>
    <el-form-item label="密码" label-position="right" prop="password">
      <el-input v-model.trim="form.password" type="password" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import {
    save
  } from '../../../api/system/user.js'
  import {
    getInfo
  } from '../../../api/user.js'

  export default {
    data() {
      return {
        form: {
          id: "",
          name: "",
          username: "",
          password: ""
        },
        rules: {
          name: [{
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          }]
        },
      }
    },
    created() {
      getInfo().then(res => {
        this.form.id = res.id;
        this.form.name = res.name
        this.form.username = res.username
      })
    },
    methods: {
      submit() {
        let that = this;
        this.$refs["form"].validate((valid) => {
          if (valid) {
            save(that.form).then(response => {
              this.$Message.success('操作成功');
            }).catch(error => {
              //reject(error)
            })
          } else {
            this.$Message.error('数据校验失败');
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>
