import { Component } from '../../lib/vues6/vues6.js'
import { session } from '../service/Session.service.js'
import { location_service } from '../service/Location.service.js'
import { auth_api } from '../service/AuthApi.service.js'

const template = `
<div class="coreid-auth-page col-lg-6 col-md-8 col-sm-10 col-xs-12 offset-lg-3 offset-md-2 offset-sm-1 offset-xs-0 text-left">
    <div class="coreid-auth-page-inner">
        <div class="coreid-header font-weight-light">{{ app_name }}</div>
        <span v-if="step === 0">
            <div class="coreid-message">
                <span v-html="t['mfa.disable_prompt']"></span>
            </div>
            <div v-if="error_message" class="error-message">{{ error_message }}</div>
            <div v-if="other_message" class="other-message">{{ other_message }}</div>
            <div class="buttons text-right pad-top">
                <button type="button" class="btn btn-primary" @click="back_click">{{ t['common.cancel'] }}</button>
                <button type="button" class="btn btn-primary" @click="continue_click">{{ t['mfa.disable'] }}</button>
            </div>
        </span>
        <div class="coreid-loading-spinner" v-if="loading"><div class="inner"></div></div>
    </div>
</div>
`

export default class MFADisableComponent extends Component {
    static get selector() { return 'coreid-mfa-disable-page' }
    static get template() { return template }
    static get props() { return [] }

    constructor() {
        super()

        this.app_name = ''
        this.step = 0
        this.loading = false
        this.error_message = ''
        this.other_message = ''
        this.t = {}
    }

    async vue_on_create() {
        this.app_name = session.get('app.name')

        this.t = await T(
            'common.cancel',
            'common.unknown_error',
            'mfa.disable_success',
            'mfa.disable',
            'mfa.disable_prompt'
        )
    }

    async back_click() {
        this.loading = true
        await location_service.redirect('/dash/profile', 500)
    }

    async continue_click() {
        this.loading = true
        const success = await auth_api.mfa_disable()
        if ( success ) {
            this.other_message = this.t['mfa.disable_success']
            await location_service.redirect('/dash/profile', 3000)
        } else {
            this.error_message = this.t['common.unknown_error']
            await location_service.reload(4000)
        }
    }
}
