/**
 *       _  ___   Another      
 *      / |/ (_)______ __ _____
 *     /    / / __(_-</ // (_-<
 *    /_/|_/_/\__/___/\_, /___/ 
 *                   /___/     
 *       production !
 * 
 * Licensed under the MIT License: 
 * This code is free to use and modify, 
 * as long as the copyright notice and license are kept.
 */
/**
 * @category MyEic
 * @subcategory Views
 * 
 * @extends WindozDialogContent
 */
class ConfirmDialog extends WindozDialogContent {

    actions = [
        {
            label: 'Cancel',
            onclick: this.cancel.bind(this),
            severity: 'secondary',
            role: 'cancel'
        }
    ]

    constructor(options) { 
        super(options); 
        this.options = options;
        if(this.options.cancelLabel) this.actions[0].label = this.options.cancelLabel
        if(this.options.cancelSeverity) this.actions[0].severity = this.options.cancelSeverity
        if(this.options.okLabel != '') {
            this.actions.push({
                label:  this.options.okLabel || 'OK',
                onclick: this.comply.bind(this),
                severity: options.okSeverity||options.severity,
                role: 'comply'
            });
        }

    }
    
    DOMContentLoaded() { 
        ui.eicfy(this.el); 
        if(typeof(this.options.contentLoaded)=='function') this.options.contentLoaded(this.el)
        if(this.options.promptsClass) this.form = new Form(this.find('.'+this.options.promptsClass))
    }

    comply() {
        let data = true
        if(this.options.promptsClass){
            if(!this.form.validate()) return 
            data = this.form.value
        } 

        this.actions.find(o => o.role == 'comply').button.loading = true;
        this.actions.find(o => o.role == 'cancel').button.disabled = true;
        if(this.options.okPromise){
            this.options.okPromise(data).then( this.onSuccess.bind(this,data), this.onFailed.bind(this) )
        } else {
            this.onSuccess(data)
        }
    }

    onSuccess(data) {
        this.actions.find(o => o.role == 'comply').button.loading = false;
        this.actions.find(o => o.role == 'cancel').button.disabled = false;
        this.commit(data);
    }

    onFailed() {
        this.actions.find(o => o.role == 'comply').button.loading = false;
        this.actions.find(o => o.role == 'cancel').button.disabled = false;
        this.abort();
    }
}

app.registerClass('ConfirmDialog',ConfirmDialog);