<template>
  <div class="app-container">
    <el-form
      :model="filter.pageVo"
      class="filter-container"
      label-position="right">
      <div class="filter-view">
        <el-form-item>
          <el-input
            clearable
            v-model="filter.pageVo.criteria"
            placeholder="Search by Number/Email"
            @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="filter.pageVo.date"
            type="month"
            format="yyyy-MM"
            value-format="yyyy-MM"
            placeholder="Filter Date"/>
        </el-form-item>
        <div>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getTableData">
            Search
          </el-button>
        </div>
      </div>
    </el-form>
    <el-table
      :data="table.list"
      v-loading="loading">
      <el-table-column
        label="Transaction ID"
        align="center"
        prop="creditHistoryPk"
        min-width="120"/>
      <el-table-column
        label="User ID"
        align="center"
        min-width="80">
          <template slot-scope="{row}">
            <span>{{ row.userPk }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Email"
        align="center"
        min-width="100">
        <template slot-scope="{row}">
          <span :title="row.email">{{ row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="Platform"
        align="center"
        prop="paymentPlatform"
        min-width="100"/>
      <el-table-column
        label="Top Up Type"
        align="center"
        prop="payType"
        min-width="120"/>
      <el-table-column
        label="Amount"
        align="center"
        prop="amount"
        min-width="100"/>
      <el-table-column
        label="Creation Time"
        align="center"
        prop="creationTime"
        min-width="120"/>
      <el-table-column
        label="Payment Status"
        align="center"
        prop="paymentStatus"
        min-width="130"/>
      <el-table-column
        label="Payment Reference"
        align="center"
        prop="paymentReference"
        min-width="150">
        <template v-slot="{ row }">
          <div @dblclick="copyReference(row.paymentReference)">
            <el-input
              type="textarea"
              readonly
              v-model="row.paymentReference"
              class="payment-reference"
              :title="row.paymentReference"/>
          </div>
        </template>
        <template slot="header">
          <div title="Double click to copy">Payment Reference</div>
        </template>
      </el-table-column>
      <el-table-column
        label="Update Time"
        align="center"
        prop="updateTime"
        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 site from '@/http/api/site'
import ocpp from '@/http/api/ocpp'
import financial from '@/http/api/financial'
import Pagination from '@/components/Pagination'
import handleClipboard from '@/utils/clipboard'
export default {
  data() {
    return {
      loading: false,
      listLoading: false,
      siteOptions: [],
      stationOptions: [],
      table: {
        list: [],
        total: 0
      },
      filter: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          date: "",
          sitePk: "",
          criteria: "",
          chargeBoxId: ""
        }
      },
    }
  },
  components: { Pagination },
  created() {
    //this.getSiteOptions()
    this.getTableData()
  },
  methods: {
    getSiteOptions(search) {
      site.getAllSiteList({siteName: search ?? ""}).then(res => {
        if (res.data && res.data.length > 0) {
          this.siteOptions = res.data
        }
      });
    },
    getStationOptions() {
      this.listLoading = true;
      ocpp.getStationPages({
        pageNo: 1,
        pageSize: 20,
        pageVo: {
          date: "",
          sitePk: this.filter.pageVo.sitePk
        }
      }).then(res => {
        if (res.data) {
          this.stationOptions = res.data;
        }
      }).finally(() => {
        this.listLoading = false;
      })
    },
    getTableData() {
      this.loading = true;
      financial.getTopUpPages(this.filter).then(res => {
        this.loading = false;
        if (res.data) {
          this.table.total = res.total
          this.table.list = res.data
        }
      }).catch(err => {
        this.loading = false;
        this.$message({
          message: err,
          type: 'error'
        })
      })
    },
    copyReference(data) {
      handleClipboard(data)
    }
  }
}
</script>

<style>
  .payment-reference {
    width: 100%;
  }
  .payment-reference .el-textarea__inner {
    border: none;
  }
</style>