<template>
<div class="app-container calendar-list-container">
  <div class="app-container">
    <el-table :data="detail" :span-method="objectSpanMethod" border style="width: 100% margin-top: 20px">
      <el-table-column prop="MemberRecord.GambleMember.name" label="玩家" width="180">
      </el-table-column>
      <el-table-column prop="MemberRecord.door" label="押">
      </el-table-column>
      <el-table-column prop="MemberRecord.wager" label="額度">
      </el-table-column>
      <el-table-column prop="DealingRecord.totalPoints" label="點數">
      </el-table-column>
      <el-table-column prop="MemberRecord.earned" label="輸贏">
      </el-table-column>
      <el-table-column prop="amount3" label="抽水">
      </el-table-column>
      <el-table-column prop="Membehips" label="輸贏合計">
      </el-table-column>
      <el-table-column prop="amount3" label="福利">
      </el-table-column>
      <el-table-column prop="MemberRecord.GambleMember.chips" label="最終積分">
      </el-table-column>
    </el-table>
  </div>
</div>
</template>

<script>
import { fetchDetails, fetchMemberRecords, fetchDealingRecords } from '@/api/gambleGameBucket'
import _ from 'lodash'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      total: null,
      memberRecords: [],
      dealingRecords: [],
      groupMember: [],
      detail: [],
    }
  },
  props: [ 'bucket' ],
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      let memberTemp ,dealingTemp ,group ,detailTemp
      fetchMemberRecords(this.bucket).then(response => {
        memberTemp = response.data.rows.filter(record => {
          return record.door >= 1 && record.door <= 6
        })
        group = _.groupBy(memberTemp, record => {
          return record.GambleMember.name;
        });
        // this.groupMember = _.values(group)
        // console.log('temp', this.groupMember)
        group = _.values(group)
        // console.log('temp', group)
        group.map(person => {
          // console.log('preson',preson)
          if(person.length !== 6){
            for(let i = 1 ; i<7 ;i++){
              if(! _.find(person, {door: i})){
                // 補上 lost
                let tempObj = Object.assign({}, person[0])
              }
            }
          }
        })
        memberTemp.map(record => {
          this.memberRecords.push(record)
        })
      })
      fetchDealingRecords(this.bucket).then(response => {
        dealingTemp = response.data.rows.filter(record => {
          return record.door !== -1
        })
        response.data.rows.map(record => {
          this.dealingRecords.push(record)
        })
      })
      fetchDetails(this.bucket).then(reponse => {
        let group
        dealingTemp = reponse.data.rows.filter(record => {
          return  record.DealingRecord.door >= 1 && record.DealingRecord.door <= 6 && 
            record.MemberRecord.door >= 1 && record.MemberRecord.door <= 6 &&
            record.MemberRecord.door === record.DealingRecord.door
        })
        console.log('dddd',this.detail)

        group = _.groupBy(dealingTemp, record => {
          return record.MemberRecord.GambleMember.name;
        });
        // this.groupMember = _.values(group)
        // console.log('temp', this.groupMember)
        group = _.values(group)
        console.log('temp', group)


        group.map(person => {
          // console.log('preson',preson)
          if(person.length !== 6){
            console.log('hi')
            for(let i = 1 ; i<7 ;i++){
              console.log(i)
              if(! _.find(person, {MemberRecord:{door: i}})){
                // 補上 lost
                let tempObj = Object.assign({}, person[0])
                tempObj.DealingRecord.door = i
                tempObj.MemberRecord.door = i
                console.log('lost',i,tempObj)
                this.detail.push(tempObj)
              }
            }
          }
        })

        // dealingTemp = _.sortBy(dealingTemp, 'MemberRecord.GambleMember.name');
        dealingTemp.map(record => {
          this.detail.push(record)
        })
        console.log('ccc',this.detail)
        this.detail =  _.sortBy(this.detail, 'MemberRecord.GambleMember.name');
        console.log('AAA',this.detail)
      })
      console.log('memberRecords', this.memberRecords )
      console.log('dealingRecords', this.dealingRecords)

      this.listLoading = false
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 5|| columnIndex === 6|| columnIndex === 7|| columnIndex === 8) {
        this.groupMember
        if (rowIndex % 6 === 0) {
          return {
            rowspan: 6,
            colspan: 1
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    }
  }
}
</script>
