import Vue from "vue";
const btnDefs = {
    async: false,
    type:'text',
    _loading:false,
    disabled:false,
    size:'small',
    ghost:false,
    shape:null,
    to:null,
    replace:false,
    target:null
}
import is from 'is';
export default Vue.extend({
    template: `
        <div>
            <template v-if="!subTotalRow">
                <Button
                    class="btn-in-cell"
                    style="margin:0 4px;"
                    :type="button.type"
                    :size="button.size"
                    :ghost="button.ghost"
                    :shape="button.shape"
                    :disabled="button.disabled"
                    :to="button.to"
                    :replace="button.replace"
                    :target="button.target"
                    @click.prevent.stop="onClick(button)"
                    v-for="(button,index) in btnOPts"
                    :key="index"
                    :loading="button._loading"
                >
                    <Icon :type="button.icon" v-if="button.icon && !button._loading"/>
                    {{ button.text }}
                </Button>
            </template>
        </div>
    `,
    data() {
        return {
            btnOPts: [],
            columns: [],
            context: null,
        };
    },
    computed:{
        subTotalRow(){
            return this.params.data._subTotal_row
        }
    },
    beforeMount() {
        this.columns = this.params.colDef;
        this.context = this.params.context;
        let originColumn;
        for ( let column of this.context.flatColumns ){
            if ( column.colId === this.columns.colId ){
                originColumn = column;
                break;
            }
        }
        for ( let btn of originColumn.buttons ){
            this.btnOPts.push(Object.assign({},btnDefs,btn));
        }
    },
    methods: {
        onClick(button, index) {
            //console.log(button)
            if (button.funcName && this.context.onBtnClick[button.funcName] && is.function(this.context.onBtnClick[button.funcName])) {
                if (button.async) {
                    button._loading = true;
                    let onclick = this.context.onBtnClick[button.funcName](this.params)
                        .then(data => {
                        })
                        .catch(error => {
                            console.error(error);
                        })
                        .then(() => {
                            button._loading = false;
                        })
                } else {
                    this.context.onBtnClick[button.funcName](this.params)
                }
            }
        }
    }
});