<template>
  <el-row>
    <el-col :md="12">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>我的钱包</span>
          <el-button style="float: right; padding: 3px 0" type="text" @click="getBillRecord">账单</el-button>
        </div>
        <div class="text item">
          <el-row>
            <h1>余额: <span style="color: red">￥{{wallet.balance}}</span></h1>
          </el-row>
          <el-row>
            <el-button
              size="medium"
              type="success"
              @click="showChargeDialog"
            >充值</el-button>
          </el-row>
        </div>
      </el-card>
    </el-col>

    <el-dialog
      title="充值"
      append-to-body
      :visible.sync="chargeDialog"
      width="30%"
      center
    >
      <el-form ref="form" :model="chargeForm">
        <el-form-item label="充值金额">
          <el-input
            v-model="chargeForm.quantity"
            placeholder="0.00"
            style="width: 70%; padding-right: 2px"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="chargeWallet"
          >确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-row>
</template>

<script>

import {chargeWallet, getWallet, getWalletBill} from "@/api/mall";

export default {
  name: 'MyWallet',
  data() {
    return {
      chargeDialog: false,
      chargeForm: {
        quantity: null
      },
      wallet: {
        balance: '0.00'
      }
    }
  },
  created() {
    document.title = '我的钱包'
    getWallet().then(resp => {
      if (resp.code === 0) {
        this.wallet = resp.data
      }
    })
  },
  mounted() {
  },
  methods: {
    showChargeDialog() {
      this.chargeDialog = true
    },
    chargeWallet() {
      if (this.chargeForm.quantity === null) {
        this.$message('请填写充值金额')
        return
      }

      this.chargeDialog = false
      chargeWallet(this.chargeForm).then(resp => {
        if (resp.code === 0) {
          this.$message('充值请求已提交')
        }
      })
    },
    getBillRecord() {
      getWalletBill().then(resp => {
        if (resp.code === 0) {
          console.log(resp.data)
        }
      })
    }
  }
}
</script>

<style scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
</style>
