<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 style="width: 45%;margin-bottom: 10rpx;" :loading='loading' type="success" text="通过"
				@click="handleAudit(1)">
			</u-button>
			<u-button style="width: 45%;" :loading='loading' type="error" text="驳回" @click="handleAudit(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,
		AssignPurchasePlanUserAPI,
		rejectTask,
		cancelTask
	} from '@/api/wt/index.js'
	import { UpdateInformation } from '@/api/purchasingManage/inquiryManage'
	import {
		listAllUserBind
	} from '@/api/common.js'
	export default {
		name: 'taskSubmit',
		props: {
			businessId: {
				default: ''
			},
			taskId: {
				default: ''
			},
			id: {
				default: ''
			},
			taskDefinitionKey: {
				default: ''
			}
		},

		data() {
			return {
				showAction: false,
				loading: false,
				technicianShow: false,
				userOptions: [],
				form: {
					userId: '',
					userName: '',
					reason: '',
				},
				actionList: [{
					name: '作废',
					fontSize: '28',
					color: '#ffaa7f'
				}],
				rules: {
					reason: {
						type: 'string',
						required: true,
						message: '请输入审批建议',
						trigger: 'blur'
					},
					userName: {
						type: 'string',
						required: true,
						message: '请选择负责人',
						trigger: ['blur','change']
					},
				}
			}
		},
		mounted() {
			this.$refs.uForm.setRules(this.rules)
			// this.getUserOptions()
		},
		methods: {
			async getUserOptions() {
				const data = await listAllUserBind()
				this.userOptions.push(data)
			},
			showTechnicianPicker(val) {
				if (val.target.nodeName == 'I') {
					this.form.userId = ''
					this.form.userName = ''
					return
				}
				this.technicianShow = true
			},
			selectTechnicianInfo(e) {
				this.form.userId = e.value[0]?.id
				this.form.userName = e.value[0]?.name
				this.technicianShow = false
			},
			async handleAudit(status) {
				if (!!status) await this.$refs.uForm.validate()
				let res = await this.getTableValue();

				console.log('res~~~~', res)

				const data = await UpdateInformation(res)
				// if (data.code != '0') {
				// 	return;
				// }
				this.loading = true
				await this._approveTaskWithVariables(status);
			},
			async _approveTaskWithVariables(status) {
				let variables = {
					pass: !!status
				};

				let API = !!status ? approveTaskWithVariables : rejectTask;
				let res = await API({
					id: this.taskId,
					reason: this.form.reason,
					variables
				})

				if (res.code != '-1') {
					this.$emit('handleAudit', {
						status,
						title: status === 0 ? '驳回' : ''
					});
				}
				this.loading = false
			},

			selectActionClick(item) {
				console.log('selectActionClick', item)
				if (item.name == '作废') {
					uni.showModal({
						title: '提示',
						content: '是否确认作废？',
						success: (res) => {
							if (res.confirm) {
								this.loading = true
								cancelTask({
									taskId: this.taskId,
									id: this.id,
									reason: this.form.reason,
									businessId: this.businessId,
								}).then(() => {
									if (res.code != '-1') {
										this.loading = false
										this.$emit('handleAudit', {
											title: '作废'
										});
									}
								}).catch(() => {
									this.loading = false
									this.$message.error("流程作废失败");
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},

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

<style lang="scss" scoped>
.btnList {
		display: flex;

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