<template>
  <div class="stats-reviewer">
    <div ref="listContainer" class="list-container">
      <el-table
        v-if="tableHeight"
        v-loading="listLoading"
        size="small"
        stripe
        height="97%"
        :data="list"
        show-summary
        :summary-method="getSummaries"
        @sort-change="sortByColumn"
        @selection-change="handleSelectChange"
      >
        <el-table-column type="selection" />
        <el-table-column type="index" width="40" />
        <el-table-column
          prop="Hospital"
          sortable="custom"
          label="Hospital"
          min-width="130"
          show-overflow-tooltip
        />
        <el-table-column
          label="Name"
          show-overflow-tooltip
          min-width="100"
          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="105"
          show-overflow-tooltip
        />

        <el-table-column prop="ReviewerCode" label="ID" min-width="80" show-overflow-tooltip sortable="custom" />
        <el-table-column prop="Pending" label="Pending" min-width="80" show-overflow-tooltip sortable="custom" />
        <el-table-column prop="Approved" label="Approved" min-width="90" show-overflow-tooltip sortable="custom" />
        <el-table-column prop="Reading" label="Reading" min-width="90" show-overflow-tooltip sortable="custom" />
        <el-table-column prop="Finished" label="Finished" min-width="90" show-overflow-tooltip sortable="custom" />
        <el-table-column prop="Total" label="Total" min-width="90" show-overflow-tooltip sortable="custom" />
        <el-table-column prop="EntryRate" label="Entry Rate" min-width="100" show-overflow-tooltip sortable="custom">
          <template slot-scope="scope">{{ scope.row.EntryRate }}%</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 { getEnrollStatByReviewer } from '@/api/statistics'
import Pagination from '@/components/Pagination'
import { exportExcelWithTotal } from '@/utils/export'
export default {
  components: { Pagination },
  props: {
    param: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      list: [],
      listQuery: {},
      total: 0,
      listLoading: false,
      tableHeight: 0,
      arrID: []
    }
  },
  watch: {
    param: {
      handler(newValue, oldValue) {
        this.listQuery.HospitalId = newValue.HospitalId
        this.listQuery.Reviewer = newValue.Reviewer
        this.listQuery.BeginDate = newValue.BeginDate
        this.listQuery.EndDate = newValue.EndDate
      },
      deep: true
    }
  },
  mounted() {
    this.tableHeight = this.$refs.listContainer.clientHeight + 'px'
    this.listQuery.HospitalId = this.param.HospitalId
    this.listQuery.Reviewer = this.param.Reviewer
    this.listQuery.BeginDate = this.param.BeginDate
    this.listQuery.EndDate = this.param.EndDate
    this.listQuery.PageIndex = 1
    this.listQuery.PageSize = 20
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getEnrollStatByReviewer(this.listQuery)
        .then(res => {
          this.listLoading = false
          this.list = res.Result.CurrentPageData
          this.total = res.Result.TotalCount
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    handleSelectChange(val) {
      const arr = []
      for (let index = 0; index < val.length; index++) {
        arr.push(val[index].Id)
      }
      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 = []
      columns.forEach((column, index) => {
        if (index === 2) {
          sums[index] = 'Total (Current Page)'
          return
        }

        if (column.property === 'EntryRate') return
        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 prev + curr
            } else {
              return prev
            }
          }, 0)
          // sums[index] += ' ?';
        } else {
          // sums[index] = 'N/A';
        }
      })
      sums[11] = (((sums[9] + sums[8]) * 100) / sums[10]).toFixed(2) + '%'
      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()
    },
    resetList() {
      this.getList()
    },
    export() {
      const sheetName = `${this.listQuery.BeginDate}~${this.listQuery.EndDate}`
      const columns = [
        { key: 'Index', width: 5 },
        { key: 'Hospital', width: 25 },
        { key: 'LastName', width: 15 },
        { key: 'FirstName', width: 15 },
        { key: 'ChineseName', width: 10 },
        { key: 'ReviewerCode', width: 10 },
        { key: 'Pending', width: 10 },
        { key: 'Approved', width: 10 },
        { key: 'Reading', width: 10 },
        { key: 'Finished', width: 10 },
        { key: 'Total', width: 10 },
        { key: 'Rate', width: 20 }
      ]
      const conditional = `Beginning Month:${this.listQuery.BeginDate}  End Month:${this.listQuery.EndDate}`
      const headerArr = [
        '',
        'Hospital',
        'Surname',
        'Given Name',
        'Name CN',
        'Reviewer ID',
        'Pending',
        'Approved',
        'Reading',
        'Finished',
        'Total',
        'Entry rate(%)'
      ]

      var pending = 0
      var approved = 0
      var reading = 0
      var finished = 0
      var total = 0
      let index = 1
      var exportExcelData = this.list.filter(item => this.arrID.indexOf(item.Id) > -1)
      exportExcelData.forEach(element => {
        element.Index = index++
        element.Rate = element.EntryRate / 100
        pending += element.Pending
        approved += element.Approved
        reading += element.Reading
        finished += element.Finished
        total += element.Total
      })
      const totalRow = ['', 'Total', '', '', '', '', pending, approved, reading, finished, total, (reading + finished) / total]
      const numFmts = [{ colIndex: 12, format: '0.00%' }]
      exportExcelWithTotal(sheetName, columns, 'Enrollments Statistics', conditional, headerArr, exportExcelData, totalRow, numFmts)
    }
  }
}
</script>
<style lang="scss" scoped>
.stats-reviewer {
  height: 100%;
  display: flex;
  flex-direction: column;
  .list-container {
    flex: 1;
    .el-table {
      overflow: visible !important;
    }
  }
  .pagination{
    text-align: right;
  }
}
</style>
