<template>
  <div class="payment-list">
    <div class="list-container">
      <el-table
        ref="paymentList"
        v-loading="listLoading"
        size="small"
        stripe
        :data="list"
        height="99%"
        show-summary
        :summary-method="getSummaries"
        @sort-change="sortByColumn"
        @selection-change="selectMore"
      >
        <el-table-column type="selection" />
        <el-table-column type="index" width="40" />
        <el-table-column
          label="Name"
          show-overflow-tooltip
          min-width="130"
          prop="FirstName"
          sortable="custom"
        >
          <template slot-scope="scope">{{ scope.row.LastName }} / {{ scope.row.FirstName }}</template>
        </el-table-column>
        <el-table-column
          prop="ChineseName"
          sortable="custom"
          label="Name CN"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          prop="ReviewerCode"
          sortable="custom"
          label="Reviewer ID"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          prop="TotalUSD"
          label="Total($)"
          min-width="120"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.TotalUSD | rounding }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="TotalCNY"
          label="Total(¥)"
          min-width="120"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.TotalCNY }}</span>
          </template>
        </el-table-column>
        <el-table-column label="Action" min-width="60">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="handDetail(scope.row)">Detail</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <pagination :total="total" :page.sync="listQuery.PageIndex" :limit.sync="listQuery.PageSize" @pagination="getList" />
    </div>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { exportExcelWithTotal } from '@/utils/export'
import { getPaymentHistoryList } from '@/api/financials'
import floatObj from '@/utils/float-operation'
export default {
  components: { Pagination },
  filters: {
    rounding(value) {
      return value ? Number(value).toFixed(2) : value
    }
  },
  props: {
    param: {
      type: Object,
      default() { return {} }
    }
  },
  data() {
    return {
      list: [],
      listQuery: { PageIndex: 1, PageSize: 20 },
      listLoading: false,
      total: 0,
      arrID: []
    }
  },
  watch: {
    param: {
      handler(newValue, oldValue) {
        this.listQuery.PageIndex = newValue.PageIndex
        this.listQuery.Reviewer = newValue.Reviewer
        this.listQuery.BeginMonth = newValue.BeginMonth
        this.listQuery.EndMonth = newValue.EndMonth
        this.listQuery.Nation = newValue.Nation
      },
      deep: true
    }
  },
  mounted() {
    this.listQuery.Reviewer = this.param.Reviewer
    this.listQuery.BeginMonth = this.param.BeginMonth
    this.listQuery.EndMonth = this.param.EndMonth
    this.listQuery.Nation = this.param.Nation
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getPaymentHistoryList(this.listQuery).then(res => {
        this.listLoading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.listLoading = false })
    },
    handDetail(row) {
      this.$router.push({
        name: 'PaymentHistoryDetail',
        query: {
          name: row.LastName + ' ' + row.FirstName,
          beginMonth: this.listQuery.BeginMonth,
          endMonth: this.listQuery.EndMonth,
          reviewerId: row.ReviewerId
        }
      })
    },
    resetList() {
      this.listQuery.PageIndex = 1
      this.getList()
    },
    selectMore(val) {
      const arr = []
      for (let index = 0; index < val.length; index++) {
        arr.push(val[index].ReviewerId)
      }
      this.arrID = arr
      if (this.arrID.length > 0) {
        this.$emit('selectMore', false)
      } else {
        this.$emit('selectMore', true)
      }
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      // const scope = this
      columns.forEach((column, index) => {
        if (index === 2) {
          sums[index] = 'Total (Current Page)'
          return
        }
        if (column.property === 'TotalUSD') {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values
              .reduce((prev, curr) => {
                const value = Number(curr)
                if (!isNaN(value)) {
                  return floatObj.add(prev, curr)
                } else {
                  return prev
                }
              }, 0)
          }
        }
        if (column.property === 'TotalCNY') {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values
              .reduce((prev, curr) => {
                const value = Number(curr)
                if (!isNaN(value)) {
                  return floatObj.add(prev, curr)
                } else {
                  return prev
                }
              }, 0)
          }
        }
      })

      return sums
    },
    sortByColumn(column) {
      if (column.order === 'ascending') {
        this.listQuery.Asc = true
      } else {
        this.listQuery.Asc = false
      }
      this.listQuery.SortField = column.prop
      this.listQuery.PageIndex = 1
      this.getList()
    },
    export() {
      const sheetName = `${this.listQuery.BeginMonth}--${this.listQuery.EndMonth}`
      const columns = [
        { key: 'Index', width: 5 },
        { key: 'LastName', width: 15 },
        { key: 'FirstName', width: 15 },
        { key: 'ChineseName', width: 15 },
        { key: 'ReviewerCode', width: 20 },
        { key: 'TotalUSD', width: 15 },
        { key: 'TotalCNY', width: 15 }
      ]
      const conditional = `Type:Payment  Beginning Month:${this.listQuery.BeginMonth}  End Month:${this.listQuery.EndMonth}`
      const headerArr = ['', 'Surname', 'Given Name', 'Name CN', 'Reviewer ID', 'Total($)', 'Total(¥)']
      const numFmts = [
        { colIndex: 6, format: '#,##0.00' },
        { colIndex: 7, format: '#,##0.00' }
      ]
      let totalCNY = 0
      let totalUSD = 0
      var exportExcelData = this.list.filter(item => this.arrID.indexOf(item.ReviewerId) > -1)
      let index = 1
      exportExcelData.forEach(element => {
        totalCNY += element.TotalCNY
        totalUSD += element.TotalUSD
        element.Index = index++
      })
      const totalRow = ['', 'Total', '', '', '', totalUSD, totalCNY]
      exportExcelWithTotal(sheetName, columns, 'Payment History', conditional, headerArr, exportExcelData, totalRow, numFmts)
    }
  }
}
</script>
<style lang="scss" scoped>
.payment-list{
  height: 100%;
  display: flex;
  flex-direction: column;
  .list-container{
    padding: 5px 0px;
    flex: 1;
    .el-table{
      overflow:visible !important;
    }
  }
  .pagination{
    text-align: right;
  }
}
</style>
