/**
 * 只针对答题使用其他使用需要改装
 * bg弹窗背景图
 * type：按钮类型和事件 end结束 quit退出 report报告
 * raceId 比赛id
 * reportCon报告数据
 */
import Utils from '../../../../util/utils'
import { postQuestionSubmit } from '../../../../service/match';

class matchDialogScene extends scene {
    constructor(scope) {
        super(scope);
    }

    onCreate(data) {
        const hide = data.type === 'report' ? 'block' : 'none';
        this.setContentView(require('../../../../res/tpl/match/matchDialog.tpl'), {
            bg: data.bg,
            type: data.type,
            hide,
            reportCon: data.reportCon,
            totalScore: data.reportCon.totalScore || 0
        }, 'matchDialog', {
            'isParentShow': true
        }, () => {
            this.type = data.type;
            if (this.type === 'report' || this.type === 'end') {
                Utils.$('.one-btn').style = 'display: block';
            } else {
                Utils.$('.two-btn').style = 'display: flex';
            }
            if (this.type === 'report') {
                Utils.$('.match-dialog').style.zIndex = '100'
            }
            this.raceId = data.raceId;
        });
    }


    onResume() {

    }

    onPause() {

    }

    onDestroy() {

    }

    onActive() {

    }

    onInactive() {

    }

    // 所有事件类函数默认触发时会传递过来一个Event,其中包含着事件响应节点以及其他相关信息
    // 查看信息：console.log(e)
    // 获取Event节点Id的方法(前提是触发事件的节点存在Id)
    // 在使用Atv时Id对应：e.target.id
    // 非使用Atv是Id对应：e.target.id
    onOK(e) {
    const flag = e.target.con.attributes['data-event'].nodeValue;
    // 代替switch
    const actions = new Map([
        ['back', () => {
            this.hideScene({}, 'match')
        }],
        ['end', () => {
            this.peport();
            Utils.$('.one-btn').setAttribute('data-event', 'report');
            Utils.$('.one-btn').style = 'bottom: .5rem';
            this.moye.root.reRender();
        }],
        ['report', () => {
            console.log('去排行榜')
            this.hideScene();
            this.showScene(require('./rankingScene'), {
                raceId: this.raceId
            });
        }],
        ['quit', () => {
            // this.hideScene();
            if (this.raceId) {
                this.submitRecord().then(res => {
                    console.log(res)
                    console.log(res.rightAmount)
                    const {rightAmount, answerAmount, totalScore} = res;
                    Utils.$('.two-btn').setAttribute('data-event', 'report');
                    Utils.$('.two-btn').setAttribute('fe-role', 'Widget');
                    Utils.$('.two-btn').style = 'bottom: .5rem;width: 4.25rem;height: .74rem;';
                    Utils.$('.two-btn').innerHTML = '';
                    this.peport()
                    Utils.$('.report-container').innerHTML = `
                    <div>用时：1分钟</div>
                    <div>答对：${rightAmount}/${answerAmount}题</div>
                    <div>得分：${totalScore || 0}分</div>
                    `
                    this.moye.root.reRender();
                })
            }
        }],
        ['cancel', () => {
            this.hideScene();
        }]
      ])
      const action = actions.get(flag)
      if (action) {
        clickMp3();
        action()
      }
    }

    onBack() {
    }
    peport() {
        Utils.$('.match-dialog').style.zIndex = '100';
        Utils.$('.dialog-container').style.background = 'url(assets/match/baogao_bg.png) 0 0 / 100% 100%'
        Utils.$('.report-container').style = 'display: block';
    }
    submitRecord() {
        return new Promise((resolve, reject) => {
            postQuestionSubmit({
                raceId: this.raceId
            }).success(res => {
                resolve(res.data)
                // this.hideScene();
            })
        })

    }
}

module.exports = matchDialogScene;
