<template>
  <div class="record record--order--book">
    <x-header></x-header>
    <div class="record-head">
      <router-link tag="p"
                   replace
                   :to="RecordOrder">章节</router-link>
      <p class="is-active">整本</p>
      <router-link tag="p"
                   replace
                   :to="RecordGift">送礼物</router-link>
    </div>
    <transition-group tag="div"
                      name="fade-list"
                      class="record-list"
                      v-if="total">
      <div class="record-item"
           v-for="(v) in record"
           :key="v.id">
        <p class="record-item__name">{{v.book_name}}</p>
        <p class="record-item__time">{{v.created_at}}</p>
        <p class="record-item__fee record-item__fee--book red">{{v.fee}} 书币</p>
      </div>
    </transition-group>
    <x-page v-if="record.length"
            :current="current"
            @change="getOrder"
            :total="total"></x-page>
    <div class="record-none"
         v-else>
      暂无消费记录
    </div>
  </div>
</template>
<script>
import { RecordOrderBook, RecordOrder, RecordGift } from "./namespace.js";
// import record from "../mock/record-order-book.json";
import { getRecordOrderByBook } from "../api";
export default {
  name: RecordOrderBook.name,
  data() {
    return {
      RecordOrder: RecordOrder.route,
      RecordGift: RecordGift.route,
      record: [],
      total: 0,
      current: 1
    };
  },
  methods: {
    getOrder(page) {
      getRecordOrderByBook(page).then(r => {
        this.record = r.list.map((item, i) => {
          return Object.assign(item, {
            id: i + 1
          });
        });
        this.current = r.meta.current_page;
        this.total = r.meta.last_page;
        scrollTo(0, 0);
      });
    }
  },
  created() {
    this.getOrder();
  }
};
</script>
