<template>
<div>
  <sticky className="sub-navbar">
    <div style="text-align:right;">
      <el-button type="success" :loading="onSubmit" @click="handleSave">保存</el-button>
      <el-button @click="handleCancel">取消</el-button>
    </div>
  </sticky>
  <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="rechargeMoney">
        <el-input-number :min="1" v-model="form.rechargeMoney" placeholder="请输入充值金额"></el-input-number>
      </el-form-item>

      <!-- 到账金额 -->
      <el-form-item label="到账金额" prop="toMoney">
        <el-input-number :min="1" v-model="form.toMoney" placeholder="请输入到账金额"></el-input-number>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
import Sticky from '@/components/Sticky'
import { find, save } from '@/api//recharge'

export default {
  components: { Sticky },
  data() {
    return {
      form: {
        id: undefined,
        rechargeMoney: '',
        toMoney: '',
        platformId: ''
      },

      onSubmit: false,

      // 表单验证规则
      rules: {
        rechargeMoney: [
         { required: true, message: '请输入充值金额', trigger: 'blur' }
        ],
        toMoney: [
         { 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.rechargeMoney = response.recharge.rechargeMoney
        this.form.toMoney = response.recharge.toMoney
        this.form.platformId = response.recharge.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
        })
      })
    },

    /**
     * 取消
     */
    handleCancel() {
      this.$router.go(-1)
    }
  }
}
</script>
