<template name="button">
	<view class="button-template">
		<!-- 底部按钮 -->
		<view class="button-content">
			<view class="btn btn-confirm" v-if="btnState.isConfirm" @click.stop="btnConfirm">操作</view>
		</view>
		<tui-actionsheet
			:show="showActionSheet"
			:tips="tips"
			:item-list="itemList"
			:mask-closable="maskClosable"
			:color="color"
			:size="size"
			:is-cancel="isCancel"
			@click="handleClickItem"
			@cancel="closeActionSheet"
		></tui-actionsheet>
	</view>
</template>

<script>
export default {
	name: 'button',
	props: {
		status: {//收款款项类型：1订单款，2非订单款，3返佣款 4订单款或者非订单款（因财务阶段无法区分订单非订单）, 5供应商退款
			type: Number
		},
		dataInfo: {
			type: Object
		}
	},
	data() {
		return {
			receipt:{},
			btnState: this.initStatus(),
			mapStateArr: [{ label: 'isConfirm', val: [1,4,5], status: true }],
			souStateArr:[
				{ 
					type:1,
					action:[
						{ text: '作废',handleId:0},
						{ text: '设为非订单款',handleId:2},
						{ text: '设为返佣款',handleId:3},
						{ text: '设为供应商退款',handleId:4},
					],
				},
				{ 
					type:2,
					action:[
						{ text: '作废',handleId:0},
						{ text: '设为普通款',handleId:1},
						{ text: '设为返佣款',handleId:3},
						{ text: '设为供应商退款',handleId:4}
					],
				},
				{ 
					type:3,
					action:[
						{ text: '作废',handleId:0},
						{ text: '设为普通款',handleId:1},
						{ text: '设为供应商退款',handleId:4}
					],
				},
				{ 
					type:5,
					action:[
						{ text: '作废',handleId:0},
						{ text: '设为普通款',handleId:1},
						{ text: '设为返佣款',handleId:3}
					],
				},
			],
			showActionSheet: false,
			maskClosable: true,
			tips: '',
			itemList: [],
			color: '#9a9a9a',
			size: 24,
			isCancel: true
		}
	},
	created() {
		this.initData(this.status)
		this.initActionSheet(this.dataInfo)
	},
	computed: {},
	watch: {
		status: {
			handler: function(val) {
				this.initData(val)
			},
			deep: true //对象内部的属性监听，也叫深度监听
		},
		dataInfo:{
			handler: function(val) {
				this.initActionSheet(val)
			},
			deep: true //对象内部的属性监听，也叫深度监听
		}
	},
	methods: {
		initData(resVal,dataVal) {
			/**
			 * @按钮根据状态显示
			 * @收款状态：1待确认、4审核未通过、5收款撤销【线上支付成功为审核通过】
			 */
			this.receipt = dataVal
			this.btnState = this.initStatus()
			this.mapStateArr.forEach(el => {
				el.val.forEach(value => {
					if (resVal === value) {
						this.btnState[el.label] = el.status
					}
				})
			})
		},
		initActionSheet(dataVal){
			this.receipt = dataVal
			this.souStateArr.forEach(el => {
				if (dataVal.receiptType === el.type) {
					this.itemList = el.action
				}
			})
		},
		initStatus() {
			let btnState = {
				isConfirm: false
			}
			return btnState
		},
		handleClickItem(data){
			const handleAction = this.handleArrayReturn(data.index)
			const receipt ={
				handleAction:handleAction,
				receipt: this.receipt
			}
			this.$emit('buttonConfirm',receipt)
			this.showActionSheet = false
		},
		handleArrayReturn(data){// 处理操作后结果
			let handleValue = {}
			this.souStateArr.forEach(el => {
				if(this.receipt.receiptType === el.type){
					el.action.forEach((value,index) => {
						if (data === index) {
							handleValue = value
						}
					})
				}
			})
			return handleValue
		},
		btnConfirm() {
			this.showActionSheet = true
		},
		closeActionSheet() {
			this.showActionSheet = false
		},
		// btnConfirm(){
		// 	this.$emit('buttonConfirm',data)
		// }
	}
}
</script>
<style lang="scss">
.button-template {
	width: 100%;
	height: auto;
	float: left;
	background: #ffffff;
	.button-content {
		width: 100%;
		height: auto;
		float: left;
		position: relative;
		.btn {
			width: 160rpx;
			height: 64rpx;
			margin: 10rpx 0 0 0;
			line-height: 64rpx;
			font-size: $font-size-26;
			text-align: center;
			border-radius: 32rpx;
			float: right;
		}
		.btn-cancel {
			background-color: #f6f6f6;
			color: #333333;
		}
		.btn-delete {
			background-color: #f6f6f6;
			color: #333333;
		}
		.btn-query {
			background-color: #ff5000;
			color: #ffffff;
		}
		.btn-confirm {
			background-color: #4688fa;
			color: #ffffff;
		}
		.btn-pay {
			background-color: #4688fa;
			color: #ffffff;
			margin-right: 0;
		}
	}
}
.tui-actionsheet-btn{
	box-sizing: content-box;
}
</style>
