<template>
  <el-main>
    <el-row class="movie-list">
      <el-col :md="8" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>我的钱包</span>
            <el-button style="float: right; padding: 3px 0" type="text" @click="showChargeDialog">充值</el-button>
          </div>
          <div class="text item">
            <el-row>
              <h1>余额: <span style="color: red">￥{{ wallet.balance }}</span></h1>
            </el-row>
          </div>
        </el-card>
      </el-col>
      <el-col :md="16" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>我的账单</span>
          </div>
          <el-table
            :data="billList"
            style="width: 100%"
          >
            <el-table-column
              prop="createAt"
              label="时间"
            />
            <el-table-column
              prop="type"
              label="类型"
            >
            </el-table-column>
            <el-table-column
              prop="quantity"
              label="金额"
            >
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="handleDetail(scope.$index, scope.row)"
                >详情</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog
      title="充值"
      append-to-body
      :visible.sync="chargeDialog"
      width="30%"
      center
    >
      <el-form ref="form" :model="chargeForm">
        <el-form-item label="充值金额">
          <el-input-number v-model="chargeForm.quantity" :min="1" :max="100" style="margin-left: 5px" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="chargeWallet"
          >确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-main>
</template>

<script>
import { chargeWallet, getWallet, getWalletBill } from '@/api/mall'

export default {
  name: 'MyWallet',
  data() {
    return {
      chargeDialog: false,
      chargeForm: {
        quantity: null
      },
      wallet: {
        balance: '0.00'
      },
      billList: []
    }
  },
  created() {
    document.title = '我的钱包'
    getWallet().then(resp => {
      if (resp.code === 0) {
        this.wallet = resp.data
      }
    })

    this.getBillRecord()
  },
  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.$notify.info({
            title: '充值请求已提交',
            duration: 3000
          })
        } else {
          this.$notify({
            title: '充值失败',
            message: resp.msg,
            type: 'error',
            duration: 3000
          })
        }
      }).catch(error => {
        this.$notify({
          title: '充值失败',
          message: error.message,
          type: 'error',
          duration: 3000
        })
      })
    },
    getBillRecord() {
      getWalletBill().then(resp => {
        if (resp.code === 0) {
          this.billList = resp.data
        }
      })
    },
    handleDetail(index, row) {
      this.$message.info('账单详情')
    }
  }
}
</script>

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

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