<!-- 用量记录 -->
<template>
  <div>
    <div class="row">
      <el-button type="primary" icon="Refresh" :loading="tableData.loading" @click="getTableData">刷新</el-button>
    </div>
    <div class="data_show">
      <div class="data_list">
        <div class="item">
          <div class="title">
            返现总金额
          </div>
          <div class="num">
            {{ tableData.sendTotal }}条/￥{{ multiplyAndFormat(tableData.sendAmountTotal, 1) }}
          </div>
        </div>
      </div>
    </div>
    <div class="table" style="width: 100%;">
      <el-table :data="tableData.list" stripe border v-loading="tableData.loading" height="500px">
        <el-table-column label="ID" prop="id"></el-table-column>
        <el-table-column label="记录类型" prop="type">
          <template #default="scope">
            <div class="column">
              <div v-if="scope.row.type === 1">增加</div>
              <div v-else-if="scope.row.type === 2">减少</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="变动原因" prop="reason"></el-table-column>
        <el-table-column label="变动金额" prop="expense"></el-table-column>
        <el-table-column label="变动后余额" prop="balance"></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
      </el-table>
    </div>
    <div class="row" style="margin-top: 14px;">
      <el-pagination v-model:current-page="tableData.page" v-model:page-size="tableData.size"
        :page-sizes="[100, 200, 300, 400]" background layout="total, sizes, prev, pager, next, jumper"
        :page-count="tableData.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { smsMoneyDetail } from '@/api/coupon/index.js'
import { multiplyAndFormat } from '@/utils'

const tableData = reactive({
  sendAmountTotal: 0,
  sendTotal: 0,
  loading: false,
  page: 1,
  size: 10,
  list: []
})

// 分页大小发生变化
function handleSizeChange(e) {
  tableData.pageSize = e;
  getTableData();
}

// 分页发生变化
function handleCurrentChange(e) {
  tableData.page = e;
  getTableData();
}

// 获取店铺短信余额明细
async function getTableData() {
  try {
    tableData.loading = true
    const res = await smsMoneyDetail({
      page: 1,
      size: 10
    })
    tableData.totalAmount = res.totalAmount
    tableData.list = res.records
    tableData.total = +res.totalRow
  } catch (error) {
    console.log(error);
  }
  setTimeout(() => {
    tableData.loading = false
  }, 500);
}

onMounted(() => {
  getTableData()
})
</script>

<style scoped lang="scss">
.data_show {
  padding: 14px 0;

  .data_list {
    display: flex;

    .item {
      width: 300px;
      border: 1px solid #ddd;
      border-radius: 4px;
      padding: 14px;

      .title {
        color: #666;
      }

      .num {
        font-size: 24px;
        color: #333;
        font-weight: bold;
      }
    }
  }
}

.column {
  display: flex;
  flex-direction: column;
  line-height: 16px;
}
</style>