<template>
	<view class="negative-wrapper">
		<negative :data="data" :loading="loading" />
		
		<view class="container" v-if="flowKey === 'first_distribute' || flowKey === 'second_distribute'">
			<uni-forms ref="distributeFormRef" :modelValue="actionData" :rules="distributeRules" label-width="200rpx" style="margin: 12px">
				<uni-forms-item label="主办层级" name="hostLevel" required>
					<uni-data-checkbox v-model="actionData.hostLevel" :localdata="hostLevel" :map="{text: 'dictLabel', value: 'dictValue'}" />
				</uni-forms-item>
				<uni-forms-item label="办理单位" name="departId" required>
					<uni-data-picker :localdata="departs" placeholder="请选择办理单位" :border="false"
						:map="{text:'shortName', value: 'id'}" v-model="actionData.departId" />
				</uni-forms-item>
			</uni-forms>
		</view>
	</view>
	<view class="footer col-24 flex flex-wrap justify-end">
		<n-button v-for="item in flowActions" :type="item.buttonType" class="button" :plain="item.plain" :label="item.buttonLabel" @tap="submit(item)" :disabled="loading" />
	</view>
	
	<modal ref="approveModelRef" @confirm="handleApproval" title="审批意见" placeholderText="请输入审批意见" confirmText="审批通过" />
	
	<modal ref="returnModelRef" @confirm="handleApproval" title="退回意见" placeholderText="请输入退回意见" confirmType="warn" />
	
</template>
<script>
	import { getDictOptions } from '@/common/dict'
	import { getNegative, executeNegative } from '@/api/negative'
	import {
		departTree,
		secondList
	} from '@/api/depart'
	
	let _this;
	export default {
		inheritAttrs: false,
		data() {
			return {
				data: {},
				flowActions: [],
				loading: true,
				actionData: {},
				activeFlowAction: {},
				departs: [],
				distributeRules: {
					hostLevel: {
						rules: [{
							required: true,
							errorMessage: '请选择主办层级',
						}]
					},
					departId: {
						rules: [{
							required: true,
							errorMessage: '请选择办理单位',
						}]
					},
				},
				flowKey: '',
				confirmationCompletionFlag: false
			}
		},
		setup() {
			const hostLevel = getDictOptions("hostLevel");
			return {
				hostLevel
			}
		},
		onLoad() {
			
			_this = this;
			uni.setNavigationBarTitle({
				title: `问题编号 ` + this.$page.options.id
			})
			this.getData()
			
		},
		methods: {
			getData() {
				getNegative(this.$page.options.id, this.$page.options.workId).then(data => {
					this.data = data
					this.flowActions = data.flowActions
					this.flowKey = data.flowNode.flowKey
					this.confirmationCompletionFlag = data.confirmationCompletionFlag
					this.loading = false
					if (this.flowKey === 'first_distribute' || this.flowKey === 'second_distribute') {
						if (this.flowKey === 'first_distribute') {
							secondList().then(data => {
								this.departs = data
							})
						} else {
							departTree().then(data => {
								this.departs = data[0].children
							})
						}
						
					}
				})
			},
			async submit(item) {
				console.log(item.actionKey)
				const actions = ['save', 'apply_extension', 'apply_completion', 'apply_countersign', 'update_verify'];
				if (actions.indexOf(item.actionKey) > -1) {
					uni.showModal({
						title: '温馨提示',
						content: '该功能暂不支持在APP上操作，如有需要请在电脑端操作',
						success: function (res) {
							if (res.confirm) {
								console.log('用户点击确定');
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					return
				}
				if (item.openDialog) {
					const approveActionKeys = ['second_approve', 'first_approve']
					if (approveActionKeys.indexOf(item.actionKey) > -1) {
						if (
							this.confirmationCompletionFlag
						) {
							item = {
								actionName: "认定办结",
								actionKey: "confirmationCompletion",
								doClose: true
							};
						}
						this.$refs.approveModelRef.open()
						this.activeFlowAction = item
						return
					}
					const returnActionKeys = ['second_sign_return', 'three_sign_return', 'second_approve_return', 'first_approve_return']
					if (returnActionKeys.indexOf(item.actionKey) > -1) {
						this.$refs.returnModelRef.open()
						this.activeFlowAction = item
					}
					return
				}
				if (item.actionKey === 'second_distribute') {
					await this.$refs.distributeFormRef.validate()
				}
				this.loading = true
				executeNegative(this.$page.options.id, {
					workId: this.$page.options.workId,
					actionKey: item.actionKey,
					nextFlowKey: item.nextFlowKey,
					actionName: item.actionName,
					data: this.actionData
				}).then(data => {
					console.log(data)
					if (item.actionKey === 'second_sign') {
						uni.showToast({
							title: '签收成功',
							icon: 'success',
							duration: 3000
						})
					}
					if (item.doClose) {
						uni.showToast({
							title: '操作成功',
							icon: 'success',
							duration: 3000
						})
						uni.navigateBack({
							
						});
					} else {
						this.getData()
					}
					
				})
			},
			handleApproval(comments) {
				this.activeFlowAction.openDialog = false
				this.actionData.comments = comments
				this.submit(this.activeFlowAction)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.footer {
		background-color: #fff;
		gap: 8rpx;
	}
	.negative-wrapper {
		background-color: #f5f5f5;
		min-height: 100vh;
		box-sizing: border-box;
		padding-bottom: 60px;
		.container {
			background-color: #fff;
			margin-bottom: 12rpx;
		}
	}
</style>