<template>
  <div class="app-container">
    <div class="filter-container filter-view">
      <el-date-picker
        v-model="filter.pageVo.dateRange"
        type="daterange"
        value-format="yyyy-MM-dd"
        start-placeholder="Start Date"
        end-placeholder="End Date"
        clearable
        class="filter-view-item"
        @change="toSearch"/>
      <el-select
        class="filter-view-item"
        v-model="filter.pageVo.creditType"
        placeholder="Credit Type"
        @change="toSearch"
        clearable>
        <el-option
          v-for="item in options.creditType"
          :key="item"
          :label="item"
          :value="item" />
      </el-select>
      <el-select
        class="filter-view-item"
        v-model="filter.pageVo.paymentStatus"
        placeholder="Status"
        @change="toSearch"
        clearable>
        <el-option
          v-for="item in options.paymentStatus"
          :key="item"
          :label="item"
          :value="item" />
      </el-select>
    </div>
    <el-table
      :data="table.list"
      v-loading="loading">
      <el-table-column
        label="Credit ID"
        prop="creditId"
        align="center"
        min-width="120"/>
      <el-table-column
        label="Credit Type"
        prop="creditType"
        align="center"
        min-width="150"/>
      <el-table-column
        label="Amount"
        align="center"
        min-width="120">
        <template slot-scope="{row}">
          <div>{{row.preAmount}}</div>
          <div class="text-post" v-if="row.creditType == 'PAY-PER-USE'">{{row.postAmount}}</div>
          <div class="text-refund" v-if="row.creditType == 'PAY-PER-USE'">{{row.refundAmount}}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="DateTime"
        prop="dateTime"
        align="center"
        min-width="120"/>
      <el-table-column
        label="E-mail"
        prop="email"
        align="center"
        min-width="120"/>
      <el-table-column
        label="Module"
        prop="module"
        align="center"
        min-width="120"/>
      <el-table-column
        label="Reference"
        prop="reference"
        align="center"
        min-width="120"/>
      <el-table-column
        label="Status"
        prop="status"
        align="center"
        min-width="120"/>
    </el-table>
    <div class="right">
      <Pagination
        v-show="table.total > 0"
        :total="table.total"
        :page.sync="filter.pageNo"
        :limit.sync="filter.pageSize"
        @pagination="getTableData" />
    </div>
  </div>
</template>

<script>
import financial from '@/http/api/financial';
import Pagination from '@/components/Pagination';
export default {
  data() {
    return {
      loading: false,
      filter: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: "",
          dateRange: [],
          creditType: "",
          paymentStatus: ""
        }
      },
      options: {
        creditType: [],
        paymentStatus: []
      },
      table: {
        list: [],
        total: 0
      }
    };
  },
  components: { Pagination },
  created() {
    this.getCreditTypeOption();
    this.getPaymentStatusOption();
    this.toSearch();
  },
  methods: {
    toSearch() {
      this.filter.pageNo = 1;
      this.getTableData();
    },
    getPaymentStatusOption() {
      financial.getPaymentStatusOptions().then(res => {
        if (res.data) {
          this.options.paymentStatus = res.data
        }
      }).catch(error => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    getCreditTypeOption() {
      financial.getCreditTypeOptions().then(res => {
        if (res.data) {
          this.options.creditType = res.data
        }
      }).catch(error => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    getTableData() {
      this.loading = true;
      financial.getSystemTransactionsPages(this.filter).then(res => {
        this.loading = false;
        if (res.data) {
          this.table.total = res.total
          this.table.list = res.data
        } else {
          this.table.total = 0
          this.table.list = []
        }
      }).catch(err => {
        this.loading = false;
        this.$message({
          message: err,
          type: 'error'
        });
        this.table.total = 0
        this.table.list = []
      })
    },
  }
}
</script>

<style scoped>
.text-post {
  color: #009E81;
}
.text-refund {
  color: #F8A300;
}
</style>