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

      <!-- 昵称 -->
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="form.nickname" placeholder="请输入昵称"></el-input>
        <el-input v-show="false" v-model="form.nickname" 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="助理教练微信二维码(110*50px)" prop="wechatCode">
        <el-upload
          class="avatar-uploader"
          action="/api/upload/image"
          :show-file-list="false"
          :on-success="handleWechatSuccess">
          <img v-if="form.wechatCode" :src="form.wechatCode" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>


      <!-- 短文介绍 -->
      <!-- <el-form-item label="短文介绍" prop="content">
        <el-input v-model="form.content" placeholder="请输入短文介绍"></el-input>
      </el-form-item> -->

      <!-- 介绍 -->
      <el-form-item label="介绍" prop="description">
        <el-input type="textarea" :rows="4" v-model="form.description" placeholder="请输入介绍"></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//coach'

export default {
  data() {
    return {
      form: {
        id: undefined,
        nickname: '',
        avatar: '',
        content: '',
        description: '',
        venueType: 2,
        platformId: 1,
        wechatCode: ''
      },

      onSubmit: false,

      // 表单验证规则
      rules: {
        nickname: [
         { required: true, message: '请输入教练名称', trigger: 'blur' }
        ],
        avatar: [
         { required: true, message: '请上传教练图像', trigger: 'blur' }
        ],
        wechatCode: [
         { required: true, message: '请上传微信二维码图片', 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.coach.nickname
        this.form.avatar = response.coach.avatar
        this.form.content = response.coach.content
        this.form.description = response.coach.description
        this.form.qrCode = response.coach.qrCode
        this.form.venueType = response.coach.venueType
        this.form.platformId = response.coach.platformId
        this.form.wechatCode = response.coach.wechatCode
      })
    },
    handleWechatSuccess(res, file) {
      this.form.wechatCode = res.img
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 0.3

      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 300kb !')
      }
      return isLt2M
    },
    // 处理头像上传成功
    handleAvatarSuccess(res, file) {
      this.form.avatar = res.img
    },

    /**
     * 保存教练
     */
    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
        })
      })
    },

    /**
     * 取消
     */
    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>
