<template>
  <div class="app-container app-edit">
    <el-form class="small-space" :model="form" :rules="rules" ref="form" label-position="right" label-width="120px" style="width: 700px;">

      <!-- 昵称 -->
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="form.nickname" placeholder="请输入昵称" readonly="true"></el-input>
      </el-form-item>

      <!-- 图像 -->
      <!-- <el-form-item label="图像" prop="avatar">
        <el-input v-model="form.avatar" placeholder="请输入图像"></el-input>
      </el-form-item> -->

      <el-form-item label="头像(110*50px)" prop="avatar">
        <el-upload
          class="avatar-uploader"
          action="/api/upload/image"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.avatar" :src="form.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <!-- 状态 -->
      <el-form-item label="状态">
        <el-radio class="radio" v-model="form.status" :label="0">可用</el-radio>
        <el-radio class="radio" v-model="form.status" :label="1">禁用</el-radio>
      </el-form-item>

      <!-- 手机号 -->
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>

      <!-- 姓名 -->
      <el-form-item label="姓名" prop="nane">
        <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-radio class="radio" v-model="form.sex" :label="0">男</el-radio>
        <el-radio class="radio" v-model="form.sex" :label="1">女</el-radio>
      </el-form-item>

      <!-- 年龄 -->
      <el-form-item label="年龄" prop="age">
        <el-input-number v-model="form.age" :min="0" placeholder="请输入年龄"></el-input-number>
      </el-form-item>

      <!-- 居住地 -->
      <el-form-item label="居住地" prop="address">
        <el-input v-model="form.address" placeholder="请输入居住地"></el-input>
      </el-form-item>

      <!-- 余额 -->
      <el-form-item label="余额" prop="money">
        <el-input-number v-model="form.money" :min="0" placeholder="请输入余额"></el-input-number>
      </el-form-item>

      <!-- 平台来源ID -->
      <!-- <el-form-item label="平台来源ID" prop="platformId">
        <el-input v-model="form.platformId" placeholder="请输入平台来源ID"></el-input>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" :loading="onSubmit" @click="handleSave">保存</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { find, save } from '@/api//member'

export default {
  data() {
    return {
      form: {
        id: undefined,
        nickname: '',
        avatar: '',
        status: 0,
        inviteCode: '',
        mobile: '',
        name: '',
        sex: '',
        age: '',
        address: '',
        money: '',
        platformId: 1
      },

      onSubmit: false,

      // 表单验证规则
      rules: {
        mobile: [
         { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^1[34578]\d{9}$/, message: '手机号格式错误' }
        ],
        nickname: [
         { required: true, message: '请输入昵称', trigger: 'blur' },
         { max: 10, message: '姓名长度不能超过10个字符', trigger: 'blur' }
        ]
//        email: [
//          { required: true, message: '请输入邮箱', trigger: 'blur' },
//          { type: 'email', message: '您输入的邮箱格式不正确', trigger: 'blur' }
//        ],
//        password: [
//          { required: true, message: '请输入密码', trigger: 'blur' },
//          { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
//        ]
      }
    }
  },
  created() {
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id
      this.findById(this.form.id)
    }
  },
  methods: {
    /**
     * 查询用户
     */
    findById(id) {
      find(id).then(response => {
        this.form.nickname = response.member.nickname
        this.form.avatar = response.member.avatar
        this.form.status = response.member.status
        this.form.inviteCode = response.member.inviteCode
        this.form.mobile = response.member.mobile
        this.form.name = response.member.name
        this.form.sex = response.member.sex
        if (response.member.age === undefined) {
          this.form.age = 0
        } else {
          this.form.age = response.member.age
        }
        this.form.address = response.member.address
        this.form.money = response.member.money
        this.form.platformId = response.member.platformId
      })
    },

    /**
     * 保存用户
     */
    handleSave() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }

        this.onSubmit = true
        save(this.form).then(response => {
          this.$router.go(-1)
        }).finally(() => {
          this.onSubmit = false
        })
      })
    },
    beforeAvatarUpload(file) {
      // const isLt2M = file.size / 1024 / 1024 < 2
      // if (!isLt2M) {
      //   this.$message.error('上传头像图片大小不能超过 2MB!')
      // }
      // return isLt2M
    },
    // 处理头像上传成功
    handleAvatarSuccess(res, file) {
      this.form.avatar = res.img
    },
    /**
     * 取消
     */
    handleCancel() {
      this.$router.go(-1)
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>
