import {getRankingList, getRaceRanking } from '../../../../service/match';
import {
  $,
  setBg,
  setBack,
  readLocalstorage
} from '../../../../util/utils'
class rankingScene extends scene {
    constructor(scope) {
        super(scope);

    }

    onCreate(data) {
        this.raceId = data.raceId;
        const type = this.raceId ? 'single' : 'total'
        this.getRanking().then(res => {
            console.log(res)
            this.setContentView(require('../../../../res/tpl/match/ranking.juicer'), {
                data: res,
                type,
                rankImgs: ['one', 'two', 'three']
            }, 'ranking', {
                containerId: 'root'
            }, () => {
              type === 'single' ? setBg('#403C94') : setBg('assets/match/ranking_bg.jpg');
              setBack(false, '.32');
            });
        })

    }
    onOK(e) {
        console.log(e)
        const flag = e.target.con.attributes['data-event'].nodeValue;
        switch (flag) {
            case 'back':
                this.hideScene({}, 'match')
                break;

        }
    }
    getRanking() {
        return new Promise((resolve, reject) => {
            const grade = this.grade = readLocalstorage('class');
            if(this.raceId) {
                getRaceRanking(this.raceId, grade).success(res => {
                    const data = res.data;
                    data.rankingListVOS = this.deformationArr(data.rankingListVOS)
                    this.nameToMobile(data.rankingListVOS)
                    resolve(data)
                })
            }else {
                getRankingList(grade).success(res => {
                    this.nameToMobile(res.data.list)
                    resolve(res.data.list);
                })
            }
        })

    }

    onBack(e) {

    }
    deformationArr(data, num, ) {
        const newArr = [];
        num = num || 10;
        while(data.length > 0) {
            newArr.push(data.splice(0, num));
        }
        return newArr;
    }
    // 转换手机号
    nameToMobile(data) {
        data.forEach(item => {
            const childData = item.rankingListVOS || item;
            childData.forEach(childItem => {
                if (childItem.member) {
                    childItem.member.nickName = childItem.member.nickName || childItem.member.mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
                } else {
                    childItem.member = {}
                    childItem.member.nickName = '199****8888'
                }
            })
        })
    }
}

module.exports = rankingScene;