<template>
	<view class="">
		<u--form style="margin: 0 20px;" labelPosition="left" :model="form" :rules="rules" ref="uForm"
			labelWidth='140rpx'>
			<u-form-item label="审批建议" prop="reason" required>
				<u--textarea style="width: 100%;" height='120' border='surround' placeholder="请输入审批建议"
					v-model="form.reason"></u--textarea>
			</u-form-item>
		</u--form>
		<!-- <view>
			<u-button style="width: 100%;margin-bottom: 10rpx;" icon="edit-pen" :loading='loading' type="success"
				text="通过" @click="handleAudit(1)">
			</u-button>
			<u-button style="width: 100%;" :loading='loading' type="error" icon="close" text="驳回"
				@click="handleAudit(0)" v-if="!['starter'].includes(taskDefinitionKey)"></u-button>
		</view> -->
		<view class="btnList">
			<u-button v-if="passBtnDisabled" style="width: 45%;margin-bottom: 10rpx;" :loading='loading' type="success" text="通过" @click="handleAudit(1)">
			</u-button>
			<u-button v-if="acceptBtnDisabled" style="width: 45%;margin-bottom: 10rpx;" :loading='loading' type="success" text="申请出库" @click="storemanApprove">
			</u-button>
			<u-button style="width: 45%;" :loading='loading' type="error" text="驳回" @click="rejectTask(0)"></u-button>
		</view>
		<!-- <view class="btnConcel">
			<u-button @click="showAction = true">更多</u-button>
		</view> -->
		<!-- <u-action-sheet :actions="actionList" :closeOnClickOverlay="true" :closeOnClickAction="true" title="更多操作" :show="showAction" @close="showAction = false" @select="selectActionClick"></u-action-sheet> -->

	</view>
</template>

<script>
	import {
		approveTaskWithVariables,
		getWarehouseListByIdsAPI,
		getSaleOrderSendRecordDetailAPI,
		getOutInBySourceBizNo,
		rejectTask
	} from '@/api/wt/index.js'
	import { outStorage, updateSendInformation } from '@/api/warehouseManagement/index.js'
	export default {
		name: 'taskSubmit',
		props: {
			businessId: {
				default: ''
			},
			taskId: {
				default: ''
			},
			id: {
				default: ''
			},
			taskDefinitionKey: {
				default: ''
			}
		},

		data() {
			return {
				loading: false,
				outInData: { verifyStatus: 0 },
				form: {
					technicianId: '',
					reason: '',
				},
				rules: {
					reason: {
						type: 'string',
						required: true,
						message: '请输入审批建议',
						trigger: 'blur'
					}
				},
			}
		},
		computed: {
			acceptBtnDisabled() {
				console.log('acceptBtnDisabled check:', this.taskDefinitionKey, this.outInData.verifyStatus);
				return ['storemanApprove'].includes(this.taskDefinitionKey) && [0, 3].includes(this.outInData.verifyStatus);
			},
			passBtnDisabled() {
				console.log('passBtnDisabled check:', this.taskDefinitionKey, this.outInData.verifyStatus);
				return this.taskDefinitionKey != 'storemanApprove' || (this.taskDefinitionKey == 'storemanApprove' && this.outInData.verifyStatus == 2)
			}
		},
		async mounted() {
			this.$nextTick(() => {
				this.$refs.uForm?.setRules(this.rules)
			})
			if (this.taskDefinitionKey == 'storemanApprove') {
				let data = await getSaleOrderSendRecordDetailAPI(this.businessId);
				try {
					console.log(data, '1111111111');
					data = await getOutInBySourceBizNo(data.docNo);
					console.log('data--------------', data);
					if (JSON.stringify(data) != '{}') {
						this.outInData = data;
					}
					console.log(this.outInData, '============');
				} catch (error) {
					console.log(22222222, '22222222222');
					this.outInData.verifyStatus = 0;
				}
			}
		},
		methods: {
			async storemanApprove() {
				let res = await this.getTableValue();
				let storageData = res.returnStorageData;
				console.log('storageData~~~', storageData);
				// return
				// 出库来源isSkip 0-正常  1-外部(外部跳过内部审核流程)
				storageData.isSkip = 1;
				try {
					this.loading = true;
					await outStorage(storageData);
					approveTaskWithVariables({
						id: this.taskId,
						reason: this.form.reason,
						variables: {
							pass: true
						}
					}).then((res) => {
						if (res.code != '-1') {
							this.$emit('handleAudit', {
								status: 1,
								title: '出库'
							});
						}
						this.loading = false;
					});
				} catch (error) {
					this.loading = false;
					console.error('保存失败：', error);
				}
			},

			rejectTask(status) {
				let variables = {
					pass: !!status
				};
				rejectTask({
					id: this.taskId,
					reason: this.form.reason,
					variables
				}).then((res) => {
					if (res.data.code != '-1') {
						this.$emit('handleAudit', {
						status,
						title: status === 0 ? '驳回' : ''
						});
					}
				});
			},

			async handleAudit(status) {
				let storemanIds = '';
				//发起人补充
				if (
					this.taskDefinitionKey === 'starter' ||
					this.taskDefinitionKey === 'salesmanUploadReceipt'
				) {
					let arr = await this.getTableValue();
					console.log('arr--------', arr);
					if (!arr) {
						return;
					}
					if (
						this.taskDefinitionKey === 'salesmanUploadReceipt' &&
						arr.replied === 0
					) {
						uni.$u.toast('回执附件不能为空');
						return;
					}
					// console.log(arr)
					// return
					let data = await updateSendInformation(arr);
					if (data.code != '0') {
						return;
					}
				}

				if (this.taskDefinitionKey === 'deptLeaderApprove') {
					let arr = await this.getTableValue();
					let ids = arr.form.productList.map((item) => item.warehouseId);
					let data = await getWarehouseListByIdsAPI(ids || []);
					storemanIds = data.map((item) => item.ownerId);
				}
				this.loading = true
				this._approveTaskWithVariables(
					status,
					storemanIds.length > 0
						? Array.from(new Set(storemanIds)).toString()
						: ''
				);
			},
			async _approveTaskWithVariables(status, storemanIds) {
				let variables = {
					pass: !!status
				};
				if (storemanIds) {
					variables['storemanIds'] = storemanIds;
				}
				let API = !!status ? approveTaskWithVariables : rejectTask;
				API({
					id: this.taskId,
					reason: this.form.reason,
					variables
				}).then((res) => {
					if (res.data.code != '-1') {
						this.$emit('handleAudit', {
							status,
							title: status === 0 ? '驳回' : ''
						});
					}
					this.loading = false
				});
			},

			getTableValue() {
				return new Promise((resolve, reject) => {
					this.$emit('getTableValue', async (data) => {
						resolve(await data);
					});
				});
			}
		}
	}
</script>

<style scoped>
.btnList {
	display: flex;

}
.btnConcel {
	margin-top: 20rpx;
	}
</style>