<template>
  <div class="stats-trial">
    <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="TrialCode"
          sortable="custom"
          label="Trial ID"
          min-width="130"
          show-overflow-tooltip
        />
        <el-table-column
          prop="Expedited"
          min-width="110"
          label="Expedited"
          show-overflow-tooltip
          sortable="custom"
        >
          <template
            slot-scope="scope"
          >{{ scope.row.Expedited==0?'No':scope.row.Expedited==1?'24H':'48H' }}</template>
        </el-table-column>
        <el-table-column prop="Indication" label="Indication" min-width="120" sortable="custom" show-overflow-tooltip />
        <el-table-column prop="Cro" label="CRO" min-width="120" sortable="custom" show-overflow-tooltip />

        <el-table-column prop="ReviewerNameList" label="Reviewer Name" min-width="100" show-overflow-tooltip>
          <template
            slot-scope="scope"
          >{{ scope.row.ReviewerNameList }}</template>
        </el-table-column>
        <el-table-column prop="ReviewerNameCNList" label="Reviewer Name CN" min-width="100" show-overflow-tooltip>
          <template
            slot-scope="scope"
          >{{ scope.row.ReviewerNameCNList }}</template>
        </el-table-column>
        <el-table-column prop="EnrollCount" label="Entry Count" min-width="90" sortable="custom" show-overflow-tooltip />
      </el-table>
    </div>
    <div class="pagination">
      <pagination :total="total" :page.sync="listQuery.PageIndex" :limit.sync="listQuery.PageSize" @pagination="getList" />
    </div>
  </div>
</template>
<script>
import { getEnrollStatByTrial } 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.TrialCode = newValue.TrialCode
        this.listQuery.Indication = newValue.Indication
        this.listQuery.CroId = newValue.CroId
        this.listQuery.BeginDate = newValue.BeginDate
        this.listQuery.EndDate = newValue.EndDate
      },
      deep: true
    }
  },
  mounted() {
    this.tableHeight = this.$refs.listContainer.clientHeight + 'px'
    this.listQuery.TrialCode = this.param.TrialCode
    this.listQuery.Indication = this.param.Indication
    this.listQuery.CroId = this.param.CroId
    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
      getEnrollStatByTrial(this.listQuery)
        .then(res => {
          this.listLoading = false
          this.list = res.Result.CurrentPageData
          this.list.forEach(item => {
            item.ReviewerNameList = item.ReviewerNameList.join(', ')
            item.ReviewerNameCNList = item.ReviewerNameCNList.join(', ')
          })
          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
        }
        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';
        }
      })
      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: 'TrialCode', width: 15 },
        { key: 'Expedited', width: 15 },
        { key: 'Indication', width: 15 },
        { key: 'Cro', width: 15 },
        { key: 'ReviewerNameList', width: 25 },
        { key: 'ReviewerNameCNList', width: 25 },
        { key: 'EnrollCount', width: 15 }
      ]
      const conditional = `Beginning Month:${this.listQuery.BeginDate}  End Month:${this.listQuery.EndDate}`
      const headerArr = [
        '',
        'Trial ID',
        'Expedited',
        'Indication',
        'CRO',
        'Reviewer Name',
        'Reviewer Name CN',
        'Enroll Count'
      ]
      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.Expedited = element.Expedited === 0 ? 'No' : element.Expedited === 1 ? '24H' : '48H'
        total += element.EnrollCount
      })
      const totalRow = ['', 'Total', '', '', '', '', '', total]
      exportExcelWithTotal(sheetName, columns, 'Enrollments Statistics', conditional, headerArr, exportExcelData, totalRow, [])
    }
  }
}
</script>
<style lang="scss" scoped>
.stats-trial {
  height: 100%;
  display: flex;
  flex-direction: column;
  .list-container {
     flex: 1;
    .el-table {
      overflow: visible !important;
    }
  }
  .pagination{
    text-align: right;
  }
}
</style>
