<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="button.icon"
			>{{ button.text }}</Button>
		</template>
	</div>
</template>

<script>
const btnDefs = {
	async: false,
	type: 'text',
	_loading: false,
	disabled: false,
	size: 'small',
	ghost: false,
	shape: null,
	to: null,
	replace: false,
	target: null
}
const defaultBtnOpts = {
	'编辑': Object.assign({}, btnDefs, { text: '编辑', icon: 'md-create', funcName: 'editBtn' }),
	'删除': Object.assign({}, btnDefs, { text: '删除', icon: 'md-trash', funcName: 'deleteBtn' }),
	'明细': Object.assign({}, btnDefs, { text: '明细', icon: 'md-list', funcName: 'detailBtn' }),
	'上传照片': Object.assign({}, btnDefs, { text: '上传照片', icon: 'md-camera', funcName: 'photoBtn' }),
	'授权': Object.assign({}, btnDefs, { text: '授权', icon: 'md-key', funcName: 'authBtn' }),
	'审核': Object.assign({}, btnDefs, { text: '审核', icon: 'md-checkmark', funcName: 'passBtn' }),
	'驳回': Object.assign({}, btnDefs, { text: '驳回', icon: 'md-close', funcName: 'rejectBtn' }),
	'启用': Object.assign({}, btnDefs, { text: '启用', icon: 'md-checkmark', funcName: 'enableBtn' }),
	'禁用': Object.assign({}, btnDefs, { text: '禁用', icon: 'md-close', funcName: 'disableBtn' })
};
import { Button } from 'view-design'
export default {
	data() {
		return {
			btnOPts: [],
			column: null
		};
	},
	components: {
		Button
	},
	computed: {
		subTotalRow() {
			return this.params.data._subTotal_row
		}
	},
	beforeMount() {
		this.column = this.params.colDef;
		for (let btn of this.column.cellRendererParams.btnOptions) {
			let btnOption;
			if (typeof btn === 'string') {
				btnOption = Object.assign({}, btnDefs, defaultBtnOpts[btn]);
			} else {
				btnOption = Object.assign({}, btnDefs, btn);
			}
			btnOption.funcName = btnOption.funcName ? btnOption.funcName : btnOption.name;
			this.btnOPts.push(btnOption);
		}
	},
	beforeDestroy() {
		this.btnOPts = null;
		this.column = null;
	},
	methods: {
		onClick(button, index) {
			if (button.funcName && this.params.context.onBtnClickFunc && this.params.context.onBtnClickFunc[button.funcName] && is.function(this.params.context.onBtnClick[button.funcName])) {
				if (button.async) {
					button._loading = true;
					this.params.context.onBtnClickFunc[button.funcName](this.params)
						.then(data => {
						})
						.catch(error => {
							console.error(error);
						})
						.then(() => {
							button._loading = false;
						})
				} else {
					this.params.context.onBtnClickFunc[button.funcName](this.params)
				}
			} else {
				this.params.context.onBtnClick(button.funcName, this.params);
			}
		}
	}
};
</script>



