<template>
	<view class="check-action">
		<view class="btn-wrap">
			<view>
				<u--textarea v-model="value" placeholder="请输入审批意见"></u--textarea>
			</view>
			<view class="buttons">
				<u-button @click="handlePass" class="btn first-btn" type="primary" text="通过"></u-button>
				<u-button @click="handleReject" :disabled="isBtnRefuseDisable" type="warning" text="驳回"></u-button>
			</view>
		</view>
	</view>
</template>

<!-- <check-action v-if="isLoadOK && tableKey === '1' && isAudit === 'true'" @reject="handleReject"
			@pass="handlePass" :okApi="okApi" :refuseApi="refuseApi"
			:dataSource="$store.state.auditModel"></check-action>
			 -->
<!-- @reject="handleReject" @pass="handlePass" 如果调用页面需要对审批结束后继续处理 直接 在使用页面 写方法 -->

<script>
	import {
		handlePassAudit,
		handleRejestAudit,
		handlePassAuditCallBack
	} from '../../services/apply/subPages/BS/notice'
	import '@/utils/showMsg.js'
	export default {
		props: {
			dataSource: {
				type: Object,
				default () {
					return undefined
				}
			},
			okApi: {
				type: String,
				default: ''
			},
			refuseApi: {
				type: String,
				default: ''
			},
			isBtnRefuseDisable: {
				typeof: Boolean,
				default: true
			}
		},
		data() {
			return {
				value: ''
			}
		},
		// beforeCreate()  KO
		// beforeMount()   mounted()  OK
		created() {
			//wyw 没有驳回接口 
			this.isBtnRefuseDisable = (this.__page__.options.refuseApi == null || this.__page__.options.refuseApi ==
				undefined || this.__page__.options.refuseApi == 'undefined') ? true : false
		},

		methods: {
			handlePass() {
				const genParams = this.handleGenParams(10)
				handlePassAudit(genParams, this.okApi).then(res => {
					// this.$emit('pass', this.dataSource)
					uni.$showMsgFunc('审批成功!', () => {
						// if (this.tableKey == null || this.tableKey == 0) {
						// 	uni.navigateTo({
						// 		url: 'safeCheckIndex'
						// 	})
						// } else {

						if (this.okApi == 'PF/Approve/Submit' && res.Data) {
							//最后一个节点处理问题
							handlePassAuditCallBack(res.Data + "?id=" + genParams.DATA_ID).then(
								res2 => {
									uni.navigateBack()
								})
						} else {
							uni.navigateBack()
						}
						// }
					}, 'success', 1000)
				})
			},
			handleReject() {
				if (this.value == undefined || this.value === '' || this.value.length < 1) {
					uni.$showErrorInfo('请输入驳回意见！')
					return false
				}
				const genParams = this.handleGenParams(20)
				handleRejestAudit(genParams, this.refuseApi).then(res => {
					// this.$emit('reject', this.dataSource)
					uni.$showMsgFunc('驳回成功!', () => {
						uni.navigateBack()
					}, 'success', 1000)

				})
			},
			handleGenParams(status) {
				const appInfoData = uni.getStorageSync('appInfo')
				const userId = appInfoData?.User?.ID || ''
				this.dataSource.Nav_ApproveDetails.sort((a, b) => a.NUM - b.NUM);
				let currentNodes = this.dataSource.Nav_ApproveDetails?.filter(v => v.IS_CURRENT == true);
				let isCurrent = false;
				if (currentNodes && currentNodes.length > 0) {
					let currentUserNode = currentNodes.filter(v => v.APPROVE_USER_ID === userId);
					if (currentUserNode != null) {
						isCurrent = true;
						this.dataSource.CurrentNode = currentUserNode[0];
					} else {
						this.dataSource.CurrentNode = currentNodes[0];
					}
				}
				const params = {
					...this.dataSource,
					CurrentNode: {
						...this.dataSource.CurrentNode,
						NODE_APPROVE_STATUS: status,
						CONTEXT: this.value
					}
				}
				return params
			}
		}
	}
</script>
<style scoped>
	.btn-wrap {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		box-sizing: border-box;
		padding: 16px;
		z-index: 1000;
		background: #fff;
		box-shadow: 0 -1px 2px 0 rgba(0, 0, 0, .05);
	}

	.buttons {
		margin-top: 5px;
		display: flex;
		justify-content: space-between;
	}

	.buttons .first-btn {
		margin-right: 20px;
	}
</style>